Spring Semester 2003

Lab Notes Four: Classes and objects.

Let's start by mentioning a student's observations, one past semester. It refers to how we calculate intersections of rectangles with the Java API. These are very perceptive observations. She said:

• The `intersection` method in class `Rectangle` does not return a valid (that is, meaningful) answer when the `Rectangle`s don't overlap. (This is what the book says too, but one needs to read even the text of the problem with care!)

• In other words, you can call `intersection` and you will always obtain an answer. You can't tell by just looking at this answer if the rectangles overlap or not. So what do we do when they do not overlap?

• The solution is to
1. check (see method `intersects`) if they overlap
2. before you calculate the `intersection`
3. (which you should do only if they overlap!).

Hope you found these comments useful.

With this lab we again encourage you to work out problems from the book and check your answers against ours. The problems are indexed below, as well as in the main (class notes) page.

1. Here's a set of warm-up questions.
`(http://www.cs.indiana.edu/classes/a201/sum2002/notes/w2.html`

2. Here are the answers to the warm-up questions.
`(http://www.cs.indiana.edu/classes/a201/sum2002/notes/w2Sol.html`

3. Here's a set of programming problems, to practice.
`(http://www.cs.indiana.edu/classes/a201/sum2002/notes/set2.html`

4. Here's a broad (conceptual) overview of the kind of modeling we are talking about.
`(http://www.cs.indiana.edu/classes/a201/sum2002/notes/rodS.html`

5. Here are the solutions to the programming problems.
`(http://www.cs.indiana.edu/classes/a201/sum2002/notes/set2Sol.html`

Let's go through the complete and annotated development of a solution to a problem.

(This is problem P2.11 from the book, page 76).

1. Let's implement a class `Student`.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {

}
frilled.cs.indiana.edu%javac Student.java
frilled.cs.indiana.edu%ls -ld Student*
-rw-------   1 dgerman       188 Feb  1 08:18 Student.class
-rw-------   1 dgerman        27 Feb  1 08:17 Student.java
frilled.cs.indiana.edu%```
2. Looks like we're done. Can we test it?

3. We need a tester class with a `main` method.

```frilled.cs.indiana.edu%pico StudentTest.java
frilled.cs.indiana.edu%cat StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student a = new Student();
}
}
frilled.cs.indiana.edu%javac StudentTest.java
frilled.cs.indiana.edu%ls -ld Student*
-rw-------   1 dgerman       188 Feb  1 08:18 Student.class
-rw-------   1 dgerman        27 Feb  1 08:17 Student.java
-rw-------   1 dgerman       297 Feb  1 08:21 StudentTest.class
-rw-------   1 dgerman       110 Feb  1 08:21 StudentTest.java
frilled.cs.indiana.edu%```
4. Can we test it?

5. We can run `StudentTest` but we get no output.

```frilled.cs.indiana.edu%java StudentTest
frilled.cs.indiana.edu%```
6. Does it matter?

7. Do we know what happens inside?

8. The `Student` class is empty. `Student` objects are amorphous.

9. I see... Let's make it such that each `Student` has (at least) a name, then.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;

}
frilled.cs.indiana.edu%```
10. What's the meaning of `private`?

11. It means that to know the name of a `Student` you need to ask the `Student` what its name is.

12. I don't feel very comfortable using he or she for a `Student` object.

13. Fine. How do you inquire about a `Student`'s name?

14. We need to add this functionality to class `Student` first, then make use of it.

15. Here's a more comprehensive blueprint of `Student` objects.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;

}
frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;

public String whatsYourName () {
return name;
}

}
frilled.cs.indiana.edu%```
16. What does it mean for the `whatsYourName` method to be `public`?

17. It means you can ask a `Student` "What's your name?"

18. What if we make it `private`?

19. Then we can never ask.

20. How do we create a `Student`?

21. Just invoke `new` the way we did in the tester's `main`.

22. And if we invoke it, how do things get created, and initialized.

23. Well, a default no-arg constructor is present, but we don't see it.

24. I think we should add it, so that we not forget that it's there.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;

public String whatsYourName () {
return name;
}

Student() {

}
}
frilled.cs.indiana.edu%```
25. It's empty, but it gets called at creation time.

26. Can we create a `Student` with an initial name?

27. Only if we provide that type of constructor.

28. To be able to create a `Student` with an initial name we need to

• be able to initialize the `name` of any `Student`

(at creation time)

• with an actual name (to be specified when we create the `Student`).

