summary refs log tree commit diff stats
path: root/java/code
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/code
parentcd6a9243c056710794549a1ab5d51fbdd082ce2e (diff)
downloadzadania-fc4d96a9e3e20f8ddf035c16fc32b1effb75481b.tar.gz
Complete all Java assignments
Diffstat (limited to 'java/code')
-rw-r--r--java/code/AbstractShapeCalculations.java82
-rw-r--r--java/code/CircularBaseExample.java2
-rw-r--r--java/code/EmployeeDisplay.java89
-rw-r--r--java/code/ThreadsExample.java4
4 files changed, 87 insertions, 90 deletions
diff --git a/java/code/AbstractShapeCalculations.java b/java/code/AbstractShapeCalculations.java
index 3f18c0c..2b13279 100644
--- a/java/code/AbstractShapeCalculations.java
+++ b/java/code/AbstractShapeCalculations.java
@@ -2,54 +2,54 @@ import java.util.Scanner;
 import java.util.InputMismatchException;
 
 abstract class Shape {
-	abstract double area();
-	abstract void display();
+    abstract double area();
+    abstract void display();
 }
 
 class Rectangle extends Shape {
-	double length, width;
-	Rectangle(double len, double wid) {
-		length = len;
-		width = wid;
-	}
-	double area() {
-		return length * width;
-	}
-	void display() {
-		System.out.println("A rectangle with length " + length + " units and width " + width + " units has the area of " + area() + " square units.");
-	}
+    double length, width;
+    Rectangle(double len, double wid) {
+        length = len;
+        width = wid;
+    }
+    double area() {
+        return length * width;
+    }
+    void display() {
+        System.out.println("A rectangle with length " + length + " units and width " + width + " units has the area of " + area() + " square units.");
+    }
 }
 
 class Circle extends Shape {
-	double radius;
-	Circle(double r) {
-		radius = r;
-	}
-	double area() {
-		return Math.PI * radius * radius;
-	}
-	void display() {
-		System.out.println("A circle with radius " + radius + " units has the area of " + area() + " square units.");
-	}
+    double radius;
+    Circle(double r) {
+        radius = r;
+    }
+    double area() {
+        return Math.PI * radius * radius;
+    }
+    void display() {
+        System.out.println("A circle with radius " + radius + " units has the area of " + area() + " square units.");
+    }
 }
 
 class Triangle extends Shape {
-	double a, b, c;
-	Triangle(double s1, double s2, double s3) {
-		a = s1;
-		b = s2;
-		c = s3;
-	}
-	double perimeter() {
-		return a + b + c;
-	}
-	double area() {
-		var s = perimeter() / 2.0;
-		return Math.sqrt(s * (s - a) * (s - b) * (s - c));
-	}
-	void display() {
-		System.out.println("A triangle with side lengths " + a + " units, " + b + "units and " + c + " units has the area of " + area() + " square units.");
-	}
+    double a, b, c;
+    Triangle(double s1, double s2, double s3) {
+        a = s1;
+        b = s2;
+        c = s3;
+    }
+    double perimeter() {
+        return a + b + c;
+    }
+    double area() {
+        var s = perimeter() / 2.0;
+        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
+    }
+    void display() {
+        System.out.println("A triangle with side lengths " + a + " units, " + b + " units and " + c + " units has the area of " + area() + " square units.");
+    }
 }
 
 class AbstractShapeCalculations {
@@ -65,12 +65,12 @@ class AbstractShapeCalculations {
             Shape s;
             s = new Rectangle(length, width);
             s.display();
-            System.out.println("Circle");
+            System.out.println("\nCircle");
             System.out.print("  Enter radius: ");
             double radius = sc.nextDouble();
             s = new Circle(radius);
             s.display();
-            System.out.println("Triangle");
+            System.out.println("\nTriangle");
             System.out.print("  Enter three side lengths: ");
             double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble();
             s = new Triangle(a, b, c);
diff --git a/java/code/CircularBaseExample.java b/java/code/CircularBaseExample.java
index c9a21fe..77b4a74 100644
--- a/java/code/CircularBaseExample.java
+++ b/java/code/CircularBaseExample.java
@@ -80,7 +80,7 @@ public class CircularBaseExample {
             System.out.println("Cylinder volume: " + cylinder.calVolume());
         } catch (InputMismatchException e) {
             System.err.println("Invalid input. Please enter numerical values only.");
-	    System.exit(1);
+            System.exit(1);
         }
     }
 }
