Deck 4: Writing Classes

Full screen (f)
exit full mode
Question
Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

A) m1
B) m2
C) m3
D) m5
E) main
Use Space or
up arrow
down arrow
to flip the card.
Question
Instance data for a Java class

A) are limited to primitive types (e.g., int, float, char)
B) are limited to Strings
C) are limited to objects(e.g., Strings, classes defined by other programmers)
D) may be primitive types or objects, but objects must be defined to be private
E) may be primitive types or objects
Question
Consider a Rational class designed to represent rational numbers as a pair of int's, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int's), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private.

A) Because they will never be used
B) Because they will only be called from methods inside of Rational
C) Because they will only be called from the constructor of Rational
D) Because they do not use any of Rational's instance data
E) Because it is a typo and they should be declared as public
Question
To define a class that will represent a car, which of the following definitions is most appropriate?

A) private class car
B) public class car
C) public class Car
D) public class CAR
E) private class Car
Question
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as: public String getClassRank( )
Given that s1 is a student, which of the following would properly be used to get s1's class rank?

A) s1 = getClassRank( );
B) s1.toString( );
C) s1.getHours( );
D) s1.getClassRank( );
E) getClassRank(s1);
Question
A class' constructor usually defines

A) how an object is initialized
B) how an object is interfaced
C) the number of instance data in the class
D) the number of methods in the class
E) if the instance data are accessible outside of the object directly
Question
If a method does not have a return statement, then

A) it will produce a syntax error when compiled
B) it must be a void method
C) it can not be called from outside the class that defined the method
D) it must be defined to be a public method
E) it must be an int, double, float or String method
Question
Which of the following Applet methods is called automatically when the applet is first loaded?

A) Applet (the Applet's constructor)
B) init
C) start
D) getCodeBase
E) getImage
Question
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in typical form (like 3.810)?

A) "#.###"
B) "#.0"
C) "0.# "
D) "0.000"
E) "0.0##"
Question
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

A) encapsulation
B) information hiding
C) tokenizing
D) importing
E) method overloading
Question
StringTokenizer is a class in the java.util library that can divide a String based on some delimiter String (a delimiter is a separator). If the instruction StringTokener st = new StringTokenizer(str, "&&"); is executed, where str is some String, then st divides up str into separate Strings whenever

A) a blank space is found
B) two blank spaces are found
C) a single ampersand ("&") is found
D) two ampersands are found
E) two ampersands or the substring "and" or "AND" is found
Question
A variable whose scope is restricted to the method where it was declared is known as a(n)

A) parameter
B) global variable
C) local variable
D) public instance data
E) private instance data
Question
Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this?

A) public int updateHours( ) { return hours; }
B) public void updateHours( ) { hours++; }
C) public updateHours(int moreHours) { hours += moreHours; }
D) public void updateHours(int moreHours) { hours += moreHours; }
E) public int updateHours(int moreHours) { return hours + moreHours; }
Question
Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

A) foo(0, 0.1);
B) foo(0 / 1, 2 * 3);
C) foo(0);
D) foo( );
E) foo(1 + 2, 3 * 0.1);
Question
The behavior of an object is defined by the object's

A) instance data
B) constructor
C) visibility modifiers
D) methods
E) all of the above
Question
The relationship between a class and an object is best described as

A) classes are instances of objects
B) objects are instances of classes
C) objects and classes are the same thing
D) classes are programs while objects are variables
E) objects are the instance data of classes
Question
Which of the following reserved words in Java is used to create an instance of a class?

A) class
B) public
C) public or private, either could be used
D) import
E) new
Question
An example of passing message to a String where the message has a String parameter would occur in which of the following messages?

A) length
B) substring
C) equals
D) toUpperCase
E) none of the above, it is not possible to pass a String as a parameter in a message to a String
Question
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Which of the following could be used to instantiate a new Student s1?

A) Student s1 = new Student( );
B) s1 = new Student( );
C) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
D) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
E) new Student(s1);
Question
In order to preserve encapsulation of an object, we would do all of the following except for which one?

A) Make the instance data private
B) Define the methods in the class to access and manipulate the instance data
C) Make the methods of the class public
D) Make the class final
E) All of the above preserve encapsulation
Question
A GUI container is an object that defines a screen element.
Question
A constructor may contain a return statement so long as no value (or expression) is returned.
Question
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
Question
The interface of a class is based on those data instances and methods that are declared public.
Question
Every class definition must include a constructor.
Question
Defining formal parameters requires including each parameters type.
Question
What happens if you declare a class constructor to have a void return type?

A) You'll likely receive a syntax error
B) The program will compile with a warning, but you'll get a runtime error
C) There's nothing wrong with declaring a constructor to be void
D) The class' default constructor will be used instead of the one you're declaring
E) None of the above
Question
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { ... }
Question
The following method header definition will result in a syntax error: public void aMethod( );
Question
The expressions that are passed to a method in an invocation are called

A) actual parameters
B) formal parameters
C) formal arguments
D) formals
E) any of the above
Question
All Java classes must contain a main method which is the first method executed when the Java class is called upon.
Question
In a UML diagram for a class

A) classes are represented as rectangles
B) there may be a section containing the name of the class
C) there may be a section containing the attributes (data) of the class
D) there may be a section containing the methods of the class
E) all of the above
Question
Java methods can return only primitive types (int, double, float, char, boolean, etc).
Question
The software failure at the Denver International Airport's baggage handling system is a good example of

A) how a large system can be obsolete by the time it is developed
B) how designers sometimes have too much faith in the technology they are using
C) how failures are often a result of multiple variables caused by a system as a whole in actual operation
D) how the use of a centralized computer is really unrealistic in today's distributed processing
E) all of the above
Question
Listener objects "wait" for an event to occur.
Question
A method defined in a class can access the class' instance data without needing to pass them as parameters or declare them as local variables.
Question
Visibility modifiers include

A) public, private
B) public, private, protected
C) public, private, protected, final
D) public, protected, final, static
E) public, private, protected, static
Question
What are the objects needed in order to create a Java GUI (graphical user interface)?

A) components
B) events
C) listeners
D) all of the above
E) only classes from AWT and/or Swing are needed
Question
Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?

A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) doublefoo(0.1, 0.2);
E) all of the above are legal except for D
Question
While multiple objects of the same class can exist, in a given program there can be only one version of each class.
Question
Write the statement to instantiate an Box object, blueBox, with a length of 6, width of 4, and height of 2.
Question
An Employee will have a name, social security number, position, and hourly wages. Define the instance data for this class.
Question
Write the constructor, which is passed the player's name and position.
Question
Write a statement to output the volume of the blue box.
Question
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
Question
Write a statement to instantiate a cube with a length of 3, width of 3, and height of 3.
Question
Write the constructor for this class.
Question
When reasoning about a car, we use such information as its make and model, year, color, cost (or worth), number of miles, gas mileage, insurance cost, and so forth. Consider a program that will attempt to determine how practical a car might be on a road trip. Which of these types of information might you use in your program as instance data?
Provide a justification for your selection.
Question
Assume method0 calls method1 and method2, method1 calls method3 which calls method4 and method5. Explain the chain of method calls (what order methods are called and from which method).
Question
Consider the "Push Me!" program described in section 4.7. As given the push counter starts at zero and is incremented each time the "Push Me!" button is pressed. Let's say that you want to add a class called ResetListener that resets the count to zero. Write the ResetListener class.
Question
Explain why it would be a poor decision to make instance data public instead of private. Give an example of why this could result in a problem.
Question
Write a toString method that returns the player's name, position and batting average formatted to have 1 digit to the left of the decimal and 3 digits to the right of the decimal. Assume DecimalFormat has been imported.
Question
What visibility modifiers would you use for the methods that move the elevator up one floor, move the elevator down one floor, that request the elevator, and that start the elevator moving?
Question
Add a constructor to class Box for a cube, which has only 1 parameter, side.
Question
An object should be encapsulated in order to guard its data and methods from inappropriate access.
Question
Regarding the software failure described at the Denver International Airport, it is critical to factor in errors and inefficiencies that always occur in a complex system.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/56
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 4: Writing Classes
1
Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

A) m1
B) m2
C) m3
D) m5
E) main
B
Explanation: B) Once a method terminates, control resumes with the method that called that method. In this case, m2 calls m4, so that when m4 terminates, m2 is resumed.
2
Instance data for a Java class

