ADVANCE JAVA 1. create a program that convert inches to centimeters.Have the use
ID: 3786222 • Letter: A
Question
ADVANCE JAVA
1. create a program that convert inches to centimeters.Have the user enter the number of inches(use double) and then display the value as a centimeter.If possible ,display using 2 decimal points. The formular and two sample values as follow. Formula: Centimeters=inches*2.54. where inches are 2,3.5 and centimeters are 5.08,and 8.89.
2. Display the times table. Ask the user for a number. Based on the input ,the program will output the times table for that number. For the highest efficiency,use a for loop to do the math and generate an output.
3.Display a question to the user. Allow the user to answer the question.If the user gets the question wrong,the question is redisplayed repeatedly until the user gets the question correct. A do-while loop is best here.
4. Have the user display temperature .Displays a message based on the temperature.For temperatures below 10,and display"polar vortex". Between 10 and 32,"Below Freezing".Between 33 and 50,"Cold",Between 51 and 70,"Mild",Between 71 and 85,"Comfortable",86 and 95,"Hot",Between 95-115 "Really hot",Abobe 116- "Ludicrous Heat".Two sample runs follow,Enter a temperature-->5 Polar Vortex,Enter a temperature-->212 Ludicrously Hot.
Explanation / Answer
1)
/**
* The java program ConvertToCentimeters that prompts
* user to enter inches and convets inches to centimetes
* and prints inches in centimeters to console
* */
//ConvertToCentimeters.java
import java.util.Scanner;
public class ConvertToCentimeters
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare double variables
double inches;
double centimeters;
//prompt for inches
System.out.println("Enter number of inches ");
inches=scanner.nextDouble();
//calculate centimeters
centimeters=inches*2.54;
//pritn incehs in centimeters
System.out.printf("%-5.2f in = %-5.2f cm ",inches,centimeters);
} //end of main method
}//end of class
Output:
Enter number of inches
2
2.00 in = 5.08 cm
Enter number of inches
3.5
3.50 in = 8.89 cm
-------------------------------------------------------------------------------------
2)
/**
* The java program Table that prompts user to enter
* table number and prints the corresponding table
*
* */
//Table.java
import java.util.Scanner;
public class Table
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare double variable
int tableNumber;
//prompt for inches
System.out.println("Enter table number ");
tableNumber=scanner.nextInt();
System.out.println(tableNumber+" Table >>>");
//print table upto 12 using for loop
for (int i = 1; i <=12; i++)
{
System.out.printf("%-4d x %-4d = %-4d ",tableNumber,i,tableNumber*i);
}
}
}
Output:
Enter table number
5
5 Table >>>
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60
-------------------------------------------------------------------------------------
3)
/**
* The java program MathsTester that prompts user to enter
* answer for multiplication of two numbers.
* If user enters correct answer then prints
* a message correct answer otherwise repeat
* to ask the question until user enters a correct answer.
*
* */
//MathsTester.java
import java.util.Scanner;
public class MathsTester
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare integer variable
int a,b,result;
boolean repeat=true;
//generate two random numbers in a range of 1 to 10
a=(int)(Math.random()*10)+1;
b=(int)(Math.random()*10)+1;
do
{
//prompt for inches
System.out.println("How much "+a+"x"+b);
result=scanner.nextInt();
//Checking condition
if(result==a*b)
{
System.out.println("Correct answer");
repeat=false;
}
else
{
System.out.println("Incorrect");
repeat=true;
}
}while(repeat);
}
}
Output:
How much 7x4
5
Incorrect
How much 7x4
28
Correct answer
-------------------------------------------------------------------------------------
4)
/*
* The java program that prompts the temperature
* and checkng the condition of temperature
* and prints the corresponding message to console.
* */
//Temperature.java
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//declare double variables
double temp;
//prompt for inches
System.out.println("Enter a temperature-->");
//prompt for temp value from user
temp=scanner.nextDouble();
//cehcking temperature condition using if else if case
if(temp<10)
System.out.println("polar vortex");
else if(temp>=10 && temp<=32)
System.out.println("Below Freezing");
else if(temp>=33 && temp<=50)
System.out.println("cold");
else if(temp>=51 && temp<=70)
System.out.println("Mild");
else if(temp>=71 && temp<=85)
System.out.println("Comfortable");
else if(temp>=56 && temp<=95)
System.out.println("Hot");
else if(temp>=96 && temp<115)
System.out.println("Really Hot");
else
System.out.println("Ludicrous Hea");
} //end of main method
}
Output:
Enter a temperature-->
1
polar vortex
Enter a temperature-->
212
Ludicrous Hea
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.