Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions 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
Check Mark
Still need help?
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
by Bartleby Expert
SEE SOLUTION
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
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
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.
Similar questions
    Recommended textbooks for you
    Text book image
    Computer Networking: A Top-Down Approach (7th Edi...
    Computer Engineering
    ISBN:9780133594140
    Author:James Kurose, Keith Ross
    Publisher:PEARSON
    Text book image
    Computer Organization and Design MIPS Edition, Fi...
    Computer Engineering
    ISBN:9780124077263
    Author:David A. Patterson, John L. Hennessy
    Publisher:Elsevier Science
    Text book image
    Network+ Guide to Networks (MindTap Course List)
    Computer Engineering
    ISBN:9781337569330
    Author:Jill West, Tamara Dean, Jean Andrews
    Publisher:Cengage Learning
    Text book image
    Concepts of Database Management
    Computer Engineering
    ISBN:9781337093422
    Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
    Publisher:Cengage Learning
    Text book image
    Prelude to Programming
    Computer Engineering
    ISBN:9780133750423
    Author:VENIT, Stewart
    Publisher:Pearson Education
    Text book image
    Sc Business Data Communications and Networking, T...
    Computer Engineering
    ISBN:9781119368830
    Author:FITZGERALD
    Publisher:WILEY