A) are limited to primitive types (e.g., int, float, char)
B) are limited to Strings
C) are limited to objects(e.g., Strings, classes defined by other programmers)
D) may be primitive types or objects, but objects must be defined to be private
E) may be primitive types or objects
E
Explanation: E) The instance data are the entities that make up the class and may be any type available whether primitive or object, and may be public or private. By using objects as instance data, it permits the class to be built upon other classes. This relationship where a class has instance data that are other classes is known as a has-a relationship.
3
Consider a Rational class designed to represent rational numbers as a pair of int's, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int's), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private.

A) Because they will never be used
B) Because they will only be called from methods inside of Rational
C) Because they will only be called from the constructor of Rational
D) Because they do not use any of Rational's instance data
E) Because it is a typo and they should be declared as public
B
Explanation: B) All items of a class that are declared to be private are only accessible to entities within that class, whether they are instance data or methods. In this case, since these two methods are only called from other methods (including the constructor) of Rational, they are declared private to promote information hiding to a greater degree. Note that answer C is not a correct answer because the reduce method calls the gcd method, so one of the methods is called from a method other than the constructor.
4
To define a class that will represent a car, which of the following definitions is most appropriate?

A) private class car
B) public class car
C) public class Car
D) public class CAR
E) private class Car
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
5
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as: public String getClassRank( )
Given that s1 is a student, which of the following would properly be used to get s1's class rank?

A) s1 = getClassRank( );
B) s1.toString( );
C) s1.getHours( );
D) s1.getClassRank( );
E) getClassRank(s1);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
6
A class' constructor usually defines

A) how an object is initialized
B) how an object is interfaced
C) the number of instance data in the class
D) the number of methods in the class
E) if the instance data are accessible outside of the object directly
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
7
If a method does not have a return statement, then

A) it will produce a syntax error when compiled
B) it must be a void method
C) it can not be called from outside the class that defined the method
D) it must be defined to be a public method
E) it must be an int, double, float or String method
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following Applet methods is called automatically when the applet is first loaded?

A) Applet (the Applet's constructor)
B) init
C) start
D) getCodeBase
E) getImage
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
9
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in typical form (like 3.810)?

A) "#.###"
B) "#.0"
C) "0.# "
D) "0.000"
E) "0.0##"
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
10
Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

A) encapsulation
B) information hiding
C) tokenizing
D) importing
E) method overloading
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
11
StringTokenizer is a class in the java.util library that can divide a String based on some delimiter String (a delimiter is a separator). If the instruction StringTokener st = new StringTokenizer(str, "&&"); is executed, where str is some String, then st divides up str into separate Strings whenever

A) a blank space is found
B) two blank spaces are found
C) a single ampersand ("&") is found
D) two ampersands are found
E) two ampersands or the substring "and" or "AND" is found
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
12
A variable whose scope is restricted to the method where it was declared is known as a(n)

A) parameter
B) global variable
C) local variable
D) public instance data
E) private instance data
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
13
Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this?

A) public int updateHours( ) { return hours; }
B) public void updateHours( ) { hours++; }
C) public updateHours(int moreHours) { hours += moreHours; }
D) public void updateHours(int moreHours) { hours += moreHours; }
E) public int updateHours(int moreHours) { return hours + moreHours; }
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
14
Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

A) foo(0, 0.1);
B) foo(0 / 1, 2 * 3);
C) foo(0);
D) foo( );
E) foo(1 + 2, 3 * 0.1);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
15
The behavior of an object is defined by the object's

A) instance data
B) constructor
C) visibility modifiers
D) methods
E) all of the above
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
16
The relationship between a class and an object is best described as

A) classes are instances of objects
B) objects are instances of classes
C) objects and classes are the same thing
D) classes are programs while objects are variables
E) objects are the instance data of classes
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following reserved words in Java is used to create an instance of a class?

A) class
B) public
C) public or private, either could be used
D) import
E) new
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
18
An example of passing message to a String where the message has a String parameter would occur in which of the following messages?

A) length
B) substring
C) equals
D) toUpperCase
E) none of the above, it is not possible to pass a String as a parameter in a message to a String
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
19
For the questions below, use the following class definition.
import java.text.DecimalFormat;
public class Student
{
private String name;
private String major;
private double gpa;
private int hours;
public Student(String newName, String newMajor, double newGPA, int newHours)
{
name = newName;
major = newMajor;
gpa = newGPA;
hours = newHours;
}
public String toString( )
{
DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced
return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours
}
}
Which of the following could be used to instantiate a new Student s1?

