AP CSA 2023 Q2 - Sign Class
| <-- Back to Solution of Q1 (AppointmentBook) - FRQ - 2023 | Next to Solution of Q3 (WeatherData) - FRQ - 2023 --> |
Solution of Q2 (Sign) - Free Response Question - 2023
The original question can be found at: https://apcentral.collegeboard.org/media/pdf/ap23-frq-comp-sci-a.pdf
Sign class
public class Sign {
private String message;
private int width;
public Sign(String message, int width)
{
this.message= message;
this.width= width;
}
public int numberOfLines()
{
int lines=1;
String newMsg = message;
if (message.isEmpty()) return 0;
else
{
while (newMsg.length() > width)
{
newMsg = newMsg.substring(width);
lines++;
}
}
return lines;
}
public String getLines()
{
String lines=null;
String newMsg = message;
if (message.isEmpty()) return "null";
else
{
while (newMsg.length() > width)
{
if (lines!=null) lines = lines+ newMsg.substring(0,width)+";";
else lines = newMsg.substring(0,width)+";";
newMsg = newMsg.substring(width);
}
if (lines!=null) lines = lines+ newMsg.substring(0);
else lines = newMsg.substring(0);
}
return lines;
}
}
Java project files (with Runner code):
| <-- Back to Solution of Q1 (AppointmentBook) - FRQ - 2023 | Next to Solution of Q3 (WeatherData) - FRQ - 2023 --> |