Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COMPSCI 111 S1C - Laboratory Eight Computer Science 1 COMPSCI 111 S1C - Laboratory 08 May 9th, 2003 Programming in Java: The Applet and Java Console Windows Due: May 19th – May 23rd (before your lab session starts) Welcome to your first programming lab for COMPSCI 111. This lab is intended to acquaint you with Java on the PC. If you feel confident about the material covered, you should continue to work through the lab at your own pace. You should read the Explorer's Guide Chapters 1 and 2 and this laboratory assignment before attending your laboratory. Expectations When you have completed this lab, you are expected to: Know how to run and modify a Java program. Know how to draw in the Applet window Know how to display Strings, integers, and expressions in the Java Console window. Section One: Drawing in the Applet window Copy the Lab08 Folder From the Course Related Folder \ compsci.111.c.s1.2003 onto your G:drive A. Running a Java program 1. Open the Lab08 folder, (G:drive) then the FirstHello folder. Notice that it has a FirstHello.html file and a FirstHello.java file in it. 2. Select TextPad from the Start menu. 3. Open the FirstHello.java source file from the File menu. 4. Now choose Run Java Applet from the Tools menu. After a short wait the Applet window will appear with Hello Computer in it: COMPSCI 111 S1C - Laboratory Eight B. Editing a Java program 1. Change the FirstHello program so that it displays at least three lines that include your own name, address, and the papers you are taking. 2. Draw a simple pattern that includes at least one vertical line, one horizontal line, one sloping line, one rectangle, one oval, and one circle. 3. Compile the changed program by selecting Compile Java from the Tools menu. 4. Choose Run Java Applet to run the changed program. 5. Check that the output is exactly what you expected. If it isn't, correct the program, re-compile, and re-run. 6. Print the changed FirstHello.java file. Please highlight your changes with a highlighter pen. 7. Quit TextPad by clicking the close box. Section Two: Displaying Text in the Java Console Window A. Displaying Integers and Strings in the Java Console Window 1. 2. 3. 4. Select TextPad from the Start Menu Open the BlastOff.java source file from the File menu. Run the program by choosing Run Java Applet from the Tools menu. Change, Compile, and Run Java Applet so that it produces the following window: 1. Print the changed BlastOff.java file. Please highlight your changes. 2. Exit TextPad. B. Using expressions 1. 2. 3. 4. Select TextPad from the Start Menu Open the Expressions1.java source file from the File menu. Run the program. Change the program so that it displays the result of dividing 4 by 5. What is the result? Why is this so? 5. Print the changed Expressions1.java file. Please highlight your changes. 6. Exit TextPad. C. Using Strings and Expressions 1. 2. 3. 4. Select TextPad from the Start Menu Open the Expressions2.java source file from the File menu. Run the program. Now remove the opening and closing brackets around the expression so that the line looks like: System.out.println("200 + 300 = " + 200 + 300); 2 COMPSCI 111 S1C - Laboratory Eight 3 5. Compile and Re-run the program. What did the program do? Why did the program do this? 6. Put the brackets in again and check the program works properly. 7. Exit TextPad. Section Three: Complete a program segment using expressions & the Java Console window Complete a program segment using only System.out.println with strings and expressions that will produce output as shown in the Java Console window below. You must not use variables and you must only use the integers: 2, 3, 4, 5, 6, 10, and 100. No others! The GST is calculated by multiplying the total by 3 and dividing by 100. 1. First write your statements – in pencil, away from the computer - in the space provided on page 5 of this assignment. Note: The page must be handed in to be marked. 2. Next open TextPad, then the FishSale.java file 3. Copy your statements from your handwritten page, one at a time, into the spaces following each existing comment line. 4. Compile, run and check your program with each added line until it looks EXACTLY the same as the screen shot shown above. 5. Print out your completed program. Section Four: Using Variables A. Displaying the value of a variable in the Java console window 1. 2. 3. 4. Select TextPad from the Start Menu Open the MyVariables.java source file from the File menu. Run the program. Change the program so that it displays your name and all the courses you are taking this year. Please use meaningful variable names. 5. Compile and Run and then check your output. 6. Print out the changed MyVariables file. Please highlight your changes. 7. Exit TextPad. COMPSCI 111 S1C - Laboratory Eight 4 Section Five: Drawing a Landscape Draw a landscape in the Applet window using as many graphics routines as you can. Add a title that includes your name and Student ID number. Write your program on paper, away from a computer. Enter you program, one or two lines at a time, into the Landscape.java file which is in the Landscape folder. Bonus marks will be given to exceptionally good landscapes and appropriate use of variables. Print out your completed program. Print a screenshot of the Applet window containing the landscape. Exit TextPad. Questions (Refer to Your Lecture Notes and Explorer's Guide) 1. 2. 3. 4. 5. Give one good reason for using an Applet window. Give one good reason for using the Java Console window. What is the rule that Java uses when we divide one integer by another? Why should we bracket an expression if it follows a string? Give one good reason why we use variables in programs. Handing in your lab work You must answer all the questions posed in this lab. Your answers should be typed and printed out. Attach all printouts requested. You should use a cover sheet and hand your completed lab into the appropriate hand-in box before the beginning of Lab09. What to hand in: 1. Printouts for Section One Part B and Section Two Parts A and B (Please highlight your changes). 2. The last page of this assignment, signed by your tutor, and with the program statements written in pencil. 3. Printout of your Section Three program 4. Printout of your Section Four program 5. Printout of your Section Five program 6. Screenshot of your Section Five landscape 7. Your answers to questions 1 - 5 in Section Five 8. Your signed attendance sheet Jennifer Lennon May 2003 COMPSCI 111 S1C - Laboratory Eight 5 PLEASE COMPLETE BOTH SIDES OF THIS PAGE AND HAND IT IN WITH YOUR OTHER WORK TO BE MARKED. Ten percent will be allocated for lab attendance and homework. Sections One and Two Check List You are expected to know how to complete the following tasks. Please tick the box when the task is complete. 1. Ran a Java program 2. Modified a Javar program 3. Found out what happened when we omit brackets in the Expressions2 program. 4. Know how to mix strings and integers Your name (please print)_____________________________________________ Lab session: _______________________________________________________ I certify that the work I submit for this lab is my own (sign here) :_________________ Teaching Assistant: __________________ Date__________________ Section Three Program: Complete a program using expressions & the Java Console window You must fill in the spaces in pencil before entering the program into a computer. In the lab you may be asked to show this work. // * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Fish Sale: 2003 System.out.println(); // Display the heading line // Display the three detail lines System.out.println(); // Display the Total line // Display the GST // * * * * * * * * * * * * * * * * * * * * * * * * * * * *