Question

In: Computer Science

Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...

Please write the program in JAVA and provide me with the output screenshots!!

Assignment Objectives
• Be able to write methods
• Be able to call methods
Introduction
Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can be called as many times as needed without rewriting the code each time.
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.
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 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

All explanation is provided in the comments of the code itself

Task #1:

Code--

import java.util.*;
public class Geometry
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       //call the printMenu
       printMenu();
       //get input
       int op=sc.nextInt();
       //we will get 0 for the answer
       System.out.println("Answer: 0");
   }
   //create a static method called print menu
   public static void printMenu()
   {
       System.out.println("This is a geometry calculator");
       System.out.println("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 a triangle");
       System.out.println("4. Find the circumference 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.print("Enter the number of your choice:");
   }
}

Code Screenshot--

Output Screenshot--

==========================================================================================

Task #2:

Code--

import java.util.*;
public class Geometry
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       //call the printMenu
       printMenu();
       //get input
       int op=sc.nextInt();
       //we will get 0 for the answer
       switch(op)
       {
       case 1:
           System.out.print("Area of circle is: ");
           System.out.println(circleArea(2.0));
           break;
       case 2:
           System.out.print("Area of rectangle is: ");
           System.out.println(rectangleArea(2.0,7.9));
           break;
       case 3:
           System.out.print("Area of triangle is: ");
           System.out.println(triangleArea(4.5,2.0));
           break;
       case 4:
           System.out.print("Circumference of circle is: ");
           System.out.println(circleCircumference(2.0));
           break;
       case 5:
           System.out.print("Perimeter of rectangle is: ");
           System.out.println(rectanglePerimeter(2.0,5.0));
           break;  
       case 6:
           System.out.print("Perimeter of triangle is: ");
           System.out.println(trianglePerimeter(1.0,4.5,2.0));
           break;
       default:
           System.out.println("Wrong option...!");
       }
   }
   //create a static method called print menu
   public static void printMenu()
   {
       System.out.println("This is a geometry calculator");
       System.out.println("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 a triangle");
       System.out.println("4. Find the circumference 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.print("Enter the number of your choice:");
   }
   //method to find the area of circle
   public static double circleArea(double r)
   {
       return Math.PI*r*r;
   }
   //method to find the area of rectangle
   public static double rectangleArea(double l,double w)
   {
       return l*w;
   }
   //method to find the area of triangle
   public static double triangleArea(double b,double h)
   {
       return 0.5*b*h;
   }
   //method to find the circumference of a circle
   public static double circleCircumference(double r)
   {
       return (double)2*Math.PI*r;
   }
   //method to find the rectangle perimeter
   public static double rectanglePerimeter(double l,double w)
   {
       return 2.0*(l+w);
   }
   //method to find the perimeter of triangle
   public static double trianglePerimeter(double a,double b,double c)
   {
       return a+b+c;
   }
}

Code Screenshot--

code is quite long so I did not paste the screenshot to avoid chaois.

Output Screenshot--

===========================================================================================

Note--

I have given default data to call the static methods you can even take the user inputs and call then methods.

Please upvote if you like the effort.


Related Solutions

PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string...
Java and please have screenshots with source code and output \item[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as...
can you assist me in getting the desired output? Homework 5-3 Write a Java program that...
can you assist me in getting the desired output? Homework 5-3 Write a Java program that prompts the user for an int n. You can assume that 1 ≤ n ≤ 9. You program should use two embedded for loops that produce the following output:     1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 . . . This is the code: import java.util.Scanner; public class PatternOne {    public static void main(String[] args) {...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to...
Please attach the output screenshots, narrative descriptions, or paste the Python codes. Please write code to print the type of the following variables. Please write down the codes and the output as well. x = 3.5 y = '3.5' z = {1:'John', 2:'Wick', 3:'Barry', 4:'Allen'}
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
I am struggling with this assignment for my java class. Objectives: Your program will be graded...
I am struggling with this assignment for my java class. Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1.Create a new project and class in Eclipse and add the main method. (5 points) 2. Construct a Scanner to read input from the keyboard. (5 points) 3. Prompt the user with three questions from the Psychology Today quiz, and use the Scanner to read their...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Please in C++, show screenshots of output plus .txt file. thanks Breakfast Billing System Write a...
Please in C++, show screenshots of output plus .txt file. thanks Breakfast Billing System Write a program to help a local restaurant automate its breakfast billing system. The program should do the following: Show the customer the different breakfast items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT