IMPORTANT. Make sure you attend the lab session to which you registered, show the result(s) of each exercise to your TA, and submit the .java files that you wrote through OWL by 11:55pm on the same day as your lab session to get your marks for the lab.
Preparation. Read this introduction to Javadoc.
SocialNetworking
project that you created in Lab 1.CompareObjects
.
main
method in StringComparison.txt and answer the first question in
AnswersLab2.txt BEFORE running program CompareObjects
and answer question 2 in AnswersLab2.txt.
new
operator. Hence a String object in java can be created as follows:String s = new String("hello")
String
can be found in Oracle's
documentation. Since strings are very common, Java provides a shorter way of creating a string:String s = "hello";
new
operator is called a string literal. String literals are objects, but the Java compiler will not create two copies of the same string literal. This optimization technique is called interning and allows the Java compiler to reduce the amount of memory that a Java program uses. String s = "hello";
String t = "hello";
s
and in t
.
String
provides a method called equals
that compares two strings character by character. Read the description of the class equals
in Oracle's documentation page and answer question 4 in Answers.txt. You MUST submit your completed Answers.txt file through OWL along with your other .java files.
A program might consist of several Java classes each one invoking
methods and accessing data provided by other classes. Once you have written a Java class, you should test all methods in it to make sure they work as they are supposed before adding that class to the
program and allowing other classes to interact with it.
Since the execution of a Java program always starts at a method
called main
, to test in isolation the methods of a class we can temporarily
add to that class a main
method that acts as a "test harness".
A test harness is a main
method where you add calls to methods that you
wish to test. For convention this main
method is the last method of the
class.
SocialNetworking
project that you created in Lab 1.Person
class, in the file
PersonTestHarness.txt. It creates a Person
object
(this tests the constructor) and prints the result of invoking most of the methods of the class. Add this main method to your Person.java
.Person.java
; it will execute the test harness (main
method).equals
method: it should print the message "Same persons" if two objects of
the class Person have the same first name and last name, or it should print
print "Different persons" if they do not have the same first name or last name.
(Hint: you can use
friend1 and friend2 to compare two objects of class Person that are not the
same, but for thorough testing, you should also create a third Person object
who has the same name as one of the first two.) Person.java
again.
ReverseString
, make sure it includes a main
method.
nextInt()
of class Scanner
and how to read a character using next().charAt(0)
. To read a string use method next()
from class Scanner
. Do not forget to include java.util.Scanner
in your class.
private static String reverse(String s)
that receives a String s
as parameter and it reverses it storing it in another string r
, so the first character in s
is the last character in r
, the second character in s
is the
second last character in r
, and so on. For example, if s = "hello"
then r = "olleh"
.reverse
at the end returns this string r
.
reverse
from the main
method passing as parameter the string that was typed by the used and print the reversed string. So when you run your program the output will be like this: Enter string:
hello
reversed string: olleh
reverse
on your own. If you need help, read the following
hints.
Person.java
and SocialNetwork.java
are Javadoc comments. (You should have read the Introduction to Javadoc document before this Lab.)
ReverseString
to explain purpose and author of the class,
purpose, parameter, and return value of method reverse
and purpose of method main
.
Use Javadoc to generate HTML documentation for this class; since class ReverseString
includes a private method, to generate documentation for it select Private and Use Standard Doclet in the "Javadoc Generation" window.