Write the main method that will test the following methods: In java please Write
ID: 3849600 • Letter: W
Question
Write the main method that will test the following methods: In java please
Write the method to convert Celsius to Fahrenheit.
Write the method to convert Fahrenheit to Celsius
Write a method operator(a,b,c) which will receive 2 integers and one of the 5 operators (*,/,+ -, %) as a character and perform the operation and return the integer value . Example operator(2,3,’*’) will return a 6. (assume no division by zero!))
Write the overloaded method operator(a,b,c) which will receive 2 doubles and one of the 4 operators (*,/,+ -) as a character and perform the operation and return the integer value . Example operator(2.1,3,’*’) will return a 63. (assume no division by zero!))
Write the method sumOfDigits(a) which will receive and integer and return the sum of their digits. (This was a lab!!) Example sumOfDigits(941) return 15.
Explanation / Answer
Hi,
Please find the program that has been requested by you.
TemperatureConversion.java:
import java.util.Scanner;
public class TemperatureConversion {
public static void main(String[] args) {
//takes input from the keyboard
Scanner scanner = new Scanner(System.in);
// Converting Fahrenheit to Celsius
System.out.println("Enter temperature in Fahrenheit :");
float temperatue = scanner.nextFloat();
float celsius = fahrenheitToCelsius(temperatue);
System.out.printf("%.02f degree fahrenheit temperature is equal to %.02f degree celsius %n", temperatue,
celsius);
// Converting Celsius to Fahrenheit
System.out.println("Enter temperature in degree celsius :");
temperatue = scanner.nextFloat();
float fahrenheit = celsiusToFahrenheit(temperatue);
System.out.printf("%.02f degree celsius is equal to %.02f degree fahrenheit %n", temperatue, fahrenheit);
}
public static float celsiusToFahrenheit(float celsius) {
float fahrenheit = 9 * (celsius / 5) + 32;
return fahrenheit;
}
public static float fahrenheitToCelsius(float fahrenheit) {
float celsius = (fahrenheit - 32) * 5 / 9;
return celsius;
}
}
Calculation.java:
public class Calculation {
public static void main(String[] args) {
Calculation methodOperator = new Calculation();
int resultInt= methodOperator.twoArgumentIntegerOperation(2, 3, '*');
System.out.println("Two Argument Integer Operation Result:"+resultInt);
int resultDouble= (int) methodOperator.twoArgumentDoubleOperation(2.1, 3, '*');
System.out.println("Two Argument Double Operation Result:"+resultDouble);
}
public int twoArgumentIntegerOperation(int a, int b, char operator) {
int result = 0;
switch (operator) {
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '%':
result = a % b;
break;
default:
System.out.println("Invaild Operator:" + operator);
break;
}
return result;
}
//Overloaded method
public double twoArgumentDoubleOperation(double a, double b, char operator) {
double result = 0;
switch (operator) {
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
default:
System.out.println("Invaild Operator:" + operator);
break;
}
return result;
}
}
SumOfDigits.java:
public class SumOfDigits {
public static void main(String[] args) {
SumOfDigits sumOfDigits = new SumOfDigits();
int result = sumOfDigits.sumOfDigits(941);
System.out.println("Sum of given number:"+result);
}
public int sumOfDigits(int digit) {
int r = 0;
int sum = 0;
while (digit > 0) {
r = digit % 10;
sum = sum + r;
digit = digit / 10;
}
return sum;
}
}
Hope this was useful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.