CSci 2101 Data Structures: Problem set 1

Due Monday, February 7th at 8pm.

Problem 1 (5 points)

The following program computes the circumference and the area of a circle with an integer diameter. The program has rounding errors. Identify the errors and correct them so that the program computes with the maximum precision that the type double allows. Make sure to test your program carefully and convince yourselves that the there is no precision loss (Hint: diameter = 1 is a good test case). You may not change the type of the variable diameter, but you may change its value to test the program.

// Written by Elena Machkasova 9/5/04
// Modified by 

public class Precision {
    public static void main(String[] args) {
	int diameter = 1; // change values to test
	final double PI = 3.141592654; // a constant (usually named in CAPS)
	int circumference = diameter * (int) PI;
	double radius = diameter / 2;
	double area = PI * radius * radius;

	System.out.println("If the diameter of a circle is " + diameter);
	System.out.println("then the circumference is " + circumference);
	System.out.println("and the area is " + area);

Please submit the corrected program with comments explaining the errors. Don't worry about the formatting of the output.

Problem 2 (10 points)

Write a program that has two variables of type double: fahrenheit and celsius. Initialize fahrenheit to some value. Then the program must perform the following operations:
  1. Convert the temperature to Celsius. Click here for the formula.
  2. Print out the value in Celsius.
  3. If the temperature is above 100 degrees Celsius, print out "The water is boiling", otherwise print out "The water is not boiling"

This is a problem set from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.