Chapter 6 A First Look at Classes (Reading)

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

_____ variables, also known as class type variables, can be used only to reference objects.

Reference

Java allows you to create ______ objects in the same way that you create primitive variables.

String

You create primitive variables with simple declaration statements, and you create objects with the new operator. There is one class, however, that can be instantiated without the new operator: the _________class.

String

6.9 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.

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

The memory address of the object.

6.5 What does the new operator do?

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

6.1 What does an object use its fields for?

To store data

When designing a class it is often helpful to draw a UML diagram. UML stands for _____ _____ ______.

Unified Modeling Language

6.16 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.

6.20 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 method that gets a value from a class's field but does not change it is known as an _______method

accessor

When an object is passed as an argument to a method, the object's ______ is passed into the method's parameter variable. As a result, the parameter references the object.

address

When using a constructor that has parameter variables, you must provide________ for them.

arguments

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

binding

In a UML diagram, you have the option to place a _____ character before a member name to indicate that it is private, or a ____ character to indicate that it is public.

-, +

Creating an object typically requires the following two steps:

1. You declare a reference variable. 2. You create the object in memory, and assign its memory address to the reference variable

What is a problem domain?

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

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

6.26 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.

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

6.27 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.

What are a class's responsibilities?

6.28 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 two questions should you ask to determine a class's responsibilities?

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

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

6.30 No. Often responsibilities are discovered through brainstorming.

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

A String object.

When designing a class, you should take care not to store in a field calculated data that can potentially become stale. Instead, provide a method that returns the result of the _______.

calculation

A ______ is code that describes a particular type of object. It specifies the data that an object can hold (the object's fields), and the actions that an object can perform (the object's methods).

class

The Unified Modeling Language also provides notation that you may use to indicate the data types of fields, methods, and parameter variables. To indicate the data type of a field, place a ____ followed by the name of the data type after the name of the field.

colon

A _______ is a method that is automatically called when an object is created.

constructor

A ______________method has the same name as the class.

constructor

They are called "_________" because they help construct an object.

constructors

The only time that Java provides a ______constructor is when you do not write your own constructor for a class.

default

When an object is created, its constructor is always called. But what if we do not write a constructor in the object's class? If you do not write a constructor in a class, Java automatically provides one when the class is compiled. The constructor that Java provides is known as the _______ constructor.

default

Two or more methods in a class may have the same name as long as their parameter lists are _______. This also applies to constructors.

different

An _______import statement identifies the package location of a single class.

explicit

There are two types of import statements: _______ and ______.

explicit, wildcard

An object can store data. The data stored in an object are commonly called _____.

fields

Each instance of a class has its own set of fields, which are known as instance _______

fields

You can create several instances of a class and store different values in each instance's_____

fields

A semicolon never appears at the end of a method ______.

header

6.22 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.

6.15 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.

Many of the classes in the Java API are not automatically available to your program. Quite often, you have to ______ an API class in order to use it.

import

The classes in the Java API are organized into packages. An ______ statement tells the compiler which package a class is located in.

import

This statement tells the compiler that the Scanner class is located in the java.util package.

import java.util.Scanner;

Each object that is created from a class is called an _______ of the class.

instance

The Java API does have one package,__________, which is automatically imported into every Java program.

java.lang

This package contains general classes, such as String and System, that are fundamental to the Java programming language.

java.lang

You do not have to write an import statement for any class that is part of the _______ package.

java.lang

A ____ reference variable must reference an object before it can be used.

local

_____variables must be initialized or assigned a value before they can be used.

local

An object can perform operations. The operations that an object can perform are called ____.

methods

The methods that operate on an instance of a class are known as instance ______

methods

A method that stores a value in a field or changes the value of a field in some other way is known as a________ method.

mutator

Declaring a variable to reference an object does not create an object. You must use the _________ key word to create the object.

new

A constructor that does not accept arguments is known as a ________constructor.

no-arg

If you write a constructor that accepts arguments, you must also write a ______ constructor for the same class if you want to be able to create instances of the class without passing arguments to the constructor.

no-arg

An ______ is a software component that exists in memory and serves a specific purpose in a program.

object

An ______ is created from a class that contains code describing the object.

object

A ________ is simply a group of related classes.

package

All of the classes in the Java API are organized into _________.

packages

Overloaded methods must have unique _______ lists.

parameter

When a method is overloaded, it means that multiple methods in the same class have the same name, but use different types of _________.

parameters

The name of a class appears after the new key word, and a set of parentheses appears after the class name. You must write the ________ even if no arguments are passed to the constructor.

parentheses

The memory that is allocated for a _____ variable is the actual location that will hold any value that is assigned to that variable

primitive

By using the ______ access modifier, a class can hide its data from code outside the class.

private

When the_______ access specifier is applied to a class member, the member cannot be accessed by code outside the class. The member can be accessed only by methods that are members of the same class.

private

An access specifier indicates how the class may be accessed. The ______ access specifier indicates that the class will be publicly available to code that is written outside the class.

public

When the ______ access specifier is applied to a class member, the member can be accessed by code inside the class or outside.

public

the constructor's header doesn't specify a ______ type—not even void. This is because constructors are not executed by explicit method calls and cannot return a value.

return

Mutator methods are sometimes called "_______" and accessor methods are sometimes called "_______."

setters, getters

When a method's local variable has the same name as a field in the same class, the local variable's name _______ the field's name.

shadows

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

signature

Java uses a method's ________to distinguish it from other methods of the same name.

signature

method's return type is not part of the__________.

signature

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

When a method is designed to work on an instance of a class, it is referred to as an instance method, and you do not write the word _____ in the header.

static

instance methods do not have the key word ____ in their headers.

static

When you are working with a primitive variable, you are using a _____ location that holds a piece of data.

storage

Using a ____ import statement does not affect the performance or the size of your program. It merely tells the compiler that you want to make every class in a particular package available to your program.

wildcard

A _________import statement tells the compiler to import all of the classes in a package.

wildcard (ex: import java.util.*;)

When you are working with an object, you are typically using two things:

• The object itself, which must be created in memory • A reference variable that refers to the object

Where Objects Come From

Classes

6.8 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.

_________ normally perform initialization or setup operations, such as storing initial values in instance fields.

Constructors

6.14 What does the key word new do?

Creates an instance of an object in memory.

6.7 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.

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

In the Java API

_______ fields are visible to all of the class's instance methods.

Instance

6.19 What is a constructor's return type?

It has no return type, not even void.

6.18 How is a constructor named?

It has the same name as the class.

6.6 What values do reference variables hold?

It holds an object's memory address.

6.3 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.

________ variables, such as ints, doubles, and so forth, are simply storage locations in the computer's memory. A primitive data type is called "primitive" because a variable created with a primitive data type has no built-in capabilities other than storing a value.

Primitive

6.24 How many default constructors may a class have?

Only one

6.2 What are an object's methods?

Operations that the object can perform

6.21 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.


Kaugnay na mga set ng pag-aaral

UGA 19th Century Art History test 1

View Set

Introduction to Respiratory System(Exam 3)

View Set

2.09 | Radicals and Complex Numbers | Part 1

View Set

Chapter 16: The Nursing Role in Providing Comfort During Labor and Birth

View Set

Atrial fibrillation and other dysrhythmias

View Set