Write a program that will first allow a user to choose one of two options: Conve
ID: 3666303 • Letter: W
Question
Write a program that will first allow a user to choose one of two options: Convert a temperature from degrees Celsius to degrees Fahrenheit. Convert a temperature from degrees Fahrenheit to degrees Celsius. The program should then prompt for the temperature value to be entered and output the new value that results from the conversion. To convert from Celsius to Fahrenheit you can multiply the value by 1.8 and then add 32. To convert from Fahrenheit to Celsius, you can subtract 32 from the value, then multiply by 5, and divide the result by 9. Write a program that takes 4 numbers a, b, c and d, then find and prints the maximum value. Write a program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31 st December 2003 when the user enters 12 31 2003, for example. You will need to work out when th, nd, st, and rd need to be appended to the day value. Don't forget 1st,2nd, 3rd, 4th; but 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Explanation / Answer
I have written function for all three questions in one class. Please go through each function
package cgegg;
import java.util.Scanner;
public class Assignment3 {
// function to convert celsius to fahrenheit
public static float toFahrenheit(float celsius) {
float fahrenheit = 9 * (celsius / 5) + 32;
return fahrenheit;
}
// function to convert fahrenheit to celsius
public static float toCelsius(float fahrenheit) {
float celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
// function to calculate max of 4 numbers, it uses max function
public static int maxOf4(int a, int b, int c, int d) {
return max(a, max(b, max(c,d)));
}
public static int max(int a, int b){
return a>b ? a: b;
}
//function to show date in prescribed format
public static String showDate(int month, int day, int year) {
String monthNameArr[] = {"January","Feburary","March","April","May","June","July","August",
"September","October","November","December"};
String prefix = prefix(day);
String monthName = monthNameArr[month-1];
return day+prefix +" "+monthName+" "+year;
}
//function to get prefix for day of date
public static String prefix(int day) {
if(day==1||day==21||day==31)
return "st";
else if(day==2 || day==22)
return "nd";
else if(day==3 || day==23)
return "rd";
else
return "th";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// for Q1
System.out.println("Choose: ");
System.out.println("1. from Celsius to Fahrenheit. 2. from Fahrenheit to Celsius");
int n = sc.nextInt();
System.out.println("Enter value of temperature: ");
float temp = sc.nextFloat();
if(n==1)
System.out.println(toFahrenheit(temp));
else
System.out.println(toCelsius(temp));
//For Q2
System.out.println("Enter four number: ");
int a = sc.nextInt();
int b= sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
System.out.println("Max= "+maxOf4(a, b, c, d));
//For Q3
System.out.println("Enter month number, day and year: ");
int month = sc.nextInt();
int day = sc.nextInt();
int year = sc.nextInt();
System.out.println(showDate(month, day, year));
}
}
/*
OUTPUT:
1. from Celsius to Fahrenheit. 2. from Fahrenheit to Celsius
1
Enter value of temperature:
12
53.6
Enter four number:
4
2
7
9
Max= 9
Enter month number, day and year:
12
31
2008
31st December 2008
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.