I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n).
First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that?
Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ?


Answer First :
You miss the else condtion in your while loop which should be execute when n = 0.
So when you are not using break statement for while loop then the code after while loop is unreachable.
You should give a condition in loop after that program can be continue.
I make this for you now reader.close() is reachable.
// yout main method
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter the size of grid : ");
do {
try {
int n = reader.nextInt();
if (n < 0) {
System.out.println(
n + " is not a positive integer. Please enter a apositive integer (0 to terminate): ");
} else if (n > 0) {
walk(n);
System.out.println("\nEnter the size of grid: ");
} else {
break;
}
} catch (InputMismatchException e) {
System.out.println("Input is not an integer. Please enter a positive (0 to terminate):");
reader.nextLine();
}
} while (true);
reader.close();
}
Answer Second :
For use color you can use different color for StdDraw.point()
example :
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.point(n+1, n-1);
StdDraw.setPenColor(StdDraw.MAGENTA);
I have the following java-program, that creates a random walk starting at (0,0) and continuing until...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Welcome to this Odd program!"); int userInput1 = input.nextInt(); } public...
In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...
Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count { public static void main(String args[]) { int n = getInt("Please enter an integer value greater than or equal to 0"); System.out.println("Should count down to 1"); countDown(n); System.out.println(); System.out.println("Should count up from 1"); countUp(n); } private static void countUp(int n) {...
JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; } public static void main(String[] args) { int n, i, k; System.out.println("Enter...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in); System.out.println("Seed:"); int seed = scanner.nextInt(); System.out.println("Length"); int length = scanner.nextInt(); Random random...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
the code needs to be modifed. require a output for the
code
Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...
DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one just syntax issues. //start import java.util.Scanner; public class NumbersDemo { public static void main (String args[]) { Scanner kb = new Scanner(System.in); int num1, num2; System.out.print("Enter an integer >> "); num1 = kb.nextInt(); System.out.print("Enter another integer >> "); num2 = kb.nextDouble(); displayTwiceTheNumber(num1); displayNumberPlusFive(num1); displayNumberSquared(num1) displayTwiceTheNumber(num2); displayNumberPlusFive(num2); displayNumberSquared(num2); } ...