Lab 03 Solution
Version 1

 


Problem 01

  Write a program which prompts the user for the height, width, and depth of a rectangular solid and computes and prints the volume of the solid. The program should use ints to store the values.

Click Here To Run This Program On Its Own Page
// Program: LAB03PR01
// Author:  Joshua S. Hodas
// Date:    October 6, 1996
// Purpose: Calculate volume of rectangular solid
 
import HMC.HMCSupport;
 
class LAB03PR01 {
 
  public static void main(String args[]) {
 
    int height, width, depth, volume;
 
    HMCSupport.out.print("This program computes the volume of"
                     + " a rectangular solid.");
    HMCSupport.out.println();
 
    HMCSupport.out.print("Please enter the height: ");
    height = HMCSupport.in.nextInt();
    HMCSupport.out.print("Please enter the width: ");
    width = HMCSupport.in.nextInt();
    HMCSupport.out.print("Please enter the depth: ");
    depth = HMCSupport.in.nextInt();
 
    volume = height * width * depth;
 
    HMCSupport.out.println("The volume of a " + height
                         + " x " + width + " x " + depth
                         + " rectangular solid is " + volume);
  }
 
}


Problem 02

  Write a program which asks the user for an annual salary and tells them the equivalent hourly wage, based on working forty hours per week for fifty weeks per year. You should use doubles throughout the program. When you print the hourly wage, do so with exactly two digits to the right of the decimal (as you'd expect for monetary values).

Click Here To Run This Program On Its Own Page
// Program: LAB03PR02
// Author:  Joshua S. Hodas
// Date:    October 6, 1996
// Purpose: Translate annual salary to hourly wage
 
import HMC.HMCSupport;
 
class LAB03PR02 {
 
  public static void main(String args[]) {
 
    // We put the weeks per year and hours per week in
    // variables even though they will only be used
    // once. It improves the clarity of the program and
    // makes it easier to change to one where the
    // values are used more than once, or are entered by
    // the user.
 
    double annualSalary, hourlyWage,
           hoursPerWeek = 40, weeksPerYear = 50;
 
    HMCSupport.out.print("This program computes hourly wage"
                     + " based on annual salary.");
    HMCSupport.out.println();
 
    HMCSupport.out.print("Please enter the annual salary: ");
    annualSalary = HMCSupport.in.nextDouble();
 
    hourlyWage = annualSalary / (hoursPerWeek * weeksPerYear);
 
    // The numbers are printed in separat statements to
    // give setPrecision and setFixed a chance to apply.
 
    HMCSupport.out.setPrecision(2);
    HMCSupport.out.setFixed(true);
    HMCSupport.out.print("An annual salary of ");
    HMCSupport.out.print(annualSalary);
    HMCSupport.out.print(" works out to an hourly wage of ");
    HMCSupport.out.println(hourlyWage);
  }
}


Problem 03

 

Write a program which asks the user for an angle in radians and tells them the value of all three basic trig functions for that value.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB03PR03
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print the trig functions' values for a user-supplied number
*/
 
import HMC.HMCSupport;  
 
class LAB03PR03 {
    
    public static void main(String args[]) {
 
        double theta;   // the angle
 
        HMCSupport.out.print("Please enter an angle in radians: ");
        theta = HMCSupport.in.nextDouble();
 
        HMCSupport.out.println();
        HMCSupport.out.println("sin(" + theta + ") = " + Math.sin(theta));
        HMCSupport.out.println("cos(" + theta + ") = " + Math.cos(theta));
        HMCSupport.out.println("tan(" + theta + ") = " + Math.tan(theta));
 
    }
    
}


Problem 04

  Write a program which asks the user for an angle in radians and then prints output that will allow the user to confirm some of the basic trig identities (i.e. sin2 + cos2 = 1)

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB03PR04
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To confirm a trig identity
*/
 
import HMC.HMCSupport;  
 
class LAB03PR04 {
    
    public static void main(String args[]) {
 
        double theta;                // the angle
        double sinTheta, cosTheta;   // sin and cos of the angle
 
        HMCSupport.out.print("Please enter an angle in radians: ");
        theta = HMCSupport.in.nextDouble();
 
        // We store of the value of the sine and cosine as we will need
        // them more than once each (to compute the square) and they
        // are expensive to calculate.
 
        sinTheta = Math.sin(theta);
        cosTheta = Math.cos(theta);
 
        HMCSupport.out.println();
        HMCSupport.out.println("sin(" + theta + ") = " + sinTheta);
        HMCSupport.out.println("cos(" + theta + ") = " + cosTheta);
        HMCSupport.out.println("sin^2(" + theta + ")  + cos^2(" +
                               theta + ") = " + 
                               (sinTheta * sinTheta  + cosTheta * cosTheta));
 
    }
    
}


Problem 05

  Write a program which displays a random floating point value in the range between 0 and 1.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB03PR05
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print a random value in the range 0 to 1
*/
 
import HMC.HMCSupport;  
 
class LAB03PR05 {
    
    public static void main(String args[]) {
 

HMCSupport.out.println(Math.random() + "is a random value in the interval [0.0,1.0)"); } }


Problem 06

  Write a program which displays a random floating point value in the range between 0 and 10.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB03PR06
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print a random value in the range 0 to 10
*/
 
import HMC.HMCSupport;  
 
class LAB03PR06 {
    
    public static void main(String args[]) {
 

HMCSupport.out.println((10 * Math.random()) + "is a random value in the interval [0.0,10.0)"); } }


Problem 07

  Write a program which simulates a throw of a pair of dice. That is, it generates and reports two random integers from 1 to 6 and their sum.

Click Here To Run This Program On Its Own Page
/* 
   Program: LAB03PR07
   Author:  Joshua S. Hodas 
   Date:    9/19/98
   Purpose: To print the trig functions' values for a random angle
*/
 
import HMC.HMCSupport;  
 
class LAB03PR07 {
    
    public static void main(String args[]) {
 
        double theta = Math.random() * 2 * Math.PI;   // the random angle
 
        HMCSupport.out.println("A random angle in the range of 0 to 2*pi is " +
                               theta);
        HMCSupport.out.println("sin(" + theta + ") = " + Math.sin(theta));
        HMCSupport.out.println("cos(" + theta + ") = " + Math.cos(theta));
        HMCSupport.out.println("tan(" + theta + ") = " + Math.tan(theta));
 
    }
    
}

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Sat, Sep 19, 1998 at 6:04:16 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_03/labsolutions.html