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

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

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

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • Write a while loop that prints all positive numbers that are divisible by 10 and less...

    Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90. import java.util.Scanner; public class PositiveLess {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        System.out.print("n: ");        int n = in.nextInt();        ...        while (...)        {           ...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • I wrote two java classes. One is main and other one is getter and setter. I...

    I wrote two java classes. One is main and other one is getter and setter. I brought methods from Digit class to Main class to print result. However, for some reason, only welcome print statement got printed and others got ignored. On the output screen, only welcome statement appear. nDigit should be input from user. import java.util.Scanner; public class Main {    public static void main(String[] args)    {        Digit digitGet = new Digit();              ...

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
ADVERTISEMENT