Abstraction and Program Design
Help Questions
AP Computer Science A › Abstraction and Program Design
A programmer is designing a class to model a specific type of laptop computer. The specification notes that all laptops of this model share the same manufacturer name and screen resolution. However, each individual laptop has its own unique serial number and current battery percentage. How should these attributes best be represented in the Laptop class?
All four attributes (manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as class variables.
serialNumber as a class variable; manufacturer, screenResolution, and batteryPercentage as instance variables.
All four attributes (manufacturer, screenResolution, serialNumber, batteryPercentage) should be designed as instance variables.
manufacturer and screenResolution as class variables; serialNumber and batteryPercentage as instance variables.
Explanation
Class variables (static) are used for data that is shared by all instances of a class. manufacturer and screenResolution fit this description. Instance variables are used for data that is unique to each instance. serialNumber and batteryPercentage are specific to each individual laptop object, making them ideal instance variables.
A programmer designs a single, complex method named generateSalesReport. To improve the design, they break its logic into several smaller, more focused helper methods: fetchSalesData, calculateTotalRevenue, and formatReport. This design strategy is a direct example of which concept?
Method decomposition, which breaks down a complex behavior into smaller, manageable procedures.
Object instantiation, which is the process of creating a new instance of a class.
Data encapsulation, which hides the internal state of an object from the outside world.
Method overloading, which allows multiple methods to have the same name with different parameter lists.
Explanation
Method decomposition is the process of breaking a large, complex method into several smaller, simpler helper methods. This improves code readability, makes it easier to test individual pieces of logic, and promotes code reuse. The scenario described is a classic example of this design principle.
A programmer designs a method, calculateArea, that computes the area of a rectangle. The initial version only works for a rectangle with a fixed width of 10 and height of 5. Which of the following modifications would best generalize this method using abstraction?
Change the method's name to calculateAreaOf10x5Rectangle to make its limited purpose more explicit.
Add width and height parameters to the method so it can calculate the area for any rectangle, not just one specific size.
Create multiple overloaded versions of the method, one for each common width and height combination.
Keep the method as is, but add extensive comments explaining that it only works for a 10x5 rectangle.
Explanation
Generalization is achieved by replacing specific, hard-coded values with parameters. By adding width and height as parameters, the method is no longer tied to a single case and becomes a reusable tool for calculating the area of any rectangle, which is a much more abstract and useful design.
A programmer implements a method public static int[] sort(int[] data) that uses an insertion sort algorithm. Later, they discover that a merge sort algorithm would be more efficient for large data sets. They rewrite the internal logic of the sort method to use merge sort but do not change the method signature. Why do other parts of the program that call this sort method not need to be modified?
Because of inheritance, which ensures that changes to a superclass method are automatically applied to all subclasses.
Because of procedural abstraction, which separates the method's interface (its signature) from its implementation (its internal logic).
Because of polymorphism, which allows an object to take on many forms and behave differently depending on its type.
Because of data encapsulation, which bundles the sort method together with the data it operates on.
Explanation
Procedural abstraction means that the caller of a method only needs to know what the method does (as defined by its signature and documentation), not how it does it. As long as the signature (public static int[] sort(int[] data)) and the overall behavior (sorting an array) remain the same, the internal implementation can be changed without affecting the code that uses it.
Based on the class design for the online store, which method signature correctly implements calculating a percentage discount on a Product price?
public double applyDiscount(double percent)
public int applyDiscount(double percent)
public void applyDiscount(int percent)
private double applyDiscount(double percent)
Explanation
This question tests AP Computer Science A skills, specifically abstraction and program design through selecting appropriate method signatures. The applyDiscount method should calculate a percentage discount on a product price, requiring it to accept a percentage parameter and return the discounted price as a double. Choice A is correct because it has public visibility (allowing external access), accepts a double percent parameter (supporting decimal percentages like 15.5%), and returns a double (the calculated discounted price). Choice B is incorrect because it returns void, preventing the method from returning the calculated discount amount, and uses int for percent which limits precision. To help students: Emphasize matching return types to the method's purpose - calculations typically return values. Practice identifying when methods should return values versus void. Watch for: confusion between void and value-returning methods, and inappropriate parameter types.
A student record console app computes GPA from a list of grades; calculateGpa returns a double. Which method signature correctly implements calculateGpa for the Student class?
public int calculateGpa()
private double calculateGpa()
public double calculateGpa()
public void calculateGpa(double gpa)
Explanation
This question tests AP Computer Science A skills, specifically abstraction and program design through selecting appropriate method signatures for calculations. The calculateGpa method computes GPA from grades and returns it as a double, which is the standard data type for GPA values that include decimals (like 3.75). Choice B is correct because it has public visibility (allowing external access to this important calculation) and returns double, which properly represents GPA values with decimal precision. Choice A is incorrect because it returns int, which would truncate GPA values like 3.75 to 3, losing important precision in academic calculations. To help students: Emphasize choosing appropriate return types based on the nature of the data - GPAs need decimal precision. Practice identifying when to use int versus double for different types of calculations. Watch for: using integer types for values that require decimal precision.
Based on the class design for the weather station, updateReadings sets temperature, humidity, and windSpeed from parameters. Which method signature correctly implements updateReadings for WeatherData?
private void updateReadings(double t, double h, double w)
public void updateReadings(double t, double h, double w)
public double updateReadings(double t, double h, double w)
public void updateReadings(int t, int h, int w)
Explanation
This question tests AP Computer Science A skills, specifically abstraction and program design through implementing setter methods. The updateReadings method should accept three double parameters (temperature, humidity, windSpeed) and update the corresponding fields without returning a value. Choice A is correct because it has public visibility (allowing external sensor updates), accepts three double parameters matching the weather data types, and returns void since it's a setter method that updates state. Choice C is incorrect because it uses int parameters instead of double, which would lose precision for weather measurements like 72.5°F or 65.3% humidity. To help students: Emphasize that setter methods typically return void and use parameter types that match the field types. Practice writing setter methods with appropriate parameter types and visibility. Watch for: using incorrect parameter types that lose precision or returning values from setter methods.
Based on the class design for the weather station, how does encapsulation benefit the design of the WeatherData class storing temperature, humidity, and windSpeed?
It requires WeatherData to extend ArrayList.
It forces all fields to be public.
It allows controlled updates via public methods.
It prevents methods from updating readings.
Explanation
This question tests AP Computer Science A skills, specifically abstraction and program design through understanding encapsulation benefits. Encapsulation involves making fields private and providing public methods to control access, ensuring data integrity and hiding implementation details. Choice C is correct because encapsulation allows the WeatherData class to provide public methods (like updateReadings) that validate and control how temperature, humidity, and windSpeed are modified, preventing invalid data. Choice A is incorrect because encapsulation actually requires fields to be private, not public, which is the opposite of what this choice states. To help students: Emphasize that encapsulation protects data by hiding it (private fields) while providing controlled access (public methods). Practice identifying the benefits of data hiding and controlled access. Watch for: confusion between making fields public (breaks encapsulation) versus providing public methods (proper encapsulation).
In the design of a Student class for a university, each student must have a unique ID number. The university also needs to maintain a running total of how many Student objects have been created in the system. Which of the following represents the most appropriate design for these two pieces of data?
An instance variable for the unique ID number and a class variable to store the total count of students.
Both the unique ID number and the total count of students should be stored as instance variables.
Both the unique ID number and the total count of students should be stored as class variables.
A class variable for the unique ID number and an instance variable to store the total count of students.
Explanation
An instance variable is appropriate for the ID number because each Student object needs its own unique value. A class variable (static) is appropriate for the total count because this value is shared across all Student objects and belongs to the class as a whole, not to any single instance.
Which statement accurately describes the relationship between an attribute and an instance variable in object-oriented design?
An attribute is always a class variable shared by all objects, while an instance variable is unique to a single object.
An instance variable is a specific type of attribute; its value is unique to each object and helps define that object's state.
An attribute and an instance variable are completely unrelated concepts; attributes relate to design, and instance variables relate to syntax.
An attribute is a synonym for a method's behavior, while an instance variable is a specific type of data storage used only within constructors.
Explanation
An attribute is a general term for a property or characteristic of a class in the design phase. An instance variable is the concrete implementation of an attribute whose value is distinct for each instance (object) of the class. Therefore, an instance variable is a specific kind of attribute.