Home / Questions / Task #1 void Methods 1. Copy the file Geometry.java (code listing 5.1) from www.aw.com/cssupport or
Task #1 void Methods
1. Copy the file Geometry.java (code listing 5.1) from www.aw.com/cssupport or as directed by your instructor. This program will compile, but when you run it, itdoesn’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 createthis.
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 simplyprint 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.
Gaddis_516907_Java 4/10/07 2:10 PM Page 42
Chapter 5 Lab Methods 43
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 = pr2. 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 = 1??2bh. 4. Write a static method called circleCircumference that takes in the radius of the
circle and returns the circumference using the formula C = 2pr.
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 formulaP = 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 cal-culated by adding up the three sides.
Gaddis_516907_Java 4/10/07 2:10 PM Page 43
44
Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects
Task #3 Calling Methods
1. Add lines in the main method in the GeometryDemo class which will call these methods. The comments indicate where to place the method calls.
2. Below, 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.
Gaddis_516907_Java 4/10/07 2:10 PM Page 44
Task #4 Java Documentation
1. Write javadoc comments for each of the 7 static methods that you just wrote. They should include
a) A one line summary of what the method does.
a) A description of what the program requires to operate and what the result of that operation is.
a) @param listing and describing each of the parameters in the parameter list (if any).
a) @return describing the information that is returned to the calling statement (if any).
2. Generate the documentation. Check the method summary and the method details to ensure your comments were put into the Java Documentation correctly.
Chapter 5 Lab Methods 45
Gaddis_516907_Java 4/10/07 2:10 PM Page 45
46 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects
Code Listing 5.1 (Geometry.java)
import java.util.Scanner;
/** This program demonstrates static methods
*/
public class Geometry {
public static void main (String [] args) {
int choice; double value = 0; char letter;
double radius; double length; double width; double height; double base; double side1; double side2; double side3;
//the user’s choice //the value returned from the method
//the Y or N from the user’s decision //to exit
//the radius of the circle //the length of the rectangle //the width of the rectangle //the height of the triangle
//the base of the triangle //the first side of the triangle //the second side of the triangle //the third side of the triangle
//create a scanner object to read from the keyboard Scanner keyboard = new Scanner (System.in);
//do loop was chose to allow the menu to be displayed //first do {
//call the printMenu method choice = keyboard.nextInt();
switch (choice)
{
Code Listing 5.1 continued on next page.
Gaddis_516907_Java 4/10/07 2:10 PM Page 46
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;
Code Listing 5.1 continued on next page.
Chapter 5 Lab Methods 47
Gaddis_516907_Java 4/10/07 2:10 PM Page 47
48
Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects
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:
Code Listing 5.1 continued on next page.
Gaddis_516907_Java 4/10/07 2:10 PM Page 48
}
System.out.println( "You did not enter a valid choice.");
//consumes the new line character after the number keyboard.nextLine();
System.out.println("Do you want to exit the program " + "(Y/N)?: ");
String answer = keyboard.nextLine();
letter = answer.charAt(0); }while (letter != ‘Y’ &&letter != ‘y’);
Apr 17 2020 View more View Less
Feedback can be used[A]As a barrier [B]As a way for sources to evaluate their communication effectiveness [C]To ensure that instructions will be carried out [D]To eval...
Jan 05 2018You are vice president of Sales for a medium sized technology firm and have been asked to design a new compensation package for the company’s national sales force of 50 p...
May 21 2020Consider an economy in which all taxes are autonomous and the following values oof autonomous consumption, planned investment, government expenditure, autonomous taxes, a...
Apr 23 2020IntroductionForces outside the company's conventional boundaries are progressively important in determining the company's achievements. These causes in “the environment o...
Jul 27 2020Hastings Corporation is interested in acquiring Vandell Corporation. Vandell has 1 million shares outstanding and a target capital structure consisting of 30% debt. Vande...
Aug 31 2020On January 1, 2019, YTR Company issued $1,700,000 of 15-year, 8% coupon bonds. The bonds pay interest semi-annually and they sold for $2,040,000. The bonds can be called ...
May 17 2020Pablo Company calculates the cost for an equivalent unit of production using processing costing. Compute cost per equivalent unit for both direct materials and conversion...
Aug 11 2020The following information is available for Sunset Valley Hotel for July 2013: The following is a list of checks and deposits recorded on the books of the Sunset Valley ...
Apr 11 2020Easy Nutrition completed the following selected transactions:Requirements1. Record the transactions in Easy Nutrition’s journal. Round all amounts to the nearest dollar. ...
Mar 31 2020Estimate the change in the Gibbs energy of 1.0 dm3 of water when the pressure acting on it is increased from 100 kPa to 300 kPa.
Apr 01 2020