Please complete the following 4 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners). There is an additional extra credit problem, but don't try it until you have solved the first 4. After Tuesday's lecture, you should be able to complete the first two problems, and after Thursday's lecture the remaining ones.
% java ReadPositive This program reads in a positive int Enter a positive int: -3 Hey, -3 isn't positive. Try again... Enter a positive int: 0 Hey, 0 isn't positive. Try again... Enter a positive int: -1000 Hey, -1000 isn't positive. Try again... Enter a positive int: 9 You entered the positive value 9
% java SumOfEvens This program computes the sum of the even values from 0 to an upper bound you enter. Enter a positive upper bound value: -9 Hey, -9 isn't positive. Try again... Enter a positive upper bound value: 0 Hey, 0 isn't positive. Try again... Enter a positive upper bound value: 10 The sum of the even values from 0 to 10 is 30 % java SumOfEvens This program computes the sum of the even values from 0 to an upper bound you enter. Enter a positive upper bound value: 8 The sum of the even values from 0 to 8 is 20
Try using the printf method of the String class to format your output so that it looks something like this:
1 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 0 1 2 3 4 0 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 1 2 3 4 5 6 0 0 0 0 1 2 3 4 5 6 7 0 0 0 1 2 3 4 5 6 7 8 0 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 10
% java Stars This program prints a pattern of stars. Enter the number of rows you want up to 20: -3 Hey, -3 isn't in range, try again... Enter the number of rows you want up to 20: 33 Hey, 33 isn't in range, try again... Enter the number of rows you want up to 20: 8 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * % java Stars This program prints a pattern of stars. Enter the number of rows you want up to 20: 13 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * % java Stars This program prints a pattern of stars. Enter the number of rows you want up to 20: 4 * * * * * * * * * * * * * * * *If you have problems getting this one, then try to do something easier first, like print the following patterns of stars, then see if you can use these solutions to help you solve the above problem:
// try this: * * * * * * * * * * * * * * * // then try this: * * * * * * * * * * * * * * * // then try to put it all together
% appletviewer stars.html
See the code in section 2.9 (on page 103) for an example of how to write a Java applet (copy this code directly, try it out, and then modify it to solve the above problem.
To run your applet, first create a file named stars.html with the following contents:
<html> <body> <h3>Pattern of Stars Applet</h3> <applet code="StarsApplet.class" width=300 height=225> </applet> </body> </html>Then run appletviewer on this file:
% appletviewer stars.html