Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

lalokio

  • one year ago

Hello! I Need help writing a small java program... This is what it's suppose to do 1. Write a program RandomArrayAnalyzer to randomly generate 20 numbers between 0 and 100 (inclusive), then print out the 20 numbers as follows: 20 | 15 | 5|…… Then print the sum of the 20 numbers, average , minimum and maximum number.

  • This Question is Closed
  1. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so far i have this /** * Generates 20 random numbers and then returns average and maximum values. */ public class RandomDataAnalyzer { public static void main(String [] args) { // Creates constructor and sets value to 1000. DataSet numbers = new DataSet(100); // Creates loop and increaments number generated by one until there are 100 generated between 0 and 1000. final int count = 20; for(int i = 1; i <= count; i++) { // Generates random numbers. int randomize = numbers.generate(); // Displays all random numbers generated. System.out.println(randomize + " "); // Adds all numbers. numbers.add(randomize); } // Prints average. System.out.print("The average is: " + numbers.getAverage()); // Prints maximum value from the 100 random generated numbers. System.out.println("The maximum is: " + numbers.getMaximum()); } }

  2. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I need to change this so it will use arrays instead.

  3. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    how can i print the sum of the random numbers being generated? numbers.add(randomize);

  4. eSpeX
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Why not just print with a System.out.println("Your random sum: " + randomize); statement?

  5. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I tried that but got this error System.out.println("Your random sum: " + randomize); Error: /Users/lalo/Dropbox/Notebook/ITEC 2120 - INTRO TO JAVA/PROJECTS/8/RandomDataAnalyzer.java:36: cannot find symbol symbol : variable randomize location: class RandomDataAnalyzer

  6. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think problem might be in my class public int add(int x) { sum = sum + x; if (count == 100 || maximum < x) maximum = x; count++; return (x); }

  7. eSpeX
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Troubleshooting your problem is going to be difficult without the entirety of your code. If you are calling your randomize variable from outside a class that does not have access to the variable, then it will not be able to find said variable. It also appears that you are declaring your randomize variable from within your 'for' loop, and re-declaring it with each pass. This variable will not have scope outside of the loop, so while you are within the loop you are able to print its value because you have local access to it but outside randomize no longer exists.

  8. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.util.Random; public class DataSet { private double x; private double sum; private double count; private double average; private double maximum; private Random generator; public DataSet(int randomnum) { // initialise instance variables x = randomnum; generator = new Random(); } /** Adds values. @return int x value randomly generated. If statement sets maximum value when x is less than max. */ public void add(int x) { sum = sum + x; if (count == 100 || maximum < x) maximum = x; count++; } /** @return Maximum value. */ public double getMaximum() { return maximum; } /** Calculate average from all 100 numbers generated. @return average value. */ public double getAverage() { average = (sum/100); return average; } public int generate() { return 1 + generator.nextInt((int)x); } }

  9. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    /** * Generates 20 random numbers and then returns average and maximum values. */ public class RandomDataAnalyzer { public static void main(String [] args) { // Creates constructor and sets value to 100. DataSet numbers = new DataSet(100); // Creates loop and increaments number generated by one until there are 20 generated numbers between 0 and 100. final int count = 20; for(int i = 1; i <= count; i++) { if (i>0) { System.out.print(" | "); } // Generates random numbers. int randomize = numbers.generate(); // Displays all random numbers generated. System.out.print(numbers.generate()); // Adds all numbers. numbers.add(randomize); } // Prints sum. System.out.println("sum" + randomize); // Prints average. System.out.println("The average is: " + numbers.getAverage()); // Prints maximum value from the 100 random generated numbers. System.out.println("The maximum is: " + numbers.getMaximum()); } }

  10. lalokio
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That's all I have. I just need to get the sum to print out now.

  11. WhoElseButMe
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    int randomize is out of scope when you're attempting to print its value.

  12. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.