Villanova University CSC 1051 www.csc.villanova.edu/~map/1051 Dr. Papalaskari (instructor or TA initials) Lab 1 Name:________________________ Checked: ______ Objectives: • Learn about jGrasp - the programming environment that we will be using (IDE) • Compile and run a Java program • Understand the relationship between a Java class names and file names • Practice using basic Java output statements and adding documentation (comments) to your source code • Experience Java errors • Learn the basics of sequential execution, variables, and the assignment statement Instructions: • Complete and submit the preparation before coming to the lab. • You will be assigned a partner for this lab. Each of you will complete the lab separately., but feel free to help each other learn! • If both are stuck or don't understand something, get help from instructor or TA • At various points, you are asked to compare your work with your classmate’s and sign each other’s worksheet. o Be sure to check your classmate’s work before signing. o Who is right? Who is wrong? It may be you are both right, but ask for help if you are not sure. • When finished, demo your programs and get your worksheet checked and initialed by the instructor or TA. Preparation: Follow the instructions to install Java JDK and jGrasp on your laptop. 1. Create a folder on your computer for this course. You are responsible for organizing your files for each lab into separate subfolders. For example, use a folder Lab01 for this lab’s files. 2. Download and save Lincoln.java. Open the file using jGrasp. 3. In jGrasp, compile Lincoln.java by clicking big green plus button: o Note that you now have a file Lincoln.class in the same folder o It is NOT required (or desirable) to open that file, but you may, if you are curious. It is not meant to be read by humans! It contains the bytecode that you will run, as instructed in the next step. 4. In jGrasp, run Lincoln.class by clicking on the red runner button: o This causes your program to execute its instructions o See the output of this run in the interactions (lower pane) in jGrasp 5. When finished, submit the files Lincoln.java and Lincoln.class through blackboard under the assignment “Lab 1 Prep” Villanova University CSC 1051 www.csc.villanova.edu/~map/1051 Dr. Papalaskari Part A: Running and modifying code 1. Open, compile and run the other two examples from chapter 1: Lincoln2.java and Lincoln3.java These work fine but are hard to read. Practice fixing up the code and note that you can use the “Generate CSD” and “Remove CSD” buttons to make the job easier. Show your work to a classmate. Classmate signature: ________________________________________ 2. Open Lincoln.java again. Edit it by renaming the class to MyLincoln. o Run the program. What happens? o Save this modified program as MyLincoln.java and run MyLincoln. o Add or modify the comments at the top of the program to include: § Your name (you are now the Author) § A note about the original author ("based on a program by Lewis & Loftus") § Today's date § A very short summary of the modifications you made o Re-compile and run to make sure it still works (there should be no difference in its functionality) Classmate signature: ________________________________________ Part B: Debugging 3. Experiment with MyLincoln.java and introduce some errors (“bugs”). Try some of the following to see what happens when you re-compile and (if successful) run the program. If you get an error right away, mark it as “syntax,” if it compiles but you get an error when you try to run it, mark it as “runtime,” and if it compiles and runs but produces a wrong (or different result than intended, which we take to be the original), mark it as “logic”. • Change the first println to print Error: ______________ • Change the second println to bogus Error: ______________ • Remove the semicolon at the end of one of the statements Error: ______________ • Remove the last brace of the program Error: ______________ • Change main to man Error: ______________ • Something else you tried? _____________________________________________ Error: ______________ Classmate signature (compare notes): ________________________________________ Villanova University CSC 1051 www.csc.villanova.edu/~map/1051 Dr. Papalaskari Part C: Creating a program from scratch 4. Using MyLincoln as a model, create a new program named MyQuote and make it do the following: • Print out a new quote of your choosing • Add variables to the program such as: int x = 42; int count = 100; double pi = 3.14; String name = "Kripke"; (we will be studying variables next week; for the moment we are just experimenting!) • Add more output statements to use your new variables, such as: System.out.println ("Howdy " + name); System.out.println ("The answer is " + x); System.out.print ("Counting... up: " + (count + 1)); System.out.println (" ... and\n ... down: " + (count - 1)); System.out.println("The perimeter of a circle with radius " + x + " is " + (2* pi * x)); • Be sure to pay attention to correct structure, syntax, indentation, and comments! Feel free to be creative! Compile and run your program to make sure it works and that it prints out the values of your variables where you expect them. Try changing it around to use different values or different variable names (we are using the names x, count, and name here). Be sure to stay away from the reserved words (see fig 1.18, p32). We will be learning a lot more about variables, but for the moment you can just experiment with whatever comes to mind. Don’t worry about making mistakes – that’s the best way to learn. Classmate signature: ________________________________________ REMINDERS: 1) Review worksheet. • All questions answered and in agreement with partner • Signatures from your partner where required 2) Demo your programs and get initials from instructor or TA on this worksheet 2) Detach and hand in the comments sheet. Villanova University CSC 1051 www.csc.villanova.edu/~map/1051 Dr. Papalaskari Villanova University CSC 1051 www.csc.villanova.edu/~map/1051 Dr. Papalaskari Lab 1 Comments Name:_____________________ Checked: ______ What was the most valuable thing you learned in this lab? What did you like best about this lab? Was there any particular problem? Do you have any suggestions for improving this lab as an effective learning experience?