Question: Write a method called writeSequence that accepts an integer n as a parameter and prints to the console a symmetric sequence of n numbers composed

Write a method called writeSequence that accepts an integer n as a parameter and prints to the console a symmetric sequence of n numbers composed of descending integers that ends in 1, followed by a sequence of ascending integers that begins with 1. The following table indicates the output that should be produced for various values of n:

Method call Output produced writeSequence (1); 1 writeSequence (2); 1 1 writeSequence (3); 2 1 2 writeSequence (4); 2 1 1 2 writeSequence (5); 3 2 1 2 3 writeSequence (6); 32 112 3 writeSequence (7); 4 3 2 1 2 3 4 writeSequence (8); 4 3 21 1 2

Notice that when is odd the sequence has a single 1 in the middle, whereas for even values it has two 1s in the middle. Your method should throw an IllegalArgumentException if it is passed a value less than 1.

Method call Output produced writeSequence (1); 1 writeSequence (2); 1 1 writeSequence (3); 2 1 2 writeSequence (4); 2 1 1 2 writeSequence (5); 3 2 1 2 3 writeSequence (6); 32 112 3 writeSequence (7); 4 3 2 1 2 3 4 writeSequence (8); 4 3 21 1 2 3 4 writeSequence (9); 5 4 3 2 1 2 3 4 5 writeSequence (10); 5 4 3 2 1 1 2 3 4 5

Step by Step Solution

3.37 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public void writeSequence int n if n 1 bet throw ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!