a Programming exercise: Create a class called Collatz Create the main method Inside, create these variables: Create an int called collatzNum and assign it any positive integer from 1-100 (Please refer to the PE Clarification Thread on Ed for example output.) Create an int called numSteps and assign it 0. The number of “steps” corresponds to the number of times you apply one of the operations. Create an int called highestNumReached and assign it the value collatzNum Create an int called initValue and assign it the value collatzNum Print Create a while loop that terminates when collatzNum is equal to 1 6. Within the while loop, use an if-else statement to do the following: If collatzNum is even, collatzNum should be assigned half of its current value Otherwise, (if it’s odd), collatzNum should be three times its value plus one. Check whether the new collatzNum is greater than If so, update its value to collatzNum Print out collatzNum and add 1 to numSteps Close the while loop After the while loop, use three print statements to output: “Initial value: [initValue]” “Number of steps: [numSteps]” “Highest number reached: [highestNumReached]” Finally, use a switch statement to print one of the following statements according to the value of numSteps: 0 steps: “No steps required” 1 step: “Only took one step!” 2 steps: “Two steps” 3 steps: “Three steps” 4 steps: “Four steps” All other amounts: “Wow, [numSteps] steps was a lot of steps!” (For example, if 8 steps were taken, “Wow, 8 steps was a lot of steps!” should be printed without the quotes.) Example Outputs Please refer to the PE clarification thread on Ed for examples. HINT: To help debug your code, try inserting print statements in places where variables are changed. My Code:

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Please help me debug my code. Thank you!

Java Programming exercise:

  1. Create a class called Collatz
  2. Create the main method
  3. Inside, create these variables:
    1. Create an int called collatzNum and assign it any positive integer from 1-100 (Please refer to the PE Clarification Thread on Ed for example output.)
    2. Create an int called numSteps and assign it 0. The number of “steps” corresponds to the number of times you apply one of the operations.
    3. Create an int called highestNumReached and assign it the value collatzNum
    4. Create an int called initValue and assign it the value collatzNum
  4. Print
  5. Create a while loop that terminates when collatzNum is equal to 1 6. Within the while loop, use an if-else statement to do the following:
    1. If collatzNum is even, collatzNum should be assigned half of its current value
    2. Otherwise, (if it’s odd), collatzNum should be three times its value plus one.
  6. Check whether the new collatzNum is greater than If so, update its value to collatzNum
  7. Print out collatzNum and add 1 to numSteps
  8. Close the while loop
  9. After the while loop, use three print statements to output:
    1. “Initial value: [initValue]”
    2. “Number of steps: [numSteps]”
    3. “Highest number reached: [highestNumReached]”
  10. Finally, use a switch statement to print one of the following statements according to the value of numSteps:
    1. 0 steps: “No steps required”
    2. 1 step: “Only took one step!”
    3. 2 steps: “Two steps”
    4. 3 steps: “Three steps”
    5. 4 steps: “Four steps”
    6. All other amounts: “Wow, [numSteps] steps was a lot of steps!” (For example, if 8 steps were taken, “Wow, 8 steps was a lot of steps!” should be printed without the quotes.)

Example Outputs

Please refer to the PE clarification thread on Ed for examples.

HINT: To help debug your code, try inserting print statements in places where variables are changed.

My Code:

 

public class Collatz {
    
    /**
     * Main mehtod.
     */
    public static void main(String[] args){
        
        /**
         *@param numStep.
         */
        Random r = new Random();
        int collatzNum = r.nextInt(100) + 1;
        int numSteps = 0;
        int highestNumReached = collatzNum;
        int initValue = collatzNum;

        /**
         * Prints collatzNum.
         */
        System.out.println(collatzNum);


        /**
         *
         * While loop that computes equation.
         */
        while (collatzNum != 1) {
            if (collatzNum %= 2)
                collatzNum = collatzNum * 2;
            }
            else if (collatzNum %= 1){
                collatzNum = 3 * collatzNum + 1;
            }
            else (collatzNum > highestNumReached) {
                highestNumReached = collatzNum;
            }
            System.out.println(collatzNum);
            numSteps ++;
        }
        
        /**
         * Printing statments.
         */
        System.out.println("Initial Value: " + initValue);
        System.out.println("Number of steps: " + numSteps);
        System.out.println("Highest number reached: " + highestNumReached);

        /**
         *
         *@param numSteps
         * Switch statement for numSteps.
         */
        switch (numSteps) {
            case 0:
                System.out.println("No steps required!");
                break;
            case 1:
                System.out.println("Only took one step!");
                break;
            case 2:
                System.out.println("Two steps!");
                break;
            case 3:
                System.out.println("Three steps!");
                break;                
            case 4:
                System.out.println("Four steps!");
                break; 
            default:
                System.out.println("Wow, " + numSteps + " steps was a lot of steps!");
        }

    
    }

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

How would I do the loop without importing anything? I'm not supposed to import any packages for this exercise. Thank you

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY