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
|