Please! pretty please, I need help! *** Is there any computer sicence Genius who
ID: 3818623 • Letter: P
Question
Please! pretty please, I need help!
*** Is there any computer sicence Genius who can help me with this assigment***
Objectives
This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows:
If the user enters an invalid menu option, or invalid data for a given option, have the program print a helpful error message and re-prompt for that input. Note, this could be either a wrong value or invalid data type.
Code Specifications
For this assignment, you will write two different classes. The goal is to separate the user interface code, or client code, from the supplier code that performs calculations. The sample files MyMath.java and TestMyMath.java are a good reference.
In a class named SquareRootMath, write static methods that use parameters to get information and return a value. You must provide 2 public methods to perform the 3 different operations:
Note that the SquareRootMath class does not need a 'main' method. However, you may write one for testing purposes if you wish. You must use parameter lists and return statements to move data in and out of these methods. No calculation method in this class should use System.out.println or the Scanner class (except in an optional test method). In other words, calculation methods not should interact with the end user; that's the job of the other class.
Your methods must implement precondition tests on their parameters and throw exceptions when appropriate. You can read about this in section 4.4 of the textbook.
In a class named SquareRootApp, create a console-based, menu-driven user interface that behaves like the one described above. This class must use a 'main' method to start things off. However, you are encouraged to decompose your solution to organize your code.
No class variables may be used; class constants are ok.
Your program should not throw any exceptions to the user. If a user enters an invalid menu option, the program should display a helpful error message and ask for new input.
You must implement your solution using loops.
Testing & Documentation
As always, I recommend doing this in pieces. For example, you might choose to do the calculation code first (testing as you go). Then focus on the user interface.
The area under the curve over the interval [0, 2] should approach 1.885618
Your documentation in both classes is an integral part to your solution since you are the designer/author of these classes.
Each class should have an overall description comment at the top
Be sure to include a block comment before each method that describes what it does, describes the parameters and the return.
Make sure to include internal comments to guide the reader through your algorithms.
File Submission
Please compress both your files into one .jar or .zip file and upload that.
Grading
/15 execution and design
/5 documentation and style
Good Luck!
The Rectangle Method specifies to divide up the area under the curve into rectangles. The interval to be divided up is [0, x]. For example, consider the image here, to find the area of the curve over the interval [0, 2].In addition to a value for x, the client programmer specifies the number of rectangles in the interval, call that n. In this example n = 4. The method divides up the interval into n sections, then calculates f(x) for each section to find the height of the upper left corner of a rectangle. Each rectangle's width is 1/n, or 1/4 in the example. Width * height gives you the area of the rectangle. You approximate the area of the curve by adding up the areas of the rectangles in the interval. The more rectangles, the better the approximation.
NOTE: We're using the Leftbox Method, where the point on the curve is the upper left corner of the rectangle. Notice then that the first rectangle has height 0.
Explanation / Answer
Here are the 2 files of code that you need.
SquareRootMath.java (Supplier class):
/**
* Class to perform calculations (Supplier class).
*/
public class SquareRootMath {
/* Error value for methods. */
public static final double ERROR_RESULT = -1;
/* Rectangle count for the lftbox method to get desired result (e.g. 1.885618 for [0,2])*/
private static long RECTANGLE_COUNT = 20000000L;
private static String ERROR_MSG_X_NEGATIVE = "x should be greater than 0, please re-check input.";
/**
* Static method to get slope of tangent to the curve.
* @param x The point where the tangent line slope is needed.
* @return Slope of the tangent at the point (x, f(x)) if x > 0, -1 otherwise.
*/
public static double getSlopeOfTangent(float x) {
if (x <= 0) {
// Display an error message.
System.out.println(ERROR_MSG_X_NEGATIVE);
return -1;
}
double slope = 1/ (2* Math.sqrt(x));
return slope;
}
/**
* Static method to get area under the curve f(x) = sqrt(x)
* @param x x-coordinate of the curve.
* @return Area under the curve f(x) = sqrt(x) if x > 0, -1 in case of invalid input.
*/
public static double getAreaOfCurve(double x) {
if (x < 0) {
System.out.println(ERROR_MSG_X_NEGATIVE);
return -1;
}
double area = 0.0;
double xTemp = 0.0;
double height = 0.0;
double width = x/((double) RECTANGLE_COUNT);
// Get height of each rectangle by looping through RECTANGLE_COUNT.
// Also compute area simultaneously for each rectangle and add it to area field.
// First rectangle's x is always 0 so we start from i = 1.
for (int i = 1; i < RECTANGLE_COUNT; i++) {
// Get height of each rectangle using it's x-coordinate.
xTemp = i * width;
height = Math.sqrt(xTemp);
// Add each rectangle's area to area field.
area += width * height;
}
return area;
}
}
SquareRootApp.java (Client class) :
import java.util.Scanner;
/**
* Client class to call methods.
**/
public class SquareRootApp {
public static void main (String[] args) {
int choice = 0;
Scanner scanner = new Scanner(System.in);
int x;
while (choice != 3) {
// Display a choice based menu.
displayMenu();
choice = scanner.nextInt();
// Check choice before proceeding further.
if (choice < 1 || choice > 3) {
// Display error.
displayError(choice);
continue;
}
// Quit if choice = 3.
if (choice == 3) {
System.out.println("Quitting.");
break;
}
System.out.println("Enter x: ");
x = scanner.nextInt();
double result = callMethod(choice, x);
// Keep looping while x < 0.
while (result == SquareRootMath.ERROR_RESULT) {
// Re-input x.
System.out.println("Enter x: ");
x = scanner.nextInt();
result = callMethod(choice, x);
}
// Display results.
displayResult(result, choice);
}
}
/* Display result on basis of method selected. */
private static void displayResult(double result, int choice) {
if (choice == 1) {
System.out.println("Slope of the tangent is: " + result);
} else if (choice == 2) {
System.out.println("Area under the curve is: " + result);
}
}
/* Method to display user-menu. */
private static void displayMenu() {
System.out.println(" Welcome to Square Root App. Choose any of 3 options (Press 1,2 or 3.)");
System.out.println("1. Find the slope at a given point");
System.out.println("2. Approximate area");
System.out.println("3. Quit");
}
/* Method to display user-menu input error. */
private static void displayError(int choice) {
System.out.println(choice + " is not a valid input option, please choose between 1-3");
}
/* Call methods on basis of choice. */
private static double callMethod(int choice, int x) {
if (choice == 1) {
return SquareRootMath.getSlopeOfTangent(x);
} else {
return SquareRootMath.getAreaOfCurve(x);
}
}
}
Also, I would like to point out an error in the diagram. If you check, x-axis coordinate 2 is smaller than y-axis 2. I think it should be 1 on x-axis then it can be divided into 1/n pieces.
I have included relevant comments/documentation where I felt necessary. Also, I have tested the program for any invalid inputs/ corner cases. I hope this will help.