Question

Project Objectives: To develop ability to write void and value returning methods and to call them...

Project Objectives: To develop ability to write void and value returning methods and to call them --
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

0. Develop void Methods named printHeader and printTrailer which prints the required information (as used in the previous Programming Assignments)– Call printHeader at the beginning of the main method and printTrailer at the end of main method.

1.   Type the code (Geometry.java) shown in at the end of this handout. 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.

2.   Above 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 Geometry class which will call these methods. The comments indicate where to place the method calls.

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

What to submit: Submit source code file Geometry.java and an output file (PPA5out.docx) which produces output for each of the six cases shown above.

Code Listing (Geometry.java)

import java.util.Scanner;

// Demonstration of static methods

public class Geometry

{

//insert your method definitions here

public static void main (String [] args)

{

int choice;          //the user's choice

double value = 0;    //the value returned from the method

char letter;         //the Y or N from the user's decision to exit

double radius;                   //the radius of the circle

double length;              //the length of the rectangle

double width,height;                     //the width and height of the rectangle

double base;                //the base of the triangle

double side1,side2,side3;                     // sides of the triangle

//create a scanner object named keyboard – Note we are using keyboard instead of scnr

Scanner keyboard = new Scanner (System.in);

//do loop was chose to allow the menu to be displayed first
do

{

//call the printMenu method here

choice = keyboard.nextInt();

switch (choice)

{

case 1:

System.out.print("Enter the radius of the circle: ");

radius = keyboard.nextDouble();

//call the circleArea method and store the result in the value

System.out.println("The area of the circle is " + value);

break;

case 2:

System.out.print("Enter the length of the rectangle: ");

length = keyboard.nextDouble();

System.out.print("Enter the width of the rectangle: ");

width = keyboard.nextDouble();

//call the rectangleArea method and store the result in the value

System.out.println("The area of the rectangle is " + value);

break;

case 3:

System.out.print("Enter the height of the triangle: ");

height = keyboard.nextDouble();

System.out.print("Enter the base of the triangle: ");

base = keyboard.nextDouble();

//call the triangleArea method and store the result in the value

System.out.println("The area of the triangle is " + value);

break;

case 4:

System.out.print("Enter the radius of the circle: ");

radius = keyboard.nextDouble();

//call the circumference method and store the result in the value

System.out.println("The circumference of the circle is " + value);

break;

case 5:

System.out.print("Enter the length of the rectangle: ");

length = keyboard.nextDouble();

System.out.print("Enter the width of the rectangle: ");

width = keyboard.nextDouble();

//call the perimeter method and store the result in the value

System.out.println("The perimeter of the rectangle is " + value);

break;

case 6:

System.out.print("Enter the length of side 1 of the triangle: ");

side1 = keyboard.nextDouble();

System.out.print("Enter the length of side 2 of the triangle: ");

side2 = keyboard.nextDouble();

System.out.print("Enter the length of side 3 of the triangle: ");

side3 = keyboard.nextDouble();

//call the perimeter method and store the result in the value

System.out.println("The perimeter of the triangle is " + value);

break;

default:

System.out.println("You did not enter a valid choice.");

     }//end of switch statement

//consumes the new line character after the number before reading a String

keyboard.nextLine();

System.out.println("Do you want to exit the program (Y/N)?: ");

String answer = keyboard.nextLine();

letter = answer.charAt(0); //select first character of response

}while (letter != 'Y' && letter != 'y'); //end of do-while loop

}//end of main

}//end of class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Geometry {
  
// method definitions
private static void printHeader()
{
System.out.println("This is a geometry calculator.");
}
  
private static void printTrailer()
{
System.out.println("Thank you for using the geometry calculator.\nGoodbye!");
}
  
private static void printMenu()
{
System.out.print("Choose what you would like to calculate\n" +
"1. Find the area of a circle\n" +
"2. Find the area of a rectangle\n" +
"3. Find the area of a triangle\n" +
"4. Find the circumference of a circle\n" +
"5. Find the perimeter of a rectangle\n" +
"6. Find the perimeter of a triangle\n" +
"Enter the number of your choice:");
}
  
private static double circleArea(double radius)
{
return (Math.PI * Math.pow(radius, 2));
}
  
private static double rectangleArea(double length, double width)
{
return(length * width);
}
  
private static double triangleArea(double base, double height)
{
return(0.5 * base * height);
}
  
private static double circleCircumference(double radius)
{
return(2 * Math.PI * radius);
}
  
private static double rectanglePerimeter(double length, double width)
{
return(2 * (length + width));
}
  
private static double trianglePerimeter(double side1, double side2, double side3)
{
return(side1 + side2 + side3);
}
  
// man method
public static void main(String[] args)
{
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter = 0; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width, height; //the width and height of the rectangle
double base; //the base of the triangle
double side1, side2, side3; // sides of the triangle
  
Scanner keyboard = new Scanner (System.in);
printHeader();
  
do
{
printMenu();
choice = keyboard.nextInt();
switch(choice)
{
case 1:
{
System.out.print("Enter the radius of the circle: ");
radius = keyboard.nextDouble();
value = circleArea(radius);
System.out.println("The area of the circle is " + value);
break;
}
case 2:
{
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
value = rectangleArea(length, width);
System.out.println("The area of the rectangle is " + value);
break;
}
case 3:
{
System.out.print("Enter the height of the triangle: ");
height = keyboard.nextDouble();
System.out.print("Enter the base of the triangle: ");
base = keyboard.nextDouble();
value = triangleArea(base, height);
System.out.println("The area of the triangle is " + value);
break;
}
case 4:
{
System.out.print("Enter the radius of the circle: ");
radius = keyboard.nextDouble();
value = circleCircumference(radius);
System.out.println("The circumference of the circle is " + value);
break;
}
case 5:
{
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
value = rectanglePerimeter(length, width);
System.out.println("The perimeter of the rectangle is " + value);
break;
}
case 6:
{
System.out.print("Enter the length of side 1 of the triangle: ");
side1 = keyboard.nextDouble();
System.out.print("Enter the length of side 2 of the triangle: ");
side2 = keyboard.nextDouble();
System.out.print("Enter the length of side 3 of the triangle: ");
side3 = keyboard.nextDouble();
value = trianglePerimeter(side1, side2, side3);
System.out.println("The perimeter of the triangle is " + value);
break;
}
default:
System.out.println("You did not enter a valid choice.");
}
//consumes the new line character after the number before reading a String
keyboard.nextLine();
System.out.print("Do you want to exit the program (Y/N)?: ");
String answer = keyboard.nextLine();
letter = answer.charAt(0); //select first character of response
}while(letter != 'Y' && letter != 'y');
printTrailer();
}
}

***************************************************************** SCREENSHOT **********************************************************

Add a comment
Know the answer?
Add Answer to:
Project Objectives: To develop ability to write void and value returning methods and to call them...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • This exercise guides you through the process of converting an Area and Perimeter application from a...

    This exercise guides you through the process of converting an Area and Perimeter application from a procedural application to an object-oriented application. Create and use an object 1. Open the project named ch04_ex2_Area and Perimeter that’s stored in the ex_starts folder. Then, review the code for the Main class. 2. Create a class named Rectangle and store it in the murach.rectangle package. 3. In the Rectangle class, add instance variables for length and width. The, code the get and set...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

    In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • 6. define a C++ class having the following members: (1) 2 instance methods  the prototype...

    6. define a C++ class having the following members: (1) 2 instance methods  the prototype of the first method is double funA(int r). The parameter r represents the radius of a circle. The funA will calculate the area of a circle and the value of its radius is from the parameter r. It will return the area of the circle.  the prototype of the second method is double funB(int w, int h). The parameters w and h represent...

  • I have this program: It passes on certain test cases. The Demo five has the %.1f...

    I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as: Enter length: \n Enter width: \n You entered: 87.3, 2.0, and 174.7\n -- Rectangle info --\n Length: 87.34\n Width: 2.0\n Area: 174.68\n And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you. Program Content:...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT