Google Answers Logo
View Question
 
Q: Java Program ( Answered,   1 Comment )
Question  
Subject: Java Program
Category: Computers > Programming
Asked by: math01-ga
List Price: $20.00
Posted: 20 Jan 2003 10:55 PST
Expires: 19 Feb 2003 10:55 PST
Question ID: 146009
Description

You should extend the program below to compute the average score for
the whole class. You will compute the total score for each student and
the average grade for each assignment. The big difference is that all
the values should be store in an array before computing the summaries.
Assume that there are 10 students in the class and there are eight
assignments. You must determine the types and sizes of the arrays you
will need to store the scores for all students as well as the total
score for each student and the average grade for each student.

You will add the appropriate code so that the program will: 

Define and use the necessary arrays to store the raw and processed
grades.
Ask the user to enter the assignment scores for each student. 
Print out the numeric score and the letter grade for each student 
Print out the average grade for each assignment.



Procedure

Modify the program below using arrays. Hint: You will have to use
one-dimensional arrays, and possibly two-dimensional arrays.
Modify the program to accommodate array structures. 
Add code to compute the average grade for each assignment. 




// Program to manage the grades of students in a course.  
  
public class ExamsApp {  
  
    public static void main(String[] args) {  
        Exams ex = new Exams();  
        ex.findSumAndCount();  
        System.out.println("Average score for class is " +
ex.average);
 System.exit(0); 
    }  
}  
 
 
// Program to manage the grades of students in a course.  
  
import javax.swing.JOptionPane;  
  
public class Exams {  
    // Data fields {  
    public double average;  
    private double average_temp;  
    private int sum;          //sum of scores  
    private int count;       // count of scores  
    private int sum_temp; 
    private int count_temp; 
  
