Question

In: Computer Science

**********************************java********************************************************** Purpose: The purpose for this project is to reinforce the knowledge from Chapter 9 of...

**********************************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;
}
  
}

Solutions

Expert Solution

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:

  • Temperature t1 initially has this.degree = 0.00 and this.scale = '';
    • In getDegreeInCelsius(), value is converted to Celsius, since there is no scale value. Hence, output is -17.77777777777778
    • In getDegreeInFahrenheit(), value is converted to Fahrenheit, since there is no scale value. Hence, output will be 32
    • When user input (scale = ‘C’ and degree = 100) is given,
      • In getDegreeInCelsius(), value is in Celsius. So, the output is 100.0
      • In getDegreeInFahrenheit(), the value is in Celsius. So, the output is converted to Fahrenheit and the output is 212.0
  • Temperature t2 initially has this.degree = 25.00 and this.scale = '';
    • In getDegreeInCelsius(), value will be converted to Celsius, since there is no scale value. Hence, output will be -3.888888888888889
    • In getDegreeInFahrenheit(), value will be converted to Fahrenheit, since there is no scale value. Hence, output is 77.0
    • When user input (scale = ‘F’) is given,
      • In getDegreeInCelsius(), value is in Fahrenheit. So, output is converted to Celsius and the output is -3.888888888888889
      • In getDegreeInFahrenheit(), the value is in Fahrenheit. So, the output is 25.0
  • Temperature t3 initially has this.degree = 0.00 and this.scale = ‘C’;
    • In getDegreeInCelsius(), value is in Celsius. Hence, output is 0.00
    • In getDegreeInFahrenheit(), value is in Celsius. So, the value is converted to Fahrenheit. Hence, output is 32.0
    • When user input (degree = 100) is given,
      • In getDegreeInCelsius(), value is in Celsius. So, output is 100.0
      • In getDegreeInFahrenheit(), the value is in Celsius. So, output is converted to Fahrenheit and the output is 212.0
  • Temperature t4 initially has this.degree = 77.00 and this.scale = ‘C’;
    • In getDegreeInCelsius(), value is in Celsius. Hence, output is 77.00
    • In getDegreeInFahrenheit(), value is in Celsius. So, the value is converted to Fahrenheit. Hence, output is 170.6
    • When user input (scale = ‘F’ and degree = 200) is given,
      • In getDegreeInCelsius(), value is in Fahrenheit. So, output is converted to Celsius and the output is 93.33333333333334
      • In getDegreeInFahrenheit(), the value is in Fahrenheit. So, output is 200.0
  • Finally,
    • t1 (scale = ‘C’ and degree = 100.0)
    • t2 (scale = ‘F’ and degree = 25.0)
    • t3 (scale = ‘C’ and degree = 100.0)
    • t4 (scale = ‘F’ and degree = 200.0)

OUTPUT:


Related Solutions

Question: The purpose for this project is to reinforce the knowledge from Chapter Two of the...
Question: The purpose for this project is to reinforce the knowledge from Chapter Two of the textbook. The ... The purpose for this project is to reinforce the knowledge from Chapter Two of the textbook. The students will practice how to implement stack and queue data structure using list structure, for instance, ArrayList. The students will also apply stack and queue in real project, for instance, to check if a string is a palindrom. Tasks:      1. Use ArrayList to...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts...
The purpose of this problem set is to reinforce your knowledge of some basic chemical concepts that are important for the origin of the elements. 1. Use the abundances of the stable isotopes of strontium and the masses of these nuclides (found at http://atom.kaeri.re.kr/nuchart/) to calculate the atomic weight of strontium. Compare the value that you get with the value shown in the periodic table found at http://www.rsc.org/periodic-table. Show your work. 2. Consult the chart of the nuclides (http://atom.kaeri.re.kr/nuchart/ )...
The main purpose of this lab is to reinforce the lectures on code style and documentation....
The main purpose of this lab is to reinforce the lectures on code style and documentation. Make as many improvements to the code style as you can including white space, variable names, etc. (NOTE: do this manually – no automated formatting tools allowed). Comment the file completely. #include <iostream> #include <cmath>using namespace std; const int A_CONSTANT = 3; void functionA(int a[], int aNumber); void functionB(int a[], int anotherNumber); void functionC(const int anArray[], int aNumber); void functionD(int& sum); int functionE(double number);...
New Retirementality Chapters 9 - 10 - 11 Chapter 9: A New Mindset: Retire on Purpose...
New Retirementality Chapters 9 - 10 - 11 Chapter 9: A New Mindset: Retire on Purpose “Too many people die with their music still in them.” -Oliver Wendell Holmes 1a. The revolution of retirement planning for the purposeful retiree is: A. the most fertile period of life for the meaningful pursuit B. a time to enjoy one’s life accomplishments C. a time to see the world D. take it easy and relax Chapter 10: The New Meaning of Re-tired: Your...
The following is a list of six control plans from this chapter or from Chapter 9....
The following is a list of six control plans from this chapter or from Chapter 9. These are followed by a list of six statements describing either a control goal or a system deficiency. Match the five control goals or system deficiencies with a control plan that would best prevent the system failure from occurring. Because there are six control plans, you should have one letter left over. Control Plans A. One-for-one checking of the goods, picking ticket, and sales...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to text files with the help of Scanner class methods, and PrintStream class methods. You will also learn to implement some simple Exception Handling. Carefully examine and follow ALL the program specifications. Take a look at the PPT slides for Chapter 7 File I/O for examples that will help with this program. Hotel Expense Recording Keeping: A hotel bookkeeper enters client hotel expenses in a...
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the...
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the Student class that compare two Student objects. One method should test for equality. The other methods should support the other possible comparisons. In each case, the method returns the result of the comparison of the two students' names. Place several Student objects into a list and shuffle it. Then run the sort method with this list and display all of the students' information. I...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on...
PURPOSE: The purpose of this assignment is to enable the students to enhance their knowledge on the environmental engineering and sustainable development for environmental engineering. TASK 1 (DOCUMENTATION) Development in science and technology has increase the level of human lifestyle which leads to technology advancement. However, everything good comes with a price. One of the greatest problems that the world is facing today is that of environmental pollution, increasing with every passing year and causing grave and irreparable damage to...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the...
The purpose of this assignment is to reinforce Ada concepts. Define a Complex-numbers package includes the following operations: Addition Subtraction Multiplication Division A “main program” needs to create one or more complex numbers, perform the various arithmetic operations, and then display results. Develop the program in ADA code with several packages. Write a short report that documents all work done, including the overall design, explanation of the implementation, the input data used to run the program, the output of the...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who are paid...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT