-
-
Save rumaisaabdulhai/935ec4ccbe7a302cce2bc02a4f9eb0f4 to your computer and use it in GitHub Desktop.
Week 5 Homework Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* This class contains the solutions to | |
* the Week 5 homework exercises. | |
*/ | |
public class WeekFiveHW { | |
public static void main(String[] args) { | |
// Mandatory Problem 1 | |
System.out.println("\nMandatory Problem 1\n"); // to separate problems | |
double s = 6.3; // change numbers to see what happens | |
double r = 2.5; | |
double h = 5.0; | |
// calling each method and storing volumes in variables | |
double vol1 = sphereVolume(r); | |
double vol2 = cubeVolume(s); | |
double vol3 = coneVolume(r, h); | |
// printing volumes | |
System.out.println("\t" + "Sphere volume is " + vol1); | |
System.out.println("\t" + "Cube volume is " + vol2); | |
System.out.println("\t" + "Cone volume is " + vol3); | |
// Mandatory Problem 2 | |
System.out.println("\nMandatory Problem 2\n"); // to separate problems | |
guessFruit("orange"); | |
// Optional Problem 1 | |
System.out.println("\nOptional Problem 1\n"); // to separate problems | |
System.out.println("\tDistance: " + getDistance(4.5, 9.0, 8.3, 1.7)); | |
} | |
/* Mandatory Problem 1: Volumes | |
- Create three methods: sphereVolume(), cubeVolume(), and coneVolume() that | |
calculate and return the volume of a sphere, cube, and cone | |
- sphereVolume() should have 1 parameter (the radius) | |
- cubeVolume() should have 1 parameter (the side length) | |
- coneVolume() should have 2 parameters (the radius and height) | |
- Each method should use one or more Math class methods | |
*/ | |
// sphereVolume() method | |
public static double sphereVolume(double radius) { | |
return (4.0/3.0) * Math.PI * Math.pow(radius, 3.0); | |
} | |
// cubeVolume() method | |
public static double cubeVolume(double side_length) { | |
return Math.pow(side_length, 3.0); | |
} | |
// coneVolume() method | |
public static double coneVolume(double radius, double height) { | |
return (1.0/3.0) * Math.PI * Math.pow(radius, 2.0) * height; | |
} | |
/* Mandatory Problem 2: Guess the Fruit | |
- Create a method called guessFruit() that returns void (null) that: | |
- Takes in String input from the user | |
- User must guess the correct fruit | |
- While the guess does not equal the correct fruit, the program should | |
keep asking the user to guess a fruit | |
- If the user input equals the correct answer, the program should print | |
“You guessed the right fruit!” | |
- Should use one or more String methods | |
- Hint: You should use a while loop! And the “!=” operator will NOT | |
work in this problem | |
*/ | |
// guessFruit method | |
public static void guessFruit(String fruit) { | |
Scanner userInput = new Scanner(System.in); // create scanner object | |
System.out.println("\tGuess the fruit!"); // ask user question | |
String guess = userInput.nextLine(); // get input from user | |
// while guess is not equal to correct fruit | |
while(!guess.equals(fruit)) { | |
System.out.println("\tSorry, try again: "); | |
guess = userInput.nextLine(); | |
} | |
// congratulate user on guessing the fruit | |
System.out.println("\tYay! You guessed the right fruit!"); | |
userInput.close(); | |
} | |
/* Optional Problem 1: Get Distance | |
- Create a method called getDistance that calculates the | |
distance between any two points in a coordinate plane | |
- It should have 4 parameters | |
- It should return the distance | |
- It should use one or more Math class methods | |
*/ | |
// getDistance() method | |
public static double getDistance(double x1, double y1, double x2, double y2) { | |
// get delta x and delta y | |
double dx_squared = Math.pow((x2 - x1), 2); | |
double dy_squared = Math.pow((y2 - y1), 2); | |
return Math.sqrt(dx_squared + dy_squared); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment