Chapter 4 - Writing Classes

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/86

flashcard set

Earn XP

Description and Tags

C&P II Midterm

Last updated 3:18 AM on 3/31/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

87 Terms

1
New cards

What is true object-oriented programming based on?

Defining classes that represent objects with well-defined characteristics and functionality

2
New cards

Can you only have 1 constructor?

No; you can have multiple, BUT you need to identify parameters

3
New cards

What does every object have?

State and behavior

4
New cards

Dice analogy for state and behavior

State can be defined as which face is showing

Behavior that it can be rolled

5
New cards

What can a class contain?

Data declarations and method declarations

6
New cards

What do the values of the data define in a class?

The state of an object created from the class

7
New cards

What do the functionality of the methods define in a class?

The behaviors of the object

8
New cards

What does final mean?

The value CANNOT be updated

9
New cards

What does private mean?

Must go through method?

10
New cards

What is the difference between public class Die and public Die()?

public class Die → class

public Die() → method

11
New cards

What are two data values of the Die class

  • A constant MAX that represents the maximum face value

  • An integer faceValue that represents the current face value

12
New cards

What does the roll method do?

Uses the random method of the Math class to determine a new face value

13
New cards

What is the toString method?

Method returns a character string that represents the object in some way

14
New cards

When is toString called?

Called automatically when an object is concatenated to a string OR when it is passed to the println method

15
New cards

What is the toString method convenient for?

Debugging problems

16
New cards

What is a constructor?

Used to set up an object when it is initially created

17
New cards

What do the constructor and class have in common?

A constructor has the same name as the class

18
New cards

What is the data scope?

The scope of data is the area in a program in which that data can be referenced (used)

19
New cards

What is important to note about data declared at the class level?

It can be referenced by all methods in that class

20
New cards

What is important to note about data declared within a method?

That data can only be used in that method

21
New cards

What is local data?

Data declared with a method

22
New cards

What is instance data?

Variable declared at the class level

23
New cards

What is an instance?

A specific object created from a class

24
New cards

What is important to note about each instance?

Each instance (object) has its own instance variable

25
New cards

Does a class reserve memory space for data?

No; a class declares the type of data, but does not reserve memory space for it

26
New cards

Do objects share the method definitions?

Yes; objects of a class share the method definitions

27
New cards

Do objects share data space?

No; each object has its own data space

28
New cards

What are the only two ways two objects can have different states?

The objects of a class share the method definitions but each object has its own data space

29
New cards

What does UML stand for?

Unified Modeling Language

30
New cards

What do UML diagrams show?

Shows relationships among classes and objects

31
New cards

What does a UML class diagram consist of and what does it contain?

One or more classes, each with sections for the class name, attributes (data) and operations (methods)

32
New cards

What do lines between classes represent in UML diagrams?

Associations

33
New cards

What do dotted arrows show in UML diagrams?

Shows that one classes uses the other (aka calls its methods)

34
New cards

What does it mean when an arrow points to something in a UML diagram?

The place where the arrow originates from is DEPENDENT

35
New cards

What is the relationship between a class and an object?

Class is the definition/pattern/blueprint of an object; it defines data that will be managed by an object, but doesn’t reserve memory space for it

Multiple objects can be created from a class, and each object has its own copy of the instance data.

36
New cards

Where is instance data declared?

At the class level

37
New cards

What is the scope of instance data?

It can be referenced in any method of the class

38
New cards

What is local data?

Local data is declared within a method, and is only accessible in that method

39
New cards

What are the two views of an object?

Internal and external

40
New cards

What is internal view?

The details of the variables and methods of the class that defines it

41
New cards

What is the external view?

The services that an object provides and how the object interacts with the rest of the system

42
New cards

What is known about the object from the external view?

An objected is an encapsulated entity, providing a set of specific services

43
New cards

What do the services define in an object?

They define the interface to the object

44
New cards

What is encapsulation and how does it work?

An object where the inner workings are hidden form the client; the client invokes the interface methods and they manage the instance data

45
New cards

What does it mean for an object to be self-governing?

When an object (client) uses another object for the services it provides, the client of an object can request its services, but it should not be aware of how those services are accomplished

Any changes to the object’s state (variables) should be made by that object’s methods; it should be difficult/impossible for a client to access an object’s variables directly

46
New cards

What are visibility modifiers?

A Java reserved word that specifies particular characteristics of a method or data

47
New cards

What are the three visibility modifiers?

Public, Protected, Private

48
New cards

What is the difference between public, protected, and private

Public - for everyone

Protected - somewhat restricted, belongs to some members

Private - restricted

49
New cards

What visibility do members declared without a visibility modifier have?

Default visibility; can be referenced by any class in the same package

50
New cards

Why do public variables violate encapsulation?

Because they allow the client to modify the values directly

51
New cards

Should instance variables be declared with public visibility?

NO

52
New cards

Do public constants violate encapsulation?

No; although the client can access it, its value cannot be changed

53
New cards

What visibility modifier do methods that provide the object’s services have?

Declared with public visibility so that they can be invoked by clients

54
New cards

What is another word for public methods?

Service methods

55
New cards

What is a support method?

Method created simply to assist a service method

56
New cards

What visibility modifier should support methods not be declared with?

Public visibility, since support methods are not intended to be called by a client

57
New cards

What is an accessor method?

Returns the current value of a variable

58
New cards

What is a mutator method?

Changes the value of a variable

59
New cards

What is the format of an accessor method?

getX

60
New cards

What is the format of the mutator method?

setX

61
New cards

What does the use of mutators give the class desginer?

the ability to restrict a client’s options to modify an object’s state

62
New cards

How is a mutator often designed?

So that the values of variables can be set only within particular limits

63
New cards

Why was the faceValue variable declares as private in the Die class?

By making it private, each Die object controls its own data and allows it to be modified only by the well-defined operations it provides

64
New cards

Why is it okay to declare MAX as public in the Die class?

MAX is a constant. Its value cannot be changed. Therefore, there is no violation of encapsulation

65
New cards

What is a method declaration?

Specifies the code that will be executed when the method is invoked

66
New cards

What happens when a method is invoked?

the flow of control jumps to the method and executes its code

67
New cards

What does a method declaration begin with?

A method header

68
New cards

What does the parameter list specify?

The type and name of each parameter

69
New cards

What is a formal parameter?

The name of a parameter in the method declaration

70
New cards

What is the method header followed by?

The method body

71
New cards

What type of data is sum and result?

Local data

72
New cards

When is sum and data created?

Each time the method is called, and are destroyed when it finishes executing

73
New cards

What must be consistent with the return type?

The return expression

74
New cards

What does the return type of a method indicate?

The type of value that the method sends back to the calling location?

75
New cards

What is a void return type?

A method that does not return a value

76
New cards

What does a return statement specify?

The value that will be returned; its expression must conform to the return type

77
New cards

What occurs to the parameters when a method is called?

The actual parameters in the invocation are copied into the formal parameters in the method head

78
New cards

What do the formal parameters of a method create?

Automatic local variables

79
New cards

What occurs when the method finishes?

All local variables are destroyed, including formal parameters

80
New cards

What level are instance variables declared at?

The class variables

81
New cards

What happens to local variables after the method finishes?

They are destroyed, including formal parameters

82
New cards

What is a driver program?

Drives the use of other, more interesting parts of a program

83
New cards

What are driver programs used for?

To test other parts of the software

84
New cards

Does a constructor have a return type?

NO; it has no return type specified in the method header, not even void

85
New cards

What is a common error with constructors?

Putting a return type on a constructor, which makes it a “regular” method that happens to have the same name as the class

86
New cards

Does the programmer have to define a constructor for a class?

No

87
New cards

What is important to note about classes and constructors?

Each class has a default constructor that accepts no parameters

Explore top notes

note
Microorganisms
Updated 1047d ago
0.0(0)
note
3.4 Quantum Numbers
Updated 1103d ago
0.0(0)
note
Using the Periodic Table
Updated 354d ago
0.0(0)
note
CAPITULO 1
Updated 1160d ago
0.0(0)
note
Pancoast’s Tumor Syndrome
Updated 1143d ago
0.0(0)
note
Viruses, Viroids, AND Lichens
Updated 902d ago
0.0(0)
note
Chapter 8 - Hypothesis Testing
Updated 1455d ago
0.0(0)
note
Chapter 9: Nanoparticles
Updated 1073d ago
0.0(0)
note
Microorganisms
Updated 1047d ago
0.0(0)
note
3.4 Quantum Numbers
Updated 1103d ago
0.0(0)
note
Using the Periodic Table
Updated 354d ago
0.0(0)
note
CAPITULO 1
Updated 1160d ago
0.0(0)
note
Pancoast’s Tumor Syndrome
Updated 1143d ago
0.0(0)
note
Viruses, Viroids, AND Lichens
Updated 902d ago
0.0(0)
note
Chapter 8 - Hypothesis Testing
Updated 1455d ago
0.0(0)
note
Chapter 9: Nanoparticles
Updated 1073d ago
0.0(0)

Explore top flashcards

flashcards
ART 149 FINAL EXAM STUDY GUIDE
145
Updated 482d ago
0.0(0)
flashcards
abeka biology 10 section 5.1
31
Updated 920d ago
0.0(0)
flashcards
TFN 3 - ORPEZA
161
Updated 696d ago
0.0(0)
flashcards
434 Unit 2 Lec 9-10
28
Updated 1121d ago
0.0(0)
flashcards
Vocab Unit 5
25
Updated 1173d ago
0.0(0)
flashcards
(cz. 2) Historyzm, realizm
28
Updated 400d ago
0.0(0)
flashcards
T2 - Full Exam Prep
53
Updated 198d ago
0.0(0)
flashcards
Government
71
Updated 1117d ago
0.0(0)
flashcards
ART 149 FINAL EXAM STUDY GUIDE
145
Updated 482d ago
0.0(0)
flashcards
abeka biology 10 section 5.1
31
Updated 920d ago
0.0(0)
flashcards
TFN 3 - ORPEZA
161
Updated 696d ago
0.0(0)
flashcards
434 Unit 2 Lec 9-10
28
Updated 1121d ago
0.0(0)
flashcards
Vocab Unit 5
25
Updated 1173d ago
0.0(0)
flashcards
(cz. 2) Historyzm, realizm
28
Updated 400d ago
0.0(0)
flashcards
T2 - Full Exam Prep
53
Updated 198d ago
0.0(0)
flashcards
Government
71
Updated 1117d ago
0.0(0)