Question

In java The Java language has a math class with several methods you can use for...

In java The Java language has a math class with several methods you can use for mathematical calculations. For instance, to find the square root of a number you can use the Math.sqrt method: double value = 9.0; double result = Math.sqrt(value); In the above example the variable result will hold 3.0. The same holds true for the cube root: double value = 9.0; double result = Math.cbrt(value); You can also get the base 10 logarithm of a number: double value = 9.0; double result = Math.log10(value); For this question you are going to create a simple calculator. From the keyboard take in a character that represents the operation and a double that represents the value to operate on. The operations are S - square root C - cube root L - base 10 logarithm Your input should be in this format S 81 This indicates that you want the square root of 81. Just as C 45 indicates that you want the cube root of 45. And L 45 indicates that you want the log 10 of 45 Using a switch statement determine the operation and using the appropriate math function output either the Square root, Cube root, or Log 10 of the value input. Use a default statement to catch when the input is not valid. Format your code with hilite.me and submit it in the text box provided.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// MathCalculator.java

import java.util.Scanner;

public class MathCalculator {

   public static void main(String[] args) {
       double val;
       char ch;
       int choice;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Getting the input entered by the user
       System.out.println("S.Square Root");
       System.out.println("C.Cube Root");
       System.out.println("L.Logarithm base 10");

       ch = sc.next(".").charAt(0);
       val = sc.nextDouble();

       switch (ch) {
       case 'S':
       case 's': {
           System.out.println("Square root of " + val + " is "+ Math.sqrt(val));
           break;
       }
       case 'C':
       case 'c': {
           System.out.println("Cube root of " + val + " is " + Math.cbrt(val));
           break;
       }
       case 'L':
       case 'l': {
           System.out.println("Logarithm of " + val + " is " + Math.log10(val));
           break;
       }
       default: {
           System.out.println("Invalid Choice");
       }
       }

   }

}

==============================

Output:

S.Square Root
C.Cube Root
L.Logarithm base 10
S 9.0
Square root of 9.0 is 3.0

==============================

Output#2:

S.Square Root
C.Cube Root
L.Logarithm base 10
C 27.0
Cube root of 27.0 is 3.0

============================

Output#3:

S.Square Root
C.Cube Root
L.Logarithm base 10
L 25
Logarithm of 25.0 is 1.3979400086720377


=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
In java The Java language has a math class with several methods you can use for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • This problem demonstrates the use of import module. The Python programming language has many strengths, but...

    This problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows: import math This statement enables your program to use a math...

  • A class Scanner in Java can be used to get user input and its methods can...

    A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the...

  • Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program...

    Java Program Create a Project called “SquareRoot” with a class called “SquareRoot”. Write a Java program to compute the square root of a double using the algorithm The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a / 2. Then find the average of your guess g and a / g. That’s your next guess. Repeat until two consecutive guesses are close enough. For example, Square Root by...

  • (a) A class Scanner in Java can be used to get user input and its methods...

    (a) A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally...

  • Hello! So this is a Java project. And I have completed the initial class with the...

    Hello! So this is a Java project. And I have completed the initial class with the methods. With that first library I created I needed to export it as a .jar file, which I have done. And import it in a second java project as a external jar, which I have also done. But I need to complete a test class so that I can run and test the initial class. I have pasted the requirements, as well as the...

  • Write a java calculator program that perform the following operations: 1. function to Addition of two...

    Write a java calculator program that perform the following operations: 1. function to Addition of two numbers 2. function to Subtraction of two numbers 3. function to Multiplication of two numbers 4. function to Division of two numbers 5. function to Modulus (a % b) 6. function to Power (a b ) 7. function to Square root of () 8. function to Factorial of a number (n!) 9. function to Log(n) 10. function to Sin(x) 11. function to Absolute value...

  • The Scanner class in Java has several methods for reading values entered from the keyboard. The...

    The Scanner class in Java has several methods for reading values entered from the keyboard. The methods include next, nextInt, and nextLine. Which of the following statements is INCORRECT about the methods? Pick the best applicable answer Assuming that the user has typed: Hello World! If you invoke the next method for the first time, it will return the string "Hello", and if you invoked next method for the second time, it will return the string "World!" If you want...

  • You are allowed to import the scanner and math methods. Not allowed to use the var...

    You are allowed to import the scanner and math methods. Not allowed to use the var variable Solution Description TemperatureConverter A formula for converting temperature in Kelvin to degrees Fahrenheit is as follows: Fahrenheit Kelvin(9/5)-459.67 Your goal for this assignment is to write one file, TemperatureConverter.java, which prompts the user for a temperature in Kelvin, converts the temperature to degrees Fahrenheit, and then outputs the results by printing it to the terminal. Note that the temperature provided by the user...

  • Hi can anyone help me with this question? Please use python when you do it. THANKS...

    Hi can anyone help me with this question? Please use python when you do it. THANKS 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are or * The tree represents...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT