Results 1 to 10 of 10
  1. #1
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default [SOLVED] Ready to Program Java IDE

    Hey,

    I need help with this assignment:

    Create a Java program, called ExamMark, that will first generate and store 100 random student grades between 40 and 100 percent. Store these in a data file called "ExamData.dat". Re-open the data so that the random numbers can be read in. Next, calculate and output to the screen, the average of the 100 marks. As well, output the number of marks that fall into one of the following five categories: 40-49, 50-59, 60-69, 70-79, and 80-100. Be sure to include a suitable program header and use a generous amount of comment statements to explain your work. A sample output is provided below:
    Writing 100 marks to a data file called "ExamData.dat"

    Average of the 100 marks is XXX%

    There are 5 marks between 40-49 percent
    There are 6 marks between 50-59 percent


    Here is what I have done so far:

    // The "ExamMark" class.
    import java.awt.*;
    import java.io.*;
    import hsa.Console;

    public class ExamMark
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    int mark;
    PrintWriter output;
    output = new PrintWriter (new FileWriter ("ExamData.dat"));
    for (int count = 1 ; count <= 100 ; count++)
    {
    mark = (int) (Math.random () * 61) + 40;
    output.println (mark);
    }
    output.close ();
    c.println ("Writing 100 marks to a data file called 'ExamData.dat'"); // Informs the user where the data of the 300 throws are stored ('dice.dat')
    c.println ();

    int newMark;
    int sum = 0, count = 0;

    String line;
    BufferedReader input;
    input = new BufferedReader (new FileReader ("ExamData.dat"));
    line = input.readLine ();
    while (line != null) //Loop terminates at end of file
    {
    newMark = Integer.parseInt (line); //Change to interger
    count++; // Adds 1 to 'count'
    sum += newMark;
    line = input.readLine (); //Read next line of characters
    }
    double average = (double) sum / count;
    c.println ("The average of the 100 marks is " + average + "%.");


    } // main method
    } // ExamMark class

    Thanks.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default ok...

    ... and the problem is?

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    "As well, output the number of marks that fall into one of the following five categories: 40-49, 50-59, 60-69, 70-79, and 80-100."

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    I added a few lines in the code which are bolded.

    // The "ExamMark" class.
    import java.awt.*;
    import java.io.*;
    import hsa.Console;

    public class ExamMark
    {
    static Console c; // The output console

    public static void main (String[] args) throws IOException
    {
    c = new Console ();

    int mark;
    PrintWriter output;
    output = new PrintWriter (new FileWriter ("ExamData.dat"));
    for (int count = 1 ; count <= 100 ; count++)
    {
    mark = (int) (Math.random () * 61) + 40;
    output.println (mark);
    }
    output.close ();
    c.println ("Writing 100 marks to a data file called 'ExamData.dat'"); // Informs the user where the data of the 300 throws are stored ('dice.dat')
    c.println ();

    int newMark;
    int sum = 0, count = 0;
    int [] numMarks = new int [1000];

    String line;
    BufferedReader input;
    input = new BufferedReader (new FileReader ("ExamData.dat"));
    line = input.readLine ();
    while (line != null) //Loop terminates at end of file
    {
    newMark = Integer.parseInt (line); //Change to interger
    numMarks [newMark]++;
    count++; // Adds 1 to 'count'
    sum += newMark;
    line = input.readLine (); //Read next line of characters
    }
    double average = (double) sum / count;
    c.println ("The average of the 100 marks is " + average + "%.");

    for (int i = 40 ; i < 50 ; i++) // {
    c.println ("The mark " + i + " came up " + numMarks [i] + " times.");
    }



    } // main method
    } // ExamMark class

    My added code counted how many times each mark from 40-49 came up.
    How am I suppose to output the number of marks that fall into one of the following five categories: 40-49, 50-59, 60-69, 70-79, and 80-100?

    Quote Originally Posted by Eranga View Post
    Either you can work out with counter variables or with the Collection framework.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is the simple logic you need. There are five rangers, so simply use five variables and initialize all of them to zero.

    Once you receive marks in a loop, find that on which range it should be added. Once you find that increase the appropriate variable(counter). Use if-else conditions for that, mush easier.

  7. #7
    Starr29 is offline Member
    Join Date
    Jan 2009
    Posts
    26
    Rep Power
    0

    Default

    Thank you so much. That was extremely helpful. I have completed this assignment thanks to you!

    Quote Originally Posted by Eranga View Post
    Here is the simple logic you need. There are five rangers, so simply use five variables and initialize all of them to zero.

    Once you receive marks in a loop, find that on which range it should be added. Once you find that increase the appropriate variable(counter). Use if-else conditions for that, mush easier.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Nice to help you. :) If you have solve the problem please mark it as solved from the thread tools.

  9. #9
    Kouichi is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default Help...

    Hey, could you help me; my homework seems very similar to yours...
    Create a program that will generate 50 random numbers between the range of -5 and 45 and store them in an array. Have the program then print the numbers in rows of 10 and calculate the average of the numbers.
    ...mind helping me out?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Kouichi View Post
    Hey, could you help me; my homework seems very similar to yours...
    Create a program that will generate 50 random numbers between the range of -5 and 45 and store them in an array. Have the program then print the numbers in rows of 10 and calculate the average of the numbers.
    ...mind helping me out?
    Ok, what you have tried so far? Can you show-up your code here to us?

Similar Threads

  1. Java Program for doing FTP
    By Rajesh_J2EE in forum New To Java
    Replies: 1
    Last Post: 12-06-2008, 03:35 AM
  2. Java Program
    By icedragon770 in forum Java Applets
    Replies: 26
    Last Post: 10-12-2008, 01:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. java program
    By nehasahu in forum New To Java
    Replies: 5
    Last Post: 07-12-2007, 12:05 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
  •