CS49J Lesson 17 – the Object superclass

Ch 9: 9.5

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Concept - The Object class

Concept - toString

 

Activity - toString

Record your participation in Piazza clicker question Lesson17 Q1

Download overriding_project.zip. Modify the Product class to implement toString. You can use the example from the previous slide as a model. Run the tester. Verify your output looks like the Expected.

  1. Got it
  2. Got it, but it was hard
  3. Could not do it in the time allowed
  4. Did not try

Concept - toString in a subclass

In the substring method of a subclass

Activity - toString in a subclass

Record your participation in Piazza clicker question Lesson17 Q2

Now modify Painting to implement toString. Call super.toString() and then add the artist instance variable. You can use the example from the previous slide as a model. Uncomment the next two statements and run the tester again. Verify your output looks like the Expected.

  1. Got it
  2. Got it, but it was hard
  3. Could not do it in the time allowed
  4. Did not try

Activity - Printing toString

Record your participation in Piazza clicker question Lesson17 Q3

What happens when you execute this statement? Try it and see.

System.out.println(System.out);

Why?

Activity - Reference of type Object

Record your participation in Piazza clicker question Lesson17 Q4

What will happen when you try to compile and run this code

Object college = "SJSU";
System.out.println(college.length());
  1. It compiles and runs and prints 4
  2. It gets a syntax error in the first statement
  3. It gets a systax error in the second statement
  4. It compiles but throws an exception at run time

 

Concept - The equals method

Concept - Overriding equals

Concept - Overriding equals: the problem

If we are providing an equals method for a Student class with a name and gpa,

Activity - An equals method for Product

Record your participation in Piazza clicker question Lesson17 Q5

  1. Got it
  2. Got it, but it was hard
  3. Could not do it in the time allowed
  4. Did not try

Activity - Problems with this equals

Record your participation in Piazza clicker question Lesson17 Q6

 

Activity - Solution to the problems

Record your participation in Piazza clicker question Lesson17 Q7

Concept - Implementing equals in a subclass

Activity - Implementing equals in a subclass

Record your participation in Piazza clicker question Lesson17 Q8