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.
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
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 :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT