• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/40

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

40 Cards in this Set

  • Front
  • Back

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


a. class


b. method


c. parameter


d. instance

a

A class is analogous to a(n) __________.


a. house


b. blueprint


c. drafting table


d. architect

b

An object is a(n) __________. a. blueprint b. primitive data type c. variable d. instance of a class

d

This is a class member that holds data. a. method b. instance c. field d. constructor

c

This key word causes an object to be created in memory. a. create b. new c. object d. construct

b

This is a method that gets a value from a class’s field, but does not change it. a. accessor b. constructor c. void d. mutator

a

This is a method that stores a value in a field or in some other way changes the value of a field. a. accessor b. constructor c. void d. mutator

d

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? a. bitter b. stale c. asynchronous d. moldy

b

This is a method that is automatically called when an instance of a class is created. a. accessor b. constructor c. void d. mutator

b

When a local variable has the same name as a field, the local variable’s name does this to the field’s name. a. shadows b. complements c. deletes d. merges with

a

This is automatically provided for a class if you do not write one yourself. a. accessor method b. default instance c. default constructor d. variable declaration

c

Two or more methods in a class may have the same name, as long as this is different. a. their return values b. their access specifier c. their parameter lists d. their memory address

c

The process of matching a method call with the correct method is known as__________. a. matching b. binding c. linking d. connecting

b

A class’s responsibilities are __________. a. the objects created from the class b. things the class knows c. actions the class performs d. both b and c

d

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

True

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

True

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

False

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, youidentify all of the verbs in a description of the problem domain.

False

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

Assume that the following method is a member of a class. Find the error.public void total(int value1, value2, value3){ return value1 + value2 + value3;}

The method should have int as its return type

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

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

Find the error in the following class:public class TwoValues{ private int x, y; public TwoValues() { x = 0; } public TwoValues() { x = 0; y = 0; }}

The constructors must have different parameterlists. Both are empty.

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. Bothaccept an int.

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.a. Draw a UML diagram of the class. Be sure to include notation showing each fieldand method’s access specification and data type. Also include notation showing anymethod parameters and their data types.b. Write the Java code for the Pet class.

A. UML diagram


B. Class code



Look at the following partial class definition, and then respond to the questions thatfollow it:public class Book{ private String title; private String author; private String publisher; private int copiesSold;}a. Write a constructor for this class. The constructor should accept an argument foreach of the fields.b. Write accessor and mutator methods for each field.c. Draw a UML diagram for the class, including the methods you have written.

a. Constructor:

b. Accessor and mutator methods

c. UML diagram:

Consider the following class declaration:public class Square{ private double sideLength; public double getArea() { return sideLength * sideLength; } public double getSideLength() { return sideLength; }}a. Write a no-arg constructor for this class. It should assign the sideLength field thevalue 0.0.b. Write an overloaded constructor for this class. It should accept an argument that iscopied into the sideLength field.

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

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

Look at the following description of a problem domain: The bank offers the following types of accounts to its customers: savings accounts,checking accounts, and money market accounts. Customers are allowed to depositmoney into an account (thereby increasing its balance), withdraw money from anaccount (thereby decreasing its balance), and earn interest on the account. Eachaccount has an interest rate. Assume that you are writing an application that will calculate the amount of interestearned for a bank account.a. Identify the potential classes in this problem domain.b. Refine the list to include only the necessary class or classes for this problem.c. Identify the responsibilities of the class or classes.

a) After eliminating duplicates, objects,and primitive values, the potential classes are: bank, account, and customerb) Theonly class needed for this particular problem is account.c) Theaccount class knows its balance and interest rate. Theaccount can calculate interest earned.


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

A class is a collection ofprogramming statements that specify the attributes and methods that aparticular 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 actualobject that exists in memory.

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

Classes areanalogous to the blueprint.

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

An accessormethod is a method that gets a value from a class’s field but does not changeit. A mutator method is a method that stores a value in a field or in someother way changes the value of a field.

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

When an object’s fields are hidden from outsidecode, the fields are protected from accidental corruption. It is good idea tomake all of a class’s fields private and to provide access to those fields throughmethods.

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

Methods that aremembers of the same class.


What is the purpose of the new key word?

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

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

It looks in thecurrent 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. Thiscreates the file Customer.class,which makes the Customer classavailable. The same procedure is followed when the compiler searches for the Account class.

Why are constructors useful for performing “start-up” operations?

Because theyexecute when an object is created.


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

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

What do you call a constructor that accepts no arguments?

A no-arg constructor

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

By theirsignatures, which includes the method name and the data types of the methodparameters, in the order that they appear.

How does method overloading improve the usefulness of a class?

Several differentversions of the same method can be created, each performing an operation in adifferent manner. This makes the class more flexible.