Can anyone help me to write this program for me?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Can anyone help me to write this program for me?

  1. #1
    Join Date
    Apr 2006
    Posts
    2

    Can anyone help me to write this program for me?

    I have done part of the program here but not the same as what shown.
    import java.util.*;

    public class RandDemo {
    public static void main(String args[] ) {
    int accum[] = new int[6];

    System.out.println("Student id: N0604413");

    //generate random numbers in a range and tally them

    for (int i = 0; i <= 100; i++) {
    accum[RandUtils.getRange(0,accum.length - 1)]++;
    }
    //display result

    for (int i = 0; i < accum.length; i++) {
    System.out.println(i + " > " + accum[i]);
    }

    System.out.println("0123456789012345678901234567890");



    }
    }


    import java.util.*;

    class RandUtils {
    private static Random rn = new Random(0);

    //get random number in range, lo <= x <= hi

    public static int getRange(int lo, int hi) {
    //error checking

    if (lo > hi) {
    throw new IllegalArgumentException("lo > hi");
    }

    // handle special case

    if (lo == Integer.MIN_VALUE && hi == Integer.MAX_VALUE) {
    return rn.nextInt();
    }
    else {
    return rn.nextInt(hi - lo + 1) + lo;
    }
    }

    //return true perc % of the time

    public static boolean getPerc(int perc) {
    //error checking

    if (perc < 0 || perc > 100) {
    throw new IllegalArgumentException("bad perc");
    }
    return perc >= getRange(0, 100);
    }
    }
    Attached Images Attached Images
    • File Type: jpg 1.JPG (65.8 KB, 77 views)

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You post did not clearly state what error you received or how your output differs from that expected by the assignment. It looks to me that you are not yet printing the graphical representation of the occurrence of the numbers.

    What is the purpose of your contraction of the range of your random numbers?

    Why do you use the "RandUtils" class?

    When I need a random number generator I usually use an instance of Random, such as:

    Random myRandomGen = new Random();
    or
    Random myRandomGen = new Random( time ); // using current time as a seed value

    When I need an int from a range of ints which has a ceiling of a particular int, I use an instruction such as:

    nextInt = myRandomGen.nextInt( 6 );

    which will produce an int between 0 (inclusive) and 6 (exclusive). This will randomly generate ints 0, 1, 2, 3, 4, or 5.
    Last edited by nspils; 04-14-2006 at 04:31 PM.

  3. #3
    Join Date
    Apr 2006
    Posts
    20
    NOTE: THE FULL CODE IS AT THE BOTTOM OF THS POST, BUTBEFORE YOU GET THERE, I'LL TALK YOU THROUGH IT

    I'm away from my home computer right now, and have no access to a java compiler, so this will be full of bad syntax, but no doubt you can change it a little to make it work.

    To make a random number, all you need to do is

    Code:
    int randomInt=0; //creates and initialises an integer to store the random number
    Random myNewRandom = new Random(); //creates a new instance of the random class
    randomInt = myNewRandom.nextInt(5); //generates a new random number between 0 and 5
    now, to add up how many times each number occurs, you will need counters. these can easily be achieved with 6 integers and either a selection of If/Else statements, or Switch statements. I'm a fan of If/Else over Switch, so thats how I'm writing this part

    Code:
    (declare the integers 0count, 1count etc at the very start of the program)
    If (randomInt == 0) {
          0count=0count+1; //adds 1 to the counter for 0
    }
    else if (randomInt == 1) {
          1count=1count+1; //adds 1 to the counter for 1
    }
    you'll need to make an "else if" for all the other possibilities as well, all the way to "else if (randomInt == 5)"

    now, to get it to loop 100 times, you will need a "for loop".

    Code:
    for (int counter=1; counter<=100, counter++)
    {
              //insert code here
    }
    That should loop 100 times. It may loop 99 times, in which case change the first bit to "int counter=0;"

    so, in some basic pseudocode, it should look like this:
    Code:
    int count0=0;
    int count1=0;
    int count2=0;
    int count3=0;
    int count4=0;
    int count5=0;
    randomInt=0;
    Random myNewRandom = new Random();
    
    for (loop 100 times)
    {
        generate a new random number using "randomInt = myNewRandom.nextInt(5);
    
           use the if statements to check what was output and add 1 to the correct total
    
    }//end for loop
    
    then use some System.out.println to output the counters.
    to get the graphs to appear, the easiest way I can think of doing it is to use a seperate method, let's call it "drawGraph", and it takes an integer.

    so it's declared as:

    Code:
    public static void drawGraph(int output){
    
    }
    and inside the method, have this code:

    Code:
    System.out.print(output); //starts a line with the current number
    for (int count = 0; count <output; count++)
    {
         System.out.print("-"); //important that you use .print instead of .println, this will make a line with one less -'s as required
    }
    System.out.println("+"); //caps the line with a + and starts a new line
    to use this method, go back to the main method, and use the following six lines at the end:
    Code:
    drawGraph(count0); //draws the graph line for the counter of 0
    drawGraph(count1); //draws the graph line for the counter of 1
    drawGraph(count2); //draws the graph line for the counter of 2
    drawGraph(count3); //draws the graph line for the counter of 3
    drawGraph(count4); //draws the graph line for the counter of 4
    drawGraph(count5); //draws the graph line for the counter of 5
    this will pass the values of the counter through to the drawGraph method, which outputs the graph.

    So, off the top of my head, the following is what I think the code should be:

    Code:
    import java.util.*;
    
    public class RandDemo{
    
    public static void main(String args[] ){
    
    int zerocount=0;
    int onecount=0;
    int twocount=0;
    int threecount=0;
    int fourcount=0;
    int fivecount=0;
    int randomInt=0;
    Random myNewRandom = new Random();
    
    System.out.println("Student ID: 0400185");
    for (int counter = 1; counter <=100; counter++)
    {
          randomInt=myNewRandom.nextInt(6);
          if (randomInt == 0) {
                   zerocount=zerocount+1; //adds 1 to the counter for 0
          }
          else if (randomInt == 1) {
                  onecount=onecount+1; //adds 1 to the counter for 1
          }
          else if (randomInt == 2) {
                  twocount=twocount+1; //adds 1 to the counter for 2
          }
          else if (randomInt == 3) {
                  threecount=threecount+1; //adds 1 to the counter for 3
          }
          else if (randomInt == 4) {
                  fourcount=fourcount+1; //adds 1 to the counter for 4
          }
          else if (randomInt == 5) {
                  fivecount=fivecount+1; //adds 1 to the counter for 5
          } //end if
    } //end for loop
    
    for (int i=0; i<3; i++)
    {
    	System.out.print("0123456789");
    }//end for loop
    System.out.println(""); //prints a clear line
    System.out.println("0 > "+zerocount);
    System.out.println("1 > "+onecount);
    System.out.println("2 > "+twocount);
    System.out.println("3 > "+threecount);
    System.out.println("4 > "+fourcount);
    System.out.println("5 > "+fivecount);
    drawGraph(zerocount, 0);
    drawGraph(onecount, 1);
    drawGraph(twocount, 2);
    drawGraph(threecount, 3);
    drawGraph(fourcount, 4);
    drawGraph(fivecount,5);
    }//end main method
    
    public static void drawGraph(int output, int number){
        System.out.print(number + ": "); //starts a line with the current number
        for (int count = 0; count <output; count++)
        {
            System.out.print("-"); //important that you use .print instead of .println, this will make a line with one less -'s as required
        }//end for
        System.out.println("+"); //caps the line with a + and starts a new line
    }//end drawGraph method
    }//end of class
    Feel free to corrcet any syntax or errors, and check the values used i nthe for loop. Like I said, right now I have no access to a java compiler, so I can't check if this will compile and run nicely without some small fixes

    EDIT: got hold of a compiler, and my code gave me this output:

    output image
    Last edited by ftr; 04-15-2006 at 10:06 AM.

Similar Threads

  1. Replies: 1
    Last Post: 08-24-2005, 12:23 AM
  2. Replies: 7
    Last Post: 08-04-2005, 08:28 PM
  3. Replies: 0
    Last Post: 07-15-2003, 01:13 AM
  4. Can Program run without MS Access
    By Marty in forum VB Classic
    Replies: 2
    Last Post: 04-19-2001, 01:38 AM
  5. NT priveleges from an ASP program
    By Matthew Solnit in forum authorevents.appleman
    Replies: 1
    Last Post: 04-11-2000, 05:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center