summary refs log tree commit diff stats
path: root/java/text
diff options
context:
space:
mode:
authorSudipto Mallick <smlckz@termux-alpine>2024-01-28 06:38:55 +0000
committerSudipto Mallick <smlckz@termux-alpine>2024-01-28 06:38:55 +0000
commitfc4d96a9e3e20f8ddf035c16fc32b1effb75481b (patch)
tree4e13b2606e94e2098d1a288b051603cfc36d983b /java/text
parentcd6a9243c056710794549a1ab5d51fbdd082ce2e (diff)
downloadzadania-fc4d96a9e3e20f8ddf035c16fc32b1effb75481b.tar.gz
Complete all Java assignments
Diffstat (limited to 'java/text')
-rw-r--r--java/text/AbstractShapeCalculations.typ36
-rw-r--r--java/text/CircularBaseExample.typ37
-rw-r--r--java/text/CuboidCalculations.typ26
-rw-r--r--java/text/CustomExceptionExample.typ23
-rw-r--r--java/text/EmployeeDisplay.typ31
-rw-r--r--java/text/InnerClassExample.typ28
-rw-r--r--java/text/InterfaceExample.typ39
-rw-r--r--java/text/PackageExample.typ28
-rw-r--r--java/text/PackageInterfaceExample.typ32
-rw-r--r--java/text/RuntimePolymorphismExample.typ31
-rw-r--r--java/text/ThreadsExample.typ36
11 files changed, 347 insertions, 0 deletions
diff --git a/java/text/AbstractShapeCalculations.typ b/java/text/AbstractShapeCalculations.typ
new file mode 100644
index 0000000..83d3999
--- /dev/null
+++ b/java/text/AbstractShapeCalculations.typ
@@ -0,0 +1,36 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.6em)
+
+#assignment(14, block: true)[
+  Write a program to create an abstract class `Shape` with two abstract methods, `area()` and `display()` and make three concrete derived classes `Rectangle`, `Circle` and `Triangle` which can calculate area and display them seperately.
+]
+
+#scos("AbstractShapeCalculations")
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- The `Shape` abstract class:
+  - `abstract double area()`: To be implemented by subclasses for calculating the area.
+  - `abstract void display()`: To be implemented by subclasses for displaying shape details.
+
+- The `Rectangle` class extends `Shape`:
+  - `double area()`: Calculates the area of a rectangle using its length and width.
+  - `void display()`: Displays details of the rectangle.
+
+- The `Circle` class extends `Shape`:
+  - `double area()`: Calculates the area of a circle using its radius.
+  - `void display()`: Displays details of the circle.
+
+- The `Triangle` class extends `Shape`:
+  - `double area()`: Calculates the area of a triangle using its side lengths.
+  - `double perimeter()`: Calculates the perimeter of a triangle.
+  - `void display()`: Displays details of the triangle.
+
+- The `AbstractShapeCalculations` class contains the main program:
+  - `public static void main(String[])`: Implements a menu-driven program for calculating and displaying areas of different shapes (Rectangle, Circle, Triangle) based on user input. Utilizes abstract classes and polymorphism for handling various shapes and their respective calculations.
+
+#signature()
diff --git a/java/text/CircularBaseExample.typ b/java/text/CircularBaseExample.typ
new file mode 100644
index 0000000..2a9c495
--- /dev/null
+++ b/java/text/CircularBaseExample.typ
@@ -0,0 +1,37 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.7em)
+
+#assignment(18, block: true)[
+  Write a program to create a class named `CircularBase` containing a method `getArea()` to calculate the base area, an interface named `_3dShape` with two methods `calArea()` and `calVolume()`, two subclasses of `CircularBase` named `Cone` and `Cylinder` implementing `_3dShape`; calculate the base area and volume of these shapes with the help of `calArea()` method.
+]
+
+#scos("CircularBaseExample")
+
+=== Discussion
+
+#skind[Interfaces, classes and methods implemented in the program]
+
+- The `ThreeDimensionalShape` interface:
+  - `double calArea()`: Calculates the surface area of a three-dimensional shape.
+  - `double calVolume()`: Calculates the volume of a three-dimensional shape.
+
+- The `CircularBase` class:
+  - Constructor `CircularBase(double)`: Initializes a shape with a circular base using the provided radius.
+  - `double getArea()`: Calculates the area of the circular base.
+
+- The `Cone` class extends `CircularBase` and implements `ThreeDimensionalShape`:
+  - Constructor `Cone(double, double)`: Initializes a cone with a circular base, taking radius and height.
+  - `double calArea()`: Overrides the interface method to calculate the total surface area of the cone.
+  - `double calVolume()`: Overrides the interface method to calculate the volume of the cone.
+
+- The `Cylinder` class extends `CircularBase` and implements `ThreeDimensionalShape`:
+  - Constructor `Cylinder(double, double)`: Initializes a cylinder with a circular base, taking radius and height.
+  - `double calArea()`: Overrides the interface method to calculate the total surface area of the cylinder.
+  - `double calVolume()`: Overrides the interface method to calculate the volume of the cylinder.
+
+- The `CircularBaseExample` class contains the main program:
+  - `public static void main(String[])`: Takes user input for the dimensions of a cone and a cylinder, creates instances of these shapes, and displays their base area, total surface area, and volume.
+  
+#signature()
diff --git a/java/text/CuboidCalculations.typ b/java/text/CuboidCalculations.typ
new file mode 100644
index 0000000..ce1ef82
--- /dev/null
+++ b/java/text/CuboidCalculations.typ
@@ -0,0 +1,26 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.7em)
+
+#assignment(16)[
+  Write a program to create a base class named `Rectangle` and another class named `Cuboid` deriving `Rectangle` overloading the constructors and print surface area and volume of a `Cuboid` object.
+]
+#v(2em)
+#scos("CuboidCalculations")
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- The `Rectangle` class:
+  - `int area()`: Calculates the area of a rectangle based on its length and breadth.
+
+- The `Cuboid` class extends `Rectangle`:
+  - `int surfaceArea()`: Calculates the surface area of a cuboid based on its length, breadth, and height.
+  - `int volume()`: Calculates the volume of a cuboid based on its length, breadth, and height.
+
+- The `CuboidCalculations` class contains the main program:
+  - `public static void main(String[])`: Creates an instance of `Cuboid`, calculates and displays its surface area and volume.
+  
+#signature()
diff --git a/java/text/CustomExceptionExample.typ b/java/text/CustomExceptionExample.typ
new file mode 100644
index 0000000..4b653e8
--- /dev/null
+++ b/java/text/CustomExceptionExample.typ
@@ -0,0 +1,23 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.75em)
+
+#assignment(24, block: true)[
+  Write a program that prompts the user for an integer input, and if the entered value is less than zero, throw a custom exception; additionally, handle exceptions for input of non-integer data types.
+]
+
+#scos("CustomExceptionExample")
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- Class `NegativeNumberException` (extends `Exception`): Custom exception class for negative numbers.
+  - Constructor `public NegativeNumberException(String)`: Constructs an Exception object to be thrown using the provided message string.
+
+- Main program:
+  - Class `CustomExceptionExample`:
+    - `public static void main(String[])`: Contains the main program demonstrating custom exception handling for negative numbers and handling input mismatches.
+
+#signature()
diff --git a/java/text/EmployeeDisplay.typ b/java/text/EmployeeDisplay.typ
new file mode 100644
index 0000000..d7d0253
--- /dev/null
+++ b/java/text/EmployeeDisplay.typ
@@ -0,0 +1,31 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.6em)
+
+#assignment(17, block: true)[
+  Write a program to create a class `Employee` with instance variables `name` and `id`; a subclass of `Employee` named `Scientist` with instance variables `no_of_publication` and `experience`; and its subclass named `DScientist` with instance variable `award`; implement the `public String toString() { }` method in each class to describe about its object with the member variables and from `main()` method create an object of each class and print each object.
+]
+
+#scos("EmployeeDisplay")
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- The `Employee` class:
+  - Constructor `Employee(int, String)`: Initializes an employee with ID and name.
+  - `public String toString()`: Returns a string representation of the employee details.
+
+- The `Scientist` class extends `Employee`:
+  - Constructor `Scientist(int, String, int, int)`: Initializes a scientist with additional attributes like the number of publications and years of experience.
+  - `public String toString()`: Returns a string representation of the scientist details.
+
+- The `DScientist` class extends `Scientist`:
+  - Constructor `DScientist(int, String, int, int, String[])`: Initializes a distinguished scientist with additional awards.
+  - `public String toString()`: Returns a string representation of the distinguished scientist details.
+
+- The `EmployeeDisplay` class contains the main program:
+  - `public static void main(String[])`: Creates instances of different employee types (Employee, Scientist, Distinguished Scientist) and displays their details.
+  
+#signature()
diff --git a/java/text/InnerClassExample.typ b/java/text/InnerClassExample.typ
new file mode 100644
index 0000000..796eac4
--- /dev/null
+++ b/java/text/InnerClassExample.typ
@@ -0,0 +1,28 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.7em)
+
+#assignment(19, block: true)[
+  Write a program to create a class containing an inner class to show that inner class can use members of outer class directly, but the outer class can use members of inner class only through its objects. Check the name of the inner class file created when it was compiled.
+]
+#v(-1em)
+#scos("InnerClassExample")
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- The `OuterClass` class:
+  - Private field `int outerMember`: Represents a member variable in the outer class.
+  - The `InnerClass` class:
+    - Private field `int innerMember`: Represents a member variable in the inner class.
+    - Method `void accessMembers()`: Prints the values of both outer and inner members.
+  - Method `void accessInnerMembers()`: Creates an instance of the inner class and prints the value of the inner member through the object.
+
+- The `InnerClassExample` class contains the main program:
+  - `public static void main(String[])`: Demonstrates the usage of outer and inner classes by creating instances and accessing their members.
+
+/ Note : The file name of the (bytecode) compiled class file corresponding to the inner class is ```text OuterClass$InnerClass.class```.
+  
+#signature()
diff --git a/java/text/InterfaceExample.typ b/java/text/InterfaceExample.typ
new file mode 100644
index 0000000..d9955fd
--- /dev/null
+++ b/java/text/InterfaceExample.typ
@@ -0,0 +1,39 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.5em)
+
+#assignment(20, block: true)[
+  Write a program in which create two interfaces, each with two methods, inherit a new interface from the two, adding a new method, create a class by implementing the new interface and also inheriting a concrete class. In the `main()` method, create an object of the derived class and call these methods.
+]
+
+#scos("InterfaceExample")
+
+=== Discussion
+
+#skind[Interfaces, classes and methods implemented in the program]
+
+- Interface `Interface1`:
+  - `void method1()`: Declares a method.
+  - `void method2()`: Declares another method.
+
+- Interface `Interface2`:
+  - `void method3()`: Declares a method.
+  - `void method4()`: Declares another method.
+
+- Interface `NewInterface` (extends `Interface1` and `Interface2`):
+  - Inherits methods from `Interface1` and `Interface2`.
+  - `void newMethod()`: Declares a new method.
+
+- Class `ConcreteClass`:
+  - Method `void concreteMethod()`: Declares a method.
+
+- Class `DerivedClass` (extends `ConcreteClass` and implements `NewInterface`):
+  - Implements methods from `Interface1`, `Interface2`, and `NewInterface`.
+  - Method `void concreteMethod()`: Inherits and overrides a method.
+
+- Class `InterfaceExample`:
+  - `public static void main(String[])`: Contains the main program, creating an instance of `DerivedClass` and calling various methods.
+
+  
+#signature()
diff --git a/java/text/PackageExample.typ b/java/text/PackageExample.typ
new file mode 100644
index 0000000..985ad34
--- /dev/null
+++ b/java/text/PackageExample.typ
@@ -0,0 +1,28 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.65em)
+
+#assignment(22, block: true)[
+  Create a program that defines a class with variables and methods in the `pOne` package, extends this class in another package `pTwo`, and within the method of the `pTwo` class, access the variables and methods of the original class in `pOne`; finally, from the `main()` method in the working directory, access the members of the second class.
+]
+
+#scos("PackageExample", include-before: ("pOne/ClassOne", "pTwo/ClassTwo"))
+
+=== Discussion
+
+#skind[Classes and methods implemented in the program]
+
+- Package `pOne`:
+  - Class `ClassOne`:
+    - `protected int variableOne`: A protected variable.
+    - `public void methodOne()`: A public method.
+
+- Package `pTwo`:
+  - Class `ClassTwo` (extends `pOne.ClassOne`):
+    - `public void methodTwo()`: A public method that calls `methodOne()` and accesses `variableOne` from `ClassOne`.
+
+- Class `PackageExample`:
+  - `public static void main(String[])`: Contains the main program creating an object of `ClassTwo` and calling `methodTwo()`.
+
+#signature()
diff --git a/java/text/PackageInterfaceExample.typ b/java/text/PackageInterfaceExample.typ
new file mode 100644
index 0000000..639d88b
--- /dev/null
+++ b/java/text/PackageInterfaceExample.typ
@@ -0,0 +1,32 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.6em)
+
+#assignment(23, block: true)[
+  Write a program that defines an interface with three methods in the `pkgOne` package, implements the interface in a class within the `pkgTwo` package, and from the `main()` method in the working directory, instantiate the class and invoke the interface methods.
+]
+
+#scos("PackageInterfaceExample", include-before: ("pkgOne/MyInterface", "pkgTwo/MyClass"))
+
+=== Discussion
+
+#skind[Interfaces, classes and methods implemented in the program]
+
+- Package `pkgOne`:
+  - Interface `MyInterface`:
+    - `void methodOne()`: An interface method.
+    - `void methodTwo()`: An interface method.
+    - `void methodThree()`: An interface method.
+
+- Package `pkgTwo`:
+  - Class `MyClass` (implements `pkgOne.MyInterface`):
+    - `public void methodOne()`: Implementation of `methodOne` from `MyInterface`.
+    - `public void methodTwo()`: Implementation of `methodTwo` from `MyInterface`.
+    - `public void methodThree()`: Implementation of `methodThree` from `MyInterface`.
+
+- Main program:
+  - Class `PackageInterfaceExample`:
+    - `public static void main(String[])`: Contains the main program creating an object of `MyClass` (which implements `MyInterface`) and calling its methods.
+
+#signature()
diff --git a/java/text/RuntimePolymorphismExample.typ b/java/text/RuntimePolymorphismExample.typ
new file mode 100644
index 0000000..1235c27
--- /dev/null
+++ b/java/text/RuntimePolymorphismExample.typ
@@ -0,0 +1,31 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.7em)
+
+#assignment(21)[
+  Write a program, wherein create an Interface and create two sub-classes implementing the interface to show the functionalities of runtime polymorphism (_a.k.a._ dynamic method dispatch) using them.
+]
+
+#scos("RuntimePolymorphismExample")
+
+=== Discussion
+
+#skind[Interfaces, classes and methods implemented in the program]
+
+- Interface `Shape`:
+  - `void draw()`: Declares a method.
+
+- Class `Circle` (implements `Shape`):
+  - Implements the `draw()` method from the `Shape` interface.
+  - Method `void calculateArea()`: Declares a specific method for calculating the area of a circle.
+
+- Class `Square` (implements `Shape`):
+  - Implements the `draw()` method from the `Shape` interface.
+  - Method `void calculateArea()`: Declares a specific method for calculating the area of a square.
+
+- Class `RuntimePolymorphismExample`:
+  - `public static void main(String[])`: Contains the main program demonstrating runtime polymorphism by creating objects of subclasses (`Circle` and `Square`) and calling the `draw()` method dynamically.
+
+  
+#signature()
diff --git a/java/text/ThreadsExample.typ b/java/text/ThreadsExample.typ
new file mode 100644
index 0000000..5e1b2c8
--- /dev/null
+++ b/java/text/ThreadsExample.typ
@@ -0,0 +1,36 @@
+#import "/template.typ": *
+#show: A => apply(A)
+#set raw(lang: "java-new")
+#set par(leading: 0.5em)
+
+#assignment(25)[
+  Write a program in Java to create three threads printing 1 to 10, then implement the program using both inheriting Thread class and implementing Runnable interface.
+]
+
+#scos("ThreadsExample")
+
+=== Discussion
+
+#skind[Classes, interfaces and methods used from Java standard library]
+
+- `java.lang.Runnable` interface:
+  - `void run()`: The method to be implemented by the class that implements this interface.
+
+- `java.lang.Thread` class:
+  - `void start()`: Causes this thread to begin execution.
+  - `static Thread currentThread()`: Returns a reference to the currently executing thread.
+  - `long getId()`: Returns the identifier of this thread.
+
+#skind[Classes and methods implemented in the program]
+  
+- Class `NumberRunnable` (implements `Runnable`):
+  - `public void run()`: Implements the `run` method of the `Runnable` interface, printing numbers.
+
+- Class `NumberThread` (extends `Thread`):
+  - `public void run()`: Implements the `run` method of the `Thread` class, printing numbers.
+
+- Main program:
+  - Class `ThreadsExample`:
+    - `public static void main(String[])`: Demonstrates multi-threading using both `Thread` and `Runnable` approaches, creating and starting threads to print numbers.
+  
+#signature()