Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 2 1 of 2 Items Question1 Item 1 Question 1 SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

1

2

1 of 2

Items

Question1

Item 1

Question 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 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.

A mathematical sequence is an ordered list of numbers. This question involves a sequence called ahailstone sequence. If

n

is the value of a term in the sequence, then the following rules are used to find the next term, if one exists.

  • Ifn
  • is 1, the sequence terminates.
  • Ifn
  • is even, then the next term isn
  • 2
  • .
  • Ifn
  • is odd, then the next term is3n+1
  • .

For this question, assume that when the rules are applied, the sequence will eventually terminate with the term

n=1

.

The following are examples of hailstone sequences.

Example 1: 5, 16, 8, 4, 2, 1

  • The first term is 5, so the second term is5*3+1=16
  • .
  • The second term is 16, so the third term is16
  • 2
  • =8
  • .
  • The third term is 8, so the fourth term is8
  • 2
  • =4
  • .
  • The fourth term is 4, so the fifth term is4
  • 2
  • =2
  • .
  • The fifth term is 2, so the sixth term is2
  • 2
  • =1
  • .
  • Since the sixth term is 1, the sequence terminates.

Example 2: 8, 4, 2, 1

  • The first term is 8, so the second term is8
  • 2
  • =4
  • .
  • The second term is 4, so the third term is4
  • 2
  • =2
  • .
  • The third term is 2, so the fourth term is2
  • 2
  • =1
  • .
  • Since the fourth term is 1, the sequence terminates.

TheHailstoneclass, shown below, is used to represent a hailstone sequence. You will write three methods in theHailstoneclass.

public class Hailstone

{

/**Returns the length of a hailstone sequence that starts withn,

*as described in part (a).

*Precondition:n > 0

*/

public static int hailstoneLength(int n)

{ /*to be implemented in part (a)*/ }

/**Returnstrueif the hailstone sequence that starts withnis considered long

*andfalseotherwise, as described in part (b).

*Precondition:n > 0

*/

public static boolean isLongSeq(int n)

{ /*to be implemented in part (b)*/ }

/**Returns the proportion of the firstnhailstone sequences that are considered long,

*as described in part (c).

*Precondition:n > 0

*/

public static double propLong(int n)

{ /*to be implemented in part (c)*/ }

//There may be instance variables, constructors, and methods not shown.

}

(c) The methodpropLong(int n)returns the proportion of long hailstone sequences with starting values between 1 andn,inclusive.

Consider the following table, which provides data about the hailstone sequences with starting values between 1 and 10, inclusive.

StartingValue

Terms in the Sequence

Length of theSequence

Long?

1

1

1

No

2

2, 1

2

No

3

3, 10, 5, 16, 8, 4, 2, 1

8

Yes

4

4, 2, 1

3

No

5

5, 16, 8, 4, 2, 1

6

Yes

6

6, 3, 10, 5, 16, 8, 4, 2, 1

9

Yes

7

7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

17

Yes

8

8, 4, 2, 1

4

No

9

9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

20

Yes

10

10, 5, 16, 8, 4, 2, 1

7

No

The method callHailstone.propLong(10)returns0.5,since 5 of the 10 hailstone sequences shown in the table are considered long.

Write thepropLongmethod. Assume thathailstoneLengthandisLongSeqwork as intended, regardless of what you wrote in parts (a) and (b). You must useisLongSeqappropriately to receive full credit.

/**Returns the proportion of the firstnhailstone sequences that are considered long,

*as described in part (c).

*Precondition:n > 0

*/

public static double propLong(int n)

  • Class information for this question
  • public class Hailstone
  • public static int hailstoneLength(int n)
  • public static boolean isLongSeq(int n)
  • public static double propLong(int n)

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions