Unit 5-FRQ
Unit 5-FRQ
Unit 5 FRQ
1. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
This question involves the implementation of the AddtionPattern class, which generates a number pattern.
The AdditionPattern object is constructed with two positive integer parameters, as described below.
The first positive integer parameter indicates the starting number in the pattern.
The second positive integer parameter indicates the value that is to be added to obtain each subsequent number in
the pattern.
The following table illustrates the behavior of an AdditionPattern object that is instantiated by the following
statement.
Unit 5 FRQ
Value Returned
Method Call (blank if no Explanation
value)
The current number is initially the starting
plus3.currentNumber(); 2
number in the pattern.
The pattern adds 3 each time, so move to
plus3.next();
the next number in the pattern (5).
plus3.currentNumber(); 5 The current number is 5.
The pattern adds 3 each time, so move to
plus3.next();
the next number in the pattern (8).
The pattern adds 3 each time, so move to
plus3.next();
the next number in the pattern (11).
plus3.currentNumber(); 11 The current number is 11.
Move to the previous number in the pattern
plus3.prev();
(8).
Move to the previous number in the pattern
plus3.prev();
(5).
Move to the previous number in the pattern
plus3.prev();
(2).
plus3.currentNumber(); 2 The current number is 2.
There is no previous number in the pattern
plus3.prev();
prior to 2, so no action is taken.
plus3.currentNumber(); 2 The current number is 2.
Write the complete AdditonPattern class. Your implementation must meet all specifications and conform to
all examples.
Unit 5 FRQ
2. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
The following class represents a customer. The variable name represents the name of the customer, and the variable
currAccNum represents the customer’s account number. Each time a Customer object is created, the static variable
nextAccNum is used to assign the customer’s account number.
public Customer(String n)
{
name = n;
currAccNum = nextAccNum;
nextAccNum++;
}
}
(a) Write a method for the Customer class that that will return a string representing a bill notice when passed a
double value representing an amount due.
For example, if the customer has name "Jeremiah", has account number 3, and has amount due 50.50, the method
should return a string in the following format.
Write the method below. Your implementation must conform to the example above.
(b) Write a method for the Customer class that returns the value of the next account number that will be assigned.
(c) A student has written the following method to be included in the Customer class. The method is intended to
update the name of a customer but does not work as intended.
Unit 5 FRQ
{
name = name;
}
Write a correct implementation of the updateName method that avoids the error in the student’s implementation.
Unit 5 FRQ
3. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
• Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
• Unless otherwise noted in the question, assume that parameters in method calls are not null and that
methods are called only when their preconditions are satisfied.
• In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to one
of these methods will not receive full credit.
This question involves methods that distribute text across lines of an electronic sign. The electronic sign and the
text to be displayed on it are represented by the Sign class. You will write the complete Sign class, which
contains a constructor and two methods.
The Sign class constructor has two parameters. The first parameter is a String that contains the message to
be displayed on the sign. The second parameter is an int that contains the width of each line of the sign. The
width is the positive maximum number of characters that can be displayed on a single line of the sign.
A sign contains as many lines as are necessary to display the entire message. The message is split among the lines
of the sign without regard to spaces or punctuation. Only the last line of the sign may contain fewer characters
than the width indicated by the constructor parameter.
The following are examples of a message displayed on signs of different widths. Assume that in each example, the
sign is declared with the width specified in the first column of the table and with the message "Everything
on sale, please come in", which contains characters.
The numberOfLines method returns an int representing the number of lines needed to display the text on
the sign. In the previous examples, numberOfLines would return 3, 2, and 1, respectively, for the sign
widths shown in the table.
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
Unit 5 FRQ
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.
The following table contains a sample code execution sequence and the corresponding results. The code execution
sequence appears in a class other than Sign.
Unit 5 FRQ
Method Call
Statement Return Value Explanation
(blank if none)
String str;
int x;
The message for sign1 contains
Sign sign1 = new
characters, and the sign has lines of width
Sign("ABC222DE", 3);
.
The sign needs three lines to display the
x =
3 -character message on a sign with lines of
sign1.numberOfLines();
width .
str = Semicolons separate the text displayed on
"ABC;222;DE"
sign1.getLines(); the first, second, and third lines of the sign.
str = Successive calls to getLines return
"ABC;222;DE"
sign1.getLines(); the same value.
The message for sign2 contains
Sign sign2 = new
characters, and the sign has lines of width
Sign("ABCD", 10);
.
The sign needs one line to display the
x =
1 -character message on a sign with lines of
sign2.numberOfLines();
width .
str = No semicolon appears, since the text to be
"ABCD"
sign2.getLines(); displayed fits on the first line of the sign.
The message for sign3 contains
Sign sign3 = new
characters, and the sign has lines of width
Sign("ABCDEF", 6);
.
The sign needs one line to display the
x =
1 -character message on a sign with lines of
sign3.numberOfLines();
width .
str = No semicolon appears, since the text to be
"ABCDEF"
sign3.getLines(); displayed fits on the first line of the sign.
Sign sign4 = new The message for sign4 is an empty
Sign("", 4); string.
x =
0 There is no text to display.
sign4.numberOfLines();
str =
null There is no text to display.
sign4.getLines();
Unit 5 FRQ
Method Call
Statement Return Value Explanation
(blank if none)
The message for sign5 contains
Sign sign5 = new
characters, and the sign has lines of width
Sign("AB_CD_EF", 2);
.
The sign needs four lines to display the
x =
4 -character message on a sign with lines of
sign5.numberOfLines();
width .
str = Semicolons separate the text displayed on
"AB;_C;D_;EF"
sign5.getLines(); the four lines of the sign.
Write the complete Sign class. Your implementation must meet all specifications and conform to the examples
shown in the preceding table.
Unit 5 FRQ
4. This question involves the design of a class that will be used to produce practice problems. The following
StudyPracticeinterface represents practice problems that can be used to study some subject.
The MultPractice class is a StudyPracticethat produces multiplication practice problems. A MultPractice object is
constructed with two integer values: first integer and initial second integer. The first integer is a value that
remains constant and is used as the first integer in every practice problem. The initial second integer is used as the
starting value for the second integer in the practice problems. This second value is incremented for each additional
practice problem that is produced by the class.
For example, a MultPractice object created with the call new MultPractice (7, 3)would be used to create the
practice problems"7 TIMES 3","7 TIMES 4","7 TIMES 5", and so on. In the MultPractice class, the getProblem
method returns a string in the format of "first integer TIMES second integer". The nextProblem method updates
the state of the MultPractice object to represent the next practice problem.
The following examples illustrate the behavior of the MultPracticeclass. Each table shows a code segment and the
output that would be produced as the code is executed.
Example 1
Unit 5 FRQ
Example 2
Write the complete MultPractice class. Your implementation must be consistent with the specifications and the
given examples.