Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
-
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:
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!");
}
*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)