# CSCI A201/A597 Problem Set Two

Second Summer 2000

Your problem is in QuizSite. Due Wed Jun 28 11:59pm. Solutions to be posted here Thursday at noon.

 1 Write a program that displays the squares, cubes, and fourth powers of the numbers 1-5.

 2 Write a program that prompts the user for two integers and then prints The sum The difference The product The average The distance (absolute value of the difference) The maximum (the larger of the two) The minimum (the smaller of the two)

 3 Write a program that prompts the user for a measurement in meters and then converts it into miles, feet and inches.

 4 Write a program that prompts the user for a radius and then prints The area and circumference of the circle with that radius The volume and surface area of the sphere with that radius

 5 Write a program that asks the user for the lengths of the sides of a rectangle. Then print The area and perimeter of the rectangle The length of the diagonal (use the Pythagorean theorem)

 6 Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. Hint: First transform the difference into an integer balance, denominated in pennies. Then compute the whole dollar amount. Subtract it from the balance. Compute the number of quarters needed. Repeat for dimes and nickels. Display the remaining pennies.

 7 Write a program that asks the user to input The number of gallons The fuel efficiency The price Then print how far the car can go with the gas in the tank and print the cost per 100 miles.

 8 DOS file names and extensions. Write a program that prompts the user for the drive letter (`C`), the path (`\Windows\System`), the file name (`ReadMe`), and the extension (`TXT`). Then print the complete file name `C:\Windows\System\ReadMe.TXT` If you use Unix or a Macintosh, use `/` or `:` instead to separate directories).

 9 Write a program that reads a number greater than or equal to 1000 from the user and prints it out with a comma separating the thousands. Here is a sample dialog; the user input is in color: ```Please enter an integer >= 1000: 23456 23,456```

 10 Write a program that reads a number greater than or equal to 1000 from the user, where the user enters a comma in the input. Then print the number without a comma. Here is a sample dialog; the user input is in color: ```Please enter an integer between 1,000 and 999,999: 23,456 23456``` Hint: Read the input as a string. Measure the length of the string. Suppose it contains n characters. Then extract substrings consisting of the first n - 4 characters and the last three characters.

 11 Printing a grid. Write a program that prints the following grid to play tic-tac-toe. ```+--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+``` Of course, you could simply write seven statements of the form `System.out.println("+--+--+--+")` You should do it a smarter way, though. Define string variables to hold two kinds of patterns: a comb-shaped pattern and the bottom line. Print the comb three times and the bottom line once.

 12 Write a program that reads an integer and breaks it into a sequence of individual digits. For example the input 16384 is displayed as `1 6 3 8 4` You may assume that the input has no more than five digits and is not negative. Hint: There are two ways of solving this problem. You can use integer arithmetic and repeatedly divide by 10, or you can convert the number into a string and extract the digits from the string.

 13 The following program prints the values of sine and cosine for 0 degrees, 30 degrees, 45 degrees, 60 degrees, and 90 degrees. Rewrite the program for greater clarity by factoring out common code. ```public class Thirteen { public static void main(String[] args) { System.out.println("0 degrees: " + Math.sin(0) + " " + Math.cos(0)); System.out.println("30 degrees: " + Math.sin(30 * Math.PI / 180) + " " + Math.cos(30 * Math.PI / 180)); System.out.println("45 degrees: " + Math.sin(45 * Math.PI / 180) + " " + Math.cos(45 * Math.PI / 180)); System.out.println("60 degrees: " + Math.sin(60 * Math.PI / 180) + " " + Math.cos(60 * Math.PI / 180)); System.out.println("90 degrees: " + Math.sin(90 * Math.PI / 180) + " " + Math.cos(90 * Math.PI / 180)); } } ```

 14 Write a program that prints out a message "Hello, my name is Hal!" Then, on a new line, the program should print the message "What is your name?" Next the program should read the user's name and print "Hello, user name. I am glad to meet you." Then, on a new line, the program should print a message "What would you like me to do?" Then it is the user's turn to type in an input. Finally the program should ignore the user input and and print the message "I am sorry, user name. I cannot do that." Here's a typical program run. The user input is printed in color. ```Hello, my name is Hal! What is your name? Dave Hello, Dave. I am glad to meet you. What would you like me to do? Clean up my room. I am sorry, Dave. I cannot do that. ```

 15 You don't know yet how to program decisions, but it turns out that there is a way to fake them using `substring`. Write a program that asks a user to input The number of gallons of gas in the tank The fuel efficiency in miles per gallon The distance the user wants to travel Then print out `You will make it` or `You will not make it` The trick here is to subtract the desired distance from the number of miles the user can drive. Suppose that the number is `x`. Suppose further that you find a way of setting a value `n` to 1 if `x` >= 0 and to 0 if `x` < 0. Then you can solve your problem: ```String answer = " not "; // note the spaces before and after not System.out.println("You will" + answer.substring(0, 5 - 4 * n) + "make it"); ``` Hint: Note that `x + |x|` is 2`x` if `x` >= 0, and 0 if `x` < 0. Then divide by `x`, except that you need to worry about the possibility that `x` is zero.

 16 Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. Here is a sample run. User input is in color. ```Please enter the first time: 0900 Please enter the second time: 1700 8 hours 30 minutes``` Extra credit if you can deal with the case that the first time is later than the second time: ```Please enter the first time: 1730 Please enter the second time: 0900 15 hours 30 minutes```

 17 Run the following program, and explain the output you get. ```public class Seventeen { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); int total = 0; System.out.println("Please enter a positive number:"); int x1 = Integer.parseInt(console.readLine()); System.out.println("total = " + total); total = total + 1 / x1; System.out.println("total = " + total); System.out.println("Please enter a positive number:"); int x2 = Integer.parseInt(console.readLine()); total = total + 1 / x2; System.out.println("total = " + total); total = total * x1 * x2 / 2; System.out.println("total = " + total); System.out.println("The average is " + total); } }``` Note the trace messages, which are inserted to show the current contents of the `total` variable. Then fix up the program, run it with the trace messages in place to verify that it works correctly, and remove the trace messages.

 18 Writing large letters. A large letter `H` can be produced like this: ```* * * * ***** * * * *``` It can be declared as a string constant like this: ```public static final String LETTER_H = "* *\n* *\n*****\n* *\n* *\n";``` Do the same for the letters `E`, `L`, and `O`. Then write the message ```H E L L O```

 19 Write a program that transforms numbers `1`, `2`, `3`, ..., `12` into the corresponding month names `January`, `February`, `March`, ..., `December`. Hint: Make a very long string `"January February March. . ."` in which you add spaces such that each month name has the same length. Then use `substring` to extract the month you want.

 20 Change the password program to make it generate more secure passwords. Use the random numner generator `Random` in the `java.util` package to generate a random number as follows: `int r = new Random().nextInt(1000);` Multiply the age by the random number. Then concentrate the initials with the last four digits of the product.

Last updated: June 22, 2000 by Adrian German for A201