Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please follow all instructions carefully and precisely. Also use JOptionPane for

ID: 3703629 • Letter: P

Question

Please follow all instructions carefully and precisely. Also use JOptionPane for input and have the output print to the console.
For this assignment, that is all you need as the instance var's - highestValue, mostTerms, etc - must be initialized, and 0 is a reasonable initial value because all HailStone series contain only positive int's. (If the values could be _negative_, then a better initial value would be Integer.MIN_VALUE, which is the smallest int (-2,147,283,648).
(Repetition) 1. The Hailstone Series An interesting sequence of integers known as the haiistone series works like this: . Pick any starting number 2. Now, repeat the following.. If the number is odd, triple it and add one Otherwise (it's even), divide it by two ..until the number equals 4 The sequence is called the hailstone series because although the numbers bob up and down, they eventually reach a repeating "ground" state of 4. 2, 1, 4, 2, 1. For example, the series beginning at 13 has eight terms: 13 40 20 10 5 16 8 4 t. The HailstoneSeries Class Write HailstoneSeries class with the following methods: A constructor that creates a default HailstoneSeries object (a default constructor isonethat has no parameters) 2. A method that generates the series beginning at a specified value, passed as a parameter . A method that returns a multi-line string containing: . The largest number of terms in any series the starting value of the series with the largest number of terms .the highest value reached in any series the starting value of the series with the highest value 1I1. Your Test Class The main method of your test class will: i. create a HaiistoneSeries object 2. accept two ints from the user and print them, pro . Eor eac

Explanation / Answer

Hi

package hailstone;

public class HailstoneSeries {

int maxNumberOfTerms = 1;
int startValueOfSeriesWithMaxTerms = 0;
int highestValueReached = 0;
int startValueOfSeriesWithHighestValue = 0;

public HailstoneSeries() {
}

public void generateSeries(int number) {
int startNumber = number;
int numberOfTerms = 1;
System.out.print(number + " ");
while (number != 4) {
if (number % 2 == 0) {
number /= 2;
} else {
number = 3 * number + 1;
}
System.out.print(number + " ");
numberOfTerms++;

// if the number is more than highestValueReached then assign the number becomes highesValueReached and start number for that series is stored in startValueOfSeriesWithHighestValue
if (highestValueReached < number) {
highestValueReached = number;
startValueOfSeriesWithHighestValue = startNumber;
}
}

// if numberOfTerms in current series is more than maxNumberOfTerms then numberOfTerms becmes the maxNumberOfTerms and startValueOfSeriesWithMaxTerms is assigned the start value of current series.
if (maxNumberOfTerms < numberOfTerms) {
maxNumberOfTerms = numberOfTerms;
startValueOfSeriesWithMaxTerms = startNumber;
}
}

public String generateResultString() {
String resultString = "The largest number of terms in any series: " + maxNumberOfTerms +
" The starting value of the series with the largest number of terms: " +
startValueOfSeriesWithMaxTerms + " The Highest Value reached in any series: " +
highestValueReached + " The starting value of the series with the highest value: " +
startValueOfSeriesWithHighestValue;
return resultString;
}

}

package hailstone;

//import java.util.Scanner;

import javax.swing.JOptionPane;

public class HailstoneSeriesTest {

public static void main(String[] args) {
/* Scanner scanner = new Scanner(System.in);
System.out.println("Enter start and end value: ");
int startValue = scanner.nextInt();
int endValue = scanner.nextInt();*/
HailstoneSeries hailstoneSeries = new HailstoneSeries();
int startValue = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter start value"));
int endValue = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter end value"));
int count =1;
for(int i=startValue; i<=endValue; i++) {
System.out.println();
System.out.print("Series "+count+":");
hailstoneSeries.generateSeries(i);
count++;
}
System.out.println();
System.out.println("The result string is: "+hailstoneSeries.generateResultString());
}
}

Note that I have written the test code with both Scanner and JOptionPane for the input (with Scanner part commented).

Output:

Enter start and end value:

10
20
Series 1:10 5 16 8 4
Series 2:11 34 17 52 26 13 40 20 10 5 16 8 4
Series 3:12 6 3 10 5 16 8 4
Series 4:13 40 20 10 5 16 8 4
Series 5:14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4
Series 6:15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4
Series 7:16 8 4
Series 8:17 52 26 13 40 20 10 5 16 8 4
Series 9:18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4
Series 10:19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4
Series 11:20 10 5 16 8 4
The result string is: The largest number of terms in any series: 19
The starting value of the series with the largest number of terms: 18
The Highest Value reached in any series: 160
The starting value of the series with the highest value: 15

You can run the code giving values (100-1000).

Note that it prints the result string for the highest and largest values for the first series if multiple series have the same number.

You can ask if you need the numbers for all the series with equal numbers.

Also, if you don't understand any part of the code, kindly ask :)

Please rate if this solves your query :)

Thanks.