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? }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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)
Transcribed Image Text: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)
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Development strategies
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education