Homework 2
Home Up

 

CS4984 Homework #2

GIVEN: February 15, 2001

DUE: March 1, 2001

 

 

 

Problem 1:

PURPOSE: The whole purpose of Problem 1 is to give you some practice designing an abstraction as a class (Temperature) and a test program for it (TempTest) and to see how two such classes interact.

Part A: Design a class named "Temperature".

Temperature should have one constructor that initializes the temperature to zero degrees Fahrenheit.

There should be a mutator method (function) of the class that takes an integer as a parameter and changes the existing temperature to be that value in Fahrenheit.

Another mutator method of the class should take as parameters both an integer and a character value with 'f', 'c', or 'k', indicating whether the existing temperature value is to be changed to degrees Fahrenheit, Celsius, or Kelvin.

This mutator method should have exactly the same name as the mutator method that is described in the previous paragraph.

In any of the methods, a temperature of less than absolute zero (0 degrees Kelvin) is an error. Print out an error message using System.out.println if such a temperature is received.

 

Your Temperature class should have three accessor methods, one each to return the temperature in degrees Fahrenheit, Celsius, or Kelvin. The formulae for conversion are:

          Subtract 32 from the Fahrenheit temperature.

          Multiply the result by 5/9.

          The result is the Celsius temperature.
          In other words:

          F = 1.8xC + 32

          C = 5*(F - 32)/9

          Degrees Kelvin are in degrees Celsius + 273

           

For this problem it is sufficient to use an integer for the resulting degrees that you return to the user via the three accessor methods.

Note that the Temperature class should not have a main function, since it is designed to be used by other Java classes, not by a user at the command line.

Part B: Design a class entitled "TempTest".

TempTest's only purpose is to test the Temperature class. When it is invoked via "java TempTest" it will print the following menu:

  1. Change the temperature to DEGREES (C/F/K)
  2. Read the temperature in degrees Kelvin
  3. Read the temperature in degrees Celsius
  4. Read the temperature in degrees Fahrenheit.
  5. Exit this test program

 

In option one, the correct input by the user is some integer number followed by an optional space and character that is either c, f, or k. If no letter follows the integer, the temperature is assumed to be in degrees Fahrenheit. Obviously, c, f, or k would indicate degrees Celsius, Fahrenheit, or Kelvin, respectively.

Your test program should loop forever, redisplaying the menu after each user selection, until the user selects option 5, which causes the program to exit.

Note that TempTest should have a main method, since it is to be invoked by the user at the command line.

 

PROBLEM 2

Purpose: To familiarize yourself with arrays in Java.

Design a class entitled "array34".

It should contain a constructor that fills a 3x4 array with random numbers that are obtained by repeated invocations of the static method Math.random().

The class should contain a method that prints the entire array such that each row is on its own line and also so that the fractional part of each array entry has exactly 4 fractional decimal places.

The class should contain a method that calculates the minimum and maximum elements in the array and prints the values and their positions in the array to the standard output. The minimum and maximum outputs should also have exactly 4 fractional decimal places.

The main method of the class should be such that when it is invoked on the command line via "java array34" that the following is performed:

  1. A new array34 object is created.
  2. The minimum and maximum elements of the array belonging to this array34 object are printed.
  3. The entire array belonging to the array34 object is printed.

The following shows a sample of what you might see on the standard output when running this program:

C:\jdk1.2.2\bin>java array34

Maximum: 0.9464 at position 1,2

Minimum: 0.1428 at position 2,3

The array contents are:

0.8456 0.7414 0.1684 0.7868

0.3364 0.4528 0.9464 0.7364

0.5432 0.4122 0.3731 0.1428

C:\jdk1.2.2\bin>java array34

Maximum: 0.9293 at position 1,1

Minimum: 0.0458 at position 1,3

The array contents are:

0.7449 0.5575 0.6430 0.2537

0.7755 0.9293 0.1378 0.0458

0.4339 0.6952 0.4407 0.8027

C:\jdk1.2.2\bin>java array34

Maximum: 0.8864 at position 2,1

Minimum: 0.0646 at position 1,3

The array contents are:

0.6823 0.1549 0.3299 0.7327

0.2154 0.1978 0.1223 0.0646

0.5723 0.8864 0.6749 0.6101

C:\jdk1.2.2\bin>

Problem 2 HINTS: Examine the documentation for the following classes: java.lang.Math and java.text.NumberFormat


Copyright © 2000-2001 First Principles. All rights reserved.
For problems or questions regarding this web contact webmaster.
Last updated: 07 June 2001 23:06:27 -0400.