Consider the following class header for a class named Book. Which of the following is a valid constructor for this class if the class has private instance variables title (String) and pages (int)?
AP (Advanced Placement) · AP Computer Science A
Abstraction, Program Design and Its Impact: แบบฝึกหัด
ข้อปรนัย 5 ข้อ ตรวจให้ทันทีที่ตอบ และข้อเขียน 1 ข้อ พร้อมวิธีทำละเอียด ทั้งหมดจากเรื่อง Abstraction, Program Design and Its Impact
Which of the following best describes the purpose of a constructor in a Java class?
A class Student has an instance variable gpa. The constructor is defined as follows:
public Student(double gpa) {
gpa = gpa;
}
What happens when new Student(3.5) is called, assuming the instance variable is also named gpa?
A class Square is being designed. It needs to store the length of a side. Which of the following is the most appropriate way to declare the instance variable for side length to ensure encapsulation?
Consider the following class definition:
public class Runner {
private double distance;
public Runner(double d) {
distance = d;
}
public void addDistance(double d) {
distance += d;
}
}
If we execute the following code:
Runner r1 = new Runner(5.0);
r1.addDistance(2.5);
What is the value of distance in the object r1?
Part a: A software developer needs to create a Student class. Define a constructor that initializes a student's name (String) and their current GPA (double).
Part b: Write a method isEligible that returns true if the GPA is greater than or equal to 2.0, and false otherwise.
Part c: Explain why the instance variables should be declared as private and how the constructor helps in maintaining encapsulation.
ลองเขียนคำตอบด้วยตัวเองก่อน แล้วค่อยเทียบกับวิธีทำ
* เนื้อหาของ thinka สร้างโดย AI อาจไม่ถูกต้องสมบูรณ์ในทุกกรณี กรุณาใช้เป็นสื่อเสริมและตรวจสอบกับเอกสารอ้างอิงอย่างเป็นทางการ
คุณเห็นเฉลยแล้ว ทีนี้มาตรวจคำตอบของคุณบ้าง
หน้านี้บอกได้ว่าคำตอบที่ดีเป็นอย่างไร แต่บอกไม่ได้ว่าคำตอบของคุณขาดอะไร thinka ตรวจข้อเขียนของคุณตามเกณฑ์ให้คะแนนจริงในราว 15 วินาที
อยากฝึกโจทย์แบบนี้เพิ่มไหม เริ่มฝึกหัวข้อนี้ได้เลย ตรวจให้ทันทีทุกข้อ
เริ่มฝึกเลย