professor-curious-logo
professor-curious-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is the difference between a getter method and a setter method?

Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is the difference between a getter method and a setter method?

Getter: Accesses the value of an instance variable. Setter: Modifies the value of an instance variable.

What is the difference between public and private access modifiers?

Public: Accessible from anywhere. Private: Accessible only within the declaring class.

What is the difference between returning a primitive type and returning a reference type in a getter method?

Primitive: Returns a copy of the value. Reference: Returns a copy of the reference to the object.

Compare the purpose of accessor methods and mutator methods.

Accessor methods: Provide read access to object's state. Mutator methods: Provide write access to object's state.

Compare direct access of instance variables vs. using accessor methods.

Direct access: Bypasses encapsulation, can lead to data corruption. Accessor methods: Provide controlled access, maintain data integrity.

Compare the use of toString() with getter methods for printing object information.

toString(): Returns a formatted string representation of the object. Getter methods: Return specific attribute values.

Compare the effect of returning a primitive data type vs. a reference type from a method.

Primitive data type: Returns a copy of the value. Reference type: Returns a copy of the reference to the object.

Compare the use of accessor methods with and without encapsulation.

With encapsulation: Protects data integrity and allows controlled access. Without encapsulation: Exposes data directly, risking unintended modifications.

Compare the use of public and private access modifiers for instance variables.

Public: Allows unrestricted access from any class. Private: Restricts access to within the same class, promoting encapsulation.

Compare the use of accessor methods and direct variable access regarding code maintainability.

Accessor methods: Allow internal implementation changes without affecting external code. Direct variable access: Requires changes in all places where the variable is used.

What is an accessor method?

A method that allows clients to access the data of an object.

What is a getter method?

An accessor method used to get the value of a specific instance variable.

What is the toString() method?

A method that returns information about an object as a string.

What is a client in the context of accessor methods?

Objects, classes, and users that access the data of an object.

What does '@Override' do?

Indicates that a method is overriding a method in a superclass.

What is 'return by value'?

Returning a copy of the value of a primitive data type.

What is returned when a getter method returns a reference to an object?

A copy of the reference to the object is returned, not a copy of the object itself.

What happens when the 'return' keyword is triggered?

The program immediately returns to the point after whatever called the method.

What is the access modifier of accessor methods?

public

Why are accessor methods made public?

To allow other clients (objects, classes, and users) to access the data of an object.

Identify the error in the following code:

java
public class Assignment {
 private boolean answer;
 @Override
 public String toString() {
 return "This is an assignment with correct answer " + answer;
 }
}

The instance variable is named 'correctAnswer', not 'answer'.

What does the following code output?

java
Student bob = new Student(10, "Bob Smith", 16);
System.out.println(bob);

Bob Smith, a 10th grade high school student

Complete the getter method for the 'name' instance variable in the Student class:

java
public class Student {
 private String name;
 public String ________() {
 return name;
 }
}

getName

What is the return type of the getGradeLevel() method in the Student class?

int

What is the return type of the getName() method in the Student class?

String

What will be the output of the following code snippet?

java
Assignment assignment = new Assignment(true);
System.out.println(assignment);

This is an assignment with correct answer true

Identify the error in the following code:

java
public class Student {
 private int age;
 public void getAge() {
 return age;
 }
}

The return type of getAge() should be int, not void.

Complete the following getter method:

java
public class Student {
 private Assignment assignment;
 public ________ getCurrentAssignment() {
 return assignment;
 }
}

Assignment

What is the purpose of the following method?

java
public Assignment returnCurrentAssignment() {
 return assignment;
}

It returns the current assignment the student is working on.

What is wrong with the following code?

java
public class Student {
 private int gradeLevel;
 public String getGradeLevel() {
 return gradeLevel;
 }
}

The return type of the getGradeLevel() method should be int, not String.