29. We're looking for something like this:
`new Student("Larry Johnson")`
30. Let's provide class `Student` with that capability.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;
public String whatsYourName () {
return name;
}
Student() { }
Student(String givenName) {
name = givenName;
}
}
frilled.cs.indiana.edu%```
31. Let's enhance our tester's `main` to exploit the new features.

```frilled.cs.indiana.edu%pico StudentTest.java
frilled.cs.indiana.edu%cat StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student a = new Student("Larry");
Student b = new Student("Michael");
System.out.print("Printing the name of the first student: ");
System.out.print("Printing the name of the second student: ");
}
}
frilled.cs.indiana.edu%javac StudentTest.java
frilled.cs.indiana.edu%java StudentTest
Printing the name of the first student: Larry
Printing the name of the second student: Michael
frilled.cs.indiana.edu%```
32. Great! What else were we supposed to do?

33. Let's enable the `Student`s to keep track of their scores.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;
public String whatsYourName () {
return name;
}
Student() { }
Student(String givenName) {
name = givenName;
}
totalScore = totalScore + newScore;
}
private int totalScore;
}
frilled.cs.indiana.edu%```
34. I see... If there's a new score to be added to the total score for a student then we just add it to the `totalScore` as if it were an `amount` to be placed as `deposit` over a current, given, existing `balance`.

35. Yes, so you need to define an instance variable `totalScore` (which will keep the cumulative score for the `Student`) and use it as if it were a `balance`.

36. This way a `Student` is like a `BankAccount` with a `name`.

37. Let's write `getBalance`, then.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;
public String whatsYourName () {
return name;
}
Student() { }
Student(String givenName) {
name = givenName;
}
totalScore = totalScore + newScore;
}
private int totalScore;
int whatsYourTotalScore() {
}
}
frilled.cs.indiana.edu%```
38. Let's test it.

```frilled.cs.indiana.edu%pico StudentTest.java
frilled.cs.indiana.edu%cat StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student a = new Student("Larry");
Student b = new Student("Michael");
System.out.print("Printing the name of the first student: ");
System.out.print("Printing the name of the second student: ");

System.out.println("Student " + a.whatsYourName() + "reports: ");
System.out.println("   cumulative score: " + a.whatsYourTotalScore());

}
}
frilled.cs.indiana.edu%javac StudentTest.java
frilled.cs.indiana.edu%java StudentTest
Printing the name of the first student: Larry
Printing the name of the second student: Michael
Student Larryreports:
cumulative score: 290
frilled.cs.indiana.edu%```
39. I think you need a space between `Larry` and `reports`.

40. I'll let you fix that. But overall we've come a long way, don't you think?

41. I sure do think so. What if I want the `Student`s to be able to report the average score in addition to the cumulative score? I don't think this is possible at the moment, because they don't remember how many quizzes they have taken.

42. Indeed, they only keep the cumulative score.

43. To remember how many quizzes they have taken they would need to keep a counter, to be updated (incremented by 1) every time a new score is added to the `totalScore`.

44. If we kept the number updated we could easily report the average at any time, as follows.

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;
public String whatsYourName () {
return name;
}
Student() { }
Student(String givenName) {
name = givenName;
}
totalScore = totalScore + newScore;
}
private int totalScore;
int whatsYourTotalScore() {
}
private int numberOfScores;
double reportAverage() {
return (double)totalScore / numberOfScores;
}
}
frilled.cs.indiana.edu%```
45. I think you forgot to update the counter in `addQuizScore`, haven't you?

46. Ooops!...

```frilled.cs.indiana.edu%pico Student.java
frilled.cs.indiana.edu%cat Student.java
public class Student {
private String name;
public String whatsYourName () {
return name;
}
Student() { }
Student(String givenName) {
name = givenName;
}
totalScore = totalScore + newScore;
numberOfScores = numberOfScores + 1;
}
private int totalScore;
int whatsYourTotalScore() {
}
private int numberOfScores;
double reportAverage() {
return (double)totalScore / numberOfScores;
}
}
frilled.cs.indiana.edu%```
47. There you go.

48. Can you test that?

```frilled.cs.indiana.edu%pico StudentTest.java
frilled.cs.indiana.edu%cat StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student a = new Student("Larry");
Student b = new Student("Michael");
System.out.print("Printing the name of the first student: ");
System.out.print("Printing the name of the second student: ");

System.out.println("Student " + a.whatsYourName() + "reports: ");
System.out.println("   cumulative score: " + a.whatsYourTotalScore());
System.out.println("   average score:    " + a.reportAverage());

}
} ```
50. Nice. You only changed one line!

51. Indeed. And here's the actual test:

```frilled.cs.indiana.edu%javac StudentTest.java
frilled.cs.indiana.edu%java StudentTest
Printing the name of the first student: Larry
Printing the name of the second student: Michael
Student Larryreports:
cumulative score: 290
average score:    96.66666666666667
frilled.cs.indiana.edu%```
52. Good `Student`!

53. Yes. Isn't it time for a break?

54. I sure think so.

55. See you next week!

Until then, here's a brief summary of chapter 3:

Last updated: Feb 4, 2003 by Adrian German for A201