Skip to content

Instantly share code, notes, and snippets.

@rumaisaabdulhai
Created February 23, 2021 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rumaisaabdulhai/993750f12c51a979feefbd247071c6cf to your computer and use it in GitHub Desktop.
Save rumaisaabdulhai/993750f12c51a979feefbd247071c6cf to your computer and use it in GitHub Desktop.
Week 5 Classwork Solutions
/**
* This class contains the solutions to
* the Week 5 classwork exercises.
*/
public class WeekFiveCW {
public static void main(String[] args) {
// Problem 1
System.out.println("\nClasswork Problem 1\n"); // to separate problems
double num1 = 3; // change these to see what happens
double num2 = 5.5;
System.out.println("\tSum: " + add(num1, num2));
System.out.println("\tDifference: " + subtract(num1, num2));
System.out.println("\tProduct: " + multiply(num1, num2));
System.out.println("\tQuotient: " + divide(num1, num2));
// Problem 2
System.out.println("\nClasswork Problem 2\n"); // to separate problems
double radius = 1.8; // change this to see what happens
System.out.println("\tArea: " + circleArea(radius));
}
/* Problem 1: Arithmetic Operations
- Make four custom methods: add, subtract, multiply, & divide
- Each method should:
- Take two double parameters (x & y)
- Compute the arithmetic operation with the numbers
- Return the result
- Call each method in the main method
- Print the answers
*/
public static double add(double x, double y) {
return x + y;
}
public static double subtract(double x, double y) {
return x - y;
}
public static double multiply(double x, double y) {
return x * y;
}
public static double divide(double x, double y) {
return x / y;
}
/* Problem 2: Circle Area
- Create a method called circleArea() that calculates the area of
a circle
- It should have 1 parameter - the radius r
- It should return the area
- It should use one or more Math class methods
*/
public static double circleArea(double r) {
return Math.PI * Math.pow(r, 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment