In: Computer Science
**********************************java**********************************************************
Purpose: The purpose for this project is to reinforce the knowledge from Chapter 9 of the textbook. The students will learn how to write a user defined class.
Project Objectives:
Apply UML design on user defined class
Write overloaded constructors of a class
Write mutators (i.e. get methods) and accessors (i.e. set methods) of a class
Write overloaded methods
Write main method to test the user defined class
Class Diagram:
Student must implement the Temperature class according to the following class design.
Temperature
-‐degree: double
-‐scale: char
+Temperature()
+Temperature(degree: double)
+Temperature(scale: char)
+Temperature(degree: double, scale: char)
+getDegreeInCelsius(): double
+getDegreeInFahrenheit(): double
+setDegree(degree: double): void
+setDegree(scale: char): void
+setDegree(degree: double, scale: char): void
+equals(obj: Temperature): boolean
+isLessThan(obj: Temperature): boolean
+isGreaterThan(obj: Temperature): boolean
Method details
The name of a method explains it usage.
getDegreeInCelsius will return the temperature’s degree in its equivalent Celsius degree. If the temperature’s scale is ‘C’, then the return value is temperature’s degree. If the temperature’s scale is ‘F’, the return value is calculated by the following formula: C = (F-‐32)*5/9. For example, if the temperature degree is 77 and scale is ‘F’, then the method will return 25since (77-‐32)*5/9 = 25. For another example, if the temperature degree is 77 and scale is ‘C’, then the method will return 77.
getDegreeInFahrenheit will return the temperature’s degree in its equivalent Fahrenheit degree. If the temperature’s scale is ‘F’, then the return value is temperature’s degree. If the temperature’s scale is ‘C’, the return value is calculated by the following formula: F = 1.8C+32. For example, if the temperature degree is 25 and scale is ‘F’, then the method will return 25; For another example, if the temperature degree is 25 and scale is ‘C’, then the method will return 77 since 1.8*25+32 = 77.
void setDegree(double degree) will reset the temperature to given degree without change the scale of the temperature.
void setDegree(char scale) will reset the temperature to given scale without change the degree of the temperature.
void setDetree(double degree, char scale) will reset the temperature to given degree of given scale.
equals method returns true if this temperature is equal to parameter Temperature; false otherwise. You need to compare tow temperatures under same scale. For example, you either compare whether getDegreeInCelsius return same value for both temperatures, or whether getDegreeInFahrenheit return the same value for both temperatures.
isLessThan method return true if this temperature is less than parameter Temperature ; false otherwise. Again, you need to compare two temperatures under same scale.
isGreaterThan method returns true if this temperature is greater than parameter Temperature; false otherwise. Again, you need to compare two temperatures under same scale.
Main method requirements
The main method must create four Temperature objects by using all four constructors.
The main method must test all set and get methods by setting the attributes according to user’s input then displaying the temperature by printing out the result of get methods.
The main function must test equals, isLessThan, and isGreaterThan methods by compare two Temperature objects and print out the result.
Sample pseudo code for main function
Create a Temperature object by calling default constructor
Print out the temperature’s degree in Celsius
Print out the temperature’s degree in Fahrenheit
Ask user to enter the scale and degree of the temperature
Call set method(s) to set the temperature’s degree and scale
Print out the temperature’s degree in Celsius
Print out the temperature’s degree in Fahrenheit
Repeat step 1 to 7 for all other three constructorsto create three more temperatures
Test if temperature1 equals temperature2 and print out appropriate message
Test if temperature1 is less than temperature2 and print out appropriate message
Test if temperature1 is greater than temperature2 and print out appropriate message
Repeat step 9 to 11 for other pairs of temperatures
Note on main function and debugging
Fix one error a time.
If step 2 output is wrong, then either constructor is wrong or getDegreeInCelsiu function is wrong
If step 3 output is wrong, then either constructor is wrong or getDegreeInFahrenheit function is wrong
If step 6 output is wrong, then either set method(s) is wrong or getDegreeInCelsiu function is wrong
If step 7 output is wrong, then either set method(s) is wrong or getDegreeInFahrenheit function is wrong
If step 9 output is wrong, then the equals method has problem
If step 10 output is wrong, then the isLessThan method has problem
If step 11 output is wrong, then the isGreaterThan method has problem
*******************************MY CODE **********************************************
I tried to make this program but I had trouble with a few things, please complete the program and make sure it compiles. If possible, comment. Thank you
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args) {
Temperature temp = new Temperature(0.0,C);
System.out.println("The first Temperature has been created using
the default constructor which sets");
System.out.println("the degree to a default value of " +
this.degree + "and the scale to a default value of " +
this.scale);
System.out.println("The first Temperature is " + getDegreeInCelsius
+ " C ");
System.out.println("The first Temperature is " +
getDegreeInFahrenheit + " F ");
Scanner in = new Scanner(System.in);
System.out.print("set the degree (a number) and the scale (F or C)
of the first Temperature.");
double degree = in.nextdouble();
System.out.print("First set the degree: " + this.degree);
//The first Temperature has been created using the default
constructor which sets
//the degree to a deafault value of 0.0 and the scale to a default
value of C.
//The first Temperature is 0.00 C.
//The first Temperature is 32.00 F.
//Set the degree (a number) and the scale (F or C) of the first
Temperature.
//First set the degree:
}
Temperature();
Temperature(double degree){
this.degree = degree;
}
Temperature(char scale){
this.scale = degree;
}
Temperature(double degree, char scale){
this.degree = degree;
this.scale = degree;
}
private double degree;
private char scale;
public double getDegreeInCelsius(){
if(scale == 'C' || scale == 'c'){
return degree;
}else {
return (degree - 32) * 5 / 9.0;
}
}
public double getDegreeInFahrenheit(){
if(scale == 'F' || scale == 'f'){
return degree;
}else {
return (1.8 * (degree) + 3.2);
}
}
public void setDegree(double degree) {
this.setDegree = degree;
}
public void setDegree(char scale) {
this.setDegree = scale;
}
public void setDegree(double degree, char scale) {
this.setDegree = degree;
this.setDegree = scale;
}
public boolean equals(){
if(this.getDegreeInCelsius() == obj.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() ==
obj.getDegreeInFahrenheit()){ {
return false;
}
}
public boolean isLessThan(Temperature temp){
if(this.getDegreeInCelsius() < temp.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() <
temp.getDegreeInFahrenheit()){
return true;
}else{
return false;
}
}
public boolean isGreaterThan(Temperature temp){
if(this.getDegreeInCelsius() > temp.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() >
temp.getDegreeInFahrenheit()){
return true;
}else{
return false;
}
}
JAVA CODE:
import java.util.Scanner;
// Temperature class
public class Temperature {
// private variable degree of type double
private double degree;
// private variable scale of type char
private char scale;
// default constructor
public Temperature() {}
// constructor with parameter degree of type double
public Temperature(double degree) {
// parameter value is assigned to private variable degree
this.degree = degree;
}
// constructor with parameter scale of type char
public Temperature(char scale) {
// parameter value is assigned to private variable scale
this.scale = scale;
}
// constructor with parameter degree of type double and scale of type char
public Temperature(double degree, char scale) {
// parameter value is assigned to private variable degree
this.degree = degree;
// parameter value is assigned to private variable scale
this.scale = scale;
}
// getDegreeInCelsius method checks if the scale is in Celsius and returns the value in degree if
// the scale is 'C' else converts the temperature to Celsius and returns the converted value
public double getDegreeInCelsius() {
double C;
// checks if the scale is 'C'
if (Character.toString(this.scale).equals("C")) {
C = this.degree; // assigns the value in degree to C
}
else {
C = ((this.degree - 32)/9) * 5; // converts the value to Celsius
}
return C;
}
// getDegreeInFahrenheit method checks if the scale is in Fahrenheit and returns the value in degree if
// the scale is 'F' else converts the temperature to Fahrenheit and returns the converted value
public double getDegreeInFahrenheit() {
double F;
// checks if the scale is 'F'
if (Character.toString(this.scale).equals("F")) {
F = this.degree; // assigns the value in degree to F
}
else {
F = (1.8 * this.degree) + 32; // converts the value to Fahrenheit
}
return F;
}
// setDegree method assigns the value in parameter to degree variable
public void setDegree(double degree) {
this.degree = degree;
}
// setDegree method assigns the value in parameter to scale variable
public void setDegree(char scale) {
this.scale = scale;
}
// setDegree method assigns the values in parameter to degree and scale variable
public void setDegree(double degree, char scale) {
this.degree= degree;
this.scale = scale;
}
// equals method checks if this object and object in the parameter have the same degree
public boolean equals(Temperature obj) {
// checkEqual holds the boolean value (true - if equal, false - if not equal)
boolean checkEqual;
// checks if the both the object's scale are the same
if(this.scale == obj.scale) {
// and checks if this degree is equal to the obj's degree
checkEqual = (this.degree == obj.degree);
}
// else if the scales are different
else {
// checks if this object's scale is in Celsius
if(Character.toString(this.scale).equals("C")) {
// convert this object's degree to Fahrenheit
// and checks if both the degrees are same
double F = this.getDegreeInFahrenheit();
checkEqual = (F == obj.scale);
}
// else if this object's scale is not in Celsius
else {
double C = this.getDegreeInCelsius();
// checks if both the degrees are same
checkEqual = (C == obj.scale);
}
}
// returns the boolean value
return checkEqual;
}
// isLessThan method checks if this object's degree is less than the parameter object's degree
public boolean isLessThan(Temperature obj) {
// checkLessThan holds the boolean value (true - if less than, false - if not less than)
boolean checkLessThan;
// checks if the both the object's scale are the same
if(this.scale == obj.scale) {
// and checks if this degree is less than obj's degree
checkLessThan = (this.degree < obj.degree);
}
// else if the scales are different
else {
// checks if this object's scale is in Celsius
if(Character.toString(this.scale).equals("C")) {
// convert this object's degree to Fahrenheit
// and checks if this degree is less than obj's degree
double F = this.getDegreeInFahrenheit();
checkLessThan = (F < obj.scale);
}
// else if this object's scale is not in Celsius
else {
double C = this.getDegreeInCelsius();
// checks if this degree is less than obj's degree
checkLessThan = (C < obj.scale);
}
}
// returns the boolean value
return checkLessThan;
}
// isGreaterThan method checks if this object's degree is greater than the parameter object's degree
public boolean isGreaterThan(Temperature obj) {
// checkGreaterThan holds the boolean value (true - if greater than, false - if not greater than)
boolean checkGreaterThan;
// checks if the both the object's scale are the same
if(this.scale == obj.scale) {
// and checks if this degree is greater than obj's degree
checkGreaterThan = (this.degree > obj.degree);
}
// else if the scales are different
else {
// checks if this object's scale is in Celsius
if(Character.toString(this.scale).equals("C")) {
// convert this object's degree to Fahrenheit
// and checks if this degree is greater than obj's degree
double F = this.getDegreeInFahrenheit();
checkGreaterThan = (F > obj.scale);
}
// else if this object's scale is not in Celsius
else {
// checks if this degree is greater than obj's degree
double C = this.getDegreeInCelsius();
checkGreaterThan = (C > obj.scale);
}
}
// returns the boolean value
return checkGreaterThan;
}
// Main Method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char scale;
double degree;
//Temperature object t1 by calling default constructor
Temperature t1 = new Temperature();
// prints out the degree in Celsius and Fahrenheit
System.out.println("t1 objects degree in Celsius: " + t1.getDegreeInCelsius());
System.out.println("t1 objects degree in Fahrenheit: " + t1.getDegreeInFahrenheit());
// asks the user to enter scale and degree values
System.out.print("Enter the scale value: ");
scale = Character.toUpperCase(sc.nextLine().charAt(0));
System.out.print("Enter the degree value: ");
degree = Double.parseDouble(sc.nextLine());
// set method is called to set the temperature’s degree and scale
t1.setDegree(degree, scale);
// prints out the degree in Celsius and Fahrenheit
System.out.println("t1 objects degree in Celsius: " + t1.getDegreeInCelsius());
System.out.println("t1 objects degree in Fahrenheit: " + t1.getDegreeInFahrenheit() + "\n");
// Temperature object t2 by calling the second constructor
Temperature t2 = new Temperature(25);
// prints out the degree in Celsius and Fahrenheit
System.out.println("t2 objects degree in Celsius: " + t2.getDegreeInCelsius());
System.out.println("t2 objects degree in Fahrenheit: " + t2.getDegreeInFahrenheit());
// asks the user to enter scale value
System.out.print("Enter the scale value: ");
scale = Character.toUpperCase(sc.nextLine().charAt(0));
// set method is called to set the temperature’s scale
t2.setDegree(scale);
// prints out the degree in Celsius and Fahrenheit
System.out.println("t2 objects degree in Celsius: " + t2.getDegreeInCelsius());
System.out.println("t2 objects degree in Fahrenheit: " + t2.getDegreeInFahrenheit() + "\n");
// Temperature object t3 by calling the third constructor
Temperature t3 = new Temperature('C');
// prints out the degree in Celsius and Fahrenheit
System.out.println("t3 objects degree in Celsius: " + t3.getDegreeInCelsius());
System.out.println("t3 objects degree in Fahrenheit: " + t3.getDegreeInFahrenheit());
// asks the user to enter degree value
System.out.print("Enter the degree value: ");
degree = Double.parseDouble(sc.nextLine());
// set method is called to set the temperature’s degree and scale
t3.setDegree(degree);
// prints out the degree in Celsius and Fahrenheit
System.out.println("t3 objects degree in Celsius: " + t3.getDegreeInCelsius());
System.out.println("t3 objects degree in Fahrenheit: " + t3.getDegreeInFahrenheit() + "\n");
// Temperature object t4 by calling the fourth constructor
Temperature t4 = new Temperature(77, 'C');
// prints out the degree in Celsius and Fahrenheit
System.out.println("t4 objects degree in Celsius: " + t4.getDegreeInCelsius());
System.out.println("t4 objects degree in Fahrenheit: " + t4.getDegreeInFahrenheit());
// asks the user to enter scale and degree values
System.out.print("Enter the scale value: ");
scale = Character.toUpperCase(sc.nextLine().charAt(0));
System.out.print("Enter the degree value: ");
degree = Double.parseDouble(sc.nextLine());
// set method is called to set the temperature’s degree and scale
t4.setDegree(degree, scale);
// prints out the degree in Celsius and Fahrenheit
System.out.println("t4 objects degree in Celsius: " + t4.getDegreeInCelsius());
System.out.println("t4 objects degree in Fahrenheit: " + t4.getDegreeInFahrenheit() + "\n\n");
// Tests if each temperature object is equal, less than or greater than the other object
// and prints the message
Temperature[] temp_list = {t1, t2, t3, t4};
String objectNames[] = {"t1", "t2", "t3", "t4"};
for (int i = 0; i < temp_list.length; i++) {
for(int j = 0; j < temp_list.length; j++) {
if(i != j) {
boolean checkequal = temp_list[i].equals(temp_list[j]);
if(checkequal) {
System.out.println(objectNames[i] + " is equal to " + objectNames[j]);
}
else {
System.out.println(objectNames[i] + " is not equal to " + objectNames[j]);
}
boolean checkLessThan = temp_list[i].isLessThan(temp_list[j]);
if(checkLessThan) {
System.out.println(objectNames[i] + " is less than " + objectNames[j]);
}
else {
System.out.println(objectNames[i] + " is not less than " + objectNames[j]);
}
boolean checkGreaterThan = temp_list[i].isGreaterThan(temp_list[j]);
if(checkGreaterThan) {
System.out.println(objectNames[i] + " is greater than " + objectNames[j]);
}
else {
System.out.println(objectNames[i] + " is not greater than " + objectNames[j]);
}
System.out.println("\n");
}
}
}
}
}
OUTPUT Explanation:
OUTPUT: