Download Lab 9 - Keyboard and If

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COMPSCI 111 S1C - Laboratory Nine
Computer
Science
1
COMPSCI 111 S1C - Laboratory 09
May 16th, 2003
Programming in Java: Keyboard and If Statements
Due: May 26th – May 30th (before your lab session starts)
Welcome to the second programming lab for COMPSCI 111. This lab is intended to acquaint you with
the Keyboard and if statements. 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 3, 4 and 5
and this laboratory assignment before attending your laboratory.
Expectations
When you have completed this lab, you are expected to:
 Know how to assign expressions to variables, and display them in the Java Console window.
 Know how to read in data from the keyboard.
 Know how to use control statements to make choices.
Section One: Re-write the Lab08, FishSale program using variables
Copy the Lab09 Folder From the Course Related Folder \
compsci.111.c.s1.2003 onto your G:drive
1. Select TextPad from the Start menu.
2. Open the FishSale.java source file from the File menu.
3. Change the program (one line at a time) so that it uses variables. Note that there
should be no numbers in the body of the program.
4. The changed program should give the same output as before – as shown here:
5.
6.
7.
8.
9.
Choose Compile from the Tools menu.
Now choose Run Java Applet from the Tools menu.
Debug the program until it produces the above window.
Print out the FishSale.java file (please highlight your changes).
Exit from TextPad.
Section Two: Using the Keyboard
1.
2.
3.
Open TextPad.
Open the MyDetails1.java file.
Complete, Compile, and Run the program so that it produces the following window:
COMPSCI 111 S1C - Laboratory Nine
4.
5.
2
Print the changed MyDetails1.java file. Please highlight the changes.
Exit from TextPad.
Section Three: Writing a program
Write a program that draws a figure similar to one of those shown below. Successful completion of a
program to display Figure (b) will be awarded bonus marks.
Your program must:
a.
Assign the required values to variables. Dimensions are shown in Figure (c)
b.
Draw the figure in the Applet window. You may use any colours you wish.
c.
Use the variables and comment lines given on page 5 to draw the house in the Applet window.
d.
Calculate the area of the shaded part(s)
e.
Display the value of the shaded area at (0,20).
f.
NOTE: In Figure (b) you can take Pi to equal 3.
Figure (a)
Figure (b)
Figure (c)
1.
2.
3.
4.
5.
6.
First, work carefully through the design outline of Page 5 of this assignment – below the line of
stars. Then write your statements on the page – away from the computer! Your program must
include (and agree with) the comment lines.
Open TextPad.
Next Open the HouseTemplate.java file.
Type in your program between the two lines of stars.
Compile, Run Java Applet, check and re-run the program until it looks like one of the
screenshots shown above.
Print out your completed program.
Section Four: Control Statements
A. Making choices
1.
2.
3.
Open TextPad.
Open the WorldRecord.java file.
Compile and choose Run Java Applet from the Tools menu.
COMPSCI 111 S1C - Laboratory Nine
4.
5.
6.
3
Change the program so that it tests whether a high jump record has been broken. Assign the value
245 for the record height. The changed program should give the following output when the
number 250 is entered:
Print the changed WorldRecord.java file. Please highlight the changes.
Exit Textpad.
B. Using && and ||
1.
2.
3.
4.
5.
6.
7.
Open TextPad.
Open the Dice1.java file.
Compile and choose Run Java Applet from the Tools menu.
Change and re-run the program so that it prints "You win" if, and only if, the user enters either a
first number of 4, or a second number of 5.
Re-compile and Run Java Applet from the Tools menu. Debug the program until correct.
Print the changed Dice1.java file. Please highlight the changes.
Exit Textpad.
C. Using else-if
1.
2.
3.
4.
Open TextPad.
Open the Maths101.java file
Compile and choose Run Java Applet from the Tools menu.
Change the program so that it lets the user enter a choice of 3. In this case the program must
display the cube of the number entered.
5.
6.
7.
Re-Compile, re-Run and debug your program.
Print the changed Maths101.java file. Please highlight the changes.
Exit TextPad.
D. Write a program that uses variables, expressions and an if-else
control statement
Write a program that checks whether the measurements of a piece of luggage complies with the
regulations of some hypothetical airline. The regulations state that the length plus girth of the baggage
must be less than 250 cm.
You should study the Explorer's Guide Exercise 4 program on page 107 and model your program on it.
Here are two sample runs. Your program must be able to duplicate the output exactly.
COMPSCI 111 S1C - Laboratory Nine
1.
2.
3.
4.
5.
6.
4
First, work carefully through the design outline on page 6 of this assignment – below the line of
stars. Then write your statements on the page (6) away from the computer. Your program must
include (and agree with) the comment lines.
Open TextPad.
Next, open the LuggageTest.java file.
Type in your program – between the two lines of stars.
Compile, Run, check, and re-run your program until it looks EXACTLY like the screen shots
shown above.
Print out your completed program.
Questions (Refer to Your Lecture Notes and Explorer's Guide)
1.
2.
3.
4.
5.
Why should we always output a prompt before reading from the keyboard?
In the MyDetails1 program in Section Two what happens if the user enters one or more letters
instead of just numbers for their ID? Why does this happen?
Write down an example of a String constant.
Write down an example of an integer constant.
What error message will we get if we write (choice = 1) instead of (choice == 1) in the Maths101
program?
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
Lab10.
What to hand in:
1. Printouts for Section One, Section Two, Section Three, and Section Four: Parts A,
B, C, & D. (Please highlight your changes).
2. The last page of this assignment, signed by your tutor, and with the program
statements written in pencil.
3. Your answers to questions 1 - 5 in Section Four.
Jennifer Lennon
May 2003
COMPSCI 111 S1C - Laboratory Nine
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.
Know how to use expressions containing variables.
2.
Know how to get data from the keyboard.
4.
Know how to use if-else statements
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: Draw a house in the Applet 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.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Calculate the shaded area of a house and draw the house in the Applet window
// Declare the re-sizing constant
final int UNIT = 20;
// Declare variables
int roofTop, left, area;
// Assign values to the variables
int windowWidth = UNIT;
int windowHeight = UNIT;
int houseWidth = 8 * UNIT;
int roomHeight = 2 * UNIT;
int roofHeight = 2 * UNIT;
left = 20;
roofTop = 40;
// Calculate the shaded area of the wall
// Draw the roof
// Fill the wall in the colour of your choice
// Fill the windows in white
COMPSCI 111 S1C - Laboratory Nine
// Fill the double door in white
// Draw a black outline around the house wall
// Display the area
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Section Four Part D Program: Baggage Test
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.
// * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Program to check whether a piece of luggage
// is within the limits required by a hypothetical airline
// Declare your constant here
// Variable declarations
// Prompt for, and read in, the data from the keyboard
// Calculate the total dimension (length + girth)
// Display length plus girth
// Display the O.K. or too big message
// * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6