diff options
author | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-25 05:50:05 +0000 |
---|---|---|
committer | Sudipto Mallick <smlckz@termux-alpine> | 2024-01-25 05:50:05 +0000 |
commit | 13f9553a26a51b95ab194e5328ad302b35d7b118 (patch) | |
tree | 8cc94ea3ae2baac3335bd8471196f90a28a2a066 /java/code | |
parent | a5afdcfa7165e100042f03de1b3f7a601389776f (diff) | |
download | zadania-13f9553a26a51b95ab194e5328ad302b35d7b118.tar.gz |
Complete Java assignments
Diffstat (limited to 'java/code')
-rw-r--r-- | java/code/CircularBaseExample.java | 86 | ||||
-rw-r--r-- | java/code/CuboidCalculations.java | 39 | ||||
-rw-r--r-- | java/code/CustomExceptionExample.java | 26 | ||||
-rw-r--r-- | java/code/InnerClassExample.java | 27 | ||||
-rw-r--r-- | java/code/InterfaceExample.java | 59 | ||||
-rw-r--r-- | java/code/PackageExample.java | 9 | ||||
-rw-r--r-- | java/code/PackageInterfaceExample.java | 12 | ||||
-rw-r--r-- | java/code/ParentWithTwoChildren.java | 4 | ||||
-rw-r--r-- | java/code/RuntimePolymorphismExample.java | 47 | ||||
-rw-r--r-- | java/code/ThreadsExample.java | 36 | ||||
-rw-r--r-- | java/code/pOne/ClassOne.java | 9 | ||||
-rw-r--r-- | java/code/pTwo/ClassTwo.java | 12 | ||||
-rw-r--r-- | java/code/pkgOne/MyInterface.java | 7 | ||||
-rw-r--r-- | java/code/pkgTwo/MyClass.java | 19 |
14 files changed, 390 insertions, 2 deletions
diff --git a/java/code/CircularBaseExample.java b/java/code/CircularBaseExample.java new file mode 100644 index 0000000..c9a21fe --- /dev/null +++ b/java/code/CircularBaseExample.java @@ -0,0 +1,86 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +interface ThreeDimensionalShape { + double calArea(); + double calVolume(); +} + +class CircularBase { + double radius; + + CircularBase(double radius) { + this.radius = radius; + } + + double getArea() { + return Math.PI * radius * radius; + } +} + +class Cone extends CircularBase implements ThreeDimensionalShape { + double height; + + Cone(double radius, double height) { + super(radius); + this.height = height; + } + + @Override + public double calArea() { + return getArea() + Math.PI * radius * height; + } + + @Override + public double calVolume() { + return (1.0 / 3.0) * getArea() * height; + } +} + +class Cylinder extends CircularBase implements ThreeDimensionalShape { + double height; + + Cylinder(double radius, double height) { + super(radius); + this.height = height; + } + + @Override + public double calArea() { + return 2 * getArea() + 2 * Math.PI * radius * height; + } + + @Override + public double calVolume() { + return getArea() * height; + } +} + +public class CircularBaseExample { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + try { + System.out.print("Enter radius of Cone: "); + double coneRadius = scanner.nextDouble(); + System.out.print("Enter height of Cone: "); + double coneHeight = scanner.nextDouble(); + System.out.print("Enter radius of Cylinder: "); + double cylinderRadius = scanner.nextDouble(); + System.out.print("Enter height of Cylinder: "); + double cylinderHeight = scanner.nextDouble(); + Cone cone = new Cone(coneRadius, coneHeight); + Cylinder cylinder = new Cylinder(cylinderRadius, cylinderHeight); + + System.out.println("\nCone base area: " + cone.getArea()); + System.out.println("Cone total area: " + cone.calArea()); + System.out.println("Cone volume: " + cone.calVolume()); + + System.out.println("\nCylinder base area: " + cylinder.getArea()); + System.out.println("Cylinder total area: " + cylinder.calArea()); + System.out.println("Cylinder volume: " + cylinder.calVolume()); + } catch (InputMismatchException e) { + System.err.println("Invalid input. Please enter numerical values only."); + System.exit(1); + } + } +} diff --git a/java/code/CuboidCalculations.java b/java/code/CuboidCalculations.java new file mode 100644 index 0000000..b9c478f --- /dev/null +++ b/java/code/CuboidCalculations.java @@ -0,0 +1,39 @@ +class Rectangle { + int length; + int breadth; + + Rectangle(int length, int breadth) { + this.length = length; + this.breadth = breadth; + } + + int area() { + return length * breadth; + } +} + +class Cuboid extends Rectangle { + int height; + + Cuboid(int length, int breadth, int height) { + super(length, breadth); + this.height = height; + } + + int surfaceArea() { + return 2 * (area() + (length + breadth) * height); + } + + int volume() { + return area() * height; + } +} + +public class CuboidCalculations { + public static void main(String[] args) { + Cuboid cuboid = new Cuboid(5, 4, 3); + + System.out.println("Surface area of the Cuboid: " + cuboid.surfaceArea()); + System.out.println("Volume of the Cuboid: " + cuboid.volume()); + } +} diff --git a/java/code/CustomExceptionExample.java b/java/code/CustomExceptionExample.java new file mode 100644 index 0000000..9443c6e --- /dev/null +++ b/java/code/CustomExceptionExample.java @@ -0,0 +1,26 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +class NegativeNumberException extends Exception { + public NegativeNumberException(String message) { + super(message); + } +} + +public class CustomExceptionExample { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + try { + System.out.print("Enter an integer: "); + int userInput = scanner.nextInt(); + if (userInput < 0) { + throw new NegativeNumberException("Number cannot be less than zero"); + } + System.out.println("You entered: " + userInput); + } catch (InputMismatchException e) { + System.out.println("Input must be an integer."); + } catch (NegativeNumberException e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/java/code/InnerClassExample.java b/java/code/InnerClassExample.java new file mode 100644 index 0000000..74cbc4b --- /dev/null +++ b/java/code/InnerClassExample.java @@ -0,0 +1,27 @@ +class OuterClass { + private int outerMember = 10; + + class InnerClass { + private int innerMember = 20; + + void accessMembers() { + System.out.println("Outer member: " + outerMember); + System.out.println("Inner member: " + innerMember); + } + } + + void accessInnerMembers() { + InnerClass inner = new InnerClass(); + System.out.println("Inner member (through object): " + inner.innerMember); + } +} + +class InnerClassExample { + public static void main(String[] args) { + OuterClass outer = new OuterClass(); + OuterClass.InnerClass inner = outer.new InnerClass(); + + inner.accessMembers(); + outer.accessInnerMembers(); + } +} diff --git a/java/code/InterfaceExample.java b/java/code/InterfaceExample.java new file mode 100644 index 0000000..9df012e --- /dev/null +++ b/java/code/InterfaceExample.java @@ -0,0 +1,59 @@ +interface Interface1 { + void method1(); + void method2(); +} + +interface Interface2 { + void method3(); + void method4(); +} + +interface NewInterface extends Interface1, Interface2 { + void newMethod(); +} + +class ConcreteClass { + void concreteMethod() { + System.out.println("Concrete method in the concrete class"); + } +} + +class DerivedClass extends ConcreteClass implements NewInterface { + @Override + public void method1() { + System.out.println("Implementation of method1"); + } + + @Override + public void method2() { + System.out.println("Implementation of method2"); + } + + @Override + public void method3() { + System.out.println("Implementation of method3"); + } + + @Override + public void method4() { + System.out.println("Implementation of method4"); + } + + @Override + public void newMethod() { + System.out.println("Implementation of newMethod"); + } +} + +public class InterfaceExample { + public static void main(String[] args) { + DerivedClass obj = new DerivedClass(); + + obj.method1(); + obj.method2(); + obj.method3(); + obj.method4(); + obj.newMethod(); + obj.concreteMethod(); + } +} diff --git a/java/code/PackageExample.java b/java/code/PackageExample.java new file mode 100644 index 0000000..e45046d --- /dev/null +++ b/java/code/PackageExample.java @@ -0,0 +1,9 @@ +import pTwo.ClassTwo; + +public class PackageExample { + public static void main(String[] args) { + ClassTwo obj = new ClassTwo(); + + obj.methodTwo(); + } +} diff --git a/java/code/PackageInterfaceExample.java b/java/code/PackageInterfaceExample.java new file mode 100644 index 0000000..1347433 --- /dev/null +++ b/java/code/PackageInterfaceExample.java @@ -0,0 +1,12 @@ +import pkgTwo.MyClass; +import pkgOne.MyInterface; + +public class PackageInterfaceExample { + public static void main(String[] args) { + MyInterface obj = new MyClass(); + + obj.methodOne(); + obj.methodTwo(); + obj.methodThree(); + } +} diff --git a/java/code/ParentWithTwoChildren.java b/java/code/ParentWithTwoChildren.java index 20b04cb..f587637 100644 --- a/java/code/ParentWithTwoChildren.java +++ b/java/code/ParentWithTwoChildren.java @@ -26,7 +26,7 @@ class ChildOne extends Parent { @Override void display() { super.display(); - System.out.println(" Marks: " + marks); + System.out.println("\tMarks: " + marks); } } @@ -43,7 +43,7 @@ class ChildTwo extends Parent { @Override void display() { super.display(); - System.out.println(". Qualification: " + qualification + ", Salary: " + salary); + System.out.println("\tQualification: " + qualification + ", Salary: " + salary); } } diff --git a/java/code/RuntimePolymorphismExample.java b/java/code/RuntimePolymorphismExample.java new file mode 100644 index 0000000..b702ca7 --- /dev/null +++ b/java/code/RuntimePolymorphismExample.java @@ -0,0 +1,47 @@ +interface Shape { + void draw(); +} + +class Circle implements Shape { + @Override + public void draw() { + System.out.println("Drawing a Circle"); + } + + public void calculateArea() { + System.out.println("Calculating Circle Area"); + } +} + +class Square implements Shape { + @Override + public void draw() { + System.out.println("Drawing a Square"); + } + + public void calculateArea() { + System.out.println("Calculating Square Area"); + } +} + +public class RuntimePolymorphismExample { + public static void main(String[] args) { + // Creating objects of the subclasses + Shape circle = new Circle(); + Shape square = new Square(); + + // Demonstrate runtime polymorphism + // The draw() method of the appropriate subclass will be called dynamically + circle.draw(); + square.draw(); + + // Uncommenting the lines below will result in a compilation error + // since the calculateArea() method is not part of the Shape interface + // circle.calculateArea(); + // square.calculateArea(); + + // However, calculateArea() method can still be called if it is casted to the specific subclass + ((Circle) circle).calculateArea(); + ((Square) square).calculateArea(); + } +} diff --git a/java/code/ThreadsExample.java b/java/code/ThreadsExample.java new file mode 100644 index 0000000..33ad571 --- /dev/null +++ b/java/code/ThreadsExample.java @@ -0,0 +1,36 @@ +class NumberRunnable implements Runnable { + public void run() { + for (int i = 1; i <= 10; i++) { + System.out.println(Thread.currentThread().getId() + " - " + i); + } + } +} + +class NumberThread extends Thread { + public void run() { + for (int i = 1; i <= 10; i++) { + System.out.println(Thread.currentThread().getId() + " - " + i); + } + } +} + +public class ThreadsExample { + public static void main(String[] args) { + Thread[] threads = new Thread[6]; + + // Creating three threads using NumberThread class (inheriting Thread) + for (int i = 0; i < 3; i++) { + threads[i] = new NumberThread(); + } + + // Creating three threads using NumberRunnable class (implementing Runnable) + for (int i = 3; i < 6; i++) { + threads[i] = new Thread(new NumberRunnable()); + } + + // Starting all threads + for (Thread thread : threads) { + thread.start(); + } + } +} diff --git a/java/code/pOne/ClassOne.java b/java/code/pOne/ClassOne.java new file mode 100644 index 0000000..d8ee018 --- /dev/null +++ b/java/code/pOne/ClassOne.java @@ -0,0 +1,9 @@ +package pOne; + +public class ClassOne { + protected int variableOne = 10; + + public void methodOne() { + System.out.println("From methodOne in ClassOne"); + } +} diff --git a/java/code/pTwo/ClassTwo.java b/java/code/pTwo/ClassTwo.java new file mode 100644 index 0000000..7ea1f81 --- /dev/null +++ b/java/code/pTwo/ClassTwo.java @@ -0,0 +1,12 @@ +package pTwo; +import pOne.ClassOne; + +public class ClassTwo extends ClassOne { + public void methodTwo() { + System.out.println("MethodTwo in ClassTwo"); + + // Accessing variable and method from ClassOne + System.out.println("Variable from ClassOne: " + variableOne); + methodOne(); + } +} diff --git a/java/code/pkgOne/MyInterface.java b/java/code/pkgOne/MyInterface.java new file mode 100644 index 0000000..7196a3d --- /dev/null +++ b/java/code/pkgOne/MyInterface.java @@ -0,0 +1,7 @@ +package pkgOne; + +public interface MyInterface { + void methodOne(); + void methodTwo(); + void methodThree(); +} diff --git a/java/code/pkgTwo/MyClass.java b/java/code/pkgTwo/MyClass.java new file mode 100644 index 0000000..ad76398 --- /dev/null +++ b/java/code/pkgTwo/MyClass.java @@ -0,0 +1,19 @@ +package pkgTwo; +import pkgOne.MyInterface; + +public class MyClass implements MyInterface { + @Override + public void methodOne() { + System.out.println("Implementation of methodOne"); + } + + @Override + public void methodTwo() { + System.out.println("Implementation of methodTwo"); + } + + @Override + public void methodThree() { + System.out.println("Implementation of methodThree"); + } +} |