## CS 1401 Assignment #3, Solutions

Date Assigned: Monday, February 5 or Tuesday, February 6, 2007.

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

Objective: The main objective of this assignment is to practice graphical input and output and also input, output with files.

Programming assignment: similar to Programming Assignment 2.

Reminder: 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:

• ask the student for his or her name;
• ask the student for the his or her height in feet, and then height in inches;
• print a bill with the student's name and the cost of the jacket.

New features:

• set up dialog boxes for input and output;
• format the input to 2 digits after the decimal points (as is natural in representing dollars and cents);
• in addition to printing the bill on the screen, store it in a file assign03.dat.

```Solution:

import javax.swing.JoptionPane;
import java.io.*;
import java.util.*;

public class WinterJackets
{
public static void main(String [] args) throws IOException
{
String name;
int feet;
int inches;
int heightInInches;
double heightInCm;
double heightInMeters;
double cost;

String inputString;
String outputString;

inputString = JOptionPane.showInputDialog
feet = Integer.parseInt(inputString);

inputString = JOptionPane.showInputDialog
inches = Integer.parseInt(inputString);

heightInInches = inches + 12 * feet;
heightInCm = 2.54 * heightInInches;
heightInMeters = heightInCm / 100.0;
cost = 10.0 * heightInMeters * heightInMeters;

outputString = "Student name: " + name + "\n" +
"Umbrella cost: \$" + String.format("%.2f", cost);
JOptionPane.showMessageDialog(null, outputString,
"UTEP Umbrellas",
JOptionPane.INFORMATION_MESSAGE);

PrintWriter toFile = new PrintWriter("assign03.dat");
toFile.println("Bill for a UTEP umbrella" + "\n" + outputString);
toFile.close();

System.exit(0);
}
}
```
Homework assignment: on a separate sheet of paper, solve Ex. 10 and Ex. 12, p. 162.

Deliverables: as instructed by your TA.