Question

In: Computer Science

Write a program to use Math methods to calculate and display the followings: Prompts the user...

Write a program to use Math methods to calculate and display the followings:

  1. Prompts the user to enter an angle, in degrees, and then display the angle in radians and the Sine of the angle.
  2. Prompts the user to enter a positive integer and calculates the square root of the number. Computational thinking: Your program must check if it is a positive number. If a negative number is entered, it must ask for another number until the positive number is entered.
  3. Prompts the user to enter two integers m & n, and displays the result of m^n.
  4. Prompts the user to enter a decimal number and displays the number rounded, the floor, and the ceiling of the number.
  5. Prompts for lower bound number L and upper bound number U, and displays a random number between L and U using the following formula:  L + (int)(Math.random() * (U - L + 1))

Evaluate all your answers to avoid shock & regret.

Display meaningful output for each task.

You only need to check if the input is valid in requirement # 2. For all the others, you can assume valid input is entered.

In JAVA

Solutions

Expert Solution

MathDemo.java
import java.util.Scanner;

public class MathDemo {

   public static void main(String[] args) {
       double angle = 0.0;
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter angle in degrees: ");
       angle = scan.nextDouble();
       System.out.println("Angle in radians: " + Math.toRadians(angle));
       System.out.println("Sine (" + angle + ") = " + Math.sin(Math.toRadians(angle)));
       int number = 0;

//Continue asking for positive number, until positive number is entered
       do {
           System.out.println("Enter a positive integer: ");
           number = scan.nextInt();
       }while(number<0);
       System.out.println("Square root of " + number + " is " + Math.sqrt(number));
       int m, n;
       System.out.println("Enter m: ");
       m = scan.nextInt();
       System.out.println("Enter n: ");
       n = scan.nextInt();
       System.out.println("m^n = " + Math.pow(m, n));
       double decimal = 0.0;
       System.out.println("Enter a decimal number: ");
       decimal = scan.nextDouble();
       System.out.println("Decimal number rounded: " + Math.round(decimal));
       System.out.println("Decimal number floor: " + Math.floor(decimal));
       System.out.println("Decimal number ceiling: " + Math.ceil(decimal));
       System.out.println("Enter upper bound of random number: ");
       int U = scan.nextInt();
       System.out.println("Enter lower bound of random number: ");
       int L = scan.nextInt();

//Find random number, as given in the question
       int rand = L + (int)(Math.random() * (U-L+1));
       System.out.println("Random number between " + L + " & " + U + " is " + rand);
   }

}


Related Solutions

Write a program that prompts the user for their first and lastname. Display the first...
Write a program that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user.Ask the user to input a phone number.The program checks which part of Colorado a phone number is from using the values below.If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none of the digits...
Write a program that prompts the user to enter two characters and display the corresponding major...
Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write the pseudocode that prompts the user for their first and last name. Display the first...
Write the pseudocode that prompts the user for their first and last name. Display the first initial of their first name and their last name to the user. Ask the user to input a phone number. The program checks which part of Colorado a phone number is from using the values below. If the second digit of the phone number is one of the below digits, print the phone number and which part of Colorado it is from. If none...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a program in java that uses methods to input data for calculation, calculate, and display...
Write a program in java that uses methods to input data for calculation, calculate, and display the results of the calculations. That is, there are at least three methods. The problem is to write a program that calculates the area of a rectangle. This action should be repeatable.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT