Sign FRQ
The getLines method returns a String containing the message broken into lines separated by semicolons (;) or returns null if the message is the empty string. The constructor parameter that contains the message to be displayed will not include any semicolons. As an example, in the first row of the preceding table, getLines would return “Everything on s;ale, please com;e in” No semicolon should appear at the end of the String returned by getlines.
public class Sign {
// Initial variables
private int lineWidth;
private String message;
// Constructor with parameter validation
public Sign(int lineWidth, String message) {
if (lineWidth <= 0) {
throw new IllegalArgumentException("Line width must be positive");
}
if (message == null) {
throw new IllegalArgumentException("Message cannot be null");
}
this.lineWidth = lineWidth;
this.message = message;
}
// Calculate number of lines needed for the message
public int numberOfLines() {
int lengthOfMessage = message.length();
int linesNeeded = 1;
while (lengthOfMessage > lineWidth) {
linesNeeded += 1;
lengthOfMessage -= lineWidth;
}
return linesNeeded;
}
// Generate lines based on line width
public String getLines() {
if (message.length() == 0) {
return null;
}
StringBuilder output = new StringBuilder();
int startIndex = 0;
while (startIndex < message.length()) {
int endIndex = Math.min(startIndex + lineWidth, message.length());
output.append(message.substring(startIndex, endIndex)).append(";");
startIndex = endIndex;
}
return output.toString();
}
public static void main(String[] args) {
Sign sign = new Sign(5, "Hello World!");
System.out.println("Number of lines: " + sign.numberOfLines());
System.out.println("Lines:\n" + sign.getLines());
}
}
Sign.main(null)
Number of lines: 3
Lines:
Hello; Worl;d!;
Textbook Inheritance FRQ
public class Book {
private String title;
private double price;
public Book(String bookTitle, double bookPrice) {
this.title = bookTitle; // Initialize the title field
this.price = bookPrice;
}
public String getTitle() { return title; }
public String getBookInfo() {
return title + "-" + price;
}
}
public class Textbook extends Book {
private int edition;
public Textbook(String bookTitle, double bookPrice, int edition) {
super(bookTitle, bookPrice);
this.edition = edition;
}
public int getEdition() {
return edition;
}
// Takes the main constructor
public String getBookInfo() {
return super.getBookInfo() + "-" + edition;
}
public boolean canSubstituteFor(Textbook otherTextbook) {
return this.getTitle().equals(otherTextbook.getTitle()) &&
this.getEdition() >= otherTextbook.getEdition();
}
}
public class BookTester {
public static void main(String[] args) {
// Create a Book object
Book book1 = new Book("Introduction to Java", 29.99);
System.out.println("Book Title: " + book1.getTitle());
System.out.println("Book Info: " + book1.getBookInfo());
// Create a Textbook object
Textbook textbook1 = new Textbook("Data Structures and Algorithms", 49.99, 2);
System.out.println("Textbook Title: " + textbook1.getTitle());
System.out.println("Textbook Edition: " + textbook1.getEdition());
System.out.println("Textbook Info: " + textbook1.getBookInfo());
// Test canSubstituteFor method
Textbook textbook2 = new Textbook("Data Structures and Algorithms", 59.99, 1);
System.out.println("Can substitute for textbook2? " + textbook1.canSubstituteFor(textbook2));
}
}
BookTester.main(null)
Book Title: Introduction to Java
Book Info: Introduction to Java-29.99
Textbook Title: Data Structures and Algorithms
Textbook Edition: 2
Textbook Info: Data Structures and Algorithms-49.99-2
Can substitute for textbook2? true
Notes
super
is used to call the constructor of the superclassthis
is used to call the constructor of the current class- research primitive and wrapper differences
Reflection
FRQ 1
Constructor with Parameter Validation: The code demonstrates how to create a constructor that validates its parameters. This ensures that the object is created with valid initial values, in this case, a positive line width and a non-null message. Learning from this includes understanding the importance of parameter validation in constructors to maintain object integrity.
Calculating Number of Lines: The numberOfLines method calculates the number of lines needed to display the message based on the specified line width. This involves iterating through the message and counting how many lines are required. This learning point highlights the use of methods to perform calculations or operations based on object properties.
Generating Lines for Display: The getLines method generates lines of text based on the specified line width. It uses a StringBuilder to construct the lines and handles cases where the message length exceeds the line width. This part of the code teaches about string manipulation and how to format text for display.
Main Method and Testing: The main method is used to create an instance of the Sign class and test its functionality by printing the number of lines needed for the message and displaying the generated lines. This reinforces the concept of using a main method for testing and running the program.
FRQ 2
Inheritance and Constructors: The code demonstrates inheritance in Java, where the Textbook class extends the Book class. Inheritance allows Textbook to inherit the fields and methods of Book, and the use of super() in the constructor of Textbook initializes the fields inherited from Book. This reflects a key concept in object-oriented programming for code reusability and organization.
Method Overriding: The getBookInfo() method is overridden in the Textbook class to include additional information about the edition. This showcases method overriding, where a subclass provides a specific implementation for a method inherited from its superclass. In this case, Textbook adds edition information to the book info.
Polymorphism: The canSubstituteFor() method in the Textbook class demonstrates polymorphism by accepting a parameter of type Textbook (specific type) and comparing it with the current Textbook instance. This method checks if one textbook can substitute for another based on their titles and editions. Polymorphism allows for flexibility and code reuse by treating objects of different subclasses in a unified way.
Testing in BookTester: The BookTester class is used to test the functionality of Book and Textbook objects. It creates instances of these classes and calls their methods to print information and test the canSubstituteFor() method. This reflects good software testing practices by using a separate testing class.
Encapsulation: Both Book and Textbook classes encapsulate their data (title, price, edition) by making the fields private and providing getter methods (getTitle(), getEdition()) to access the data. Encapsulation helps in maintaining data integrity and hiding implementation details.