Você está na página 1de 6

Fall 2010

COM 1300
Lab Exercise #1
9/13/2010
To complete this lab you will need to familiarize yourself with
Sections 1.11-1.13 and 2.17-2.18 of your book. You may also
need to refer occasionally to the BlueJ tutorial I posted
earlier on Angel. If you lack Angel access, I have an extra
copy of everything on the faculty drive temporarily for this lab.
Objectives of this Lab:
-

Creating and removing objects


Calling object methods from the BlueJ menu
Evaluating Java expressions and debugging using Code Pad
Printing to the Terminal Window
Java string methods
Elementary conditional logic

Some lab steps are marked with an asterisk (*).


you need to enter something into your lab report
complete the step. These are the steps used for
be sure you have them all before turning in your

For these steps,


in order to
grading, so
work!

1. Type your name below:


Yaakov Sigal
2. Find the faculty drive area for this course. Within it, find a
BlueJ project directory called "lab-classes", copy it to
"My Documents" on your computer, and open it using BlueJ. If you
are not on the network, ask to borrow my flash drive.
3. Compile the project using the Rebuild Package command on the Tools menu.
4. Create a student object with the following constructor parameters:
fullName: "John Smith"
studentID: "A123456".
Accept the default value for the "Name of Instance" field.
*5. In a sense, this object has 2 names: the name in its "Name of Instance"
field (e.g., Student1) and the fullName you typed in. Is there
any relationship between these names?
The Name of Instance is the object name while the Fullname is a paramete
r
*6. Inspect the object you created and leave the inspection window open.
Under what field names do you find the values you typed in when you
created the object?
fullname is now private string name
student id is now pivate string id

7. Change the name of that student to "John Q Smithfield", and observe


the effect in the inspection window.

8. Create an object of class LabClass. As the signature indicates,


you need to specify the maximum number of students in that class
(an integer).
*9. Call the numberOfStudents method on that class and describe below
what it does:
the numberofstudents method tells how many students are currently enroll
ed in the labclass
10. Create three more students with the following details:
Name
ID
Credits
-------------------------------Snow White
100234
24
Lisa Simpson 122044
56
Charlie Brown 12203P
6
11. Create an object of class LabClass. You will have to specify
a maximum number of students in the class as an integer.
*12. Call the numberOfStudents method of that object. Describe
below what it does:
13. Look at the signature of the enrollStudent method. Notice that
the type of the expected parameter is Student. Call the enrollStudent
method. With the input cursor in the dialog entry field, click on one
of the Student objects -- this enters the name of the student object
into the parameter field of the of the enrollStudent method. Click OK
to complete the enrollment. Do the same for the other students.
14. Call the printlist method of the LabClass object. You will see a list
of all students in that class printed out to the BlueJ terminal window.
15. Use the inspector on your LabClass object to discover what fields it has.
*16. Set the instructor, room, and time for the LabClass object using the
methods provided. Print the LabClass object to the terminal window as
before, and check that these new details appear. Copy what was just
printed out on the terminal window and paste it below this line:
Lab class 6:13 AM
Instructor: Yaakov room: 1103
Class list:
John Q Smithfield (A123456)
Snow White (100234)
Lisa Simpson (122044)
Charlie Brown (12203P)
Number of students: 4
*17. The next few exercises require using the Code Pad feature of BlueJ.
Activate Code Pad using the Tools menu now. Position your input
cursor withing Code Pad and type 2+2 followed by <enter>. How
does Code Pad respond?
it replies 4 (int)
Code Pad can evaluate most (but not all) Java expressions as you type

them. It is an easy way to play with Java fragments without writing


