My issue: I've written comments in the code as indicated with "????" of instructions in the problem that I don't know how to code in order to execute. My code: import java.util.Scanner; public class Hailstone { /* Type your code here. */public int hailstone(int num) { int i=1; while(num != 1) { //as the instruction says in the 3rd bullet point "Continue until num is 1" if(num % 2 == 0){ //divisible by an even number num = num/2; //1st bullet point expression assignment } else { //if odd number, then this num = num*3+1; //2nd bullet point expression assignment } i++; //if the number is not 1, it continues //???? I want to add an if statement that stops at 10 digits as specify in the instructions, but don't know how to } System.out.print(num + "\t"); /*???? I'm assuming it would print out the 10 digits, if not I do not how to do so or would it create an infinite loop? }
My issue:
I've written comments in the code as indicated with "????" of instructions in the problem that I don't know how to code in order to execute.
My code:
import java.util.Scanner;
public class Hailstone {
/* Type your code here. */public int hailstone(int num) {
int i=1;
while(num != 1) { //as the instruction says in the 3rd bullet point "Continue until num is 1"
if(num % 2 == 0){ //divisible by an even number
num = num/2; //1st bullet point expression assignment
}
else { //if odd number, then this
num = num*3+1; //2nd bullet point expression assignment
}
i++; //if the number is not 1, it continues
//???? I want to add an if statement that stops at 10 digits as specify in the instructions, but don't know how to
}
System.out.print(num + "\t"); /*???? I'm assuming it would print out the 10 digits, if not I do not how to do so
or would it create an infinite loop?
}
/*my code ends here*/
//code below generated by lab/computer begins here
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Hailstone labObject = new Hailstone();
int num;
num = scnr.nextInt();
labObject.hailstone(num); // Call hailstone() to print out the hailstone sequence.
}
}
![6.34 LAB: Hailstone sequence (EO)
Given a positive integer num, the following rules will always create a sequence that ends with 1, called the hailstone sequence:
• If num is even, divide it by 2
If num is odd, multiply it by 3 and add 1 (i.e. 3*num +1)
• Continue until num is 1
Write a method, hailstone(), that takes in a positive integer as a parameter and prints the hailstone sequence starting with that integer.
Format the output so that ten integers, each separated by a tab character (\t), are printed per line. Assume the input to the program is
always positive.
The output format can be achieved as follows:
System.out.print(num + "\t");
Ex: If the program input is:
25
the output of method hailstone() is:
25
76
38
19
58
29
88
44
22
11
34
17
52
26
13
40
20
10
16
8
4
Note: Your program must define the method:
public int hailstone(int num)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fc14768d3-3359-407d-85b4-624ecac227c9%2F6346005f-1972-4ded-8066-2822500f953a%2F9iazcnf_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)