Issue:  I don't know how to print 10 integers in a row, add then onto a new line, and repeat this pattern until it fill out to 25 integers as displayed in the instructions   Java Code:  import java.util.Scanner; public class Hailstone { /* Type your code here. */ public int hailstone(int num) { int i=1; if (num == 1) { System.out.println("1"); return 1; } else { System.out.print(num + "\t"); if (num %2==0) return 1 + hailstone(num/2); else return 1 + hailstone(3*num +1); } } /* for (int i=0; i < 10; i++) { --> I need to print 10 integers in a row, follow by a new line until it reaches 25 integers as displayed in the instructios. I don't know where to put this for loop. */ 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. } }

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

Issue: 

I don't know how to print 10 integers in a row, add then onto a new line, and repeat this pattern until it fill out to 25 integers as displayed in the instructions

 

Java Code: 

import java.util.Scanner;

public class Hailstone {

/* Type your code here. */ public int hailstone(int num) {
int i=1;

if (num == 1) {
System.out.println("1");
return 1;
}
else {
System.out.print(num + "\t");
if (num %2==0)
return 1 + hailstone(num/2);
else
return 1 + hailstone(3*num +1);
}
}

/* for (int i=0; i < 10; i++) { --> I need to print 10 integers in a row, follow by a new line until it reaches 25 integers as
displayed in the instructios. I don't know where to put this for loop. */

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

 

Output is nearly correct; but whitespace differs. See highlights below.
Special character legend
Input
25
22
11
34
17
52
26
13
40
20
1
Your output
76
38
19
58
29
88
44
22
11
17
52
26
13
40
20
10
16
Expected output
4
2
1
2: Compare output ^
2/2
Input
1
Your output
1
3: Compare output ^
2/2
Input
512
512
256
128
64
32
16
8
4
1
Your output
Transcribed Image Text:Output is nearly correct; but whitespace differs. See highlights below. Special character legend Input 25 22 11 34 17 52 26 13 40 20 1 Your output 76 38 19 58 29 88 44 22 11 17 52 26 13 40 20 10 16 Expected output 4 2 1 2: Compare output ^ 2/2 Input 1 Your output 1 3: Compare output ^ 2/2 Input 512 512 256 128 64 32 16 8 4 1 Your output
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
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
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