CSC 15 Lab 2 Due one week from date assigned The week's assignment is in two parts. part I: Following the example of the minutes and seconds program shown in class (also on webpage), you are to write a similar program that does the following: Take as input an integer value representing an amount of money in cents. For example, if the user enters 72, it's meant to represent 72 cents. You are to compute the optimal change one should make using quarters, dimes, nickels and pennies. The optimal change is made with the fewest coins possible. For example, you don't use 1 dime and 62 pennies for 72 cents, but 2 quarters, 2 dimes, 0 nickels, and 2 pennies. SKETCH OUT the algorithm you will use on paper before coding! You'll need to turn in this draft. You'll need to define at least four variables for the number of coins of each type you might need. You may also need additional variables to hold intermediate values. You MUST describe in /* COMMENTS */ what the purpose of each variable is. The basic algorithm is: compute the number of quarters needed. determine the amount of money left over after subtracting the quarters compute the number of dimes needed. determine the amount of money left over after substracting the dimes and so on ... You program's transcript should look something like this: Enter amount of money in cents: 82 The optimal change is: 3 quarters, 0 dimes, 1 nickel and 2 cents. *** IO options *** Input can be accoumplished using code such as the following: First make sure that you're in the same director where the cs1 subdirectory resides, and make sure that import cs1.Keyboard; is the first line of your program file. ... int cents; System.out.print("enter value: "); cents = Keyboard.readInt(); ... Note that Keyboard.readInt() is a method invocation that returns a value, which is then assigned to the variable cents. Another, fancier way to do IO, is to use a little dialog box. First include import javax.swing.*; at the top of your file. ... int cents; String instring; input = JOptionPane.showInputDialog(null,"enter value:"); cents = Integer.parseInt(instring); ... Here, the "showInputDialog" method of "JOptionPane" dispays a prompt, and after you type in a value and click ok, it will return the value. However, the format of the value it returns is not an integer, but a string. We need to explicitly convert this string to an integer using the Integer.parseInt method. Thus we need the intermediate String variable instring in addition to the integer variable to be assigned the input value. To output, you can call the statement: JOptionPane.showMessageDialog(null, ... ); The ... can be anything you can use with the System.out.println command. Note that unlike the input dialog method, this method call does not return any value, and is thus a stand-alone statement. These are important concepts. Please try to absorb them. Don't just finish the assignment. LEARN from the assignment. ------------------------- part II. The above program is a good start but it's not really an example of "object-oriented" programming. There is only one class and one method. The second part of the assignment will reinforce some of the ideas concerning objects that I've described using the bankaccount and footballteam examples. First, download the files "figure.class" and "figs.java" from the class homepage. Make sure these files are in the same directory. The .class file is a pre-compiled program, while the .java file is one that you will edit. Compile (javac figs.java) and run (java figs) the figs program. You should see a blue stick figure centered on a white background in a window measuring 800 by 600 pixels. The figure is the graphical representation of an object of class "figure". You are given the definition of the figure class representing stick figure objects. These objects have arms, legs, head, nose, eyes, etc ... The class has the following interface: (the interface of objects is the set of methods you can invoke on them) public void beHappy() : puts a smile on the figure's face public void beSad() : changes expression to a frown public void armsUp() : raise arms public void armsDown() public void armsStraight() public void grow() : increases size of figure public void shrink() Note that none of the above functions return values, and none requires any additional parameters. public void move(int dx, int dy) : moves figure's position by vector (dx, dy). A positive value for dx will move the figure to the right. A negative dx will move it to the left. A positive dy will move the figure downwards, and a negative one will move it upwards. Units are measured in screen pixels. Use values that are multiples of 4, such as 4, -4, -16, etc ... public void rotate(int rangle) : rotates stick figure by rangle degrees counterclockwise. Degrees can be between 0 and 360. To rotate clockwise, subtract degrees from 360. public void setColor(Color newcolor) : changes the color of the figure The color values can be Color.black, Color.green, Color.red, etc ... For example, fig1.setColor(Color.green); will change the fig1 object's color from the default blue to green. All of the above methods are preceeded by "void" - that is, they do not return values, and therefore can be called as standalone statements. The only methods that do return values are the following: public int xpos() : returns the current x coordinate of the figure public int ypos() : returns the current y coordinate of the figure. --- Open the figs.java file, and find towards the end of the file the skeleton of the the "animate" function that you will be completing. A figure object (fig1) has already been created. This assignment simply asks you to understand how to use the interface of the object as described above. You are to: Call EACH of the above methods on the object. For the move method, you should call it multiple times, with different values of dx, dy as parameters. Call it at least eight times. See if you can get the figure to move across the screen. Call the shrink figure multiple times. What will happen if the figure becomes too small? Finally, call the xpos and ypos functions to find the position of the figure at the end of your program. Since these functions return values, you can't just put a ";" after them and be done with it. You should print them using the System.out.println function. That is You should print something like: The final position of the figure is x:200, y:250 Refer to the footballteam example (on webpage) as a guide if you are confused about the syntactic conventions. Try to see how the same concepts are manifested in the two programs. You are encouraged to be critical. What aspects of the stick figure can be made more interesting? Include your /* comments */ at the end of the file figs.java.