Search Header Logo
#201 Unit 2 Lesson 12 Notes

#201 Unit 2 Lesson 12 Notes

Assessment

Presentation

Computers

11th Grade

Practice Problem

Easy

Created by

Myra Deister

Used 3+ times

FREE Resource

34 Slides • 8 Questions

1

media
media

Computer Science A

Unit 2 - Lesson 12
Printing Objects

2

media

Warm Up

3

media
media
media
media

CSA

Printing Objects - Warm Up

A software engineer is developing a travel
app. The user must be able to choose a
hotel and a flight for their trip.

What classes should the software
engineer develop? What attributes and
behaviors might each class have?

💡

Discuss:

📝

Unit 2 Guide

4

Open Ended

A software engineer is developing a travel app. The user must be able to choose a hotel and a flight for their trip.

What classes should the software engineer develop?

What attributes and behaviors might each class have?

5

media

Activity

6

media
media

CSA

🎯

Lesson Objectives

By the end of this lesson, you will be able to . . .

Write a toString() method to display information about the
state of an object to the console

Use escape sequences to format output

Printing Objects - Activity

7

media
media

CSA

🚀

Question of the Day

How can I easily display information

about an object?

Printing Objects - Activity

8

media
media
media
media
media
media

CSA

Do This:

Predict
and Run

Predict

1. Predict the output of

the program

There are no wrong

answers!

Printing Objects - Activity

10

media
media

CSA

Printing Objects - Activity

When printing an object, the toString() method

of the Object class is called.

PizzaRunner.java

1 public class PizzaRunner {

2 public static void main(String[] args) {

3 Pizza cheesePizza = new Pizza("cheese");

4 System.out.print(cheesePizza);

5 }

6 }

Console

Pizza@3c717ef2

11

media
media

CSA

Printing Objects - Activity

The toString() method in the Object class returns a String
that consists of the name of the class, the @ symbol, and the

hash code of the object.

A hash code is the

value associated with
each object in Java.

PizzaRunner.java

1 public class PizzaRunner {

2 public static void main(String[] args) {

3 Pizza cheesePizza = new Pizza("cheese");

4 System.out.print(cheesePizza);

5 }

6 }

Console

Pizza@3c717ef2

12

media
media

Discuss:
HOLDthat
THOUGHT

CSA

Why do you think the toString()
method in the Object class doesn't
print the values stored in the
instance variables?

Printing Objects - Activity

📝

Unit 2 Guide

13

Open Ended

Why do you think the toString() method in the Object class doesn't print the values stored in the instance variables?

14

media
media

💡

Discuss:
HOLDthat
THOUGHT

CSA

What did you notice about the way
the toString() method is called?

Printing Objects - Activity

📝

Unit 2 Guide

15

Open Ended

What did you notice about the way the toString() method is called?

16

media
media

CSA

Printing Objects - Activity

The toString() method is automatically called when an object

reference is passed to a print statement or when an object reference

is concatenated with a String.

PizzaRunner.java

1 public class PizzaRunner {

2 public static void main(String[] args) {

3 Pizza cheesePizza = new Pizza("cheese");

4 System.out.print(cheesePizza);

5 }

6 }

Console

Pizza@3c717ef2

17

media
media
media

CSA

Printing Objects - Activity

To override is to define a method in a subclass with the

same method signature as a method inherited from a

superclass.

Since the Object class is the
superclass of all classes, we can
override the toString() method
in our classes to provide more
useful information about the
state of our objects.

📝

Unit 2 Guide

18

media
media

CSA

Printing Objects - Activity

Overriding the toString() Method

public String toString() {
return topping + " pizza";
}

The method signature is the same as the toString() method in the
Object class. It has the same return type, name, and no parameters.

📝

Unit 2 Guide

19

media
media

CSA

Printing Objects - Activity

Overriding the toString() Method

public String toString() {
return topping + " pizza";
}

In the body of the toString() method, we return a String that

contains information about the object.

📝

Unit 2 Guide

20

media
media
media
media
media

💡

Discuss:

Retrieve
your knowledge

and ideas and write

it down silently

Pair

up with a neighbor
and talk about your

reflections

Share

your thoughts in a
class discussion

CSA

How could we include the
information in the superclass
toString() method in a
subclass toString() method?

Printing Objects - Activity

