Objectives: Learn to use the Java API methods for some basic Math operations. Or
ID: 3733059 • Letter: O
Question
Objectives:
Learn to use the Java API methods for some basic Math operations.
Organize your code into proper methods. See final exam guide lines as well in announcements.
Problem
Take a look at the Javadocs for the java.lang.Math class:
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html (Links to an external site.)Links to an external site.
Write a program that serves as a special calculator. It knows how to solve 3 things. When the program starts, print the following main menu:
Area of Circle
Solve a Quadratic Equation
Compound interest
Exit
At this time let the user enter a number and based on the choice perform the action (as described below). Assume a correct answer will always be given so you can skip error checking.
For each question print the result to the console and make sure to submit your program and the output.
Print the Area of Circle (Links to an external site.)Links to an external site.. Let the user input a number which is the Radius. Print the area rounded upto 2 places of decimal. Eg. Radius=5, Area is 78.54
Solve the equation (for eg: 2x2 - 4x -3 = 0 )(Quadratic equation (Links to an external site.)Links to an external site.). Let the user enter three values A, B and C. Round your answers upto 2 places of decimal.
Compute the compound interest. Eg: What is the total amount if $1500 is deposited in a bank paying 4.3% quarterly interest for 6 years? Round upto 2 places of decimal. (Formula (Links to an external site.)Links to an external site.).
Accept all the input values:
P = principal amount (the initial amount you borrow or deposit)
r = annual rate of interest (as a decimal)
t = number of years the amount is deposited or borrowed for.
n = number of times the interest is compounded per year
Exit the program
After solving the question selected go back and print the main menu again and let the user make a choice again.
Notes
You will have to use a number of functions defined on the page above!
Your style of code is important. See how you can leverage what you have learnt about subroutines and reusing them in this class so far
Explanation / Answer
MenuOperations.java
import java.util.Scanner;
public class MenuOperations {
public static void main(String[] args) {
//Declaring variables
int choice;
double radius, areaOfCircle, a = 0, b = 0, c = 0;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" :: Menu ::");
System.out.println("1.Area of Circle");
System.out.println("2.Solve a Quadratic Equation");
System.out.println("3.Compound interest");
System.out.println("4.Exit");
System.out.print(" Enter Choice :");
choice = sc.nextInt();
switch (choice) {
case 1:
{
System.out.print("Enter radius :");
radius = sc.nextDouble();
System.out.printf("Area of Circle :%.2f", Math.PI * radius * radius);
continue;
}
case 2:
{
//Getting the number 'a' entered by the user
System.out.print(" Enter a,b,c:");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
//Declaring variables
double rootx = 0,
rooty = 0;
int val = 0;
double discriminate = Math.pow(b, 2) - 4 * a * c;
double part1 = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
//If the discriminate is positive calculate the two real roots an return 2 to the caller
if (discriminate > 0) {
//calculating the two real roots
rootx = ((-b + part1) / (2 * a));
rooty = ((-b - part1) / (2 * a));
//Displaying the two real roots
System.out.printf("The equation has Two roots %.2f and %.2f ", rootx, rooty);
}
//If the discriminate is equal to zero calculate the single root an return 1 to the caller
else if (discriminate == 0) {
//calculating the single root
rootx = ((-b + part1) / (2 * a));
//Displaying the single root
System.out.println("The Equation has one real root :" + rootx);
}
//If the discriminate is less than zero return 0 to the caller
else if (discriminate < 0) {
//Displaying the message
System.out.println("The equation has no Real Roots");
}
continue;
}
case 3:
{
double p,
r,
n,
t;
System.out.print("Enter the Principal Amount :$");
p = sc.nextDouble();
System.out.print("Enter annual rate of interest :%");
r = sc.nextDouble();
System.out.print("Enter no of years deposited for :");
t = sc.nextDouble();
System.out.print("Enter number of times the interest is compounded per year :");
n = sc.nextDouble();
double term = Math.pow((1 + (r / 100) / n), t * n);
double Amount = (p * term);
System.out.printf("Total Amount :$ %.2f", Amount);
continue;
}
case 4:
{
break;
}
default:
{
System.out.println("** Invalid Choice **");
continue;
}
}
break;
}
}
}
____________________
:: Menu ::
1.Area of Circle
2.Solve a Quadratic Equation
3.Compound interest
4.Exit
Enter Choice :1
Enter radius :5.5
Area of Circle :95.03
:: Menu ::
1.Area of Circle
2.Solve a Quadratic Equation
3.Compound interest
4.Exit
Enter Choice :2
Enter a,b,c:1.0 3 1
The equation has Two roots -0.38 and -2.62
:: Menu ::
1.Area of Circle
2.Solve a Quadratic Equation
3.Compound interest
4.Exit
:: Menu ::
1.Area of Circle
2.Solve a Quadratic Equation
3.Compound interest
4.Exit
Enter Choice :3
Enter the Principal Amount :$5000
Enter annual rate of interest :%5
Enter no of years deposited for :10
Enter number of times the interest is compounded per year :12
Total Amount :$ 8235.05
:: Menu ::
1.Area of Circle
2.Solve a Quadratic Equation
3.Compound interest
4.Exit
Enter Choice :4
__________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.