Question

In java code: Write a Temperature class that has two private instance variables: • degrees: a...

In java code:

Write a Temperature class that has two private instance variables:

• degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase)

The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius);

(2) two getter methods to return the temperature, one to return the degrees Celsius, the other to return the degrees Fahrenheit – use the following formulas to write the two methods, and round to the nearest tenth of a degree:

degreesC = (degreesF – 32) *5 /9 degreesF = (degreesC *9 /5) + 32

(3) three setter methods, one to set the value, one to set the scale (‘F’, ‘f’, ‘C’, or ‘c’, if any other invalid character is given, terminate your program), and one to set both;

(4) three comparison methods, an equals method to test whether two temperatures are equal, one method to test whether one temperature is greater than another, and one method to test whether one temperature is less than another (note that a Celsius temperature can be equal to a Fahrenheit temperature as indicated by the above formulas). Write a driver program in TemperatureTest that tests each of the constructors, getters, setters, and include at least one true and one false case for each of the comparison methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Temperature.java

class Temperature
{
private
double degrees;
char scale;
//constructors
public
Temperature()
{
degrees=0;
scale='c';
}
Temperature(double deg)
{
degrees=deg;
scale='c';
}
Temperature(char ch)
{
degrees=0;
scale=ch;
}
Temperature(double deg, char ch)
{
degrees=deg;
scale=ch;
}
//getter methods
double getCelcius()
{
return ((degrees-32) * (5/9));
  
}
double getFahrenheit()
{

return (degrees*(9/5)+32);

}
//setter methods
void setDegrees(double deg)
{degrees=deg;
  
}
void setScale(char ch)
{
if(ch=='F'||ch=='f'||ch=='c'||ch=='C')
scale=ch;
else
System.exit(0);
}
void setBoth(double deg, char ch)
{degrees=deg;
scale=ch;
}
//comparision methods
boolean equals(Temperature t)
{
  
double temp;
if(this.degrees==t.degrees)
return true;
  
else if(this.scale=='c'||this.scale=='C' && t.scale=='F'||t.scale=='f')
{
temp=t.getCelcius();
if(this.degrees==temp)
return true;
else
return false;
}
else if(this.scale=='F'||this.scale=='f' && t.scale=='c'||t.scale=='C')
{
temp=t.getFahrenheit();
if(this.degrees==temp)
return true;
else
return false;
}
else
return false;
  
  
}
  
boolean lessThan(Temperature t)
  
{
double temp;
if(this.scale=='c'||this.scale=='C' && t.scale=='F'||t.scale=='f')
{
temp=t.getCelcius();
if(this.degrees<=temp)
return true;
else
return false;
}
else if(this.scale=='F'||this.scale=='f' && t.scale=='c'||t.scale=='C')
{
temp=t.getFahrenheit();
if(this.degrees<=temp)
return true;
else
return false;
}
else
{
if(this.degrees<=t.degrees)
return true;
else
return false;
}
}
boolean greaterThan(Temperature t)
  
{
double temp;
if(this.scale=='c'||this.scale=='C' && t.scale=='F'||t.scale=='f')
{
temp=t.getCelcius();
if(this.degrees>=temp)
return true;
else
return false;
}
else if(this.scale=='F'||this.scale=='f' && t.scale=='c'||t.scale=='C')
{
temp=t.getFahrenheit();
if(this.degrees>=temp)
return true;
else
return false;
}
else
{
if(this.degrees>=t.degrees)
return true;
else
return false;
}
}
  
}

//Main.java

public class Main{
public static void main(String[] args)
{
Temperature t1=new Temperature(34.5,'c');
Temperature t2=new Temperature(94.5,'f');
if(t1.equals(t2))
System.out.println("t1 temperature equals t2");
else if(t1.lessThan(t2))
System.out.println("t1 temperature less than or equals t2");
else if(t1.greaterThan(t2))
System.out.println("t1 temperature is greater than or equal to t2");
//Now t1 and t2 objects are assingned with same values
t2.setDegrees(34.5);
t2.setScale('c');
if(t1.equals(t2))
System.out.println("t1 temperature equals t2");
  
}
}

Add a comment
Know the answer?
Add Answer to:
In java code: Write a Temperature class that has two private instance variables: • degrees: a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit....

    Java program Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating- point number for the temperature and a character for the scale, eitherでfor Celsius or 'F' for fahrenheit. The class should have Four constructors: one for the number of degrees, one for the scale, one for both the degrees and scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and celsius if no...

  • A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:     &

    A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of temp for the object                                                 getTempScale– returns temperature scale (‘C’ or...

  • CIT-111 Homework #4, Part A                                      

    CIT-111 Homework #4, Part A                                                                    Chapter 4, problem #7 (pages 254-255) A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of...

  • c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees...

    c++ please 2. (50 points) Create a Temperature class that internally stores a temperature in degrees Kelvin as a double. Create mutator functions named setTempKelvin, setTempFahrenheit, and set TempCelsius that take an input temperature in the specified temperature scale (i.e. Kelvin, Fahrenheit, and Celsius, respectively), and convert the temperature to Kelvin, and store that temperature in the class member private variable (only in Kelvin, no other scales). Also, create functions that return the stored temperature in degrees Kelvin, Fahrenheit, or...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • B. Suppose a thermometer reads TC a. Write an expression for how many Fahrenheit degrees there ar...

    B. Suppose a thermometer reads TC a. Write an expression for how many Fahrenheit degrees there are between 0 and TC. Your expression would give the Fahrenheit temperature if both temperatures had their o" mark at the same place. However, when the Celsius scale reads o', the Fahrenheit scale reads 32°. Write an equation relating the Fahrenheit temperature to the Celsius temperature. Define your variables clearly. b. B. Suppose a thermometer reads TC a. Write an expression for how many...

  • Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables,...

    Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables, a constructor with two parameter to initialize length and width, set and get methods for each variables and the following methods: a. The first method calculates and returns the value of the ratio of the length to the width of the rectangle. b. The second method determines whether a rectangle is a Square depending on the value of the ratio, which should be calculated...

  • Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write...

    Java - Object Oriented Programming Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...

  • use java follow my code: public class q1 {    // Instance Variables    int currentFloor;...

    use java follow my code: public class q1 {    // Instance Variables    int currentFloor;    double maximumWeight;    int topFloor;       // Constructor Declaration of Class    public q1 (int currentFloor, double maximumWeight, int topFloor)    {    this.currentFloor = currentFloor;    this.maximumWeight = maximumWeight;    this.topFloor = topFloor;    }       // Property to get value of currentFloor instance variable    public int getCurrentFloor()    {    return currentFloor;    }       // Property...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT