Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 notnulland 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 theAddtionPatternclass, which generates a number pattern. TheAdditionPatternobject 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.

TheAdditionPatternclass also supports the following methods.

  • currentNumber,which returns the current number in the pattern
  • next,which moves to the next number in the pattern
  • prev,which moves to the previous number in the pattern or takes no action if there is no previous number

The following table illustrates the behavior of anAdditionPatternobject that is instantiated by the following statement.

AdditionPattern plus3 = new AdditionPattern(2, 3);

image text in transcribed
Method Call Value Returned (blank if no value) Explanation The current number is initially the starting number in the p1us3.currentNumber(); 2 pattern. lus3 next( ) I The pattern adds 3 each time. so move to the next number p I ' in the pattern (5). p1us3 . currentNumber( ); 5 The current number is 5. lus3 next( ) I The pattern adds 3 each time. so move to the next number p I ' in the pattern (8). lus3 next( ) I The pattern adds 3 each time. so move to the next number p ' ' in the pattern (11). p1us3 . currentNumber( ); 11 The current number is 11. p1us3 . prev( ) : Move to the previous number in the pattern (8). p1us3 . p rev( ) : Move to the previous number in the pattern (5). p1us3 . p rev( ) ; Move to the previous number in the pattern (2). p1us3 . currentNumber( ); 2 The current number is 2. lus3 rev( ) I There is no previous number in the pattern prior to 2. so no p ' p ' action is taken. p1us3 . currentNumber( ); 2 The current number is 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transport Operations

Authors: Allen Stuart

2nd Edition

978-0470115398, 0470115394

Students also viewed these Programming questions

Question

What is the advantage of LMM over HJM?

Answered: 1 week ago