CMPU 102, Spring 2017 Lab 1: Solving Problems with DrJava Lab assignment in 3 parts Part I: MODIFYING AN EXISTING PROGRAM 1. Log in to your Vassar CS account. You should see your Desktop. 2. Create a cs102 directory and then change control into that directory. If you are in a terminal, type the following: mkdir cs102 cd cs102 3. Create a new directory for lab 1 by entering: mkdir lab1 4. Open the directory you created in the last step by entering: cd lab1 5. Open the web browser from the menu called Internet at the bottom left corner of the screen. 6. Go to the course web pag and download the file AddABCDv1.java program from the course web page, Labs section, into your cs102/lab1 directory. 7. Open the DrJava integrated development environment (IDE) by choosing DrJava from the Programming menu. 8. Open the AddABCDv1.java file in DrJava. Make sure the class name inside the file matches the prefix of the filename exactly (take note that upper and lower case characters in the name). 9. Compile and then run the file using the buttons at the top of the DrJava window. The file should compile with no syntax errors initially. 10. Read the comments in the AddABCDv1.java file and make the changes marked as "TASKS". Save, compile, and run the file when you are finished making each change. Part II: Getting input from the keyboard using the javax.swing.JOptionPane class. -------------------------------------------------------------------- A fancier way to get input from the user is by using a class from another java package: javax.swing. The class and method we want to use from the javax.swing package creates a pop-up window for entering data and, without an import statement, the showInputDialog method is called as shown below: String x = javax.swing.JOptionPane.showInputDialog("Please enter x:"); The javax.swing part of the line above is the package that the JOptionPane class is written in and is called the "fully qualified name". Open the Interactions window of DrJava and copy the line above at the prompt. When you press return, a pop-up box should open, with a input area. Type something in the box and then type a value for x at the prompt. You should see the text the user entered in the pop-up box. If you import javax.swing.* in a file, you can use the shorter version to read the data as shown below: String x = JOptionPane.showInputDialog("Please enter x:"); --------------------------------------------------------------------- 1. Save AddABCDv1.java as AddABCDv2.java and change the class name in the AddABCDv2.java file to match the file name. Import the package necessary to use the JOptionPane class instead of the import for the Scanner class. 2. Use the showInputDialog method of the JOptionPane class to prompt for and store each String (you should use 4 separate calls to the showInputDialog method to obtain the inputs from the user for tempA, tempB, tempC, and tempD). Use "Enter value a: ", etc. as the argument to showInputDialog. 3. Print the sum using the showMessageDialog method of the JOptionPane class. The first argument to this method is null. 4. Try to decipher the errors on your own and then ask your lab instructor or a coach for help figuring out the syntax errors if necessary. NOTE: EVERY OPEN CLASS IN DRJAVA WILL SHOW UP ON THE LEFT SIDE MENU. PRESSING COMPILE AFFECTS EVERY ONE OF THEM. SO DON’T HAVE ANY FILES WITH SYNTAX ERRORS OPEN. Part III: Write a Java program from scratch. 1. Create a new class called StringFun in DrJava and name the file as StringFun.java 2. Inside the main method, declare 4 String variables, giving each variable a name of your choosing (make sure the names obey the conventions for variable names). 3. Use the showInputDialog method of the JOptionPane class to prompt for and store each String (you should use 4 separate calls to the showInputDialog method to obtain the strings). Try to make the prompt for input clear in the call to showInputDialog, e.g., "Enter the first string: ", "Enter the second string: ", etc. Import any packages required to use the JOptionPane class or use the fully qualified name of this class in your program. 4. Look up the 2-parameter showMessageDialog method in the Java API. This method requires 2 arguments, the first is a parent component and the second a String. In this program, the parent component should be the keyword null. To make your output a little neater, insert a " " (blank space) between each entry when it is displayed, Use the + operator to concatenate the Strings in the output correctly. For example, if I entered the strings "I", "like", "Java", and "most" for tempA, tempB, tempC, and tempD, the display in the JOptionPane.showMessageDialog method would be: "I like Java most." in a pop-up dialog box. 5. Be sure to put your name and a description of what the file does in comments at the top of this file. Run all programs for your instructor or a coach to get credit for this lab. Also, go to the 102 Moodle site and submit a folder containing all lab files at the “submit lab 1” link. BE SURE TO HAVE A COACH OR YOUR PROFESSOR CHECK YOUR NAME OFF ON THE LAB ROSTER AS HAVING COMPLETED THE LAB.