summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--java/code/CircularBaseExample.java86
-rw-r--r--java/code/CuboidCalculations.java39
-rw-r--r--java/code/CustomExceptionExample.java26
-rw-r--r--java/code/InnerClassExample.java27
-rw-r--r--java/code/InterfaceExample.java59
-rw-r--r--java/code/PackageExample.java9
-rw-r--r--java/code/PackageInterfaceExample.java12
-rw-r--r--java/code/ParentWithTwoChildren.java4
-rw-r--r--java/code/RuntimePolymorphismExample.java47
-rw-r--r--java/code/ThreadsExample.java36
-rw-r--r--java/code/pOne/ClassOne.java9
-rw-r--r--java/code/pTwo/ClassTwo.java12
-rw-r--r--java/code/pkgOne/MyInterface.java7
-rw-r--r--java/code/pkgTwo/MyClass.java19
-rw-r--r--java/index.typ2
-rw-r--r--java/output/CuboidCalculations.typ6
-rw-r--r--java/output/EmployeeDisplay.typ7
-rw-r--r--java/output/ParentWithTwoChildren.typ4
-rw-r--r--java/state.sql19
19 files changed, 416 insertions, 14 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");
+    }
+}
diff --git a/java/index.typ b/java/index.typ
index b9a4907..e2caab9 100644
--- a/java/index.typ
+++ b/java/index.typ
@@ -47,7 +47,7 @@
 + 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.
 + 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 [do all without package statement].
 + Create an Interface. Create two sub-classes implementing the interface. Show the functionalities of runtime polymorphism / dynamic method dispatch using them.
-5. Create a class with variable(s) and method(s) (all will be default accessed) under package `pOne`. Now create a class under package `pTwo`, which is subclass of firstly created class. In the method here (_i.e._ class of `pTwo`) call variable(s) and method(s) of previous class (_i.e._ class of `pOne`). If errors come, rectify them. Now from `main()` (under working directory) access members of the second class.
++ Create a class with variable(s) and method(s) (all will be default accessed) under package `pOne`. Now create a class under package `pTwo`, which is subclass of firstly created class. In the method here (_i.e._ class of `pTwo`) call variable(s) and method(s) of previous class (_i.e._ class of `pOne`). If errors come, rectify them. Now from `main()` (under working directory) access members of the second class.
 + Create an interface containing three methods, in a package `pkgOne`. Implement the interface from a class under package `pkgTwo`. From `main()`, under working directory, create object of the class and call methods of interface.
 + Write a program to take an integer number from the user. If the number is less then zero then throw a custom exception. Also, catch the exception when other datatypes except integers are given. 
 + Write a program in Java to create three threads printing 1 to 100. Implement the program using both inheriting Thread class and implementing Runnable interface.
diff --git a/java/output/CuboidCalculations.typ b/java/output/CuboidCalculations.typ
new file mode 100644
index 0000000..28593ef
--- /dev/null
+++ b/java/output/CuboidCalculations.typ
@@ -0,0 +1,6 @@
+#import "/template.typ": highlight-output
+#highlight-output[```
+Surface area of the Cuboid: 94
+Volume of the Cuboid: 60
+```]
+
diff --git a/java/output/EmployeeDisplay.typ b/java/output/EmployeeDisplay.typ
new file mode 100644
index 0000000..9891423
--- /dev/null
+++ b/java/output/EmployeeDisplay.typ
@@ -0,0 +1,7 @@
+#import "/template.typ": highlight-output
+#highlight-output[```
+Employee named "Kim Ji-hyung" with ID 87416846.
+Scientist named "Daniel Lemire" with ID 14534, 80 publications and 25 years of experience.
+Distinguished scientist named "Donald Ervin Knuth" with ID 11, 185 publications, 60 years of experience and awardee of Grace Murray Hopper Award (1971), Turing Award (1974), National Medal of Science (1979), John von Neumann Medal (1995), Harvey Prize (1995), Kyoto Prize (1996), Faraday Medal (2011).
+```]
+
diff --git a/java/output/ParentWithTwoChildren.typ b/java/output/ParentWithTwoChildren.typ
index de253ec..0fb3ff1 100644
--- a/java/output/ParentWithTwoChildren.typ
+++ b/java/output/ParentWithTwoChildren.typ
@@ -2,8 +2,8 @@
 #highlight-output[```
 Parent: ID: 1, Name: John Doe, Address: 123 Main St.
 ChildOne: ID: 2, Name: Jane Doe, Address: 456 Elm St.
-    Marks: 85
+	Marks: 85
 ChildTwo: ID: 3, Name: Mike Smith, Address: 789 Oak St.
-    Qualification: B.Tech, Salary: 50000.0
+	Qualification: B.Tech, Salary: 50000.0
 ```]
 
diff --git a/java/state.sql b/java/state.sql
index 51d5dc1..24bc8fe 100644
--- a/java/state.sql
+++ b/java/state.sql
@@ -20,14 +20,15 @@ INSERT INTO `assignments` VALUES
     ('ShapeAreaCalculations', 1, 0, 0, 0),
     ('AbstractShapeCalculations', 1, 0, 0, 0),
     ('ParentWithTwoChildren', 1, 1, 1, 0),
-    ('CuboidCalculations', 0, 0, 0, 0),
-    ('EmployeeDisplay', 1, 0, 0, 0),
-    ('CircularBaseExample', 0, 0, 0, 0),
-    ('InnerClassExample', 0, 0, 0, 0),
-    ('InterfaceExample', 0, 0, 0, 0),
-    ('RuntimePolymorphismExample', 0, 0, 0, 0),
-    ('PackageExample', 0, 0, 0, 0),
-    ('CustomExceptionExample', 0, 0, 0, 0),
-    ('ThreadsExample', 0, 0, 0, 0);
+    ('CuboidCalculations', 1, 1, 0, 0),
+    ('EmployeeDisplay', 1, 1, 0, 0),
+    ('CircularBaseExample', 1, 0, 0, 0),
+    ('InnerClassExample', 1, 0, 0, 0),
+    ('InterfaceExample', 1, 0, 0, 0),
+    ('RuntimePolymorphismExample', 1, 0, 0, 0),
+    ('PackageExample', 1, 0, 0, 0),
+    ('PackageInterfaceExample', 1, 0, 0, 0),
+    ('CustomExceptionExample', 1, 0, 0, 0),
+    ('ThreadsExample', 1, 0, 0, 0);
 
 -- SELECT * FROM `assignments`;