Results 1 to 10 of 10
Thread: help.. what to do with this?
- 12-16-2009, 03:53 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
help.. what to do with this?
Hi,
I'm a newbie here and I need some help.
Problem:
Create a two-dimensional array program to store student grades on multiple exams. This is used by a professor to store and analyze a set of student grades. Each row of the array represents a single student's grades for the entire course, and each column represents a grade on one of the exams the students took during the course.
The output should be:
***Input Grades***
Student1
Test1: 87
Test2: 96
Test3: 70
Student2
Test1: 68
Test2: 87
Test3: 90
.......
.......
.......
.......
***Summary***
The Grades are:
________Test1__Test2__Test3__Average
Student1:__87___96_____70_____84.33
Student2:__68___87_____90_____81.67
Student3:__94___100____90_____94.67
Student4:__100__81_____82_____87.67
.......
.......
.......
Lowest grade: 68
Highest grade: 100
Overall grade distribution:
50-59:
60-69: *
70-79: *
80-89: ****
90-99: ****
100: **
And here's what I came up with...
and my real problem comes here:Java Code:import java.io.*; public class StudentGrades{ public static void main(String[]args)throws Exception{ DataInputStream k=new DataInputStream(System.in); int grade[][]=new int [10][4]; float ave[]=new float [10]; int i, j; float a=0; System.out.println("\n***Input Grades***"); for (i=0;i<10;i++){ System.out.println("Student "+(i+1)); for (j=1;j<4;j++){ System.out.print("Test "+j+" "); grade[i][j]=Integer.parseInt(k.readLine()); if (grade[i][j]>100){ do{ System.out.println("Grades CANNOT exceed 100, re-enter..."); System.out.print("Test "+j+" "); grade[i][j]=Integer.parseInt(k.readLine()); }while (grade[i][j]>100); } else a=a+stud[i][j]; } ave[i]=a/3; a=0; } System.out.println("\n***Summary***\n"); System.out.println("\t\tTest1\tTest2\tTest3\tAverage"); for (i=0;i<10;i++){ System.out.print("Student "+(i+1)+"\t"); for(j=1;j<4;j++){ System.out.print(stud[i][j]+"\t"); } System.out.println(ave[i]); } } }
How do I get the Lowest & Highest grades and that overall distribution thingy?
I know its a long problem, but please, someone help me. :(
- 12-16-2009, 04:40 PM #2
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Man, was this exercise proposed by your teacher? He's trying to make you learn Java without the concept of OO.
1) Please consider using Scanner instead of DataInputStream. k.readLine() is deprecated;
2) Check out the code below and use it to make yours. Don't copy and paste... your teacher will not like it hehe
Java Code:public static void main(String[]args)throws Exception{ double numbers[][] = {{25, 39, 47.8, 6}, {3, 78, 68, 95}}; int[] amount = new int[6]; //Now we'll assume the first number is the lowest double lowest = numbers[0][0]; //Now we'll assume the first number is the highest double highest = numbers[0][0]; for(double[] a : numbers){ for(double b : a){ if (b < lowest){ lowest = b; }else if (b > highest){ highest = b; } if (b < 50){ continue; } if (b < 60){ amount[0]++; } else if (b < 70){ amount[1]++; } else if (b < 80){ amount[2]++; } else if (b < 90){ amount[3]++; } else if (b < 100) { amount[4]++; } else if (b == 100){ amount[5]++; } } } System.out.println("Lowest: " + lowest); System.out.println("Highest: " + highest); System.out.print("50 - 59: "); for(int i = 0; i < amount[0]; i++){ System.out.print("*"); } System.out.println(); System.out.print("60 - 69: "); for(int i = 0; i < amount[1]; i++){ System.out.print("*"); } System.out.println(); System.out.print("70 - 79: "); for(int i = 0; i < amount[2]; i++){ System.out.print("*"); } System.out.println(); System.out.print("80 - 89: "); for(int i = 0; i < amount[3]; i++){ System.out.print("*"); } System.out.println(); System.out.print("90 - 99: "); for(int i = 0; i < amount[4]; i++){ System.out.print("*"); } System.out.println(); System.out.print("100: "); for(int i = 0; i < amount[5]; i++){ System.out.print("*"); } System.out.println(); }Please don't laugh at my English... I'm trying my best! :)
- 12-17-2009, 10:17 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
haha. yeah, this was proposed by my teacher, and we haven't even reached OO in our course yet.. anyway thanks for the reply.
but uhmm... since you said not to copy and paste.. would you mind explaining abit on how to get the lowest and highest numbers part, coz i know my teacher would surely ask me where i got this codeJava Code:for(double[] a : numbers)
- 12-17-2009, 10:59 AM #4
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
This part of the code is responsible for find lowest and highest values. The first thing we do is to assume that the first value in the array is the lowest and the highest value. The lines below do exactly this:Java Code://Now we'll assume the first number is the lowest double lowest = numbers[0][0]; //Now we'll assume the first number is the highest double highest = numbers[0][0]; for(double[] a : numbers){ for(double b : a){ if (b < lowest){ lowest = b; }else if (b > highest){ highest = b; }
Now that we have an reference to compare the other numbers, we will go to each element in the array and we'll do two comparisons with it:Java Code:double lowest = numbers[0][0]; double highest = numbers[0][0];
1) If the element we are visiting now is lower than the lowest, we have now a new lowest element;
2) If the element we are visiting now is higher than the highest, we have now a new highest element;
These two steps are presented here:
Consider "b" being the element we are visiting now.Java Code:if (b < lowest){ lowest = b; }else if (b > highest){ highest = b; }
I think you're having problem with the following commands:
If you want, you can change these two lines toJava Code:for(double[] a : numbers){ for(double b : a){
The two pieces of code above work at the same way, but I think you are more familiar with the latter! =)Java Code:for (int i = 1; i < numbers[i-1].length; i++) { double a[] = numbers[i]; for (int r = 0; r < a.length; r++) { double b = a[r];
Using this new way, your code would be something familiar to it:
ok?Java Code:double[][] numbers = { { 1.2, 2 }, { 7, 4 } }; double lowest = numbers[0][0]; double highest = numbers[0][0]; for (int i = 1; i < numbers[i-1].length; i++) { double a[] = numbers[i]; for (int r = 0; r < a.length; r++) { double b = a[r]; if (b < lowest) { lowest = b; } else if (b > highest) { highest = b; } } }Please don't laugh at my English... I'm trying my best! :)
- 12-17-2009, 11:48 AM #5
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Ok,, so here's what i came up with (and still got some problems with it). Please take a look, and tell me if this is good enough. :(
Java Code:import java.io.*; public class StudentGrades{ public static void main(String[]args)throws Exception{ DataInputStream k=new DataInputStream(System.in); double[][] grade=new double[10][4]; double[] ave=new double [10]; int[] amount=new int[7]; int i, j; double s=0; System.out.println("\n***Input Grades***"); for (i=0;i<10;i++){ System.out.println("Student "+(i+1)); for (j=1;j<4;j++){ System.out.print("Test "+j+" "); grade[i][j]=Integer.parseInt(k.readLine()); s=s+grade[i][j]; } ave[i]=s/3; s=0; } System.out.println("\n***Summary***\n"); System.out.println("\t\tTest1\tTest2\tTest3\tAverage"); for (i=0;i<10;i++){ System.out.print("Student "+(i+1)+"\t"); for(j=1;j<4;j++){ System.out.print(grade[i][j]+"\t"); } System.out.println(ave[i]); } System.out.println(); double lowest = grade[0][0]; double highest = grade[0][0]; for(double[] a : grade){ for(double b : a){ if (b < lowest){ lowest = b; }else if (b > highest){ highest = b; } if (b < 50){ continue; } if (b < 60){ amount[0]++; } else if (b < 70){ amount[1]++; } else if (b < 80){ amount[2]++; } else if (b < 90){ amount[3]++; } else if (b < 100) { amount[4]++; } else if (b == 100){ amount[5]++; } } } System.out.println("Lowest: " + lowest); System.out.println("Highest: " + highest); System.out.println(); System.out.print("50 - 59: "); for(i = 0; i < amount[0]; i++){ System.out.print("*"); } System.out.println(); System.out.print("60 - 69: "); for(i = 0; i < amount[1]; i++){ System.out.print("*"); } System.out.println(); System.out.print("70 - 79: "); for(i = 0; i < amount[2]; i++){ System.out.print("*"); } System.out.println(); System.out.print("80 - 89: "); for(i = 0; i < amount[3]; i++){ System.out.print("*"); } System.out.println(); System.out.print("90 - 99: "); for(i = 0; i < amount[4]; i++){ System.out.print("*"); } System.out.println(); System.out.print("100: "); for(i = 0; i < amount[5]; i++){ System.out.print("*"); } System.out.println(); } }I'm not familiar with the Scanner thingy, so i thought i should just stick with the DataInputStream :)1) Please consider using Scanner instead of DataInputStream. k.readLine() is deprecated;
- 12-17-2009, 03:39 PM #6
DataInputStream is not compatible.Just use
below or Scanner class
for a beginner this below method is always good.
-----------------------------------------------
BufferedReader systemIn =
new BufferedReader(new InputStreamReader(System.in));
then use systemIn.readLine()
Why u have to use that many forloops?
Make it as single method and use it wherever u want.Dont repeat the code.Ramya:cool:
- 12-17-2009, 04:10 PM #7
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
The lowest number is not working because I'm unable to find out what's the first grade that is valid in your array. I thought it was grade[0][0];
Try to find it out and then change it in the code and it will work properly.
=)Please don't laugh at my English... I'm trying my best! :)
- 12-17-2009, 04:19 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
Thanks. I'll be using this then.DataInputStream is not compatible.Just use
below or Scanner class
for a beginner this below method is always good.
-----------------------------------------------
BufferedReader systemIn =
new BufferedReader(new InputStreamReader(System.in));
then use systemIn.readLine()
Why you ask? maybe because i don't know how to..lolWhy u have to use that many forloops?
Make it as single method and use it wherever u want.Dont repeat the code.
that's what i've bee trying to find out for the past 3 hours!!! perhaps you guys can help me out with this? :DThe lowest number is not working because I'm unable to find out what's the first grade that is valid in your array. I thought it was grade[0][0];
Try to find it out and then change it in the code and it will work properly.
=)
- 12-17-2009, 04:26 PM #9
Member
- Join Date
- Dec 2009
- Location
- Rio de Janeiro
- Posts
- 38
- Rep Power
- 0
Now it's working!
Java Code:public class StudentGrades{ public static void main(String[]args)throws Exception{ //DataInputStream k=new DataInputStream(System.in); Scanner k = new Scanner(System.in); //I created it //double[][] grade=new double[10][4]; double[][] grade=new double[10][3]; //I created it double[] ave=new double [10]; int[] amount=new int[7]; int i, j; double s=0; System.out.println("\n***Input Grades***"); for (i=0;i<10;i++){ System.out.println("Student "+(i+1)); //for (j=1;j<4;j++){ for (j=0;j<3;j++){ //I created it System.out.print("Test "+j+" "); //grade[i][j]=Integer.parseInt(k.readLine()); grade[i][j] = k.nextInt(); //I created it s=s+grade[i][j]; } ave[i]=s/3; s=0; } System.out.println("\n***Summary***\n"); System.out.println("\t\tTest1\tTest2\tTest3\tAverage"); for (i=0;i<10;i++){ System.out.print("Student "+(i+1)+"\t"); //for(j=1;j<4;j++){ for(j=0;j<3;j++){ //I created it System.out.print(grade[i][j]+"\t"); } System.out.println(ave[i]); } System.out.println(); double lowest = grade[0][0]; double highest = grade[0][0]; for(double[] a : grade){ for(double b : a){ if (b < lowest){ lowest = b; }else if (b > highest){ highest = b; } if (b < 50){ continue; } if (b < 60){ amount[0]++; } else if (b < 70){ amount[1]++; } else if (b < 80){ amount[2]++; } else if (b < 90){ amount[3]++; } else if (b < 100) { amount[4]++; } else if (b == 100){ amount[5]++; } } } System.out.println("Lowest: " + lowest); System.out.println("Highest: " + highest); System.out.println(); System.out.print("50 - 59: "); for(i = 0; i < amount[0]; i++){ System.out.print("*"); } System.out.println(); System.out.print("60 - 69: "); for(i = 0; i < amount[1]; i++){ System.out.print("*"); } System.out.println(); System.out.print("70 - 79: "); for(i = 0; i < amount[2]; i++){ System.out.print("*"); } System.out.println(); System.out.print("80 - 89: "); for(i = 0; i < amount[3]; i++){ System.out.print("*"); } System.out.println(); System.out.print("90 - 99: "); for(i = 0; i < amount[4]; i++){ System.out.print("*"); } System.out.println(); System.out.print("100: "); for(i = 0; i < amount[5]; i++){ System.out.print("*"); } System.out.println(); } }Please don't laugh at my English... I'm trying my best! :)
- 12-17-2009, 04:48 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks