Related questions
Please help me debug my code. Thank you!
Java 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
- 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:
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!");
}
}
}
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 2 images
How would I do the loop without importing anything? I'm not supposed to import any packages for this exercise. Thank you
How would I do the loop without importing anything? I'm not supposed to import any packages for this exercise. Thank you
- Description is " A MathVector object will be passed to your method. Return its contents as a String. If you look in the file MathVector.java you'll see there is a way to output the contents of a MathVector object as a String. This makes it useful for displaying to the user. You might have noticed that there's an @override term there. That's because many objects already have a "toString()" method associated with them... because Java was designed to include them by default. Here, the override tells Java "I know, I know. You already have a toString() that you'd assign here. But it's not good enough. Here's a better one for this particular kind of object." It's a little bit like saying "Most humans have two legs. So, by default, I'll give everyone two legs. But sometimes we override that and give no legs, or just one leg to a person. And sometimes we give them four so that they can be a centaur!" To use this in a println() method, just name your object. The toString()...arrow_forwardWrite a class name FileDisplay with the following methods: Constructor: takes the name of a file as an argument displayHead: displays only the first five lines of the file’s contents. If the file contains less than 5 lines, it should display the file’s entire contents. displayContents: displays the entire contents of the file, the name of which was passed to the constructor. writeNew: displays the contents of a file, the name of which was passed to the constructor. Each line should be preceded with a line number followed by a colon. The line numbering should start at 1. As each line is displayed it should also be written to a new file, the name of which is passed in as a parameter. I have provided a test file called "testfile.txt". I have also provided a driver for the application called "filedisplaytest.java". This file is complete and should not be altered. Test File: This is line one.This is line two.This is line three.This is line four.This is line five.This is line...arrow_forwardWhen comparing two objects using the equals method inherited from the Object class, what is actually compared is the contents of the objects in java. True or Falsearrow_forward
- PLEASE DONT COPY OFF OTHER POSTS CODE help with java..plzz paste indented code add comments tooarrow_forwardJAVA PPROGRAM ASAP Please Modify this program ASAP BECAUSE IT IS HOMEWORK ASSIGNMENT so it passes all the test cases. It does not pass the test cases when I upload it to Hypergrade. Because RIGHT NOW IT PASSES 0 OUT OF 1 TEST CASES. I have provided the failed the test cases and the inputs as a screenshot. The program must pass the test case when uploaded to Hypergrade. import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String fileName; System.out.println("Please enter the file name or type QUIT to exit:"); fileName = keyboard.nextLine().trim(); while (!fileName.equalsIgnoreCase("QUIT")) { File file = new File(fileName); if (file.exists()) { int wordCount = countWords(file); System.out.println("Total number of words: " + wordCount + "\n);...arrow_forwardPythonarrow_forward
- JAVA PPROGRAM ASAP Please Modify this program ASAP BECAUSE IT IS HOMEWORK ASSIGNMENT so it passes all the test cases. It does not pass the test cases when I upload it to Hypergrade. Because RIGHT NOW IT PASSES 1 OUT OF 7 TEST CASES. I have provided the failed the test cases as a screenshot. The program must pass the test case when uploaded to Hypergrade. import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String fileName; while (true) { System.out.println("Please enter the file name or type QUIT to exit:"); fileName = keyboard.nextLine().trim(); if (fileName.equalsIgnoreCase("QUIT")) { break; // Exit the loop when "QUIT" is entered } File file = new File(fileName); if (file.exists()) { int wordCount = countWords(file);...arrow_forwardPls help ASAParrow_forwardWrite an application named, Lab14.java. This application will do the following: 1. Display an introductory message 2. Choose a random word from the file, wordlist.txt, and display it 3. Disguise the selected word, and display it 4. Ask the user whether to continue, i.e., whether to repeat steps 2, 3, and 4, or quit the application Once the user chooses to stop the application: 5. Display the total number of vowels hidden 6. Display the total number of consonants hidden 7. Display termination message The user will be asked whether to continue playing and will indicate that another game is to be played by answering ‘y’ or ‘Y’ in response to the question, "Want to play again?" asked by the program after displaying each chosen word and its disguised version. If the user’s response is any character other than ‘y’ or ‘Y’, the totals are displayed and then the application termination message is displayed. See examples below. About randomly choosing a word from the file, wordlist.txt, found on...arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY