Question

In: Computer Science

Assignment #3 – Geometry Calculator Assignment Objectives Task #1 void Methods 1. Name the program file...

Assignment #3 – Geometry Calculator Assignment Objectives

Task #1 void Methods 1. Name the program file Geometry.java. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create it.

2. Below the main method, but in the Geometry class,create a static method called printMenu that has no parameter list and does not return a value. It will simply print out instructions for the user with a menu of options for the user to choose from. The menu should appear to the user as: This is a geometry calculator Choose what you would like to calculate

1. Find the area of a circle

2. Find the area of a rectangle

3. Find the area of a triangle

4. Find the circumference of a circle

5. Find the perimeter of a rectangle

6. Find the perimeter of a triangle Enter the number of your choice:

3. Add a line in the main method that calls the printMenu method as indicated by the comments.

4. Compile, debug, and run. You should be able to choose any option, but you will always get 0 for the answer. We will fix this in the next task any times as needed without rewriting the code each time.

Task #2 Value-Returning Methods

1. Write a static method called circleArea that takes in the radius of the circle and returns the area using the formula A = π r power 2.

2. Write a static method called rectangleArea that takes in the length and width of the rectangle and returns the area using the formula A = lw.

3. Write a static method called triangleArea that takes in the base and height of the triangle and returns the area using the formula A = ½bh.

4. Write a static method called circleCircumference that takes in the radius of the circle and returns the circumference using the formula C = 2πr.

5. Write a static method called rectanglePerimeter that takes in the length and the width of the rectangle and returns the perimeter of the rectangle using the formula P = 2l +2w.

6. Write a static method called trianglePerimeter that takes in the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.

Task #3 Calling Methods

1. Add lines in the main method in the GeometryDemo class which will call these methods.

2. Write some sample data and hand calculated results for you to test all 6 menu items.

3. Compile, debug, and run. Test out the program using your sample data.

Solutions

Expert Solution

Code:

Geometry.java

package assignment;
import java.util.Scanner;
public class Geometry
{
   static GeometryDemo g=new GeometryDemo();  
   public static void printMenu()
   {
       System.out.println("************This is a geometry calculator Choose what you would like to calculate*********");
       System.out.println("1. Find the area of a circle");
       System.out.println("2. Find the area of a rectangle");
       System.out.println("3. Find the area of triangle");
       System.out.println("4. Find the circumfance of a circle");
       System.out.println("5. Find the perimeter of a rectangle");
       System.out.println("6. Find the perimeter of a triangle");
       System.out.println("************************************************");
       System.out.println("Enter the number of your choise");
       Scanner sc=new Scanner(System.in);
       int c=sc.nextInt();
       switch(c) {
       case 1:
           System.out.println("Find the area of a circle");
           g.circleArea();
           System.out.println("*****************************");
           break;
       case 2:
           System.out.println("Find the area of a rectangle");
           g.rectangleArea();
           System.out.println("*****************************");
           break;
   case 3:
       System.out.println("Find the area of triangle");
       g.triangleArea();
       System.out.println("*****************************");
       break;
   case 4:
           System.out.println("Find the circumfance of a circle");
           g.circleCircumfernce();
           System.out.println("*****************************");
           break;
   case 5:
           System.out.println("Find the perimeter of a rectangle");
           g.rectanglePerimeter();
           System.out.println("*****************************");
           break;
   case 6:
           System.out.println("Find the perimeter of a triangle");
           g.trianglePerimeter();
           System.out.println("*****************************");
           break;      
   }
}
   public static void main(String[] args) {  
           printMenu();
   }
}
GeometryDemo.java

package assignment;
import java.util.Scanner;
public class GeometryDemo {
   static Scanner sc=new Scanner(System.in);
   public static void circleArea()
   {
       int r;
       double A;
       System.out.println("Enter the radius of the circle");
       r =sc.nextInt();
       A=(r*r)*Math.PI;
       System.out.println("Area of circle is: "+A);
      
   }
   public static void rectangleArea() {
       int l,w,A;
       System.out.println("Enter the length of the rectangle");
       l=sc.nextInt();
       System.out.println("Enter the breadth of the rectangle");
       w=sc.nextInt();
       A=l*w;
       System.out.println("Area of the rectangle is: "+A);
      
   }
   public static void triangleArea() {
       int h,b,A;
       System.out.println("Enter the height of the triangle");
       h=sc.nextInt();
       System.out.println("Enter the base of the triangle");
       b=sc.nextInt();
       A=(b*h)/2;
       System.out.println("Area of the triangle is"+A);
      
   }
   public static void circleCircumfernce() {
       int r;
       System.out.println("Enter the radius of the circle");
       r=sc.nextInt();
       double C=(Math.PI*2*r);
       System.out.println("Circumference of the circle is: "+C);
   }
   public static void rectanglePerimeter() {
       double P,l,w;
       System.out.println("Enter the length of the Rectangle");
       l=sc.nextDouble();
       System.out.println("Enter the width of the Rectangle");
       w=sc.nextDouble();
       P=2*(l+w);
       System.out.println("perimeter of Rectangle is: "+P);
   }
   public static void trianglePerimeter() {
       float a,b,c,total;
       System.out.println("Enter the a length");
       a=sc.nextFloat();
       System.out.println("Enter the b length");
       b=sc.nextFloat();
       System.out.println("Enter the c length");
       c=sc.nextFloat();
       total=a+b+c;
       System.out.println("perimeter of triangle is: "+total);
   }
}

Note:***I hope you happy with my answer***If you have any doubts please comment me*****Thank you......


Related Solutions

Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
Ch 4 Program 3  – Geometry Calculator Please adhere to the Standards for Programming Assignments and the...
Ch 4 Program 3  – Geometry Calculator Please adhere to the Standards for Programming Assignments and the Java Coding Guideline. : List of source code with comments to document Test output listed as comments at the end of your program GeometryCalculator.java (15 pts) Write a program that displays the following menu: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the volume of a cylinder Quit Prompt the user to enter...
---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. Geometry Calculator 1. Calculate the...
---------------------------------------------------------------------------------------------------------------- C++ //code a program for the following the given Instruction. Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use 3.14159 for pi. #03   If the user enters 2, the program should ask for the length and width of...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Change Calculator Task: Create a program that will input a price and a money amount. The...
Change Calculator Task: Create a program that will input a price and a money amount. The program will then decide if there is any change due and calculate how much change and which coins to hand out. Coins will be preferred in the least number of coins (starting with quarters and working your way down to pennies). Input: total_price, amount_tender allow the user to input 'q' for either value if they want to quit the program Validate: total_price cannot be...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT