Logic Problem
import java.util.*;
publicclass areas2Calculation
{
//Generic method to calculate area of the shapes square, rectangle, triangle and circle
publicstaticdouble generic_area(double length,double height,double base_length,double radius,double PI,char shape)
{
PI = 3.1415927;
switch(shape)
{
default:return 0.0;
case'1':return length * length;
case'2':return length * height;
case'3':return base_length * height;
case'4':return radius * radius * PI;
}
}
//Main method
publicstaticvoid main(String[] args)
{
//Declare variables
Scanner dataInput =new Scanner(System.in);
double length, result, square, size, height, rectangle, base_length, triangle, radius, circle;
double PI = 3.1415927d;
int i;
String choice, quit;
{
//Display input message
System.out.printf("Select An Option Below (All Dimensions in Inches & Areas in Sq. Inches)%n");
System.out.printf("Enter 1 To Calculate The Area of Squares.%n");
System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");
System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");
System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");
System.out.printf("Enter 5 to Quit.%n");
System.out.print("Enter option: ");
choice = dataInput.next();
while ((choice.compareTo("1") != 0) && (choice.compareTo("2") != 0) && (choice.compareTo("3") != 0) && (choice.compareTo("4") != 0) && (choice.compareTo("5") != 0))
{
System.out.printf("Invalid option! Please re-enter.%n");
System.out.printf("Select An Option Below%n");
System.out.printf("Enter 1 To Calculate The Area of Squares.%n");
System.out.printf("Enter 2 To Calculate the Area of Rectangles.%n");
System.out.printf("Enter 3 To Calculate The Area Of Triangles%n");
System.out.printf("Enter 4 To Calculate The Area Of Circles.%n");
System.out.printf("Enter 5 to Quit.%n");
System.out.print("Enter option: ");
choice = dataInput.next();
}
//Check for option 1
if (choice.compareTo("1") == 0)
{
//get dimentions to calculate the area of the square(s)
System.out.print("Enter the length of the square: ");
length = dataInput.nextDouble();
System.out.print("Enter the number of further square areas to calculate: ");
square = dataInput.nextDouble();
System.out.print("Enter the length increment: ");
size = dataInput.nextDouble();
//calculate area of squares until no more remain
for(i = 0; i < square; i++ )
{
result = generic_area(length,0,0,0,0,'1');
//display result and increase the length
System.out.println("Square " + (i+1) +" has a Length of " + length +" and an Area equal to " + result);
//result );
length = length + size;
}
}
//check for option 2
if (choice.compareTo("2") == 0)
{
//get dimentions to calculate the area of the rectangle(s)
System.out.print("Enter the length of the rectangle: ");
length = dataInput.nextDouble();
System.out.print("Enter the height of the rectangle: ");
height = dataInput.nextDouble();
System.out.print("Enter the number of further rectangle areas to calculate: ");
rectangle = dataInput.nextDouble();
System.out.print("Enter the length and height increment: ");
size = dataInput.nextDouble();
//calculate the area of rectangles until no more remain
for (i = 0; i < rectangle; i++)
{
result = generic_area(length, height,0,0,0,'2');
//display result, increase the length and the height
System.out.println("Rectangle " + (i+1) +" has a Length of " + length +", a Height of " + height +" and an Area equal to " + result);
length = length + size;
height = height + size;
}
}
//check for option 3
if (choice.compareTo("3") == 0)
{
//get dimentions to calculate the area of the triangle(s)
System.out.print("Enter the base length of the triangle: ");
base_length = dataInput.nextDouble();
System.out.print("Enter the height of the triangle: ");
height = dataInput.nextDouble();
System.out.print("Enter the number of further triangle areas to calculate: ");
triangle = dataInput.nextDouble();
System.out.print("Enter the base length and height increment: ");
size = dataInput.nextDouble();
//calculate the area of rectangles until no more remain
for (i = 0; i < triangle; i++)
{
result = generic_area(base_length, height,0,0,0,'3');
//display the result, increase the base_length and the height
System.out.println("Triangle " + (i+1) +" has a Base Length of " + base_length +", a Height of " + height +" and an Area equal to " + result);
base_length = base_length + size;
height = height + size;
}
}
//check for option 4
if (choice.compareTo("4") == 0)
{
//get dimentions to calculate the area of the circle(s)
System.out.print("Enter the radius of the circle: ");
radius = dataInput.nextDouble();
System.out.print("Enter the number of further circle areas to calculate: ");
circle = dataInput.nextDouble();
System.out.print("Enter the radius increment: ");
size = dataInput.nextDouble();
//calculate the area of circles until no more remain
for (i = 0; i < circle; i++)
{
result = generic_area(radius, PI,0,0,0,'4');
//display the result and increase the radius
System.out.println("Circle " + (i+1) +" has a Radius of " + radius +" and an Area equal to " + result);
radius = radius + size;
}
}
//check for option 5
if (choice.compareTo("5") == 0)
{
//user wants to quit
System.out.print("Are you sure? (Y): ");
quit = dataInput.next();
if (quit.compareTo("Y") != 0)
{
System.out.print("Are you sure? (Y): ");
quit = dataInput.next();
}
System.out.printf("Goodbye!");
}
}
}
}
The program runs fine and options 1 & 2 also work.
Options 3 & 4 however are giving results of ZERO. I know the solution is simple but I just can't see it.

