Make a java class called sequence which prompts the user


Task 1. Make a Java class called Sequence which prompts the user for a single integer value called n, and reports the sequence of integers generated by the following rules, until1 the computed value becomes 1:

  • if n is even, the next value in the sequence is half of n if n is odd, the next value in the sequence is 3n+1

At the end of the program, the number of values in the sequence starting from the original n, and going to the value 1 should be reported, as shown below. If the user enters a value of n which is non-positive, then your program should simply output the message shown below in Sample 3 below and end.

Notes on spacing:

  • Each colon output by the program should be followed by a single space character.
  • Each value in the sequence output by your program should be followed by a single space,
  • including the final 1 value.
  • The Total number of values: line starts on a new line, and after the value is printed,
  • ends with a newline character.
  • The message The entered value was not positive., when needed, starts on a new
  • line, and ends with a newline character.
  • Here are three separate sample executions, with user input shown in bold:
  • SAMPLE 1
  • Please enter a positive starting value for n: 5 Sequence: 5 16 8 4 2 1
  • Total number of values: 6
  • SAMPLE 2
  • Please enter a positive starting value for n: 7 Sequence: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Total number of values: 17
  • SAMPLE 3
  • Please enter a positive starting value for n: -3 The entered value was not positive.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Make a java class called sequence which prompts the user
Reference No:- TGS02850668

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)