From fc4d96a9e3e20f8ddf035c16fc32b1effb75481b Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sun, 28 Jan 2024 06:38:55 +0000 Subject: Complete all Java assignments --- java/code/AbstractShapeCalculations.java | 82 ++++++++++++++++---------------- 1 file changed, 41 insertions(+), 41 deletions(-) (limited to 'java/code/AbstractShapeCalculations.java') 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); -- cgit 1.4.1-2-gfad0