1111AAA
3 years ago
40
Lab05_edited.pdf
AreaRectangle.java
TeamExerciseUpdated.docx
TeamExerciseUpdated.docx
Lab5_Geometry.java
- DivisibilityTest_MainQS.png
- exercise.jmp
- Exercise_MainQS.png
- hw3.png
- exercise.jmp
- AreaRectangle_MainQS.png
- Exercise_MainQS.png
Lab05_edited.pdf
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
Chapter 5 Lab
Methods
Lab Objectives
Be able to write methods
Be able to call methods
Be able to write javadoc comments
Be able to create HTML documentation using the javadoc utility
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.
Finally, we will use documentation comments for each method, and generate HTML
documents similar to the Java APIs that we have seen.
Task #1 void Methods
1. Copy the file Geometry.java (Code Listing 5.1) from the Student Files or
as directed by your instructor. 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. 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:
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
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. 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.
Task #4 Java Documentation
1. Write javadoc comments for each of the 7 static methods you just wrote.
They should include:
a. A one line summary of what the method does.
b. A description of what the program requires to operate and what the result
of that operation is.
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
c. @param listing and describing each of the parameters in the parameter list
(if any).
d. @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.
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; // 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 the rectangle
double width; // The width of the rectangle
double height; // The height of the triangle
double base; // The base of the triangle
double side1; // The first side of the triangle
double side2; // The second side of the triangle
double side3; // The third side of the triangle
// Create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// The do loop allows the menu to be displayed first
do
{
// TASK #1 Call the printMenu method
choice = keyboard.nextInt();
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
switch (choice)
{
case 1:
System.out.print("Enter the radius of " +
"the circle: ");
radius = keyboard.nextDouble();
// TASK #3 Call the circleArea method and
// store the result in the value variable
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();
// TASK #3 Call the rectangleArea method and
// store the result in the value variable
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();
// TASK #3 Call the triangleArea method and
// store the result in the value variable
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();
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
// TASK #3 Call the circumference method and
// store the result in the value variable
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();
// TASK #3 Call the perimeter method and
// store the result in the value variable
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();
// TASK #3 Call the perimeter method and
// store the result in the value variable
System.out.println("The perimeter of " +
"the triangle is " +
value);
break;
default:
System.out.println("You did not enter " +
"a valid choice.");
}
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
keyboard.nextLine(); // Consume the new line
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');
}
// TASK #1 Create the printMenu method here
// TASK #2 Create the value-returning methods here
// TASK #4 Write javadoc comments for each method
}
AreaRectangle.java
/** You must complete this program so it calculates and displays the area of a rectangle. */ // Insert any necessary import statements here. public class AreaRectangle { public static void main(String[] args) { double length, // The rectangle's length width, // The rectangle's width area; // The rectangle's area // Get the rectangle's length from the user. length = getLength(); // Get the rectangle's width from the user. width = getWidth(); // Get the rectangle's area. area = getArea(length, width); // Display the rectangle data. displayData(length, width, area); } }
TeamExerciseUpdated.docx
Name:
Date:
Module 5 Team Exercise
Part 1
Use the GSS data from 2018 to test whether the average political attitude of respondents differs from 4, which is the middle score. (Do this using the variable “Think of self as liberal or conservative” and a one-sample t test.) Write up your results in a sentence or two, being careful to convey the direction of your findings.
|
|
Part 2
If you search (by typing in the left portion of the GSS2018 screen) for “Confidence” you will see a series of variables asking about “Confidence in various things” (like “Confidence in the military.”) You can use these variables to practice paired t tests.
For example, test whether in general people tend to be more confident in the press or in medicine. You need to be careful here about the direction of the confidence variable. (Do higher numbers mean higher or lower confidence?) Write up your results in a sentence or two, being careful to convey the direction of your findings. Do this analysis by creating a new variable that is the difference of the two paired variables.
|
|
Conduct another paired t-test using a different pair of GSS variables. This time, however, do it directly, without creating a new variable. (Use the “Matched pairs” option in JMP.) Write up your results.
|
|
TeamExerciseUpdated.docx
Name:
Date:
Module 5 Team Exercise
Part 1
Use the GSS data from 2018 to test whether the average political attitude of respondents differs from 4, which is the middle score. (Do this using the variable “Think of self as liberal or conservative” and a one-sample t test.) Write up your results in a sentence or two, being careful to convey the direction of your findings.
|
|
Part 2
If you search (by typing in the left portion of the GSS2018 screen) for “Confidence” you will see a series of variables asking about “Confidence in various things” (like “Confidence in the military.”) You can use these variables to practice paired t tests.
For example, test whether in general people tend to be more confident in the press or in medicine. You need to be careful here about the direction of the confidence variable. (Do higher numbers mean higher or lower confidence?) Write up your results in a sentence or two, being careful to convey the direction of your findings. Do this analysis by creating a new variable that is the difference of the two paired variables.
|
|
Conduct another paired t-test using a different pair of GSS variables. This time, however, do it directly, without creating a new variable. (Use the “Matched pairs” option in JMP.) Write up your results.
|
|
Lab5_Geometry.java
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 the rectangle double width; // The width of the rectangle double height; // The height of the triangle double base; // The base of the triangle double side1; // The first side of the triangle double side2; // The second side of the triangle double side3; // The third side of the triangle // Create a scanner object to read from the keyboard Scanner keyboard = new Scanner(System.in); // The do loop allows the menu to be displayed first do { // TASK #1 Call the printMenu method choice = keyboard.nextInt(); switch(choice) { case 1: System.out.print("Enter the radius of " + "the circle: "); radius = keyboard.nextDouble(); // TASK #3 Call the circleArea method and // store the result in the value variable 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(); // TASK #3 Call the rectangleArea method and // store the result in the value variable 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(); // TASK #3 Call the triangleArea method and // store the result in the value variable 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(); // TASK #3 Call the circumference method and // store the result in the value variable 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(); // TASK #3 Call the perimeter method and // store the result in the value variable 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(); // TASK #3 Call the perimeter method and // store the result in the value variable System.out.println("The perimeter of " + "the triangle is " + value); break; default: System.out.println("You did not enter " + "a valid choice."); } keyboard.nextLine(); // Consume the new line 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'); } // TASK #1 Create the printMenu method here // TASK #2 Create the value-returning methods here // TASK #4 Write javadoc comments for each method }