SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes I
Exercise:
Programming Projects
Question:7 | ISBN:9780132830317 | Edition: 5

Question

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:

(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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Explanation:

The following program contains two classes. 
•    Temperature class
•    TemperatureTest class

The Temperature class contains two instance variables, four constructor methods, two accessor methods to return the temperature and scale, three mutator methods, three comparison methods and a suitable toString() method.
The TemperatureTest is used to test the Temperature class. The TemperatureTest class contains objects of Temperature class. The Temperature class methods are used in this method. This includes at least one true and one false case for each of the comparison methods.
 

Code:

/*********** Filename: Temperature.java ***********/

// Create a class named Temperature.
public class Temperature
{
	// Declare the two instance variables.
	private double temperature;
	private char scale;
	
	// Create four constructor methods.
	// Create a no-argument constructor.
	public Temperature()
	{
		// Set to zero degrees Celsius.
		temperature = 0;
		scale = 'C';
	}
	
	// Create a constructor for the instance variable named scale.
	public Temperature(char scale)
	{
		this.scale = scale;
		// Assume zero degrees if no value is specified.
		temperature = 0;
	}
	
	// Create a constructor for the instance variable named temperature.
	public Temperature(double temperature)
	{
		this.temperature = temperature;
		// Assume Celsius, if no scale is specified
		scale = 'C';
	}
	
	// Create two parameters constructor for the two instance variables.
	public Temperature(double temperature, char scale)
	{
		this.temperature = temperature;
		this.scale = scale;
	}
	
	/********** (1) **********/
	
	// Create two accessor methods.
	// Create an accessor to return the degrees Celsius.
	public double getCelsius()
	{
		if (scale == 'C')
		{
			return temperature;
		}
		else
		{
			// Round to the nearest tenth of a degree
			return ((double) (Math.round((5 * (temperature - 32.0) / 9.0) * 10.0)) / 10);
		}
	}
	
	// Create an accessor to return the degrees Fahrenheit
	public double getFahrenheit()
	{
		if (scale == 'F')
		{
			return temperature;
		}
		else
		{
			// Round to the nearest tenth of a degree
			return (Math.round((9 * (temperature / 5) + 32) * 10) / 10);
		}
	}
	
	/********** (2) **********/
	// Create three mutator methods.
	// Create a mutator to set the temperature.
	public void setTemperature(double temperature)
	{
		this.temperature = temperature;
	}
	
	// Create a mutator to set the scale
	public void setScale(char scale)
	{
		this.scale = scale;
	}
	
	// Create a mutator to set both
	public void setTemperatureScale(double temperature, char scale)
	{
		this.temperature = temperature;
		this.scale = scale;
	}
	
	/*********** (3) *********/
	// Create three comparison methods.
	// Create an equals method to test whether 
	// two temperatures are equal or not.
	public boolean equals(Object obj)
	{
		if (obj instanceof Temperature)
		{
			Temperature t = (Temperature) obj;
			return getCelsius() == t.getCelsius();
		}
		return false;
	}
	
	// Create a method to test whether one temperature is greater than another.
	public boolean lessthan(Object obj)
	{
		if (obj instanceof Temperature)
		{
			Temperature t = (Temperature) obj;
			return getCelsius() > t.getCelsius();
		}
		return false;
	}
	
	// Create a method to test whether one temperature is less than another.
	public boolean greaterthan(Object obj)
	{
		if (obj instanceof Temperature)
		{
			Temperature t = (Temperature) obj;
			return getCelsius() < t.getCelsius();
		}
		return false;
	}
	
	/************ (4) ***********/
	// Create a suitable toString method.
	public String toString()
	{
		if (scale == 'C')
		{
			return String.format("%.1f degrees C = %.1f degrees F", temperature, getFahrenheit());
		}
		else
		{
			return String.format("%.1f degrees F = %.1f degrees C", temperature, getCelsius());
		}
	}
}

 

/*********** Filename: TemperatureTest.java ***********/

// Create a class named TemperatureTest
public class TemperatureTest
{
	// Create a main method to run the program.
	public static void main(String[] args)
	{
		System.out.println("Celsius to Fahrenheit:");
		
		// Create an object named c2fObj1.
		Temperature c2fObj1 = new Temperature();
		c2fObj1.setTemperature(0.00);
		c2fObj1.setScale('C');
		System.out.println(c2fObj1.toString());

		// Create an object named c2fObj2.
		Temperature c2fObj2 = new Temperature('C');
		c2fObj2.setTemperature(-40);
		System.out.println(c2fObj2.toString());
		
		// Create an object named c2fObj3.
		Temperature c2fObj3 = new Temperature(100.00, 'C');
		System.out.println(c2fObj3.toString());
		
		// Create an object named f2cObj1.
		System.out.println("\nFahrenheit to Celsius:");
		Temperature f2cObj1 = new Temperature(0);
		f2cObj1.setTemperatureScale(56.00, 'F');
		System.out.println(f2cObj1.toString());
		
		// Display Comparision.
		System.out.println("\nComparision:");
		System.out.println("c2fObj1 < c2fObj2: " + c2fObj1.lessthan(c2fObj2));
		System.out.println("c2fObj2 = c2fObj3: " + c2fObj1.equals(c2fObj3));
		System.out.println("c2fObj3 > f2cObj1: " + c2fObj3.greaterthan(f2cObj1));		
	}	
}

 

Output:

Celsius to Fahrenheit:
0.0 degrees C = 32.0 degrees F
-40.0 degrees C = -40.0 degrees F
100.0 degrees C = 212.0 degrees F

Fahrenheit to Celsius:
56.0 degrees F = 13.3 degrees C

Comparision:
c2fObj1 < c2fObj2: true
c2fObj2 = c2fObj3: false
c2fObj3 > f2cObj1: false

 

0 0

Discussions

Post the discussion to improve the above solution.