Chapter 6 A First Look at Classes Java

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

When a variable is said to reference an object, what is actually stored in the variable?

The memory address of the object.

A string literal, such as "Joe", causes what type of object to be created?

A String object.`

What is the difference between a class and an instance of a class?

A class is a collection of programming statements that specify the attributes and methods that a particular type of object may have. You should think of a class as a "blueprint" that describes an object. An instance of a class is an actual object that exists in memory.

What are a class's responsibilities?

A class's responsibilities are the things that the class is responsible for knowing and the actions that the class is responsible for doing.

What is a method's signature?

A method's signature consists of the method's name and the data types of the method's parameters, in the order that they appear.

A class's responsibilities are __________.

Actions the class performs, and things the class knows

What is an accessor? What is a mutator?

An accessor is a method that gets a value from a class's field but does not change it. A mutator is a method that stores a value in a field or in some other way changes the value of a field.

What is an accessor method? What is a mutator method?

An accessor method is a method that gets a value from a class's field but does not change it. A mutator method is a method that stores a value in a field or in some other way changes the value of a field.

A class is analogous to a(n) __________.

Blueprint

When the same name is used for two or more methods in the same class, how does Java tell them apart?

By their signatures, which include the method name and the data types of the method parameters, in the order that they appear.

This is a collection of programming statements that specify the fields and methods that a particular type of object may have.

Class

b. Write the Java code for the Pet class.

Class code: public class Pet { private String name; // The pet's name private String animal; // The type of animal private int age; // The pet's age /** setName method @param n The pet's name. */ public void setName(String n) { name = n; } /** setAnimal method @param a The type of animal. */ public void setAnimal(String a) { animal = a; } /** setAge method @param a The pet's age. */ public void setAge(int a) { age = a; } /** getName method @return The pet's name. */ public String getName() { return name; } /** getAnimal method @return The type of animal. */ public String getAnimal() { return animal; } /** getAge method @return The pet's age. */ public int getAge() { return age; } }

You hear someone make the following comment: "A blueprint is a design for a house. A carpenter can use the blueprint to build the house. If the carpenter wishes, he or she can build several identical houses from the same blueprint." Think of this as a metaphor for classes and objects. Does the blueprint represent a class, or does it represent an object?

Classes are the blueprints.

What does the key word new do?

Creates an instance of an object in memory.

True or False: When you write a constructor for a class, it still has the default constructor that Java automatically provides.

False

How do you identify the potential classes in a problem domain description?

Identify all the nouns (including pronouns and noun phrases) in the problem domain description. Each of these is a potential class. Then, refine the list to include only the classes that are relevant to the problem.

Under what circumstances does Java automatically provide a default constructor for a class?

If you do not write a constructor for a class, Java automatically provides one.

How is the relationship between an object and a reference variable similar to a kite and a spool of string?

In order to fly a kite, you need a spool of string attached to it. When the kite is airborne, you use the spool of string to hold on to the kite and control it. This is similar to the relationship between an object and the variable that references the object. Think of the object as the kite, and the reference variable as the spool of string.

You have programs that create Scanner, Random, and PrintWriter objects. Where are the Scanner, Random, and PrintWriter classes?

In the Java API

What is the purpose of the new key word?

It creates an object (an instance of a class) in memory.

What is a constructor's return type?

It has no return type, not even void.

How is a constructor named?

It has the same name as the class.

What values do reference variables hold?

It holds an object's memory address.

What two questions should you ask to determine a class's responsibilities?

It is often helpful to ask the questions "In the context of this problem, what must the class know? What must the class do?"

Assume a program named MailList.java is stored in the DataBase folder on your hard drive. The program creates objects of the Customer and Account classes. Describe the steps that the compiler goes through in locating and compiling the Customer and Account classes.

It looks in the current folder or directory for the file Customer.class. If that file does not exist, the compiler searches for the file Customer.java and compiles it. This creates the file Customer.class, which makes the Customer class available. The same procedure is followed when the compiler searches for the Account class.

How is a class like a blueprint?

It serves a purpose similar to that of the blueprint for a house. The blueprint itself is not a house, but rather a detailed description of a house. When we use the blueprint to build an actual house, we could say that we are building an instance of the house described by the blueprint. A class is not an object, but a description of an object. When a program is running, it can use the class to create, in memory, as many objects of a specific type as needed. Each object that is created from a class is called an instance of the class.

If a class has a private field, what has access to the field?

Methods that are members of the same class

What do you call a constructor that accepts no arguments?

No argument constructor

Will all of a class's actions always be directly mentioned in the problem domain description?

No. Often responsibilities are discovered through brainstorming.

How many default constructors may a class have?

Only one.

What are an object's methods?

Operations that the object can perform

Is it required that overloaded methods have different return values, different parameter lists, or both?

Overloaded methods must have different parameter lists. Their return types do not matter.

When designing an object-oriented application, who should write a description of the problem domain?

Someone who has an adequate understanding of the problem. If you adequately understand the nature of the problem you are trying to solve, you can write a description of the problem domain yourself. If you do not thoroughly understand the nature of the problem, you should have an expert write the description for you.

When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become?

Stale

Design a class named Pet, which should have the following fields: • name. The name field holds the name of a pet. • animal. The animal field holds the type of animal that a pet is. Example values are "Dog", "Cat", and "Bird". • age. The age field holds the pet's age.

The Pet class should also have the following methods: • setName. The setName method stores a value in the name field. • setAnimal. The setAnimal method stores a value in the animal field. • setAge. The setAge method stores a value in the age field. • getName. The getName method returns the value of the name field. • getAnimal. The getAnimal method returns the value of the animal field. • getAge. The getAge method returns the value of the age field.

Find the error in the following class: public class MyClass { private int x; private double y; public void MyClass(int a, double b) { x = a; y = b; } }

The constructor cannot have a return type, not even void.

In this chapter we used the metaphor of a kite attached to a spool of string to describe the relationship between an object and a reference variable. In this metaphor, does the kite represent an object, or a reference variable?

The kite represents an object.

What does the new operator do?

The new operator creates an object in memory, and returns that object's memory address.

The following statement attempts to create a Rectangle object. Find the error. Rectangle box = new Rectangle;

The parentheses are missing. The statement should read: Rectangle box = new Rectangle();

What is a problem domain?

The problem domain is the set of real-world objects, parties, and major events related to a problem.

Two or more methods in a class may have the same name, as long as this is different.

Their parameter lists

What does an object use its fields for?

To store data

True or False: Each instance of a class has its own set of instance fields.

True

True or False: The new operator creates an instance of a class.

True

Is it a good idea to make fields private? Why or why not?

When an object's fields are hidden from outside code, the fields are protected from accidental corruption. It is good idea to make all of a class's fields private and to provide access to those fields through methods

What is a stale data item?

When the value of an item is dependent on other data and that item is not updated when the other data is changed, it is said that the item has become stale.

A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses?

Yes , Classes are the blueprints of the objects that are created from Classes

Why are constructors useful for performing "start-up" operations?

a constructor is an method called when an object of a class is created they provide the initial values in instance fields

Look at the UML diagram in Figure 6-17 and answer the following questions: a) What is the name of the class? b) What are the fields? c) What are the methods? d) What are the private members? e) What are the public members?

a) Car b) make and yearModel c) setMake, setYearModel, getMake, and getYearModel d) make and yearModel e) setMake, setYearModel, getMake, and getYearModel

Assume that the following is a constructor, which appears in a class: ClassAct(int number) { item = number; } a) What is the name of the class that this constructor appears in? b) Write a statement that creates an object from the class and passes the value 25 as an argument to the constructor.

a) ClassAct b) ClassAct myact = new ClassAct(25);

a. Draw a UML diagram of the class. Be sure to include notation showing each field and method's access specification and data type. Also include notation showing any method parameters and their data types.

a. UML diagram: Pet - name : String - animal : String - age : int + setName(n : String) : void + setAnimal(a : String) : void + setAge(a : int) : void + getName() : String + getAnimal() : String + getAge() : int

a. Write a no-arg constructor for this class. It should assign the sideLength field the value 0.0.

a. public Square() { sideLength = 0.0; }

b. Write an overloaded constructor for this class. It should accept an argument that is copied into the sideLength field.

b. public Square(double s) { sideLength = s; }

How does method overloading improve the usefulness of a class?

because sometimes you need several different ways to perform the same operation

The process of matching a method call with the correct method is known as __________.

binding

This is a method that is automatically called when an instance of a class is created.

constructor

This is automatically provided for a class if you do not write one yourself.

default constructor

shadowing is considered good practice (T/F)

false

An object is a(n) __________.

instance of a class

Assume that limo is a variable that references an instance of the class shown in Figure 6-17. Write a statement that calls setMake and passes the argument "Cadillac".

limo.setMake("Cadillac");

This is a method that stores a value in a field or in some other way changes the value of a field.

mutator

This key word causes an object to be created in memory.

new

Assume that r1 and r2 are variables that reference Rectangle objects, and the following statements are executed: r1.setLength(5.0); r2.setLength(10.0); r1.setWidth(20.0); r2.setWidth(15.0); Fill in the boxes in Figure 6-21 that represent each object's length and width fields.

r1 address A Rectangle object width: 20 length: 5 r2 address A Rectangle object width: 15 length: 10

This is a method that gets a value from a class's field, but does not change it.

Accessor

True or False: A class may not have more than one constructor.

False

True or False: To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.

False

This is a class member that holds data.

Field

When a local variable has the same name as a field, the local variable's name does this to the field's name.

Shadows

Find the error in the following class: public class FindTheError { public int square(int number) { return number * number; } public double square(int number) { return number * number; } }

The square methods must have different parameter lists. Both accept an int.

Look at the following class: public class CheckPoint { public void message(int x) { System.out.print("This is the first version "); System.out.println("of the method."); } public void message(String x) { System.out.print("This is the second version "); System.out.println("of the method."); } } What will the following code display? CheckPoint cp = new CheckPoint(); cp.message("1"); cp.message(1)

This is the second version of the method. This is the first version of the method.


Kaugnay na mga set ng pag-aaral

Financial Leverage and Capital Structure Policy

View Set

Genetics Ch 15 Gene Regulation in Eukaryotes via Translation and Transcription

View Set

Quiz #3: CH. 2 Cognitive Neuroscience

View Set

Managerial Cost Accounting - ch. 11

View Set

Final Exam (Chapters 7-12, 17, 19)

View Set

26.; The child with GI dysfunction

View Set

World History - The Cold War Begins

View Set