Purchase Solution

Java OOP Practice

Not what you're looking for?

Ask Custom Question

/**
Class that maintains a Celsius or Fahrenheit temperature value
*/
class Temperature {
/**
Temperature value
*/
private double value;

/**
Scale, either "C" for Celsius or "F" for Fahrenheit
*/
private char scale;
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------

// --------------------------------
// --------- END USER CODE --------
// --------------------------------
/**
Returns a string containing the value and scale of the Temperature
*/
public String toString() {
return Double.toString(value) + scale;
}

}

/**
Demo class to exercise the methods in the Temperature class.
*/
public class TemperatureDemo {

public static void main(String[] args) {
// Create temperatures using each constructor
Temperature freezingC = new Temperature(0.0, 'C');
System.err.println("Water freezes at " + freezingC.toString());

Temperature freezingF = new Temperature(32.0, 'F');
freezingF.setScale('F');
System.err.println("Water freezes at " + freezingF.toString());

Temperature coldC = new Temperature();
coldC.setValue(-40.0);

Temperature coldF = new Temperature();
coldF.setScale('F');
coldF.setValue(-40.0);

System.out.println(coldC + " (" + coldF + ") is very cold.");

Temperature boilingF = new Temperature('F');
boilingF.setValue(212.0);
Temperature boilingC = new Temperature(100.0);
boilingC.setValue(100.0);
System.out.println(
"Water boils at " + boilingF + " (" + boilingC + ")");
System.out.println();

// Test equals
System.out.println(
boilingF + " = " + boilingF + " ? " + boilingF.equals (boilingF));
System.out.println(
freezingC + " = " + freezingF + " ? " +freezingC.equals(freezingF));
System.out.println(
boilingF + " = " + boilingC + " ? " + boilingF.equals(boilingC));
System.out.println(
boilingC + " = " + boilingF + " ? " + boilingC.equals(boilingF));
System.out.println(
coldC + " = " + coldF + " ? " + coldC.equals(coldF));
System.out.println();

// Test greater than and less than
System.out.println(
coldC + " < " + boilingC + " ? " + coldC.isLessThan(boilingC));
System.out.println(
coldC + " > " + boilingC + " ? " + coldC.isGreaterThan(boilingC));
System.out.println(
coldC + " < " + freezingF+ " ? " + coldC.isLessThan(freezingF));
System.out.println(
coldC + " > " + freezingF + " ? " + coldC.isGreaterThan(freezingF));
}

}

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 (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)/9 degreesF = (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.

Implement methods getCelsiusValue() and getFahrenheitValue(), then use one of these for comparing two Temperature objects in methods:
public boolean equals(Temperature obj)

public boolean isLessThan(Temperature other)

public boolean isGreaterThan(Temperature other)
These methods should handle the case where the two Temperature objects are using different scales

Purchase this Solution

Solution Summary

This solution provides step for writing a program in Java.

Purchase this Solution


Free BrainMass Quizzes
Basic Physics

This quiz will test your knowledge about basic Physics.

The Moon

Test your knowledge of moon phases and movement.

Variables in Science Experiments

How well do you understand variables? Test your knowledge of independent (manipulated), dependent (responding), and controlled variables with this 10 question quiz.

Introduction to Nanotechnology/Nanomaterials

This quiz is for any area of science. Test yourself to see what knowledge of nanotechnology you have. This content will also make you familiar with basic concepts of nanotechnology.

Intro to the Physics Waves

Some short-answer questions involving the basic vocabulary of string, sound, and water waves.