   // methods  
   public static int readInt(String prompt) {  
       String numStr = JOptionPane.showInputDialog(prompt);  
       return Integer.parseInt(numStr);  
   }  
   // preconditions: count and sum are zero  
   // postconditions: data field sum stores the sum of  scores  
   //   data field count stores the count of scores  
   public void findSumAndCount() {  
 int num; 
        int score;  
        int SENTINEL = -1;         //sentinel value  
        num = readInt("Enter number of students"); 
 for (int i = 0; i < num; ++i) { 
 sum_temp = 0; 
 count_temp = 0; 
        score = readInt("Enter student " + i + " 's first grade or " +
SENTINEL + " to stop");
        while (score != SENTINEL) {  
      sum_temp += score; 
             sum += score;                // add current score to sum
             count_temp++;                        // increase count by
1
             count++;                        // increase count by 1  
             System.out.println("score: " + score);  
             score = readInt("Enter next grade or " + SENTINEL + " to
stop");
         }  
  average_temp = findAve(sum_temp, count_temp); 
         System.out.println("Average score for student " + i + " is :
" + average_temp);
  if (average_temp >= 90) { 
          System.out.println("Average score for student " + i + " is A
" );
  } else if (average_temp >= 80) { 
          System.out.println("Average score for student " + i + " is B
" );
  } else { 
          System.out.println("Average score for student " + i + " is
C, D, or F -- who cares... this student fails! " );
  } 
  } 
  average = findAve(sum, count); 
      }  
  
   // precondition: sum and count are defined  
   // postcondition: Returns the average score  
   public double findAve(int sum, int count) {  
        double average;                   // average score  
  
        if (count > 0)  
            average = (double) sum / (double) count;  
        else  
             average = 0;  
        return average;  
   }  
}
Answer  
Subject: Re: Java Program
Answered By: studboy-ga on 20 Jan 2003 16:02 PST
 
No change to ExamsApp.java.

Exams.java:

// Program to manage the grades of students in a course. 
 
import javax.swing.JOptionPane; 
 
public class Exams { 
    // Data fields { 
    public double average; 
 
   // methods 
   public static int readInt(String prompt) { 
       String numStr = JOptionPane.showInputDialog(prompt); 
       return Integer.parseInt(numStr); 
   } 
   // preconditions: count and sum are zero 
   // postconditions: data field sum stores the sum of  scores 
   //   data field count stores the count of scores 
   public void findSumAndCount() { 
	int num;
        int score; 
        double [][] scores; 
        scores = new double[10][8];
        num = 10; // Assume 10 students
	for (int i = 0; i < num; ++i) {
          for (int j = 0; j < 8; ++j) { // Assume 8 assignments
             score = readInt("Enter student " + i + "'s grade for
assignment " + j);
             scores[i][j] = score;
             System.out.println("score: " + score); 
           } 
	 }

	 average = findAve(scores);
      } 
 
   // precondition: sum and count are defined 
   // postcondition: Returns the average score 
   public double findAve(double [][] scores) {
        double sum, sum_temp, average, average_temp;

        sum = 0;
        for (int i = 0; i < 10; ++i) { 
        sum_temp = 0;
          for (int j = 0; j < 8; ++j) { 
            sum_temp += scores[i][j]; 
          }

          sum += sum_temp;

          average_temp = sum_temp/8;

          System.out.println("Average score for student " + i + " is :
" + average_temp);
          if (average_temp >= 90) {
               System.out.println("Average score for student " + i + "
is A " );
          } else if (average_temp >= 80) {
               System.out.println("Average score for student " + i + "
is B " );
          } else {
               System.out.println("Average score for student " + i + "
is C, D, or F -- who cares... this student fails! " );
          }
        }

        average = sum/(8*10);
 
        return average; 
   } 
}

Clarification of Answer by studboy-ga on 20 Jan 2003 16:17 PST
Forgot to print out average for each assignment: here it is--

/ Program to manage the grades of students in a course. 
 
import javax.swing.JOptionPane; 
 
public class Exams { 
    // Data fields { 
    public double average; 
 
   // methods 
   public static int readInt(String prompt) { 
       String numStr = JOptionPane.showInputDialog(prompt); 
       return Integer.parseInt(numStr); 
   } 
   // preconditions: count and sum are zero 
   // postconditions: data field sum stores the sum of  scores 
   //   data field count stores the count of scores 
   public void findSumAndCount() { 
	int num;
        int score; 
        double [][] scores; 
        scores = new double[10][8];
        num = 10; // Assume 10 students
	for (int i = 0; i < num; ++i) {
          for (int j = 0; j < 8; ++j) { // Assume 8 assignments
             score = readInt("Enter student " + i + "'s grade for
assignment " + j);
             scores[i][j] = score;
             System.out.println("score: " + score); 
           } 
	 }

	 average = findAve(scores);
      } 
 
   // precondition: sum and count are defined 
   // postcondition: Returns the average score 
   public double findAve(double [][] scores) {
        double sum, sum_temp, average, average_temp;

        sum = 0;
        for (int i = 0; i < 10; ++i) { 
        sum_temp = 0;
          for (int j = 0; j < 8; ++j) { 
            sum_temp += scores[i][j]; 
          }

          sum += sum_temp;

          average_temp = sum_temp/8;

          System.out.println("Average score for student " + i + " is :
" + average_temp);
          if (average_temp >= 90) {
               System.out.println("Average score for student " + i + "
is A " );
          } else if (average_temp >= 80) {
               System.out.println("Average score for student " + i + "
is B " );
          } else {
               System.out.println("Average score for student " + i + "
is C, D, or F -- who cares... this student fails! " );
          }
        }

        for (int j = 0; j < 8; ++j) { 
          sum_temp = 0;
          for (int i = 0; i < 10; ++i) { 
            sum_temp += scores[i][j]; 
          }
          System.out.println("Average score for assignment " + j + "
is " + sum_temp/10);
        }

        average = sum/(8*10);
 
        return average; 
   } 
}

Clarification of Answer by studboy-ga on 20 Jan 2003 16:21 PST
Letter grade for each assignment:

// Program to manage the grades of students in a course. 
 
import javax.swing.JOptionPane; 
 
public class Exams { 
    // Data fields { 
    public double average; 
 
   // methods 
   public static int readInt(String prompt) { 
       String numStr = JOptionPane.showInputDialog(prompt); 
       return Integer.parseInt(numStr); 
   } 
   // preconditions: count and sum are zero 
   // postconditions: data field sum stores the sum of  scores 
   //   data field count stores the count of scores 
   public void findSumAndCount() { 
	int num;
        int score; 
        double [][] scores; 
        scores = new double[10][8];
        num = 10; // Assume 10 students
	for (int i = 0; i < num; ++i) {
          for (int j = 0; j < 8; ++j) { // Assume 8 assignments
             score = readInt("Enter student " + i + "'s grade for
assignment " + j);
             scores[i][j] = score;
             System.out.println("score: " + score); 
           } 
	 }

	 average = findAve(scores);
      } 
 
   // precondition: sum and count are defined 
   // postcondition: Returns the average score 
   public double findAve(double [][] scores) {
        double sum, sum_temp, average, average_temp;

        sum = 0;
        for (int i = 0; i < 10; ++i) { 
        sum_temp = 0;
          for (int j = 0; j < 8; ++j) { 
            sum_temp += scores[i][j]; 
          }

          sum += sum_temp;

          average_temp = sum_temp/8;

          System.out.println("Average score for student " + i + " is :
" + average_temp);
          if (average_temp >= 90) {
               System.out.println("Average score for student " + i + "
is A " );
          } else if (average_temp >= 80) {
               System.out.println("Average score for student " + i + "
is B " );
          } else {
               System.out.println("Average score for student " + i + "
is C, D, or F -- who cares... this student fails! " );
          }
        }

        for (int j = 0; j < 8; ++j) { 
          sum_temp = 0;
          for (int i = 0; i < 10; ++i) { 
            sum_temp += scores[i][j]; 
          }

          average_temp = sum_temp/10;

          System.out.println("Average score for assignment " + j + "
is " + average_temp);
          if (average_temp >= 90) {
               System.out.println("Average grade for assignment " + j
+ " is A " );
          } else if (average_temp >= 80) {
               System.out.println("Average grade for assignment " + j
+ " is B " );
          } else {
               System.out.println("Average grade for assignment " + j
+ " is C, D, or F  -- who cares... the assignment is *too easy* --the
class fails this assignment! " );
          }
        }

        average = sum/(8*10);
 
        return average; 
   } 
}

Request for Answer Clarification by math01-ga on 20 Jan 2003 23:23 PST
Great to hear that studboy-ga. My problem at this point is the
structure of a java program meaning, what goes where in a program.
Will be very greatful if you can help me in that regard.

Thanks

Clarification of Answer by studboy-ga on 21 Jan 2003 00:01 PST
Hi Math01

The structure of a Java program is, in fact, very flexible.  
Normally, there is a "main" class (well, I shouldn't say main--
unlike C where main is required, in Java you can have any class
as a "main" class)--in this example, your main class is the ExamsApp
applet class, this class calls the Exams class and executes its
child functions (findAve).  

With regard this function and variable members, you can put anything anywhere:
since anything can be passed "up" or "down" the hierarchy.
The general rule is abstraction and encapsulation--meaning if a variable
or function doesn't need to be global, hide it and make it private
within a more specific scope.  This will make your program more
object-oriented--as this is what Java is all about...

If you need more tutoring on this let me know.  Perhaps there is
a more effective way for us to communicate with regards to this.

Request for Answer Clarification by math01-ga on 21 Jan 2003 23:17 PST
Hi studboy-ga,

I will highly appreciate it if you can get me more material on this matter.

Thanks

Clarification of Answer by studboy-ga on 22 Jan 2003 00:32 PST
Dear Math01

I can point you to references and books--there are lots of them.
For example, I personally like Bruce Eckels' Thinking in Java--
you can download his book for free from:

http://www.mindview.net/Books/DownloadSites

But I think books and references are not what you are looking for at
the moment--since they pretty much says the same thing without giving
you an actual "feel" of what you will be facing in real life programming--
would you consider training/tutoring?  I can refer you to some
qualified resources that can provide this at a very, very reasonable price.
I think this might actually save you on the long run.  Let me dig up this
info for you.

Request for Answer Clarification by math01-ga on 10 Feb 2003 06:38 PST
Hi studboy-ga, 
I was just wondering if you can help me out with this program. I have
tried  to compiled and run this gui program but nothing happened (no
activity on screen just the dos box). Below is the program.
 
Thanks 
 
import java.awt.*; 
import javax.swing.*; 
 
public class NewGUI extends JApplet { 
      
    // set up row 1 
    JPanel row1 = new JPanel(); 
    JLabel ptLabel = new JLabel("Choose: "); 
    JComboBox playType = new JComboBox(); 
    // set up row 2 
    JPanel row2 = new JPanel(); 
    JLabel numbersLabel = new JLabel("Your picks: ", JLabel.RIGHT); 
    JTextField[] numbers = new JTextField[6]; 
    JLabel winnersLabel = new JLabel("Winners: ", JLabel.RIGHT); 
    JTextField[] winners = new JTextField[6]; 
    // set up row 3 
    JPanel row3 = new JPanel(); 
    ButtonGroup option = new ButtonGroup(); 
    JCheckBox stop = new JCheckBox("Stop", true); 
    JCheckBox play = new JCheckBox("Play", false); 
    JCheckBox reset = new JCheckBox("Reset", false); 
    // set up row 4 
    JPanel row4 = new JPanel(); 
    JLabel got3Label = new JLabel("3 of 6: ", JLabel.RIGHT); 
    JTextField got3 = new JTextField(); 
    JLabel got4Label = new JLabel("4 of 6: ", JLabel.RIGHT); 
    JTextField got4 = new JTextField(); 
    JLabel got5Label = new JLabel("5 of 6: ", JLabel.RIGHT); 
    JTextField got5 = new JTextField(); 
    JLabel got6Label = new JLabel("6 of 6: ", JLabel.RIGHT); 
    JTextField got6 = new JTextField(10); 
    JLabel drawingsLabel = new JLabel("Drawings: ", JLabel.RIGHT); 
    JTextField drawings = new JTextField(); 
    JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT); 
    JTextField years = new JTextField(); 
     
    public void init() { 
        GridLayout appletLayout = new GridLayout(5, 1, 10, 10); 
        Container pane = getContentPane(); 
        pane.setLayout(appletLayout); 
 
        FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 
            10, 10); 
        row1.setLayout(layout1); 
        playType.addItem("Quick Pick"); 
        playType.addItem("Personal"); 
        row1.add(ptLabel); 
        row1.add(playType); 
        pane.add(row1); 
 
        GridLayout layout2 = new GridLayout(2, 7, 10, 10); 
        row2.setLayout(layout2); 
        row2.setLayout(layout2); 
        row2.add(numbersLabel); 
        for (int i = 0; i < 6; i++) { 
            numbers[i] = new JTextField(); 
            row2.add(numbers[i]); 
        } 
        row2.add(winnersLabel); 
        for (int i = 0; i < 6; i++) { 
            winners[i] = new JTextField(); 
            winners[i].setEditable(false); 
            row2.add(winners[i]); 
        } 
        pane.add(row2); 
 
        FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 
            10, 10); 
        option.add(stop); 
        option.add(play); 
        option.add(reset); 
        row3.setLayout(layout3); 
        row3.add(stop); 
        row3.add(play); 
        row3.add(reset); 
        pane.add(row3); 
 
        GridLayout layout4 = new GridLayout(2, 6, 20, 10); 
        row4.setLayout(layout4); 
        row4.add(got3Label); 
        got3.setEditable(false); 
        row4.add(got3); 
        row4.add(got4Label); 
        got4.setEditable(false); 
        row4.add(got4); 
        row4.add(got5Label); 
        got5.setEditable(false); 
        row4.add(got5); 
        row4.add(got6Label); 
        got6.setEditable(false); 
        row4.add(got6); 
        row4.add(drawingsLabel); 
        drawings.setEditable(false); 
        row4.add(drawings); 
        row4.add(yearsLabel); 
        years.setEditable(false); 
        row4.add(years); 
        pane.add(row4); 
        setContentPane(pane); 
    } 
    public static void main(String[] arguments) { 
       NewGUI ng = new NewGUI(); 
    }   
}

Clarification of Answer by studboy-ga on 10 Feb 2003 07:46 PST
Hi Math01

This looks like a new question--
can you report this as a new question and say: for studboy only?

Thanks
Comments  
Subject: Re: Java Program
From: studboy-ga on 20 Jan 2003 16:35 PST
 
Also, if you want to save money...  post a request here and a new
question and I can give you some tips ^_^ (hint hint...)

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy