Question

JAVA HELP: Directions Write a program that will create an array of random numbers and output...

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 = new Random();

int n = random.nextInt();
int array02[] = new int[n];

for(int i = 0; i < n; i++) {

}

}
public static void print() {

   for(int i: array02){
   System.out.println(i++);
   System.out.println(i--);
  
   }
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here is the code and the related screenshots. Please go through the comments in the code for effective understanding of the program flow. Also, do drop me a comment if you have any doubts. I'll be glad to help you out. Thank you and Good Luck!!!

There where just few changes made, I have added a new method to display the array backwards. Also, I made the array02 as static. That's all, hope this helps.

Output:

Run: ArrayBackwards x /usr/lib/jvm/java-8-oracle/bin/java ... Seed : + → IR Seed :5 Length: 1 1 Length 5 Printing the array a

Please rely heavily on the screenshots below, as the formatting gets lost when I copy the code here.

Code:

import java.util.Random; import java.util.Scanner; public class ArrayBackwards { static int[] array02; public static void generate() { // Seed info System.out.println("Seed :"); Scanner scanner = new Scanner(System.in); int seed = scanner.nextInt(); System.out.println("Seed :" + seed); System.out.println("Length :"); // Length of the array int length = scanner.nextInt(); System.out.println("Length " + length); Random random = new Random(seed); // pass the seed here array02 = new int[length]; // pass the length here for(int i = 0; i < length; i++) { array02[i] = random.nextInt(); // store the items in the array } } public static void printArray() { // simply print the array System.out.println("\nPrinting the array as is ..\n"); for (int i=0; i<array02.length; i++){ System.out.println("[" + i + "] - " + array02[i]); } } public static void printArrayBackWards() { // print the array backwards System.out.println("\nPrinting the array backwards ..\n"); int j = 0; // Just for the display purpose, you can remove this var if not required for (int i = (array02.length -1); i>=0; i--){ System.out.println("[" + j++ + "] - " + array02[i]); } } public static void main(String[] args) { generate(); // random numbers gen printArray(); // regular print printArrayBackWards(); // backward print } }

import java.util.Random; import java.util.Scanner; 2 public class ArrayBackwards { static int[] array®2; public static void gpublic static void printArrayBackwards() { // print the array backwards System.out.println(\nPrinting the array backwards ..

Add a comment
Know the answer?
Add Answer to:
JAVA HELP: Directions Write a program that will create an array of random numbers and output...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

  • need help editing or rewriting java code, I have this program running that creates random numbers...

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

  • 2.13.2: Get random numbers. JAVA Type two statements using nextInt() to print two random integers between...

    2.13.2: Get random numbers. JAVA Type two statements using nextInt() to print two random integers between (and including) 0 and 9. End with a newline. Ex: 5 7 Note: For this activity, using one statement may yield different output (due to the interpreter calling randGen.nextInt() in a different order). Use two statements for this: import java.util.Scanner; import java.util.Random; public class DiceRoll { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); Random randGen = new Random();...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

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

  • Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a...

    Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       final int NUM_VALS = 4;       int[] hourlyTemp = new...

  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

  • 1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the...

    1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. What i am given: import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS =...

  • Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...

    Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class RQS { public static int[] prepareData(int[] patients){ /* Data is prepared by inserting random values between 1 and 1000. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ // what is our range? int max = 1000; // create instance of Random class Random randomNum...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT