Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: So confused, I need help!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Default So confused, I need help!

    I am taking an intro to java programming class, and I have never done programming before, ever. I'm in a little over my head and my book sucks. My teacher just gave us an assignment after not giving us anything for a month. We need to do the following:

    Write a Java program that processes students’ grades.
    The program will:
     Repeatedly prompt the user to enter new score or enter -1 to exit
     Assign a grade from F to A to this student and print both the score and
    the grade in table with an appropriate header in an output file. The
    exact form of the table is left for you to choose
     Count how many A’s, B’s, etc in the class
     Calculate the largest and the lowest score of every grade. For example
    the highest A might be 98 and the lowest is 91 and so on
     Calculate the average score of every grade
     Calculate the highest and lowest score in the entire class
     After printing the above mentioned table, report all cumulative results
    in the output file with appropriate messages. E.G.
    o There were 5 A students in class
    o The highest A in class was 99
     An example output is given next page.
    Your program should:
     Maintain good indentations that make it readable.
     Include enough comments to demonstrate your understanding of the
    different parts of the program and what their functions are.
     Store the output in file called grades.txt
     Be able to deal with the total absence of one or more grades. For
    example, in some cases there are no A’s in class
    Make sure that the program is your own work. You may be required to have
    a discussion about your program with Dr. Ali before your grade for this
    homework is finalized.

    We have to make a table that looks like this and have the information below it as well.
    Score Grade
    20 F
    98 A
    91 A
    87 B
    85 B
    89 B
    90 A
    62 D
    30 F
    99 A

    There are 4 A student(s) in class
    The highest A is 99
    The lowest A is 90
    The average A is 94.5
    There are 3 B student(s) in class
    The highest B is 89
    The lowest B is 85
    The average B is 87
    There are no C students this time
    There are 1 D student(s) in class
    The highest D is 62
    The lowest D is 62
    The average D is 62
    There are 2 F student(s) in class
    The highest F is 30
    The lowest F is 20
    The average F is 25

    To top it all off, it has to print out to a file. I have some of it figured out. I can't figure out the table. Right now the first number I enter is completely ignored and then it doesn't let me enter in any other numbers after the second number is entered and then the rest of the information is wrong.

    Here is my (working) code:
    // Programming Assingment Grades Grades.java
    // grades class uses switch statement to counter letter grades.
    import java.util.Scanner; // program uses class Scanner
    public class Grades
    	public static void main( String[] args )
    		int total = 0; //sum of grades
    		int gradeCounter = 0; // number of grades entered
    		int aCount = 0; // count of A grades
    		int aGrade = 0; // sum of A grades
    		int aMax = 0; // max of A grade
    		int aMin = 0; // min of A grade
    		int bCount = 0; // count of B grades
    		int bGrade = 0; // sum of B grades
    		int cCount = 0; // count of C grades
    		int cGrade = 0; // sum of C grades
    		int dCount = 0; // count of D grades
    		int dGrade = 0; // sum of D grades
    		int fCount = 0; // count of F grades
    		int fGrade = 0; // sum of F grades
    		int grade; // grade entered by user
    		Scanner input = new Scanner( System.in );
    		System.out.printf( "%s\n%s\n",
    			"Enter the students grade 0-100",
    			"Type -1 when finished" ); // enter grades and terminate when done
    			grade = input.nextInt();
    		while ( grade != -1 )
    			grade = input.nextInt();
    			total += grade; // add grade to total
    			++gradeCounter; // increment number of grades
    			// letter-grade counter
    			if ( grade >= 90 && grade <= 100 )
    					grade = 'A';
    					++aCount; // increment aCount
    				    aGrade += grade; // sum A grades
    			else if ( grade >= 80 )
    					grade = 'B';
    					++bCount; // increment cCount
    				    bGrade += grade; // sum B grades
    			else if ( grade >= 70 )
    					grade = 'C';
    					++cCount; // increment cCount
    				    cGrade += grade; // sum C grades
    			else if ( grade >= 60 )
    					grade = 'D';
    					++dCount; // increment dCount
    				    dGrade += grade; // sum D grades
    					grade = 'F';
    					++fCount; // increment fCount
    				    fGrade += grade; // sum F grades
    		// display grade table
    		System.out.println("| Score    |Grade    |");
    		for ( int g = grade; g <= gradeCounter; grade++)
    			System.out.printf("| ", grade);
    		if ( aCount != 0 )
    				int avgA; // A Average
    				avgA = aGrade / aCount;
    				System.out.printf("There are %d A student(s) in class \n", aCount); // aCount
    				System.out.printf("The Average A is %d\n", avgA);
    				System.out.printf("There are no A students this time \n");
    		System.out.print("++++++++++++++ \n");
    		if ( bCount != 0 )
    				int avgB; // B Average
    				avgB = bGrade / bCount;
    				System.out.printf("There are %d B student(s) in class \n", bCount); // bCount
    				System.out.printf("The Average B is %d\n", avgB);
    				System.out.printf("There are no B students this time \n");
    		System.out.print("++++++++++++++ \n");
    		if ( cCount != 0 )
    				int avgC; // C Average
    				avgC = cGrade / cCount;
    				System.out.printf("There are %d C student(s) in class \n", cCount); // cCount
    				System.out.printf("The Average C is %d\n", avgC);
    				System.out.printf("There are no C students this time \n");
    		System.out.print("++++++++++++++ \n");
    		if ( dCount != 0 )
    				int avgD; // D Average
    				avgD = dGrade / dCount;
    				System.out.printf("There are %d D student(s) in class \n", dCount); // dCount
    				System.out.printf("The Average D is %d\n", avgD);
    				System.out.printf("There are no D students this time \n");
    		System.out.print("++++++++++++++ \n");
    		if ( fCount != 0 )
    				int avgF; // F Average
    				avgF = fGrade / fCount;
    				System.out.printf("There are %d F student(s) in class \n", fCount); // fCount
    				System.out.printf("The Average F is %d\n", avgF);
    				System.out.printf("There are no F students this time \n");
    		System.out.print("++++++++++++++ \n");
    And this is what it gives me:

    c:\Java>java Grades
    Enter the students grade 0-100
    Type -1 when finished
    | Score |Grade |
    There are no A students this time
    There are no B students this time
    There are no C students this time
    There are no D students this time
    There are 1 F student(s) in class
    The Average F is 70

    First I tried to do this with a switch statement but after asking for help all my teacher really said was that I should be using an if/else statement, not a switch statement. I'm super frustrated and really need help.

  2. #2
    Join Date
    Jul 2012
    Thanked 7 Times in 7 Posts

    Default Re: So confused, I need help!

    for others to read your code easier enclose it in (highlight=Java) code here (/highlight) with brackets [ ] instead of ( )

  3. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    sessypeanut (November 2nd, 2012)

Similar Threads

  1. I'm confused
    By Wonderlandslost in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2012, 12:33 PM
  2. help me , im confused!!!!
    By sephskie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 10:52 AM
  3. Confused..
    By jadowers in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 5th, 2011, 12:56 PM
  4. [SOLVED] confused
    By joon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 12th, 2011, 11:40 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 01:18 PM