This assignment will give you practice with interactive prog

This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for information about two figures and compute area for each figure.

The sample log of execution indicates how your program is to behave. For each figure, we prompt for width, height (rectangle) and base, height (triangle) and radius (circle). After obtaining area for each figure, the program reports how they compare.

As indicated in the sample log of execution, your program is to report the area for each figure. In addition to reporting the area for each figure, the program should also produce whichever of the following messages is appropriate:

The rectangle seems to be bigger than the triangle.

The triangle seems to be bigger than the rectangle.

The triangle and rectangle seem to be equal.

You should use static methods to eliminate redundant code and to break the problem up into logical subtasks. Your main method should be short so that a person can easily see the overall structure of the program. You are to introduce at least five static methods other than main. In this program, none of your methods should have more than 12 lines of code in the body of the method (not counting blank lines or lines with just curly braces on them). The 12-line limitation is a special requirement for this assignment because I want you to practice breaking up a program into methods. Be sure to once again include a short comment at the beginning of your program as well as a short comment for each method describing what it does. Name your program:

import java.util.*;

Sample log of execution (user input bold )

Information for figure 1: Type 1) rectangle 2) triangle 3) circle 1

height? 5.0

width? 60.0

Information for figure 2: Type 1) rectangle 2) triangle 3) circle 2

base? 20.4

height? 5.5

rectangle area = 300.0

triangle area = 56.1

The rectangle seems to be bigger.

Solution

// CompareObjects.java

import java.util.Scanner;
class CompareObjects
{
   // get user choicee
   public static int menu(int n)
   {
       int choice = 0;
       Scanner scanner = new Scanner(System.in);
       if(n == 1)
       {
           System.out.print(\"Information for figure 1: Type 1) rectangle 2) triangle 3) circle: \");
           choice = scanner.nextInt();
       }

       else if(n == 2)
       {
           System.out.print(\"Information for figure 2: Type 1) rectangle 2) triangle 3) circle: \");
           choice = scanner.nextInt();
       }

       return choice;
   }

   // get triangle area
   public static double triangleArea()
   {
       Scanner scanner = new Scanner(System.in);
       System.out.print(\"base? :\");
        double base = scanner.nextDouble();
        System.out.print(\"height? \");
        double height = scanner.nextDouble();

       return 0.5*base*height;
   }

   // get circle area
   public static double circleArea()
   {
       Scanner scanner = new Scanner(System.in);
       System.out.print(\"radius? :\");
        double radius = scanner.nextDouble();
      
       return 3.14*radius*radius;
   }

   // get rectangle area
   public static double rectangleArea()
   {
       Scanner scanner = new Scanner(System.in);
        System.out.print(\"height? :\");
        double height = scanner.nextDouble();
        System.out.print(\"width? \");
        double width = scanner.nextDouble();

       return width*height;
   }

   // get comparison result
   public static String getresult(int choice1, int choice2, double area1, double area2)
   {
       if(choice1 == 1 && choice2 == 2)
       {
           if(area1 > area2)
               return \"The rectangle seems to be bigger than the triangle.\";
           else if(area1 < area2)
               return \"The triangle seems to be bigger than the rectangle.\";
           else
               return \"The triangle and rectangle seem to be equal.\";
       }
       else if(choice1 == 2 && choice2 == 3)
       {
           if(area1 > area2)
               return \"The triangle seems to be bigger than the circle.\";
           else if(area1 < area2)
               return \"The circle seems to be bigger than the triangle.\";
           else
               return \"The triangle and circle seem to be equal.\";
       }
       else if(choice1 == 3 && choice2 == 1)
       {
           if(area1 > area2)
               return \"The circle seems to be bigger than the rectangle.\";
           else if(area1 < area2)
               return \"The rectangle seems to be bigger than the circle.\";
           else
               return \"The circle and rectangle seem to be equal.\";
       }

       return \"Invaid Input\";
   }

   // call the restlt function on basis of user choicee
   public static String compare(int choice1, int choice2, double area1, double area2)
   {

       if(choice1 == 1 && choice2 == 2)
       {
           return (getresult(choice1,choice2, area1,area2));
       }
       else if(choice1 == 2 && choice2 == 3)
       {
           return (getresult(choice1,choice2, area1,area2));
       }
       else if(choice1 == 3 && choice2 == 1)
       {
           return (getresult(choice1,choice2, area1,area2));
       }
       else if(choice1 == 2 && choice2 == 1)
       {
           return (getresult(choice2,choice1, area1,area2));
       }
       else if(choice1 == 3 && choice2 == 2)
       {
           return (getresult(choice2,choice1, area1,area2));
       }
       else if(choice1 == 1 && choice2 == 3)
       {
           return (getresult(choice2,choice1, area1,area2));
       }
       else
       {
           System.out.println(\"Invalid Input\");
           System.exit(1);
       }

       return \"Invaid Input\";
   }

   // get arear of given objects
   public static double getArea(int choice)
   {
       if(choice == 1)
           return rectangleArea();
       else if(choice == 2)
           return triangleArea();
       else
           return circleArea();
      
   }

   // print area on baiss of choice and input area
   public static void printArea(int choice1, int choice2, double area1, double area2)
   {
       if(choice1 == 1 && choice2 == 2)
       {
           System.out.println(\"rectangle area: \" + area1);
           System.out.println(\"triangle area: \" + area2);
       }
       else if(choice1 == 2 && choice2 == 3)
       {
           System.out.println(\"triangle area: \" + area1);
           System.out.println(\"circle area: \" + area2);
       }
       else if(choice1 == 3 && choice2 == 1)
       {
           System.out.println(\"circle area: \" + area1);
           System.out.println(\"rectangle area: \" + area2);
       }
       else if(choice1 == 2 && choice2 == 1)
       {
           System.out.println(\"triangle area: \" + area1);
           System.out.println(\"rectangle area: \" + area2);
       }
       else if(choice1 == 3 && choice2 == 2)
       {
           System.out.println(\"circle area: \" + area1);
           System.out.println(\"triangle area: \" + area2);
       }
       else if(choice1 == 1 && choice2 == 3)
       {
           System.out.println(\"rectangle area: \" + area1);
           System.out.println(\"circle area: \" + area2);
       }
  
   }
    public static void main (String[] args)
   {
     
       int choice1 = menu(1);
       double area1 = getArea(choice1);

       int choice2 = menu(2);
       double area2 = getArea(choice2);

       printArea(choice1,choice2,area1,area2);
       System.out.println(compare(choice1,choice2,area1,area2));
   }
}

/*
output:

Information for figure 1: Type 1) rectangle 2) triangle 3) circle: 1
height? :5.0
width? 60.0
Information for figure 2: Type 1) rectangle 2) triangle 3) circle: 2
base? :20.4
height? 5.5
rectangle area: 300.0
triangle area: 56.1
The rectangle seems to be bigger than the triangle.

*/

This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for infor
This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for infor
This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for infor
This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for infor
This assignment will give you practice with interactive programs, if/else statements and methods that return values. Your program will prompt the user for infor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site