Results 1 to 5 of 5
  1. #1
    jbajwa1 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default how to record multiple input names and output later in program

    Here is the problem:

    You are to write a program that asks the user to input their name and GPA (this must be validated -- between 0.0 and 4.0). They are then to enter their score on the exam (also must be validated -- between 0 and 60). If they score at least 75%, they should see a message that they passed the exam, otherwise a message that they did not pass. You should allow the user to process as many students as they'd like and provide a summary after all students have been processed that includes the number of students that took the exam, a list of student names that passed and the percentage that passed. Appropriate constants should be used

    Here is my source code thus far:

    import javax.swing.JOptionPane;
    public class Program2b
    {

    static final int EXAM_MAX=60;
    static final double PASS_PERC=.75;

    public static void main (String [] args)
    {
    String output, recordName = "";
    int numStudents;
    int numStudentsPassed;
    int studentCounter = 0;
    int studentPassCounter = 0;


    do
    {

    String name;
    double studentGPA;
    float examScore;
    float examPercent;

    name = JOptionPane.showInputDialog(null,
    "Input student's name:",
    "Input Name",
    JOptionPane.QUESTION_MESSAGE);

    do
    {
    studentGPA = Double.parseDouble(JOptionPane.showInputDialog(nul l,
    ("Enter " +name+ "'s GPA:"),
    "Input GPA",
    JOptionPane.QUESTION_MESSAGE));
    }while (studentGPA < 0 || studentGPA > 4);

    do
    {
    examScore = Float.parseFloat(JOptionPane.showInputDialog(null,
    ("Enter " +name+ "'s exam score:"),
    "Input Exam Score",
    JOptionPane.QUESTION_MESSAGE));
    }while (examScore < 0 || examScore > 60);

    examPercent = examScore/EXAM_MAX;

    if (examPercent >= PASS_PERC)
    {
    studentPassCounter++;
    recordName = name;
    }

    studentCounter++;



    }while (JOptionPane.showConfirmDialog(null, "Would you like to input another student?")==JOptionPane.YES_OPTION);

    numStudents = studentCounter;
    numStudentsPassed = studentPassCounter;

    if (numStudentsPassed > 0)
    {
    output = "Number of students = " +numStudents+ "\nNumber of students passed = " +numStudentsPassed+
    "\nStudents who passed are: " +recordName;
    }
    else
    {
    output = "No students passed";
    }

    JOptionPane.showMessageDialog(null, output);
    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    What is your question?
    If you get errors, copy and post them here with your questions.

  3. #3
    jbajwa1 is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    You should allow the user to process as many students as they'd like and provide a summary after all students have been processed that includes the number of students that took the exam, a list of student names that passed and the percentage that passed.

    I can not figure out how to output a list of all the students that passed. Currently I can only output the last input student who passed.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    1. i don't think you should name variables with all caps. that's probably how you learned to do constants in c/c++, but it's not standard practice in java, as there really is no equivalent to c/c++ constants. i don't think it really matters, though.

    2. you are making a class for a good reason. it looks like you put EVERYTHING into the main method, which is pretty useless. you should instead have an object that contains all the necessary data, probably student name, gpa, and exam score. then in your class have an array or some kind of list of these objects. from there you can create separate methods to insert new students and do anything you want by iterating through the list. to actually have it run, you can do one of several things. for one, you can create a new instance of the class within the main method, or you can create a new class that uses the object.

    here's a little pseudo code with incomplete and bad syntax, but it's a general idea

    class student {
    string name
    float gpa
    int exam
    }

    class book {
    student[] students

    insertstudent(student)
    othermethod(...)

    main(...) {
    book b = new book
    book.insertstudent(...)
    book.othermethod
    ...
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    how to output a list of all the students that passed
    You need to save the students that passed in a collection of some type as you find them. Then when you want to do the report, pull out/get the students one by one (in a loop) from that collection.An ArrayList might be a place you could save the students.

Similar Threads

  1. Runtime.exec(), handling input and output streams
    By crookshank in forum New To Java
    Replies: 0
    Last Post: 06-05-2008, 02:41 PM
  2. Replies: 0
    Last Post: 02-11-2008, 08:47 AM
  3. Replies: 1
    Last Post: 07-26-2007, 07:28 PM
  4. Query with 1 record vs. multiple records
    By anderma8 in forum JDBC
    Replies: 5
    Last Post: 07-09-2007, 11:33 PM
  5. Multiple Line Input Dialog Box
    By johnt in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 09:30 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
  •