a complete program.
*18. Type the following line into Code Pad:
2+3*4
Which operation did Java do first, the multiplication or the addition?
Multiplication was first
Suppose you wanted Java to perform the addition before the multiplication.
How would you change what you typed into Code Pad? Verify your hypothesis
and paste your results below this line:
4*(2+3)
*19. Type the following line into Code Pad (no semicolon at end):
student1.getName()
How does this method calling the getName method compare with using
the right-click menu on the object in the object bar?
you actually get the name and name only vs getting the name plus the stu
dent id
Repeat the preceding experiment with a semicolon at the end?
What happens?
nothing
When you added the semicolon, you turned a Java expression into
a complete Java statement. Code Pad accepts both Java expressions and
Java statements, but as you see, it treats them differently.
*20. Double-click on the Student class (not one of the Student objects) to
display the source code. Scroll down to the last method, called "print".
Notice the line:
System.out.println(name + " (" + id + ") ");
There are a couple of new (to you) Java features used in this line:
-- what does System.out.println do?
it tells the program to print a new line in the terminal window
and displays whatever is within the parentheses
-- what does "+" do in this context? What does addition have to do
with Java fields and strings?
the first + adds a parenthese after the name, while the second
adds the id after the parenthese while the third adds the closing
parenthese after the id
21. System.out is the name of a special Java object known to all other
objects. It manages printing text to the Java Terminal Window.
One of the methods of System.out has the signature
public void println(String toBePrinted);
It accepts a string parameter and prints that string by itself on a
text line in the
terminal window. So, if Java executes a line in a program that says

System.out.println("Hello World!");
then the line
Hello World!
will appear in the Java Terminal Window. Note that although string
values are enclosed in double-quotes in Java programs, the quotes do
not appear when the string is printed out. They are not part of the
string, just a way for a programmer to tell Java where a string begins
and ends.
Type or paste the complete Java statement
System.out.println("Hello World!");
into Code Pad and observe what happens in the terminal window.
*22. Addition is meaningful in Java not just for numbers but for other
data types as well. This an example of what is called operator
overloading (a good thing, except when done too much).
Type the following expression into Code Pad
"Hello"+"World"
What does Java do with the two strings?
It merges the words into one
*23. Type or paste the following complete Java statements into Code Pad:
System.out.println(3+2+"Hello");
System.out.println("Hello"+3+2);
Can you account for the difference in what they print? How is
addition treated differently in the two statements?
in the first line 2 is added to 3 and then Hello is printed
in the second line Hello is printed and then 3 and then 2 is printed
Can you come up with a variation of the second statement that would
cause the 3 and 2 to be combined as numbers and print out "Hello5"?
(note: "System.out.println("Hello"+5);" would be cheating!) Paste
your change and the resulting output from Code Pad below:
System.out.println("Hello"+(3+2));
*24. Now you should understand enough about strings to be able to program
changes in the way the Student class handles printing. Edit the
print method so that the number of credits for a student is printed
out following the name and ID, as in the following example:
John Smith (A123456) 0 credits
Paste your modified print method for Student below:
System.out.println(name + " (" + id + ") " + credits + " credits")
Rerun the printList method from your LabClass object and paste the output b
elow:

Lab class 6:13 AM


Instructor: Yaakov room: 1103
Class list:
John Q Smithfield (A123456)
Snow White (100234)
Lisa Simpson (122044)
Charlie Brown (12203P)
Number of students: 4
*25. The getLoginName of the Student class uses another method of the String cla
ss:
public String substring (int start, int end)
This function pulls out part of an existing string and returns it as a new
string. Try the following expressions in Code Pad to get the feel for it.
(To save typing, you can type just the first example, then copy-paste-edit
to do the later ones.
"Hello
"Hello
"Hello
"Hello

World!".substring(0,5)
World!".substring(1,5)
World!".substring(4,9)
World!".substring(1,25)

Based on your understanding from these examples, describe below what the
method getLoginName for class Student is supposed to do. (The point of the
last case is that "Hello World!" is much shorter than 25 characters.)
the loginname was supposd to return a quantity of different characters d
epending on the parameters
*26. Suppose someone created a Student object with name "djb" and ID "A23456".
What would getLoginName do in this case, and why? Verify your conjecture.
it would create a new student with that name and id
*27. The String class defines an accessor method with the signature:
public int length()
that returns the length of the string. Modify the constructor of Student
to print out a warning message if the fullName parameter is "too small"
based on your understanding of 26 above.
-----------------------------------------------------Note: A conditional statement in Java has the form:
if ( boolean_condition_to_be_tested) {
...
statements_to_be_executed_if_condition_is_true;
...
}
for example
if ( x > 3) {
System.out.println("Whoopee!");
}

--------------------------------------------------------Paste your modified constructor below this line:

*28. The String class has many more useful methods. Experiment with the
following using Code Pad, and paste the results of your experiment at
the end of this Assignment.
public String toLowerCase()
public String toUpperCase()
public boolean startsWith(String prefix)

Você também pode gostar