Please i need some helo with this program in Java! i really apreciate it! Java P
ID: 3777917 • Letter: P
Question
Please i need some helo with this program in Java! i really apreciate it!
Java Programming
(Assignment 6 – Classes/Objects)
You will use your knowledge of Java programming to create your first object oriented program.
Concepts Learned
• Classes
• Constructors
• Objects – calling
• Objects - creating
Spec
In your first object oriented programming assignment, you will create a temperature converter program. There will be a total of 4 classes/objects. The first will be called Fahrenheit. The second class will be called Celsius. The third class will be called Kelvin . The fourth class will be called Temperature Converter. The Temperature Converter class will have a main, none of the rest will.
Fahrenheit Class
• Create a constructor that prints “Fahrenheit class created”
• will contain a method that converts a Fahrenheit temperature to Celsius (returns double)
• will contain a method that converts a Fahrenheit temperature to Kelvin (returns double)
• Overload the toString method to say “Fahrenheit class”
Celsius Class
• Create a constructor that prints “Celsius class created”
• will contain a method that converts a Celsius temperature to Fahrenheit (returns double)
• will contain a method that converts a Celsius temperature to Kelvin (returns double)
• Overload the toString method to say “Celsius class”
Kelvin Class
• Create a constructor that prints “Kelvin class created”
• will contain a method that converts a Kelvin temperature to Fahrenheit (returns double)
• will contain a method that converts a Kelvin temperature to Celsius (returns double)
• Overload the toString method to say “Kelvin class”
Temperature Converter Class
• Contains a main
• Create three objects – Fahrenheit obj, Celsius obj, Kelvin obj
• Welcome user to Temperature converter program
• Ask user to pick which temperature format they would like use (ie, Fahrenheit, Celsius, Kelvin)
• Call the toString method of the temperature format picked
• Ask user to pick which temperature format they would like to convert to (ie, Fahrenheit, Celsius, Kelvin)
• Call the toString method of the temperature format picked
• Ask user to enter temperature (double)
• Convert the temperature entered by using the proper obj and method
• The conversion should be printed
Test Cases
1. User will pick Fahrenheit and convert to Celsius. Temp entered 89.6
2. User will pick Celsius and convert to Fahrenheit. Temp entered 32.0
3. User will pick Kelvin and convert to Celsius. Temp entered 295.5
4. User will pick Kelvin and convert to Fahrenheit. Temp entered 305.5
Please submit the screenshot of these cases.
Submission
Submit the assignment in the assignments area in blackboard where you retrieved this assignment originally. You must submit a screenshot of the output and directed comments. If you have issues with the code, please print it out and submit it in class.
Explanation / Answer
Test Cases:
1)
Celsius class created.
Fahrenheit class created.
Kelvin class created.
Welcome to Temperature Convertor
Which temperature format would you like to use?
1 Fahrenheit
2 Celsius
3 Kelvin
1
Fahrenheit class
Which temperature format would you like to covert to?
1 Fahrenheit
2 Celsius
3 Kelvin
2
Celsius class
Enter the temperature
89.6
Converted temperature in Celsius= 32.0
2)
Celsius class created.
Fahrenheit class created.
Kelvin class created.
Welcome to Temperature Convertor
Which temperature format would you like to use?
1 Fahrenheit
2 Celsius
3 Kelvin
2
Celsius class
Which temperature format would you like to covert to?
1 Fahrenheit
2 Celsius
3 Kelvin
1
Fahrenheit class
Enter the temperature
32
Converted temperature in Fahrenheit= 89.6
3)
Celsius class created.
Fahrenheit class created.
Kelvin class created.
Welcome to Temperature Convertor
Which temperature format would you like to use?
1 Fahrenheit
2 Celsius
3 Kelvin
3
Kelvin class
Which temperature format would you like to covert to?
1 Fahrenheit
2 Celsius
3 Kelvin
2
Celsius class
Enter the temperature
295.5
Converted temperature in Celsius= 22.350000000000023
4)
Celsius class created.
Fahrenheit class created.
Kelvin class created.
Welcome to Temperature Convertor
Which temperature format would you like to use?
1 Fahrenheit
2 Celsius
3 Kelvin
3
Kelvin class
Which temperature format would you like to covert to?
1 Fahrenheit
2 Celsius
3 Kelvin
1
Fahrenheit class
Enter the temperature
305.5
Converted temperature in Fahrenheit= 90.22999999999996
TemperatureConvertor.java
import java.util.Scanner;
public class TemperatureConvertor {
public static void main(String[] args) {
// TODO Auto-generated method stub
Celsius celsiusObj=new Celsius();
Fahrenheit fahrenheitObj=new Fahrenheit();
Kelvin kelvinObj=new Kelvin();
System.out.println("Welcome to Temperature Convertor");
System.out.println("Which temperature format would you like to use? 1 Fahrenheit 2 Celsius 3 Kelvin");
Scanner scanner = new Scanner(System.in);
System.out.flush();
int optionFrom = Integer.parseInt(scanner.nextLine());
switch(optionFrom){
case 1:
System.out.println(fahrenheitObj);
break;
case 2:
System.out.println(celsiusObj);
break;
case 3:
System.out.println(kelvinObj);
break;
}
System.out.println("Which temperature format would you like to covert to? 1 Fahrenheit 2 Celsius 3 Kelvin");
System.out.flush();
int optionTo = Integer.parseInt(scanner.nextLine());
switch(optionTo){
case 1:
System.out.println(fahrenheitObj);
break;
case 2:
System.out.println(celsiusObj);
break;
case 3:
System.out.println(kelvinObj);
break;
}
System.out.println("Enter the temperature");
System.out.flush();
double temp = Double.parseDouble(scanner.nextLine());
if(optionFrom==1){
if(optionTo==2){
System.out.println("Converted temperature in Celsius= "+ (double)fahrenheitObj.fahrenheitToCelsius(temp));
}
else if(optionTo==3){
System.out.println("Converted temperature in Kelvin= "+ (double) fahrenheitObj.fahrenheitToKelvin(temp));
}
else{
System.out.println("Temperature in Fahrenheit "+ (temp));
}
}else if(optionFrom==2){
if(optionTo==1){
System.out.println("Converted temperature in Fahrenheit= "+ celsiusObj.celsiusToFahrenheit(temp));
}
else if(optionTo==3){
System.out.println("Converted temperature in Kelvin= "+ celsiusObj.celsiusToKelvin(temp));
}
else{
System.out.println("Temperature in Celsius "+ (temp));
}
}
else{
if(optionTo==1){
System.out.println("Converted temperature in Fahrenheit= "+ kelvinObj.kelvinToFahrenheit(temp));
}
else if(optionTo==2){
System.out.println("Converted temperature in Celsius= "+ kelvinObj.kelvinToCelsius(temp));
}
else{
System.out.println("Temperature in Kelvin "+ (temp));
}
}
}
}
Fahrenheit.java
public class Fahrenheit {
public Fahrenheit(){
System.out.println("Fahrenheit class created.");
}
public double fahrenheitToKelvin(double f){
return (double) (f + 459.67)* (double) (5/9);
}
public double fahrenheitToCelsius(double f){
return (double) ((f-32)*5)/9d;
}
@Override
public String toString() {
return "Fahrenheit class";
}
}
Kelvin.java
public class Kelvin {
public Kelvin(){
System.out.println("Kelvin class created.");
}
public double kelvinToFahrenheit(double f){
return (double) (f*(9d/5d))-459.67;
}
public double kelvinToCelsius(double f){
return f-273.15;
}
@Override
public String toString() {
return "Kelvin class";
}
}
Celsius.java
public class Celsius {
public Celsius(){
System.out.println("Celsius class created.");
}
public double celsiusToFahrenheit(double f){
return (double) (f* (9d/5d)) + 32;
}
public double celsiusToKelvin(double f){
return (double) f+273.15;
}
@Override
public String toString() {
return "Celsius class";
}
}