  Spring Semester 2002

Lab Notes Two: Problem Set One.

Try to solve these problems to practice some of the things you learned this week. In the examples that follow, your program's answers are always in `blue`, to distinguish them from what you would type as a user. Remember: the resulting programs are elementary, and the problems are interesting.

 1. Write a program that displays the squares, cubes, and fourth powers of the numbers 1-5. Here's a sample run of such program: ```frilled.cs.indiana.edu%java One First five powers of 1: 1 1 1 1 1 First five powers of 2: 2 4 8 16 32 First five powers of 3: 3 9 27 81 243 First five powers of 4: 4 16 64 256 1024 First five powers of 5: 5 25 125 625 3125 frilled.cs.indiana.edu%```

 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) Here's a sample run with your program: ```frilled.cs.indiana.edu%java Two Please enter your first integer number, then press Enter. 3 Please enter your second integer number, then press Enter. 6 3 + 6 = 9 3 - 6 = -3 3 * 6 = 18 avg(3, 6) = 4.5 dist(3, 6) = 3 max(3, 6) = 6 min(3, 6) = 3 frilled.cs.indiana.edu%```

 3. Write a program that prompts the user for a measurement in meters and then converts it into miles, feet and inches. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Three Please enter the measurement in meters: 100 Your original measurement of 100.0 meters has been converted. 0 miles, 328 feet, and 1.0078719999998889 inches. frilled.cs.indiana.edu%java Three Please enter the measurement in meters: 16000 Your original measurement of 16000.0 meters has been converted. 9 miles, 4983 feet, and 7.1495919999956445 inches. frilled.cs.indiana.edu%```

 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 Here's a sample run with your program: ```whitetip.cs.indiana.edu%java Four Please enter value for radius then hit enter. 100 Thank you. The radius is 100.0 Here are the computed values. Area of the circle: 31415.926535897932 Circumference: 628.3185307179587 Area of a sphere: 125663.70614359173 Volume of sphere: 41887.90204786391 whitetip.cs.indiana.edu%java Four Please enter value for radius then hit enter. 1 Thank you. The radius is 1.0 Here are the computed values. Area of the circle: 3.141592653589793 Circumference: 6.283185307179586 Area of a sphere: 12.566370614359172 Volume of sphere: 4.1887902047863905 whitetip.cs.indiana.edu%```

 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) Here's a sample run with your program: ```whitetip.cs.indiana.edu%java Five Please enter the value for the first side. 3 Thanks. Side one is 3.0 Please enter the value for the second side. 4 Thanks. Side two is 4.0 Area is: 12.0 Perimeter is: 14.0 Diagonal is: 5.0 whitetip.cs.indiana.edu%java Five Please enter the value for the first side. 1 Thanks. Side one is 1.0 Please enter the value for the second side. 1 Thanks. Side two is 1.0 Area is: 1.0 Perimeter is: 4.0 Diagonal is: 1.4142135623730951 whitetip.cs.indiana.edu%```

 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. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Six Type the amount due then press enter. 3.72 Type the amount received then press enter. 5 Give 1.28 in change as follows: 5 quarters 0 dimes 0 nickels 3 cents frilled.cs.indiana.edu%java Six Type the amount due then press enter. 0.08 Type the amount received then press enter. 0.5 Give 0.42 in change as follows: 1 quarters 1 dimes 1 nickels 2 cents frilled.cs.indiana.edu%```

 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. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Seven Please enter the number of gallons then press enter. 32 Please enter the fuel efficiency (miles/gallon) then press enter. 16 Please enter the price per gallon, then press enter. 1.54 With the gas in the tank you can go 512.0 miles, at a cost of 9.625 per 100 miles. frilled.cs.indiana.edu%java Seven Please enter the number of gallons then press enter. 2.8 Please enter the fuel efficiency (miles/gallon) then press enter. 18.5 Please enter the price per gallon, then press enter. 1.48 With the gas in the tank you can go 51.8 miles, at a cost of 8.0 per 100 miles. frilled.cs.indiana.edu%```

 8. 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's a sample run with your program: ```frilled.cs.indiana.edu%java Eight Please enter an integer >= 1000: 2000 2,000 frilled.cs.indiana.edu%java Eight Please enter an integer >= 1000: 2000000 2000,000 frilled.cs.indiana.edu%java Eight Please enter an integer >= 1000: 2000000000 2000000,000 frilled.cs.indiana.edu%java Eight Please enter an integer >= 1000: -3000 -3,000 frilled.cs.indiana.edu%java Eight Please enter an integer >= 1000: 20 Exception in thread "main" [...] ```

 9. 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. (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.) Here's a sample run with your program: ```frilled.cs.indiana.edu%java Nine Please enter an integer between 1,000 and 999,999: 123,456 123456 frilled.cs.indiana.edu%java Nine Please enter an integer between 1,000 and 999,999: 1,000 1000 frilled.cs.indiana.edu%java Nine Please enter an integer between 1,000 and 999,999: 1000000,000 1000000000 frilled.cs.indiana.edu%```

 10. (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. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Ten +--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+ frilled.cs.indiana.edu%```

 11. 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. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Eleven Please enter a number between 0 and 99999: 123 1 2 3 frilled.cs.indiana.edu%java Eleven Please enter a number between 0 and 99999: 0 0 frilled.cs.indiana.edu%java Eleven Please enter a number between 0 and 99999: 83021 8 3 0 2 1 frilled.cs.indiana.edu%java Eleven Please enter a number between 0 and 99999: 123456 2 3 4 5 6 frilled.cs.indiana.edu%```

 12 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 Twelve { 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)); } } ```

 13. 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 sample run with your program: ```frilled.cs.indiana.edu%java Thirteen 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, if you'd like. I am sorry, Dave. I cannot do that. frilled.cs.indiana.edu%java Thirteen 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? Get lost. I am sorry, Dave. I cannot do that. frilled.cs.indiana.edu%```

 14. This is somewhat tricky so there are lots of hints in the text. 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) + // sometimes not! "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 so use: ```// compute x, the difference between what you can and what you want double x = efficiency * gallons - distance; // avoid division by zero with this, can you see how? double epsilon = 0.00001; // make n 1 if x >= 0 and 0 otherwise long n = Math.round( ( (x + Math.abs(x) ) * x + epsilon) / ( 2 * x * x + epsilon) ); // why can't n be int? what do I need to do if I want it to be an int? // report the correct answer System.out.println("You will" + answer.substring(0, 5 - 4 * (int)n) + "make it."); // why do I need to convert n from the long it is to an int here? ``` Here's a sample run with your program: ```frilled.cs.indiana.edu%java Fourteen Please enter number of gallons then press enter. 10 Please enter fuel efficiency in miles per gallon. 20 Please enter distance in miles you want to cover. 199 You will make it. frilled.cs.indiana.edu%java Fourteen Please enter number of gallons then press enter. 5 Please enter fuel efficiency in miles per gallon. 20 Please enter distance in miles you want to cover. 101 You will not make it. frilled.cs.indiana.edu%```

 15. 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: 1730 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``` Here's a sample run with your program: ```frilled.cs.indiana.edu%java Fifteen Please enter the first time: 0920 Please enter the second time: 1025 1 hours 5 minutes frilled.cs.indiana.edu%java Fifteen Please enter the first time: 1025 Please enter the second time: 0920 22 hours 55 minutes frilled.cs.indiana.edu%```

 16 Run the following program, and explain the output you get. ```public class Sixteen { 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.

 17. 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``` Here's a sample run with your program: ```frilled.cs.indiana.edu%java Seventeen * * * * ***** * * * * ***** * ***** * ***** * * * * ***** * * * * ***** *** * * * * * * *** frilled.cs.indiana.edu%```

 18. 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. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Eighteen Please enter a month number from 1 to 12. 2 February frilled.cs.indiana.edu%java Eighteen Please enter a month number from 1 to 12. 12 December frilled.cs.indiana.edu%java Eighteen Please enter a month number from 1 to 12. 1 January frilled.cs.indiana.edu%java Eighteen Please enter a month number from 1 to 12. 14 Exception in thread "main" [...] frilled.cs.indiana.edu%```

 19. Change the password program (textbook, page 80) 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 concatenate the initials with the last four digits of the product. Here's a sample run with your program: ```frilled.cs.indiana.edu%java Nineteen Your password is: WJC0068 frilled.cs.indiana.edu%java Nineteen Your password is: WJC9494 frilled.cs.indiana.edu%java Nineteen Your password is: WJC2042 frilled.cs.indiana.edu%```

Last updated: Jan 14, 2002 by Adrian German for A201