Java Chapter 6 Flashcards
26 Questions
100 Views

Java Chapter 6 Flashcards

Created by
@EvaluativeQuantum

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?

<p>Field</p> Signup and view all the answers

What key word causes an object to be created in memory?

<p>new</p> Signup and view all the answers

What is a method that gets a value from a class's field but does not change it?

<p>Accessor</p> 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?

<p>mutator</p> 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?

<p>Stale</p> Signup and view all the answers

What is the method called that is automatically called when an instance of a class is created?

<p>constructor</p> 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?

<p>Shadows</p> Signup and view all the answers

What is automatically provided for a class if you do not write one yourself?

<p>default constructor</p> Signup and view all the answers

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

<p>Their parameter lists</p> Signup and view all the answers

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

<p>binding</p> Signup and view all the answers

A class's responsibilities are __________.

<p>Actions the class performs, and things the class knows</p> Signup and view all the answers

The new operator creates an instance of a class.

<p>True</p> Signup and view all the answers

Each instance of a class has its own set of instance fields.

<p>True</p> Signup and view all the answers

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

<p>False</p> Signup and view all the answers

A class may not have more than one constructor.

<p>False</p> 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.

<p>False</p> 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; } }

<p>The constructor cannot have a return type, not even void.</p> 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; }

<p>Missing type for second and third parameters.</p> Signup and view all the answers

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

<p>The parentheses are missing. The statement should read: Rectangle box = new Rectangle();</p> 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; } }

<p>Two constructors are defined with the same name.</p> 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; } }

<p>The square methods must have different parameter lists. Both accept an int.</p> 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?

<p>getName</p> Signup and view all the answers

What should a UML diagram of the Pet class include?

<p>Each field and method's access specification and data type, and method parameters with their data types.</p> 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

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.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

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.

Use Quizgecko on...
Browser
Browser