Question

In: Computer Science

JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...

JAVA

Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and 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). Include the following: (1) two accessor 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=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);

DegreesC=5(degreesF−32)/9DegreesF=(9(degreesC)/5+32);

(2) three mutator methods: one to set the value, one to set the scale (F or C), and one to set both; (3) 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); and (4) a suitable toString method. Then write a driver program (or programs) that tests all the methods. Be sure to use each of the constructors, to include at least one true and one false case for each of the comparison methods, and to test at least the following temperature equalities: 0.0 degrees C = 32.0 degrees F, –40.0 degrees C = –40.0 degrees F, and 100.0 degrees C = 212.0 degrees F.

Solutions

Expert Solution

Temperature.java

public class Temperature {
private double value;
private char scale;
  
public Temperature()
{
setScale('C');
setValue(0.0);
}

public Temperature(double value) {
setValue(value);
setScale('C');
}

public Temperature(char scale) {
setValue(0.0);
setScale(scale);
}

public Temperature(double value, char scale) {
setValue(value);
setScale(scale);
}

public double getValue() {
return value;
}

public char getScale() {
return scale;
}
  
public double getCelsius()
{
return(5 * (value - 32) / 9);
}
  
public double getFahrenheit()
{
return(((9 * value) / 5) + 32);
}

public void setValue(double value) {
this.value = value;
}

public void setScale(char scale) {
if(scale != 'C' && scale != 'F')
this.scale = 'C';
else
this.scale = Character.toUpperCase(scale);
}
  
public void setValueAndScale(double value, char scale)
{
setValue(value);
setScale(scale);
}
  
public boolean checkEqual(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() == other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() == other.getCelsius());
else
return(getValue() == other.getFahrenheit());
}
}
  
public boolean checkGreater(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() > other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() > other.getCelsius());
else
return(getValue() > other.getFahrenheit());
}
}
  
public boolean checkLesser(Temperature other)
{
if(getScale() == other.getScale())
{
return(getValue() < other.getValue());
}
else
{
if(getScale() == 'C' && other.getScale() == 'F')
return(getValue() < other.getCelsius());
else
return(getValue() < other.getFahrenheit());
}
}
  
@Override
public String toString()
{
return(String.format("%.1f", getValue()) + "" + getScale());
}
}

Test.java

public class Test {
  
public static void main(String[] args) {
Temperature t1 = new Temperature();
Temperature t2 = new Temperature('F');
Temperature t3 = new Temperature(-40);
Temperature t4 = new Temperature(100, 'C');
  
System.out.println("Displaying all four temperature instances..");
System.out.println("T1 = " + t1);
System.out.println("T2 = " + t2);
System.out.println("T3 = " + t3);
System.out.println("T4 = " + t4);
  
System.out.println("\nEquivalent Fahrenheit value of T1: " + String.format("%.1fF", t1.getFahrenheit()));
System.out.println("Equivalent Celsius value of T2: " + String.format("%.1fC", t2.getCelsius()));
System.out.println("Equivalent Fahrenheit value of T3: " + String.format("%.1fF", t3.getFahrenheit()));
System.out.println("Equivalent Fahrenheit value of T4: " + String.format("%.1fF", t4.getFahrenheit()));
  
System.out.println("\nComparing temperatures..");
System.out.println("0C equals 32F: " + t1.checkEqual(new Temperature(32, 'F')));
System.out.println("0C equals 23F: " + t1.checkEqual(new Temperature(23, 'F')));
System.out.println("0F is greater than 0C: " + t1.checkGreater(t2));
System.out.println("-40C is greater than 100C: " + t3.checkGreater(t4));
System.out.println("-40C is lesser than 212F: " + t3.checkLesser(new Temperature(212, 'F')));
System.out.println("100C is lesser than 21C: " + t4.checkLesser(new Temperature(21, 'C')));
  
System.out.println("\nSome equality comparisons..");
Temperature t11 = new Temperature();
Temperature t22 = new Temperature(32, 'F');
Temperature t33 = new Temperature(-40, 'C');
Temperature t44 = new Temperature(-40, 'C');
Temperature t55 = new Temperature(100, 'C');
Temperature t66 = new Temperature(212, 'F');
System.out.println("0.0 degrees C = 32.0 degrees F: " + t11.checkEqual(t22));
System.out.println("-40.0 degrees C = -40.0 degrees F: " + t33.checkEqual(t44));
System.out.println("100.0 degrees C = 212.0 degrees F: " + t55.checkEqual(t66));
}
}

******************************************************** SCREENSHOT ********************************************************


Related Solutions

Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n. public NumberUtility(int n) Class also has the following methods:   public int getN()                              // Returns instance variable n public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise. public boolean isEven()               // Returns true if number n is even and returns false if it is odd.      // Implement method by...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables....
You need to make an AngryBear class.(In java) The AngryBear class must have 2 instance variables. The first instance variable will store the days the bear has been awake. The second instance variable will store the number of teeth for the bear. The AngryBear class will have 1 constructor that takes in values for days awake and number of teeth. The AngryBear class will have one method isAngry(); An AngryBear is angry if it has been awake for more than...
Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in...
For My Programming Lab - Java: Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the following field: • ftemp: a double that holds a Fahrenheit temperature. The class should have the following methods : • Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field. • setFahrenheit: The set Fahrenheit method accepts...
Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a...
Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and width) as the calling object, this method should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT