// Example is from the AP CSA FRQ 2023, Question 2 but modified a little
import java.util.ArrayList;
public class Sign {
private String message;
private int lineWidth;
public Sign(String message, int lineWidth) { // Constructors using POJOs
this.message = message;
this.lineWidth = lineWidth;
}
public int numberOfLines() {
int lines = message.length() / lineWidth;
if (message.length() % lineWidth > 0)
lines++;
return lines;
}
public String getLines() {
if (message.length() == 0)
return null;
String lines = "";
String messageRemaining = message;
while (messageRemaining.length() > lineWidth) {
lines += messageRemaining.substring(0, lineWidth);
lines += ";";
messageRemaining = messageRemaining.substring(lineWidth);
}
lines += messageRemaining;
return lines;
}
public static void main(String[] args) { // Example of managing a collection of Sign objects
ArrayList<Sign> signs = new ArrayList<>();
signs.add(new Sign("Hey guys this is an example", 20)); // Create and add Sign objects to the collection
signs.add(new Sign("of a POJO in a collection", 15));
for (Sign sign : signs) { // Print information for each Sign object
System.out.println("Message: " + sign.message);
System.out.println("Number of Lines: " + sign.numberOfLines());
System.out.println("Lines:");
System.out.println(sign.getLines());
System.out.println();
}
}
}
Sign.main(null)
Message: Hey guys this is an example
Number of Lines: 2
Lines:
Hey guys this is an ;example
Message: of a POJO in a collection
Number of Lines: 2
Lines:
of a POJO in a ;collection
One important thing to note:
- POJOs aren’t always required for the CSA FRQ and sometimes aren’t even in the requirements for the question, however, it is something that you should know and should have as one of you different techniques to approaching code especially when it comes to working code using OOP and also serves a good way to understand/show understanding of Java better and also, the AP Exam always accepts the initialization of variable through POJO and its a good habit to have
Serialization and Deserialization (not really a focus on the AP Exam)
-
Serialization and Deserialization is the conversion of an object into a more storable or transmittable form of data (serialization) and then converting those objects back to a Java Objects
-
Examples would be: JSON and XML
Here is a diagram showing a general idea of how it works:
While not on the AP Exam, they are a very important part of Java programming and a basic understanding can help alot
Encapsulation and Access Control
- One of the things that the AP Exam commonly makes sure that you know and have during the FRQ section in teh encapsulation and access control
Encapsulation:
-
Encapsulation is one of the 4 main OOP concepts that the AP CSA Exams tests on (the others are inheritance, polymorphism and abstraction)
-
Encapsulation is done through using classes and access modifiers
-
Ways you might be asked to do this or could see this is through defining private fields and providing setters and getters and other public methods to access and modify those fields
-
Encapsulation allows for protection against other parts of the program by preventing them to modify it
Access Control
-
Determines what is visible and what isn’t in a program and protects data integrity
-
The exam might ask questions that will cause you to decide and apply the right access modifiers (private, public) and the MC section might even test your ability to determine which ones are incorrect and why
-
Declaring the right Access Controls can account for 4/10 of the points on the FRQ and so it is very important that you understand what and why each access control is used
Example:
import java.util.ArrayList;
public class Sign {
private String message; // private variables, encapsulates the 'Sign' object, hiding it from other classes
private int lineWidth;
public Sign(String message, int lineWidth) { // Constructors using POJOs
this.message = message;
this.lineWidth = lineWidth;
}
public int numberOfLines() { // public method that provides controlled access to the private data members
int lines = message.length() / lineWidth;
if (message.length() % lineWidth > 0)
lines++;
return lines;
}
public String getLines() { // public method that provides controlled access to the private data members
if (message.length() == 0)
return null;
String lines = "";
String messageRemaining = message;
while (messageRemaining.length() > lineWidth) {
lines += messageRemaining.substring(0, lineWidth);
lines += ";";
messageRemaining = messageRemaining.substring(lineWidth);
}
lines += messageRemaining;
return lines;
}
}