Please i need some helo with this program in Java! i really apreciate it! Java P
ID: 3576622 • 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
import java.util.*;
//Class Fahrenheit Defined
class Fahrenheit
{
//Default constructor for Fahrenheit
Fahrenheit()
{
System.out.println("Fahrenheit Class Created ");
}
//Converts and returns Fahrenheit to Celsius
double convertsFtoC(double f)
{
double c = (5 / 9) * (f - 32);
return c;
}
//Convert and returns Fahrenheit to Kelvin
double convertsFtoK(double f)
{
double k = (5 / 9) * (f - 32) + 273;
return k;
}
//toString overloaded
public String toString()
{
return "Fahrenheit ";
}
}
//Class Celsius defined
class Celsius
{
//Default constructor Celsius
Celsius()
{
System.out.println("Celsius Class Created ");
}
//Converts and return Celsius to Fahrenheit
double convertsCtoF(double c)
{
double f = (9 / 5) * c + 32;
return f;
}
//Converts and return Celsius to Kelvin
double convertsCtoK(double c)
{
double k = c + 273;
return k;
}
//toString overloaded
public String toString()
{
return "Celsius ";
}
}
//Class Kelvin defined
class Kelvin
{
//Default constructor Kelvin
Kelvin()
{
System.out.println("Kelvin Class Created ");
}
//Converts and return Kelvin to Fahrenheit
double convertsKtoF(double k)
{
double f = (9 / 5) * (k - 273) + 32;
return f;
}
//Converts and return Kelvin to Celsius
double convertsKtoC(double k)
{
double c = k - 273;
return c;
}
//toString overloaded
public String toString()
{
return "Kelvin ";
}
}
//Driver class
public class TemperatureConverter
{
public static void main(String ss[])
{
int ch1, ch2;
char ch;
double data;
//Scanner class to accept data
Scanner sc = new Scanner(System.in);
//Fahrenheit class object
Fahrenheit ff = new Fahrenheit();
//Celsius class object
Celsius cc = new Celsius();
//Kelvin class object
Kelvin kk = new Kelvin();
System.out.println("Welcome to Temperature converter program");
//Loops till user choice
do
{
//Accept type of conversion
System.out.println("which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)");
ch1 = sc.nextInt();
System.out.println("Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)");
ch2 = sc.nextInt();
//Accept data to convert
System.out.println("Enter the temperature: ");
data = sc.nextDouble();
//Checks for Fahrenheit to Celsius
if(ch1 == 1 && ch2 == 2)
System.out.println("User picked " + ff + " and Convert to " + cc + String.valueOf(ff.convertsFtoK(data)));
//Checks for Fahrenheit to Kelvin
else if(ch1 == 1 && ch2 == 3)
System.out.println("User picked " + ff + " and Convert to " + kk + String.valueOf(ff.convertsFtoK(data)));
//Checks for Celsius to Fahrenheit
else if(ch1 == 2 && ch2 == 1)
System.out.println("User picked " + cc + " and Convert to " + ff + String.valueOf(cc.convertsCtoF(data)));
//Checks for Celsius to Kelvin
else if(ch1 == 2 && ch2 == 3)
System.out.println("User picked " + cc + " and Convert to " + kk + String.valueOf(cc.convertsCtoK(data)));
//Checks for Kelvin to Fahrenheit
else if(ch1 == 3 && ch2 == 1)
System.out.println("User picked " + kk + " and Convert to " + ff + String.valueOf(kk.convertsKtoF(data)));
//Checks for Kelvin to Celsius
else if(ch1 == 3 && ch2 == 2)
System.out.println("User picked " + kk + " and Convert to " + cc + String.valueOf(kk.convertsKtoC(data)));
//Invalid selection
else
System.out.println("ERROR: Wrong Selection");
System.out.println("Would you like to continue? (y/n)");
System.out.println("Enter Your Choice: ");
ch = sc.next().charAt(0);
if(ch == 'n' || ch == 'N')
break;
else if(ch == 'y' || ch == 'Y')
continue;
else
{
System.out.println("Invalid Choice");
System.exit(0);
}
}while(true);
}
}
Output:
Fahrenheit Class Created
Celsius Class Created
Kelvin Class Created
Welcome to Temperature converter program
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
2
Enter the temperature:
78
User picked Fahrenheit and Convert to Celsius 0.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
3
Enter the temperature:
56
User picked Fahrenheit and Convert to Kelvin 273.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
2
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Enter the temperature:
10
User picked Celsius and Convert to Fahrenheit 42.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
2
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
3
Enter the temperature:
88
User picked Celsius and Convert to Kelvin 361.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
3
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Enter the temperature:
77
User picked Kelvin and Convert to Fahrenheit -164.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
3
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
2
Enter the temperature:
56
User picked Kelvin and Convert to Celsius -217.0
Would you like to continue? (y/n)
Enter Your Choice:
y
which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Which temperature format they would like use (ie, 1 - Fahrenheit, 2 - Celsius, 3 - Kelvin)
1
Enter the temperature:
20
ERROR: Wrong Selection
Would you like to continue? (y/n)
Enter Your Choice:
n