A) Student s1 = new Student( );
B) s1 = new Student( );
C) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
D) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
E) new Student(s1);
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
20
In order to preserve encapsulation of an object, we would do all of the following except for which one?

A) Make the instance data private
B) Define the methods in the class to access and manipulate the instance data
C) Make the methods of the class public
D) Make the class final
E) All of the above preserve encapsulation
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
21
A GUI container is an object that defines a screen element.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
22
A constructor may contain a return statement so long as no value (or expression) is returned.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
23
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
24
The interface of a class is based on those data instances and methods that are declared public.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
25
Every class definition must include a constructor.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
26
Defining formal parameters requires including each parameters type.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
27
What happens if you declare a class constructor to have a void return type?

A) You'll likely receive a syntax error
B) The program will compile with a warning, but you'll get a runtime error
C) There's nothing wrong with declaring a constructor to be void
D) The class' default constructor will be used instead of the one you're declaring
E) None of the above
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
28
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { ... }
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
29
The following method header definition will result in a syntax error: public void aMethod( );
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
30
The expressions that are passed to a method in an invocation are called

A) actual parameters
B) formal parameters
C) formal arguments
D) formals
E) any of the above
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
31
All Java classes must contain a main method which is the first method executed when the Java class is called upon.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
32
In a UML diagram for a class

A) classes are represented as rectangles
B) there may be a section containing the name of the class
C) there may be a section containing the attributes (data) of the class
D) there may be a section containing the methods of the class
E) all of the above
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
33
Java methods can return only primitive types (int, double, float, char, boolean, etc).
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
34
The software failure at the Denver International Airport's baggage handling system is a good example of

A) how a large system can be obsolete by the time it is developed
B) how designers sometimes have too much faith in the technology they are using
C) how failures are often a result of multiple variables caused by a system as a whole in actual operation
D) how the use of a centralized computer is really unrealistic in today's distributed processing
E) all of the above
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
35
Listener objects "wait" for an event to occur.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
36
A method defined in a class can access the class' instance data without needing to pass them as parameters or declare them as local variables.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
37
Visibility modifiers include

A) public, private
B) public, private, protected
C) public, private, protected, final
D) public, protected, final, static
E) public, private, protected, static
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
38
What are the objects needed in order to create a Java GUI (graphical user interface)?

A) components
B) events
C) listeners
D) all of the above
E) only classes from AWT and/or Swing are needed
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
39
Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?

A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) doublefoo(0.1, 0.2);
E) all of the above are legal except for D
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
40
While multiple objects of the same class can exist, in a given program there can be only one version of each class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
41
Write the statement to instantiate an Box object, blueBox, with a length of 6, width of 4, and height of 2.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
42
An Employee will have a name, social security number, position, and hourly wages. Define the instance data for this class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
43
Write the constructor, which is passed the player's name and position.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
44
Write a statement to output the volume of the blue box.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
45
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
46
Write a statement to instantiate a cube with a length of 3, width of 3, and height of 3.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
47
Write the constructor for this class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
48
When reasoning about a car, we use such information as its make and model, year, color, cost (or worth), number of miles, gas mileage, insurance cost, and so forth. Consider a program that will attempt to determine how practical a car might be on a road trip. Which of these types of information might you use in your program as instance data?
Provide a justification for your selection.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
49
Assume method0 calls method1 and method2, method1 calls method3 which calls method4 and method5. Explain the chain of method calls (what order methods are called and from which method).
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
50
Consider the "Push Me!" program described in section 4.7. As given the push counter starts at zero and is incremented each time the "Push Me!" button is pressed. Let's say that you want to add a class called ResetListener that resets the count to zero. Write the ResetListener class.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
51
Explain why it would be a poor decision to make instance data public instead of private. Give an example of why this could result in a problem.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
52
Write a toString method that returns the player's name, position and batting average formatted to have 1 digit to the left of the decimal and 3 digits to the right of the decimal. Assume DecimalFormat has been imported.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
53
What visibility modifiers would you use for the methods that move the elevator up one floor, move the elevator down one floor, that request the elevator, and that start the elevator moving?
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
54
Add a constructor to class Box for a cube, which has only 1 parameter, side.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
55
An object should be encapsulated in order to guard its data and methods from inappropriate access.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
56
Regarding the software failure described at the Denver International Airport, it is critical to factor in errors and inefficiencies that always occur in a complex system.
Unlock Deck
Unlock for access to all 56 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 56 flashcards in this deck.