Site Search

# CS 110: Program 5

## Student Learning Objectives

• Demonstrate the ability to write a Java user-defined class: `Temperature`
• Use private instance variables and public methods in your user-defined class
• Write a no-argument constructor method
• Write setter and getter methods
• Write Boolean methods to compare double values, accounting for a margin of error
• Use objects and method calls in your test program
• Use of jGRASP to type in, compile, execute, and print-out a simple Java program
• Include a file header comment to identify the author, account, and honor code
• Browse through the Java Style Guide for other standards and conventions to use in CS 110.

## `Temperature` class

Re-do your Lab 8's `Temperature` class to include

1. mutator methods to set a Temperature object's `degreesInKelvin` from Celsius and from Fahrenheit scales
• public void setDegreesFromCelsius(double celsius)
• public void setDegreesFromFahrenheit(double fahrenheit)
2. accessor methods to return the temperature object's `degreesInKelvin` in the Celsius, Fahrenheit, and Kelvin scale
• public double getDegreesInCelsius()
• public double getDegreesInFahrenheit()
• public double getDegreesInKelvin()
3. Add a Boolean method to compare two Temperature objects for equality plus or minus the margin of error and return true or false
• public Boolean isNearlyEqual(Temperature t2)
• Remember when comparing doubles to allow for a margin of error because of approximation errors.
• Use a named constant to set the margin of error at `0.001`
public class Temperature
{
// Declare constant value
private final double MARGIN_OF_ERROR = 0.001;

// Instance variable
private double degreesInKelvin;

// Constructor method: initialize degrees in Kelvin to zero
public Temperature()
{
degreesInKelvin = 0;
}

// Convert and save degrees in Celsius in the Kelvin scale
public void setDegreesFromCelsius(double celsius)
{
// you figure out this code
}

// Convert and save degrees in Fahrenheit in the Kelvin scale
public void setDegreesFromFahrenheit(double fahrenheit)
{
// you figure out this code
// Tip: use floating point division (not integer division)
}

// Convert and return degreesInKelvin in the Celsius scale
public double getDegreesInCelsius()
{
// you figure out this code
}

// Convert and return degreesInKelvin in the Fahrenheit scale
public double getDegreesInFahrenheit()
{
// you figure out this code
}

// Return degreesInKelvin in the Kelvin scale
public double getDegreesInKelvin()
{
// you figure out this code
}

// Return true if the two degrees are equal, plus or minus the margin of error
public boolean isNearlyEqual(Temperature t2)
{
// you figure out this code
}
}

Tip: to compare the two temperatures in method isNearlyEqual, you can use the method `getDegreesinKelvin`

Since the `isNearlyEqual` method's parameter is named `t2`, to get its temperature inside method `isNearlyEqual`, use

t2.getDegreesInKelvin()

Inside method `isNearlyEqual`, to get the temperature of the first object, use

getDegreesInKelvin()

and then figure our the difference between these two degrees in Kelvin.

## `TemperatureDemo`main method

Write a `TemperatureDemo` class with a main method to test your Temperature class.

Your TemperatureDemo main method should prompt the user for two temperatures (Celsius and Fahrenheit), display the equivalent temperatures in all three scalses, and then report if they are nearly equal or different.

import java.util.Scanner;

public class TemperatureDemo
{
public static void main(String[] args)
{
// Declare variable for user input
double degrees;

// Declare and create two Temperature objects
Temperature temp1 = new Temperature();
Temperature temp2 = new Temperature();

// Create a Scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);

// Get the first temperature in degrees Celsius
System.out.print("Enter a temperature in Celsius: ");
degrees = keyboard.nextDouble();
temp1.setDegreesFromCelsius(degrees);

// Get the second temperature in Fahrenheit
// You figure out this code

// Display the first temperature in all three scales
// You figure out this code

// Display the second temperature in all three scales
// You figure out this code

// Display the result of the isNearlyEqual method
// You figure out this code

}
}

## Sample Session

```
Enter a temperature in Celsius: `100.0`
Enter a temperature in Fahrenheit: `212.0001`

Temperature 1 in Kelvin is         373.15
Temperature 1 in Celsius is        100.0
Temperature 1 in Fahrenheit is     212.0

Temperature 2 in Kelvin is         373.1500555555555
Temperature 2 in Celsius is        100.00005555555555
Temperature 2 in Fahrenheit is     212.0001

The two temperatures are nearly equal

```

Another Sample Session

```Enter a temperature in Celsius: `1.666`
Enter a temperature in Fahrenheit: `100.756`

Temperature 1 in Kelvin is         274.816
Temperature 1 in Celsius is        1.6659999999999968
Temperature 1 in Fahrenheit is     34.998799999999996

Temperature 2 in Kelvin is         311.34777777777776
Temperature 2 in Celsius is        38.19777777777779
Temperature 2 in Fahrenheit is     100.75600000000001

The two temperatures are different
```

## What to turn in

• The CS 110 Program 5 Grading Sheet (PDF)
• Write your full name and account number on the Program 5 Grading Sheet
• Be sure both files are saved in your `U:\programs\program5\` folder on the computer science network.
• Note: if you complete the assignment on your personal computer, you must copy the file(s) to your your CS network account
• An easy way to copy the files is to use a USB portable drive, email, or a digital drop box
• Instructions for accessing your U-Drive remotely is provided if you are unable to physically come into the Hebeler lab to copy the files
• The assignment is due at the start of the lab 10
• Your program will be checked online. You do not need to print out a copy of your program to turn in.
• If you are unable to finish in time, you may turn in what you have done to receive partial credit for the assignment.
• You are allowed to drop one program assignment score for the quarter

20 points maximum

• 10 points - Output Correctness
• Conversions and display of the two temperature readings in all three scales is correct (-1 per problem, 6 points possible)
• Nearly equal or different is determined and displayed correctly (2 points possible)
• The output is displayed correctly as shown in the sample session above (2 points possible)
• Duplicate the sample session exactly
• 4 points - Class Definition
• The `Temperature` class is defined correctly with (-1 per problem, 3 points possible)
• private instance variables and named constants
• MARGIN_OF_ERROR is a private named constant
• Only one private instance variable degreesInKelvin is declared
• You do not need any other instance variables
• Instance methods (functions) are declared as public and return the appropriate type
• The `boolean` method, isNearlyEqual, is declared and used correctly
• The `TemperatureDemo` class main method uses the Temperature class methods correctly (1 point)
• 3 points - Comments and Alignment
• Skip a blank line before every single-line comment
• Generally one single-line comment is needed for every four to six lines of code
• Use a single-line comment before every method in Circle to explain what the method does
• Use jGRASP Generate CSD to properly align your printout
• Alignment and indentation are important. Read through the Java Style Guide for CS 110 expectations
• 2 points - Identifier Names
• Meaningful identifier names used for all identifiers (-1 per problem, 2 points possible)
• Variable and method names should begin with a lower case letter, and each subsequent word should start with an upper-case letter.
• Class names should begin with an upper case letter, and each subsequent word should start with an upper-case letter.
• Named constant names should be in all capital letters and the underscore character used to separate words if necessary
• Avoid the use of abbreviations in choosing variable names
• Read through the Java Style Guide for CS 110 expectations.
• 1 point - Creation of Assignment Folder, File, and Printouts
• Program files Temperature.java and TemperatureDemo.java created correctly and saved in `U:\programs\program5\` (1 point possible)