Skip to content

Commit

Permalink
java-prac-3-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajjitlai committed Oct 15, 2024
1 parent 17d2cd5 commit c4d1368
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
Binary file modified Java/Practical/practical-03/Circle.class
Binary file not shown.
Binary file modified Java/Practical/practical-03/Rectangle.class
Binary file not shown.
Binary file modified Java/Practical/practical-03/ShapeProgram.class
Binary file not shown.
57 changes: 30 additions & 27 deletions Java/Practical/practical-03/ShapeProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class Rectangle extends Shape {

@Override
public void readData() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length of the rectangle: ");
length = sc.nextDouble();
System.out.print("Enter breadth of the rectangle: ");
breadth = sc.nextDouble();
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter length of the rectangle: ");
length = sc.nextDouble();
System.out.print("Enter breadth of the rectangle: ");
breadth = sc.nextDouble();
}
}

@Override
Expand All @@ -31,9 +32,10 @@ class Circle extends Shape {

@Override
public void readData() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius of the circle: ");
radius = sc.nextDouble();
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Enter radius of the circle: ");
radius = sc.nextDouble();
}
}

@Override
Expand All @@ -44,27 +46,28 @@ public double area() {

public class ShapeProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Shape shape = null;
try (Scanner sc = new Scanner(System.in)) {
Shape shape = null;

System.out.println("Choose a shape to calculate area:");
System.out.println("1. Rectangle");
System.out.println("2. Circle");
int choice = sc.nextInt();
System.out.println("Choose a shape to calculate area:");
System.out.println("1. Rectangle");
System.out.println("2. Circle");
int choice = sc.nextInt();

switch (choice) {
case 1:
shape = new Rectangle();
break;
case 2:
shape = new Circle();
break;
default:
System.out.println("Invalid choice!");
return;
}
switch (choice) {
case 1:
shape = new Rectangle();
break;
case 2:
shape = new Circle();
break;
default:
System.out.println("Invalid choice!");
return;
}

shape.readData();
System.out.println("The area of the shape is: " + shape.area());
shape.readData();
System.out.println("The area of the shape is: " + shape.area());
}
}
}

0 comments on commit c4d1368

Please sign in to comment.