CMPS 161, Lab 2 Input and Expressions Task 0 – Prepare the Lab Work Environment 1. Use SmartCVS to check out your working environment to a folder named cvsroot in the root folder of your N: drive or in the root folder of your USB flash drive. If you have never done this before, refer to the first 5 sections of the SmartCVS tutorial. 2. Minimize the SmartCVS windows and use My Computer to navigate from the cvsroot folder to your w-number folder. Create a folder named lab2 in your w-number folder and open it. Use this folder to store programs from the following tasks. Task 1 – IDE Console Output 1. Enter the following program, compile it, and run it. Remember that you can download Template.java from the files area of the class website as a starter. Save it as Lab2P1.java. import acm.program.*; import acm.graphics.*; import acm.io.*; class Lab2P1 extends Program { void main() { int headSize; double middleMultiple; double bottomMultiple; char eye; char nose; String name; println("What is the diameter of the snowman's head? "); println("How much larger is the snowman's middle than his head? "); println("How much larger is the snowman's bottom than is middle? "); println("What character should be used for eyes? "); println("What character should be used for the nose? "); println("What is the snowman's name? "); } public static void main(String[] args) { new Lab2P1().start(); } public void run() { main(); } } 2. Open SmartCVS and refresh so that you can see Lab2P1.java. Add it to CVS control, and then commit it with the comment “Initial entry from lab handout”. 3. Proofread the program carefully and make any corrections. Make sure that it will compile and run without error. 4. Open SmartCVS and refresh so that you can see Lab2P1.class. Add it to CVS control, and then commit both Lab2P1.java and Lab2P1.class with the comment “Compiles without error”. CMPS 161 Lab 2 Page 1 Task 2 – IDE Console Input 1. Use Save As to make a new copy of Lab2P1.java called Lab2P2.java. Use Lab2P2.java for the remainder of this task. 2. Change the class name and the start() name from Lab2P1 to Lab2P2. 3. Change each of the println commands in Lab2P2.java to the read statement variant that is most appropriate, and assign the result to the appropriate variable. For example, the first line would become: headSize = readInt("What is the diameter of the snowman's head? "); Remember the syntax descriptions from class: • intIdentifier = readInt([promptString]); • byteIdentifier = readInt([promptString]); • shortIdentifier = readInt([promptString]); • doubleIdentifier = readDouble([promptString]); • floatIdentifier = (float)readDouble([promptString]); • booleanIdentifier = readBoolean([promptString]); • stringIdentifier = readLine([promptString]); • charIdentifier = readLine([promptString]).charAt(0); 4. Open SmartCVS and refresh so that you can see Lab2P2.java. Add it to CVS control, and then commit it with the comment “Initial version for IDE console input”. 5. Compile the program and correct any errors. Run the program when it compiles without error and observe its behavior. 6. Open SmartCVSand refresh so that you can see Lab2P2.class. Add it to CVS control, and then commit both Lab2P2.java and Lab2P2.class with the comment “Compiles without error”. 7. Run the program again and try entering: a. 25.5 for the head size. What happens? b. The word “double” for the middle multiple. What happens? c. Several characters, such as “(*)” for the eye character. What happens? Task 3 – Console Input 1. Use Save As to make a new copy of Lab2P2.java called Lab2P3.java. Use Lab2P3.java for the remainder of this task. 2. Change the class name and the start() name from Lab2P2 to Lab2P3. 3. Change the program so that it extends ConsoleProgram instead of Program. 4. Compile the program and correct any errors. Run the program when it compiles without error and observe its behavior. Be sure to try it with the tricky values from step 6 of Task 2. 5. Open SmartCVSand refresh so that you can see Lab2P3.java. Add it to CVS control, and then commit it with the comment “Error-free console version”. Task 4 – Dialog Input 1. Use Save As to make a new copy of Lab2P3.java called Lab2P4.java. Use Lab2P4.java for the remainder of this task. 2. Change the class name and the start() name from Lab2P3 to Lab2P4. 3. Change the program so that it extends DialogProgram instead of ConsoleProgram. 4. Compile the program and correct any errors. Run the program when it compiles without error and observe its behavior. Be sure to try it with the tricky values from step 6 of Task 2. 5. Open SmartCVSand refresh so that you can see Lab2P4.java. Add it to CVS control, and then commit it with the comment “Error-free dialog version”. CMPS 161 Lab 2 Page 2 Task 5 – Graphics and Mathematical Expressions with Input 1. Use Save As to make a new copy of Lab2P4.java called Lab2P5.java. Use Lab2P5.java for the remainder of this task. 2. Change the class name and the start() name from Lab2P4 to Lab2P5. 3. Change the program so that it extends GraphicsProgram instead of ConsoleProgram. 4. Add the following declarations to the declarations at the beginning of the main method (you can use copy and paste using Acrobat Reader’s select tool on the online version of this lab if you wish to minimize typing errors): int centerX; int headX, headY; int middleX, middleY, middleSize; int bottomX, bottomY, bottomSize; int leftEyeX, leftEyeY, rightEyeX, rightEyeY, noseX, noseY; int nameSize, nameX, nameY; 5. Add the following lines to the end of the main method (you can use copy and paste using Acrobat Reader’s select tool on the online version of this lab if you wish to minimize typing errors): middleSize = (int)(headSize * middleMultiple); bottomSize = (int)(middleSize * bottomMultiple); centerX = bottomSize / 2; headX = centerX - headSize / 2; headY = 0; middleX = centerX - middleSize / 2; middleY = headSize; bottomX = 0; bottomY = headSize + middleSize; leftEyeX = headX + (int)(headSize * 0.35); leftEyeY = headY + (int)(headSize * 0.35); rightEyeX = headX + (int)(headSize * 0.60); rightEyeY = leftEyeY; noseX = centerX; noseY = headSize / 2 + 10; nameSize = (int)(new GLabel(name)).getWidth(); nameX = centerX - nameSize / 2; nameY = bottomY + bottomSize / 2; add(new GOval(headX, headY, headSize, headSize)); add(new GOval(middleX, middleY, middleSize, middleSize)); add(new GOval(bottomX, bottomY, bottomSize, bottomSize)); add(new GLabel(String.valueOf(eye), leftEyeX, leftEyeY)); add(new GLabel(String.valueOf(eye), rightEyeX, rightEyeY)); add(new GLabel(String.valueOf(nose), noseX, noseY)); add(new GLabel(name, nameX, nameY)); 6. Compile the program and correct any errors. Run the program when it compiles without error and observe its behavior. Be sure to try it with the tricky values from step 6 of Task 2. 7. Open SmartCVS and refresh so that you can see Lab2P5.java. Add it to CVS control, and then commit it with the comment “Error-free graphic version”. CMPS 161 Lab 2 Page 3