CS 1401 Assignment #2, Solutions

Date Assigned: Monday, January 29 or Tuesday, January 30, 2007.

Due Date: Monday, February 5, or Tuesday, February 6, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to practice using input, output, arithmetic operations, and operations with strings.

Programming assignment: To protect CS students against snow and cold, the department is thinking of buying warm jackets so that students can keep warm when walking on campus. Jackets come in different size, depending on the student's height. Help the department estimate the cost of these jackets. The cost of a jacket is proportional to the square of the student's height in meters, with a cost of $10 per square meter. Your program should estimate a cost of a jacket for a student. Specifically, your program should:

Example: Suppose that the height of a hypothetical student Rodrigo Kreinovich is 5 feet and 7 inches. First, we compute the height in inches; since 1 foot = 12 inches, we get 5 * 12 + 7 = 67 inches. Then, we get the height in centimeters as 2.54 * 67 = 170.18, and then the height in meters as 170.18/100 = 1.7018. The cost of the jacket is then 10.0 * 1.7018 * 1.7018 = $28.961232.

Comments:

Solution:

public static void main(String [] args){
  Scanner console = new Scanner(System.in);
  System.out.println("Please enter your name");
  String name = console.next();
  System.out.println("Please enter you height in feet");
  int feet = console.nextInt();
  System.out.println("Please enter you height in inches, e.g., 4 or 7");
  int inches = console.nextInt();
  int heightInInches = inches + 12 * feet;
  double heightInCm = 2.54 * heightInInches;
  double heightInMeters = heightInCm / 100.0;
  double cost = 10.0 * heightInMeters * heightInMeters;
  System.out.println("Bill for a Winter Jacket");
  System.out.println("Student name: " + name);
  System.out.println("Jacket cost: " + cost);
}
Homework assignment: on a separate sheet of paper, solve Ex. 2 (p. 100), Ex. 10 (p. 102), and Ex. 12 (p. 103).

Deliverables: as instructed by your TA.