diff --git a/java/code/EmployeeDisplay.java b/java/code/EmployeeDisplay.java
index 6642540..5aab9d0 100644
--- a/java/code/EmployeeDisplay.java
+++ b/java/code/EmployeeDisplay.java
@@ -1,58 +1,55 @@
 class Employee {
-	int id;
-	String name;
-	Employee(int eid, String ename) {
-		id = eid;
-		name = ename;
-	}
-	public String toString() {
-		return "Employee named \"" + name + "\" with ID " + id + ".";
-	}
+    int id;
+    String name;
+    Employee(int eid, String ename) {
+        id = eid;
+        name = ename;
+    }
+    public String toString() {
+        return "Employee named \"" + name + "\" with ID " + id + ".";
+    }
 }
 
 class Scientist extends Employee {
-	int number_of_publications, experience;
-	Scientist(int sid, String sname, int nopubs, int yexp) {
-		super(sid, sname);
-		number_of_publications = nopubs;
-		experience = yexp;
-	}
-	public String toString() {
-		return "Scientist named \"" + name + "\" with ID " + id +
-			", " + number_of_publications + " publications and " +
-			experience + " years of experience" + ".";
-	}
+    int number_of_publications, experience;
+    Scientist(int sid, String sname, int nopubs, int yexp) {
+        super(sid, sname);
+        number_of_publications = nopubs;
+        experience = yexp;
+    }
+    public String toString() {
+        return "Scientist named \"" + name + "\" with ID " + id + ", " +
+            number_of_publications + " publications and " + experience +
+            " years of experience" + ".";
+    }
 }
 
 class DScientist extends Scientist {
-	String[] award;
-	DScientist(int sid, String sname, int nopubs, int yexp,
-			String[] dsaward) {
-		super(sid, sname, nopubs, yexp);
-		award = dsaward;
-	}
-	public String toString() {
-		return "Distinguished scientist named \"" + name +
-			"\" with ID " + id +
-			", " + number_of_publications + " publications, " +
-			experience + " years of experience and awardee of " +
-			String.join(", ", award) + ".";
-	}
+    String[] award;
+    DScientist(int sid, String sname, int nopubs, int yexp,
+               String[] dsaward) {
+        super(sid, sname, nopubs, yexp);
+        award = dsaward;
+    }
+    public String toString() {
+        return "Distinguished scientist named \"" + name + "\" with ID " + id +
+            ", " + number_of_publications + " publications, " + experience +
+            " years of experience and awardee of " + String.join(", ", award) + ".";
+    }
 }
 
 class EmployeeDisplay {
-	public static void main(String args[]) {
-		Employee e;
-		e = new Employee(87416846, "Kim Ji-hyung");
-		System.out.println(e);
-		e = new Scientist(14534, "Daniel Lemire", 80, 25);
-		System.out.println(e);
-		e = new DScientist(11, "Donald Ervin Knuth", 185, 60,
-				new String[] { "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)"});
-		System.out.println(e);
-	}
+    public static void main(String args[]) {
+        Employee e;
+        e = new Employee(87416846, "Kim Ji-hyung");
+        System.out.println(e);
+        e = new Scientist(14534, "Daniel Lemire", 80, 25);
+        System.out.println(e);
+        e = new DScientist(11, "Donald Ervin Knuth", 185, 60,
+            new String[] { "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)"});
+        System.out.println(e);
+    }
 }
 
diff --git a/java/code/ThreadsExample.java b/java/code/ThreadsExample.java
index 33ad571..e300666 100644
--- a/java/code/ThreadsExample.java
+++ b/java/code/ThreadsExample.java
@@ -1,7 +1,7 @@
 class NumberRunnable implements Runnable {
     public void run() {
         for (int i = 1; i <= 10; i++) {
-            System.out.println(Thread.currentThread().getId() + " - " + i);
+            System.out.println("Runnable Thread " + Thread.currentThread().getId() + " - Tick " + i);
         }
     }
 }
@@ -9,7 +9,7 @@ class NumberRunnable implements Runnable {
 class NumberThread extends Thread {
     public void run() {
         for (int i = 1; i <= 10; i++) {
-            System.out.println(Thread.currentThread().getId() + " - " + i);
+            System.out.println("Subclass Thread " + Thread.currentThread().getId() + " - Tick " + i);
         }
     }
 }