Wanna do my HW?

Discussion in 'World of SPAM' started by Adam, Feb 4, 2009.

  1. Adam

    Adam Level III

    Joined:
    Aug 14, 2007
    Messages:
    731
    Likes Received:
    50
    If by any amazing chance, somebody looooves programming java and would like to do my assignment, please feel free :). I have 4 tests tomorrow and dont have time to mess w/ this.




    LAB # 17 - Using an array as a counter. Write a class, PercentDigit, that creates 2000 random integers between 1 and 1000 and stores these numbers directly onto numbers2000.txt (not the array). Then read the numbers from the text file and count the number of numbers that begin with a 1 (15,132,199,1), with a 2, with a 3 and so on through 9 onto a count array of size 10. Output the % of each number using DecimalFormat. You will have a separate driver program that creates a PercentDigit object and runs the program.



    public static void main(String[]args)

    {

    PercentDigit p = new PercentDigit();

    p.writeToFile();

    p.fillArray();

    System.out.println(p);

    }
     
  2. Ak*

    Ak* Level IV

    Joined:
    Dec 24, 2007
    Messages:
    1,723
    Likes Received:
    45
    Location:
    Canada Eh!
    NO >:)
     
  3. Adam

    Adam Level III

    Joined:
    Aug 14, 2007
    Messages:
    731
    Likes Received:
    50
    BUMP
    I WANT SOME1 TO DO IT :)
     
  4. Ak*

    Ak* Level IV

    Joined:
    Dec 24, 2007
    Messages:
    1,723
    Likes Received:
    45
    Location:
    Canada Eh!
    you should ask Zer0... hes does java
     
  5. Adam

    Adam Level III

    Joined:
    Aug 14, 2007
    Messages:
    731
    Likes Received:
    50
    i think heyas gonna help me
     
  6. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    ewww, I hate how teachers make you do OOP when its just plain stupid to use it.

    Code (Text):
    1.  
    2.  
    3. import java.io.*;
    4. import java.text.DecimalFormat;
    5.  
    6. public class PercentDigit {
    7.     String[] array;
    8.     DecimalFormat df = new DecimalFormat( "00.00%" );
    9.    
    10.     public void writeToFile() throws Throwable {
    11.         PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter( "numbers2000.txt" ) ) );
    12.        
    13.         for( int i = 0; i < 2000; i++ )
    14.             out.println( (int)( 1 + 1000*Math.random() ) );
    15.        
    16.         out.close();
    17.     }
    18.    
    19.     public void fillArray() throws Throwable {
    20.         BufferedReader in = new BufferedReader( new FileReader( "numbers2000.txt" ) );
    21.         array = new String[2000];
    22.        
    23.         for( int i = 0; i < 2000; i++ )
    24.             array[i] = in.readLine();
    25.     }
    26.    
    27.     public String toString() {
    28.         int[] count = new int[9];
    29.        
    30.         for( String s : array )
    31.             count[ s.charAt( 0 )-'1' ]++;
    32.        
    33.         String s = "";
    34.        
    35.         for( int i = 0; i < 9; i++ )
    36.             s += ( i+1 ) + ":\t" + df.format( count[i]/2000d ) + '\n';
    37.        
    38.         return s;
    39.     }
    40.    
    41.     public static void main( String[] args ) throws Throwable {
    42.         PercentDigit p = new PercentDigit();
    43.         p.writeToFile();
    44.         p.fillArray();
    45.         System.out.println( p );
    46.     }
    47. }
    48.  
     
    Adam likes this.
  7. Adam

    Adam Level III

    Joined:
    Aug 14, 2007
    Messages:
    731
    Likes Received:
    50
    zer0.. I think i *might* love you. not sure, but probably..
     
  8. H3nry

    H3nry Level III

    Joined:
    Nov 16, 2007
    Messages:
    407
    Likes Received:
    3
    Location:
    London
    Aw, Zer0 did it already, just as I was about to. :maha:
     
  9. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Nice code Zer0.... it's nothing hard, but not one simple line of this code can be removed.
    Optimization baby ! :lol:
     
  10. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    lol, I tend to optimize and condense all of my code (while still retaining readability)
    Just looking at it now, I can eliminate one line.

    Without the stupid constraint to use functions, I could probably shorten the code by an extra 10 lines or so.