📝

Unit 2 Guide

21

Open Ended

How could we include the information in the superclass toString() method in a subclass toString() method?

22

media
media

CSA

Printing Objects - Activity

We can use the super keyword to call the superclass

version of a method in a subclass.

public String toString() {

return super.toString() + " with " + stuffing + " stuffed crust";

}

public String toString() {

return topping + " pizza";

}

StuffedPizza.java

Pizza.java

23

media
media

CSA

Printing Objects - Activity

We can use escape sequences to format the output.

An escape sequence starts with a \ to indicate how

to display a String.

Escape Sequence

Description

\n new line

\" double quote

\\ backslash

📝

Unit 2 Guide

24

media

CSA

Printing Objects - Activity

We use the escape
sequence \" when we want
to print a quotation ( " ).

System.out.print("\"Best cookie shop!\" - Anonymous");

What is output by
this line of code?

media

25

Open Ended

What is output by this code?

System.out.print("\"Best cookie shop!\" - Anonymous");

26

media

27

media
media

CSA

Printing Objects - Activity

We use the escape
sequence \n when we want
to print a new line.

System.out.println("C\nA\nK\nE");

What is output by
this line of code?

28

Open Ended

What is output by this code?

System.out.println("C\nA\nK\nE");

29

media
media

CSA

Printing Objects - Activity

We use the escape
sequence \n when we want
to print a new line.

System.out.println("C\nA\nK\nE");

What is output by
this line of code?

> C
A
K
E
> _

30

media
media

CSA

Printing Objects - Activity

We use the escape
sequence \\ when we want
to print a backslash.

System.out.println("Laugh \\S'more\\ worry less.");

What is output by
this line of code?

31

Open Ended

What is output by this code?

System.out.println("Laugh \\S'more\\ worry less.");

32

media
media

CSA

Printing Objects - Activity

We use the escape
sequence \\ when we want
to print a backslash.

System.out.println("Laugh \\S'more\\ worry less.");

What is output by
this line of code?

Laugh \S'more\ worry less.
> _

33

media

Wrap Up

34

media
media

🎉

Today, you learned about . . .

CSA

Writing a toString() method to display information

about the state of an object to the console

Using escape sequences to format output

Printing Objects - Wrap Up

35

media
media

🚀

Question of the Day

CSA

How can I easily display information

about an object?

Printing Objects - Wrap Up

36

media
media

CSA

override: to define a method in a subclass with

the same method signature as a method
inherited from a superclass

escape sequence: starts with a \ to indicate how

to display a String

💼

Key Vocabulary

Printing Objects - Wrap Up

37

media
media
media

CSA

Do This:

Investigate
and Modify
📝

Navigate to Lesson 12, Level 1

1. Predict and Run on Level 1

2. Investigate the code on Levels

2 and 3

3. Make changes as prompted

and observe the results

Printing Objects - Activity

38

media
media
media
media
media
media
media

CSA

Do This:

🎉

Practice
Navigate to Lesson 12, Level 4

1. Level 4 - Check for Understanding

2. Level 5 - Complete a choice level to

write a toString() method

3. Level 6 - Write a toString() method

in the Dessert class

4. Level 7 - Write a toString() method

in the Dessert subclass

A

B

C

D

Printing Objects - Activity

39

media
media
media
media
media

CSA

Printing Objects - Wrap Up

Show What You Know Week

Store

Management

Project

Burger Class

FRQ

Multiple-Choice

Assessment

40

media
media
media

CSA

Printing Objects - Wrap Up

Store Management Project

Use your knowledge of object-oriented
programming and class structure and design to
create your store management system:

Create a class hierarchy

Declare instance variables

Write constructors

Implement accessor and mutator methods

Implement a toString() method

41

media
media
media
media
media

CSA

Printing Objects - Activity

Commit Your Code!

Commit your code and add a

comment about what the

work you completed.

Commit Code

Backpack

Save your Dessert classes

to the Backpack.

42

media
media
media

CSA

Unit 2 Study Guide

The Unit 2 Study Guide covers
objectives, programming concepts,
and syntax to help you prepare to . . .

Show What You Know!

Printing Objects - Wrap Up

media
media

Computer Science A

Unit 2 - Lesson 12
Printing Objects

Show answer

Auto Play

Slide 1 / 42

SLIDE