Questions and Answers
What is a collection of programming statements that specify the fields and methods that a particular type of object may have?
Class
A class is analogous to a(n) __________.
Blueprint
An object is a(n) __________.
instance of a class
What is this class member that holds data called?
Signup and view all the answers
What key word causes an object to be created in memory?
Signup and view all the answers
What is a method that gets a value from a class's field but does not change it?
Signup and view all the answers
What is a method that stores a value in a field or changes the value of a field called?
Signup and view all the answers
When the value of an item is dependent on other data and not updated when that data changes, what has the value become?
Signup and view all the answers
What is the method called that is automatically called when an instance of a class is created?
Signup and view all the answers
When a local variable has the same name as a field, what does the local variable's name do to the field's name?
Signup and view all the answers
What is automatically provided for a class if you do not write one yourself?
Signup and view all the answers
Two or more methods in a class may have the same name, as long as this is different.
Signup and view all the answers
The process of matching a method call with the correct method is known as __________.
Signup and view all the answers
A class's responsibilities are __________.
Signup and view all the answers
The new operator creates an instance of a class.
Signup and view all the answers
Each instance of a class has its own set of instance fields.
Signup and view all the answers
When you write a constructor for a class, it still has the default constructor that Java automatically provides.
Signup and view all the answers
A class may not have more than one constructor.
Signup and view all the answers
To find the classes needed for an object-oriented application, you identify all of the verbs in a description of the problem domain.
Signup and view all the answers
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; } }
Signup and view all the answers
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; }
Signup and view all the answers
The following statement attempts to create a Rectangle object. Find the error. Rectangle box = new Rectangle;
Signup and view all the answers
Find the error in the following class: public class TwoValues { private int x, y; public TwoValues() { x = 0; } public TwoValues() { x = 0; y = 0; } }
Signup and view all the answers
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; } }
Signup and view all the answers
Design a class named Pet, which should have the following fields: name, animal, age. What method should be included to get the name value?
Signup and view all the answers
What should a UML diagram of the Pet class include?
Signup and view all the answers
Study Notes
Classes and Objects
- A class is a blueprint for creating objects, defining fields and methods an object may have.
- An object is an instance created from a class, representing a specific entity in memory.
Class Members
- Fields store data in a class, representing the object's attributes.
- Constructor is a special method called automatically when an object is instantiated.
- A default constructor is automatically provided by Java if no constructor is explicitly defined.
- Accessor methods retrieve values from fields without changing them, while mutator methods modify field values.
Data Handling
- When a data item's value is outdated due to dependency on modified data, it is termed stale.
- Local variables can shadow fields if they share the same name, affecting visibility within the class.
Method Overloading and Binding
- Method overloading is allowed when methods share a name but have different parameter lists.
- Binding is the process of matching a method call to the correct method definition.
Class Responsibilities
- A class's responsibilities encompass the actions it performs and the information it maintains.
Object-Oriented Principles
- Each instance of a class maintains its own set of instance fields.
- A class can have multiple constructors to provide different ways to create objects.
Error Checking in Classes
- Constructors cannot have return types, not even void.
- Errors in class definitions may include missing parentheses in object creation or non-distinct method signatures.
Class Design Example
- A Pet class can include:
-
Fields:
name
(String),animal
(String),age
(int). -
Methods:
-
setName(n: String): void
-
setAnimal(a: String): void
-
setAge(age: int): void
-
getName(): String
-
getAnimal(): String
-
getAge(): int
-
-
Fields:
UML Diagram
- For the Pet class:
- Notation should include:
- Fields:
name: String
,animal: String
,age: int
- Access specifications and data types for each field and method definitions.
- Fields:
- Notation should include:
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Explore key concepts of Java classes with these flashcards from Chapter 6. Each card includes essential terminology and definitions that will help you understand the structure and behavior of classes in Java. Perfect for students preparing for exams or wanting to reinforce their knowledge.