patternjavaMinor
Beginners project: calculating area
Viewed 0 times
beginnerscalculatingprojectarea
Problem
This was my second project as a beginner in Java programming. Please provide feedback/ tips on my program.
```
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner Area = new Scanner (System.in);
double length, width,totalArea, height;
String shape, unit;
int loop = 0;
while (loop == 0){
System.out.println("Which shape do you want to find the area?");
shape = Area. next();
switch (shape) {
case "rectangle":
System.out.println("Please enter the leghth of the rectangle: ");
length = Area. nextDouble ();
System.out.println("Please enter the width of the rectangle: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = length*width;
System.out.println(totalArea+unit);
break;
case "square" :
System.out.println("Please enter the leghth of the square: ");
length = Area. nextDouble ();
System.out.println("Please enter the width of the square: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = length*width;
System.out.println(totalArea+unit);
break;
case "triangle" :
System.out.println("Please enter the base of the triangle: ");
length = Area. nextDouble ();
System.out.println("Please enter the height of the triangle: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = ((length*width)/2);
System.out.println(totalArea+unit);
break;
case "parallelogram" :
System.out.println("Please e
```
import java.util.Scanner;
public class Area {
public static void main(String[] args) {
Scanner Area = new Scanner (System.in);
double length, width,totalArea, height;
String shape, unit;
int loop = 0;
while (loop == 0){
System.out.println("Which shape do you want to find the area?");
shape = Area. next();
switch (shape) {
case "rectangle":
System.out.println("Please enter the leghth of the rectangle: ");
length = Area. nextDouble ();
System.out.println("Please enter the width of the rectangle: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = length*width;
System.out.println(totalArea+unit);
break;
case "square" :
System.out.println("Please enter the leghth of the square: ");
length = Area. nextDouble ();
System.out.println("Please enter the width of the square: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = length*width;
System.out.println(totalArea+unit);
break;
case "triangle" :
System.out.println("Please enter the base of the triangle: ");
length = Area. nextDouble ();
System.out.println("Please enter the height of the triangle: ");
width = Area. nextDouble();
System.out.println("What is the unit of the shape?");
unit = Area.next();
totalArea = ((length*width)/2);
System.out.println(totalArea+unit);
break;
case "parallelogram" :
System.out.println("Please e
Solution
First, you have improved yourself. But I still see the habit of your
structure in both the programs you have written so far (Your program doesn't actually iterate as you don't change the
As your loop-body is not more than 5000 or more lines, it's better to use a
Polymorphism (Factory Pattern + Strategy Pattern)
You can use Polymorphism in your program to make it nice. (If you don't know what is polymorphism)
Switch cases are good but replacing switch-case with polymorphism is fantastic. Look, you have different things to do in each switch case. Basically, different shapes formed with different ways.
Shape Interface
ShapeClub.java (Main class)
ShapeFactory.java (Factory class)
Rectangle.java (Concrete class)
Circle.java (Another concrete class)
Hope you understood it. :)
int loop = 0;
while (loop == 0){...structure in both the programs you have written so far (Your program doesn't actually iterate as you don't change the
loop variable). Actually, while loops are faster than any other loops. On the other hand, it is also important to write more clean & clear code.As your loop-body is not more than 5000 or more lines, it's better to use a
do-while(){...} looping structure.Polymorphism (Factory Pattern + Strategy Pattern)
You can use Polymorphism in your program to make it nice. (If you don't know what is polymorphism)
Switch cases are good but replacing switch-case with polymorphism is fantastic. Look, you have different things to do in each switch case. Basically, different shapes formed with different ways.
Shape Interface
public interface Shape {
double calculateArea();
void buildShape(Scanner scanner);
}ShapeClub.java (Main class)
public class ShapeClub {
public static void main(String[] args){
Scanner scanner = new Scanner (System.in);
boolean retry = false;
do{
System.out.println("Which shape do you want to find the area?");
Shape unknownShape = ShapeFactory.getShape(scanner.next());
unknownShape.buildShape(scanner);
System.out.print("Do you want to try a different shape?[Y/N]:");
retry = scanner.next().equalsIgnoreCase("Y");
}while (retry);
scanner.close();
}
}ShapeFactory.java (Factory class)
public class ShapeFactory {
private ShapeFactory(){}
public static Shape getShape(String shapeName){
switch (shapeName){
case "rectangle":
return new Rectangle();
case "circle":
return new Circle();
default:
return null;
}
}
}Rectangle.java (Concrete class)
public class Rectangle implements Shape {
private double length = 0.0;
private double width = 0.0;
private String unit ="";
@Override
public double calculateArea() {
return length*width;
}
@Override
public void buildShape(Scanner scanner) {
System.out.println("Please enter the leghth of the rectangle: ");
length = scanner.nextDouble ();
System.out.println("Please enter the width of the rectangle: ");
width = scanner.nextDouble();
System.out.println("What is the unit of the shape?");
unit = scanner.next();
System.out.println(calculateArea()+unit);
}
}Circle.java (Another concrete class)
public class Circle implements Shape {
private double radius = 0.0;
private String unit ="";
@Override
public double calculateArea() {
return Math.PI*(Math.pow(radius, 2));
}
@Override
public void buildShape(Scanner scanner) {
System.out.println("Please enter the radius of the circle: ");
radius = scanner.nextDouble();
System.out.println("What is the unit of the shape?");
unit = scanner.next();
System.out.println(calculateArea()+unit);
}
}Hope you understood it. :)
Code Snippets
int loop = 0;
while (loop == 0){...public interface Shape {
double calculateArea();
void buildShape(Scanner scanner);
}public class ShapeClub {
public static void main(String[] args){
Scanner scanner = new Scanner (System.in);
boolean retry = false;
do{
System.out.println("Which shape do you want to find the area?");
Shape unknownShape = ShapeFactory.getShape(scanner.next());
unknownShape.buildShape(scanner);
System.out.print("Do you want to try a different shape?[Y/N]:");
retry = scanner.next().equalsIgnoreCase("Y");
}while (retry);
scanner.close();
}
}public class ShapeFactory {
private ShapeFactory(){}
public static Shape getShape(String shapeName){
switch (shapeName){
case "rectangle":
return new Rectangle();
case "circle":
return new Circle();
default:
return null;
}
}
}public class Rectangle implements Shape {
private double length = 0.0;
private double width = 0.0;
private String unit ="";
@Override
public double calculateArea() {
return length*width;
}
@Override
public void buildShape(Scanner scanner) {
System.out.println("Please enter the leghth of the rectangle: ");
length = scanner.nextDouble ();
System.out.println("Please enter the width of the rectangle: ");
width = scanner.nextDouble();
System.out.println("What is the unit of the shape?");
unit = scanner.next();
System.out.println(calculateArea()+unit);
}
}Context
StackExchange Code Review Q#135812, answer score: 6
Revisions (0)
No revisions yet.