Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating-point number (e.g. a double) for the temperature and

Write a Temperature class that represents temperatures in degrees in both Celsius and Fahrenheit. Use a floating-point number (e.g. a double) for the temperature and a character for the scale: either 'C' 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 the scale, and a default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given.

Two standard accessor methods: one to return the temperature in degrees Celsius, the other to return it in degrees Fahrenheit. Use the formulas from Practice Program 5 of Chapter 3 and round to the nearest tenth of a degree (the DollarFormat program shows how, or you can use Math.round).

Three versions of a set method: one to set the number of degrees, one to set the scale, and one to set both (with two parameters).

Three comparison methods: one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another (named equals, isGreaterThan, and isLessThan).

A writeOutput method that prints the temperature and scale.

The writeOutput method must produce output that looks like this:

Temperature =  degrees . 

where degrees is the number of degrees, rounded to the nearest tenth of a degree, and units is the capitalized scale (C or F).

Write a driver program in main that tests all the methods. Be sure to invoke every one of the constructors, to include at least one true and one false case for each comparison method, and to test at least the following three temperature pairs for equality: 0.0 degrees C and 32.0 degrees F, 40.0 degrees C and 40.0 degrees F, and 100.0 degrees C and 212.0 degrees F.

All of these tests have already been included in the supplied main method, but feel free to add additional tests above those

IN JAVA:

public class Temperature

{

private double degrees;

private char units; //'C' for Celsius, 'F' for Fahrenheit

// Four Constructors

/* Write the constructor to set both the temperature and the units here */

// Be sure to convert the character to upper case before storing it!

// You can use Character.toUpperCase(char character) to do that.

/* Write the constructor to specify just the temperature here */

/* The temperature type must default to 'C' for Celsius */

/* You should call the first constructor to complete the setup */

/* Write the constructor to specify just the units here */

/* The temperature must default to 0 degrees */

/* You should call the first constructor to complete the setup */

/* Write the default constructor here (specifies neither temperature or units) */

/* The values must default to 0 degrees Celsius */

/* You should call the first constructor to complete the setup */

/* Write a method for the user to specify the parameters interactively here */

/* This method does not have to validate that the temperature type is correct */

/* Be sure to convert the temperature type to upper case before storing it */

public void readInput()

{

Scanner170 keyboard = new Scanner170(System.in);

/* write this method here - use nextDouble and next to read the values; be

sure to use just the first character of the String returned by next */

}

// Create three "write" methods to display temperature.

/* First, write a private helper method that rounds a double value to one

decimal place and returns that value */

private double round(double value)

{

/* write this method here */

}

/* Now write a method to display all the parameter values here,

in the format specified above; use round() to make sure

the displayed temperature has only one decimal place */

public void writeOutput()

{

/* write this method here */

}

/* Write a method to display the temperature in degrees C here */

/* Use getC() to make sure the displayed temperature only has

one decimal place */

public void writeC()

{

/* write this method here */

}

/* Write a method to display the temperature in degrees F here */

/* Use getF() to make sure the displayed temperature only has

one decimal place */

public void writeF()

{

/* write this method here */

}

// Write two methods to get (return) the temperature value in either

// degrees C or degrees F, rounded to one decimal place.

/* Write a method to return temperature in degrees C, and

just return the value of variable degrees if units is

an invalid character (other than c, C, f, or F).

Note that if the temperature type is F you must convert

the temperature to Celsius before returning it. */

/* Use round() to make sure the temperature only has one

decimal place. */

public double getC()

{

/* write this method here */

}

/* Write a method to return temperature in degrees F, and

just return the value of variable degrees if units is

an invalid character (other than c, C, f, or F).

Note that if the temperature type is C you must convert

the temperature to Fahrenheit before returning it. */

/* Use round() to make sure the temperature only has one

decimal place. */

public double getF()

{

/* write this method here */

}

// Three methods to set (or reset) the parameter values.

/* Write a method to set both parameters. */

/* Convert the units to upper case and use round. */

// Postconditions: Does not guarantee legitimate value for

// units (allows any character, not just c, C, f, or F.

public void set(double newDegrees, char newUnits)

{

/* write this method here */

}

/* Write a method to set just the temperature. */

/* Use round to make sure it only has one decimal place. */

public void set(double newDegrees)

{

/* write this method here */

}

/* Write a method to set just the temperature units. */

/* Convert the units to upper case before storing. */

// Postconditions: Does not guarantee legitimate value for

// units (allows any character, not just c, C, f, or F).

public void set(char newUnits)

{

/* write this method here */

}

// Three comparison methods.

/* Write a method that tells if two temperatures are equal. */

/* Use getF or getC to convert both temperatures to an integer number

of tenths of degrees in the same units to avoid errors due to comparing

floating point numbers, which often cannot be stored in memory precisely. */

public boolean equals(Temperature another)

{

/* Write this method here */

}

/* Write a method that tells if one temperatures is greater than another. */

/* Use getF or getC to convert both temperatures to an integer number

of tenths of degrees in the same units to avoid errors due to comparing

floating point numbers, which often cannot be stored in memory precisely. */

public boolean isGreaterThan(Temperature another)

{

/* Write this method here */

}

/* Write a method that tells if one temperatures is less than another. */

/* Use getF or getC to convert both temperatures to an integer number

of tenths of degrees in the same units to avoid errors due to comparing

floating point numbers, which often cannot be stored in memory precisely. */

public boolean isLessThan(Temperature another)

{

/* Write this method here */

}

public static void main(String[] args) // this contains a *lot* of test cases!

{

/* If you want to create any additional test cases, write them here */

/* Do not use the readInput method in any tests that you create */

/* DO NOT MODIFY THIS MAIN METHOD BELOW THIS COMMENT! */

System.out.println();

System.out.println("Test case 1: default constructor and");

System.out.println("writeOutput() method.");

System.out.println();

Temperature t1 = new Temperature();

System.out.println("Results of default constructor:");

System.out.println("Verify 0 degrees C.");

System.out.println();

t1.writeOutput();

System.out.println();

System.out.println("==========================================");

System.out.println("Test case 2: readInput() method.");

t1.readInput();

System.out.println();

System.out.println("Verify temperature and units:");

System.out.println("Should be whatever you just entered, rounded.");

System.out.println();

t1.writeOutput();

System.out.println();

System.out.println("==========================================");

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data 29th British National Conference On Databases Bncod 2013 Oxford Uk July 2013 Proceedings Lncs 7968

Authors: Dan Olteanu ,Georg Gottlob ,Christian Schallhart

2013th Edition

3642394663, 978-3642394669

More Books

Students also viewed these Databases questions

Question

a. How do you think these stereotypes developed?

Answered: 1 week ago

Question

7. Describe phases of multicultural identity development.

Answered: 1 week ago