Results 1 to 10 of 10
  1. #1
    koji_kun is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default 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...

    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]);
    		}
    				
    	}
    }
    and my real problem comes here:
    How do I get the Lowest & Highest grades and that overall distribution thingy?
    I know its a long problem, but please, someone help me. :(

  2. #2
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    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! :)

  3. #3
    koji_kun is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    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 code
    Java Code:
    for(double[] a : numbers)

  4. #4
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    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;
    		}
    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:
    double lowest = numbers[0][0];	
    double highest = numbers[0][0];
    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:
    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:

    Java Code:
    if (b < lowest){
         lowest = b;
    }else if (b > highest){
         highest = b;
    }
    Consider "b" being the element we are visiting now.

    I think you're having problem with the following commands:

    Java Code:
    for(double[] a : numbers){
       for(double b : a){
    If you want, you can change these two lines to

    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];
    The two pieces of code above work at the same way, but I think you are more familiar with the latter! =)

    Using this new way, your code would be something familiar to it:

    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;
    
    				}
    			}
    		}
    ok?
    Please don't laugh at my English... I'm trying my best! :)

  5. #5
    koji_kun is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    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();
    	}
    }
    1) Please consider using Scanner instead of DataInputStream. k.readLine() is deprecated;
    I'm not familiar with the Scanner thingy, so i thought i should just stick with the DataInputStream :)

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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:

  7. #7
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    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! :)

  8. #8
    koji_kun is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    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()
    Thanks. I'll be using this then.

    Why u have to use that many forloops?
    Make it as single method and use it wherever u want.Dont repeat the code.
    Why you ask? maybe because i don't know how to..lol

    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.

    =)
    that's what i've bee trying to find out for the past 3 hours!!! perhaps you guys can help me out with this? :D

  9. #9
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    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! :)

  10. #10
    koji_kun is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •