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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 10 of 10

Thread: Grading System Program

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Grading System Program

    Ok so this is a grading program. I have it so the user can input 2 students and then at the end using an if statement it is suppose to compare them and print out who had the higher grade. Seems simple but it never runs the line of code to print out whos grade is higher:
    if (stuAverage > stuAverage2 ){
    		System.out.println(student + " has the highest grade of all students entered!");
    	}
    	else if (stuAverage2> stuAverage){
    		System.out.println(student2 + " has the highest grade of all students entered!!");
    	}
    Here is the Main class:
    import java.util.Scanner;
     
    public class Main2 {
    	int stuAverage;
    	static String student;
     
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		Students stu2 = new Students();
    		String grade;
    		int[] answers;
    		int[] correct;
    		System.out.println("Enter student's name.");
    		student = input.next();
    		System.out.println();
    		System.out.println("How many questions would you like to ask?");
     
    		int num = input.nextInt(); // get the number of times to ask
    		answers = new int[num]; // student's answers
    		correct = new int[num]; // for teachers
    		for (int x = 0; x < num; x++) {
    			System.out.println("Enter the answer for question number: "
    					+ (x + 1) + ".");
    			correct[x] = input.nextInt();
    		}
    		System.out.println("Now for the students.");
    		for (int x = 0; x < num; x++) {
    			System.out.println("Enter the student answer.");
    			answers[x] = input.nextInt();
    		}
    		int average = 0; // his/her grade
    		for (int x = 0; x < num; x++){
    			if (correct[x] == answers[x]){
    				average++;
    			}
     
    		}
    		average = (int) (((double) average) / ((double) num) * 100);
    		if (average >= 90 && average <= 100)
    			grade = "A";
    		else if (average >= 80 && average <= 89)
    			grade = "B";
    		else if (average >= 70 && average <= 79)
    			grade = "C";
    		else if (average >= 60 && average <= 69)
    			grade = "D";
    		else
    			grade = "F";
    		int stuAverage;
    		stuAverage = average;
    		stu2.forMutipleStu();
     
    	}
     
     
    }
    Here is the students class to run a second student
    import java.util.Scanner;
     
     
    public class Students extends Main2 {
    public void forMutipleStu(){
     
    	Scanner input = new Scanner(System.in);
    	String grade2;
    	int [] answers2;
    	int[] correct2;
    	String student2;
     
    	System.out.println("Enter student's name.");
    	student2 = input.next();
    	System.out.println("A=1, B=2, C=3, D=4");
    	System.out.println("How many questions would you like to ask?");
     
    	int num2 = input.nextInt(); // get the number of times to ask
    	answers2 = new int[num2]; // student's answers
    	correct2 = new int[num2]; // for teachers
    	for (int x = 0; x < num2; x++){
    		System.out.println("Enter the answer for question number: " + (x+1) + ".");
    		correct2 [x] = input.nextInt();
    	}
    	System.out.println("Now for the students.");
    	for (int x = 0; x < num2; x++){
    		System.out.println("Enter the student answer.");
    		answers2[x] = input.nextInt();
    	}
    	int average2 = 0; // his/her grade
    	for (int x = 0; x < num2; x++){
    		if (correct2[x] == answers2[x]){
    			average2++;	
    		}
     
    	}
    	average2 = (int) (((double)average2)/ ((double)num2) * 100);
    	if (average2 >= 90 && average2 <= 100)
    		grade2 = "A";
    	else if (average2 >= 80 && average2 <= 89)
    		grade2 = "B";
    	else if (average2 >= 70 && average2 <= 79)
    		grade2 = "C";
    	else if (average2 >= 60 && average2 <= 69)
    		grade2 = "D";
    	else 
    		grade2 = "F";
    	int stuAverage2;
    	stuAverage2 = average2;
     
     
     
    	if (stuAverage > stuAverage2 ){
    		System.out.println(student + " has the highest grade of all students entered!");
    	}
    	else if (stuAverage2> stuAverage){
    		System.out.println(student2 + " has the highest grade of all students entered!!");
    	}
    	}
     
    }
    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Grading System Program

    Can you copy the full contents of the console from when you execute the program and paste it here to show what the program does when it is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Grading System Program

    You have a terrible design. You almost never want to extend your main class. Is Students a special kind of Main2? A Dog is a special type of Animal so it makes sense for Dog to extend Animal. In your design it doesn't, especially since all the second class does it repeat the same code as in the main method just for the second student. What if you need it to work for 100 students? Will you create 100 classes and repeat all the code?

    Since you do have two classes you calculate the average in one class and the second average in the second class so how can you compare them? Also, what will happen in your if statement for the rare occurrence when the 2 averages are the same? In fact why not add some print statements before the if statement to display the value of your average variables to make sure they hold values that you are expecting.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Grading System Program

    Lol. Its not finished I was just stuck on this one part. So thanks for the feedback but just bashing my design doesn't help me. Tell me how I could make it better.

    Thanks

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Grading System Program

    Yes it does help you. By refining your design (in fact step away from the computer and sketch things out on a piece of paper) will help remove flaws and push you towards writing a complete and working program.

    For starters whenever you have repeated code should set alarms bells off. This can be replaced by a loop or a method which you call multiple times.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Grading System Program

    A little back ground on me.... Im 15 and just started learning java over the summer 4 months ago. Thanks for the advice.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Grading System Program

    Then you have plenty of time to learn to do things correctly
    Improving the world one idiot at a time!

  8. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Storing different strings in a loop

    Hello, I am trying to figure out how each time I run the program, in the loop it runs and ask the user for the student name to connect with the students score. But if each time the loop ran the previous students name would be overwritten. How do I use an array to after each run in the loop store a students name and create a new one each time it runs?
    Thanks, here is the code but I'm just showing you what I have. It is not finished, or close.
    import java.util.Scanner;
     
     
    public class Students extends Main2 {
    public void forMutipleStu(){
     
    	Scanner input = new Scanner(System.in);
    	String grade2;
    	int [] answers2;
    	int[] correct2;
    	String student2;
     
    	System.out.println("Enter student's name.");
    	student2 = input.next();
    	System.out.println("A=1, B=2, C=3, D=4");
    	System.out.println("How many questions would you like to ask?");
     
    	int num2 = input.nextInt(); // get the number of times to ask
    	answers2 = new int[num2]; // student's answers
    	correct2 = new int[num2]; // for teachers
    	for (int x = 0; x < num2; x++){
    		System.out.println("Enter the answer for question number: " + (x+1) + ".");
    		correct2 [x] = input.nextInt();
    	}
    	System.out.println("Now for the students.");
    	for (int x = 0; x < num2; x++){
    		System.out.println("Enter the student answer.");
    		answers2[x] = input.nextInt();
    	}
    	int average2 = 0; // his/her grade
    	for (int x = 0; x < num2; x++){
    		if (correct2[x] == answers2[x]){
    			average2++;	
    		}
     
    	}
    	average2 = (int) (((double)average2)/ ((double)num2) * 100);
    	if (average2 >= 90 && average2 <= 100)
    		grade2 = "A";
    	else if (average2 >= 80 && average2 <= 89)
    		grade2 = "B";
    	else if (average2 >= 70 && average2 <= 79)
    		grade2 = "C";
    	else if (average2 >= 60 && average2 <= 69)
    		grade2 = "D";
    	else 
    		grade2 = "F";
    	int stuAverage2;
    	stuAverage2 = average2;
     
     
    	System.out.println(stuAverage);
    	System.out.println(stuAverage2);
    	if (stuAverage > stuAverage2 ){
    		System.out.println(student + " has the highest grade of all students entered!");
    	}
    	else if (stuAverage2> stuAverage){
    		System.out.println(student2 + " has the highest grade of all students entered!");
    	}
    	else if (stuAverage2 == stuAverage){
    		System.out.println(student + " and " + student2 + " both have the same average!" );
    	}
    	}
     
    }

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Grading System Program

    I merged your new topic with your previous, because it's the same problem, and you're trying to sidestep Junky's advice. Being 15 is not an excuse or a disease or a syndrome that allows us to give you bad advice.
    How do I use an array to after each run in the loop store a students name and create a new one each time it runs?
    You don't. Create a Student class with the attributes needed to store the details of each Student, and create a Student object for each student, each with its own details. Store the Student objects in an array or an ArrayList<Student> object.

    Your current design in which class Students extends Main2 is nonsense on many levels. It's not an approach that's teaching you anything but bad habits and poor design. Your code and this most recent question indicate that you've jumped ahead or skipped around in whatever material you're using to study Java without establishing a good understanding of the foundational material. Take a step back and learn the basics.

  10. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Grading System Program

    Quote Originally Posted by GregBrannon View Post
    Create a Student class with the attributes needed to store the details of each Student, and create a Student object for each student, each with its own details. Store the Student objects in an array or an ArrayList<Student> object.
    Thanks for the answer, but I'm not sure what you are saying. Could you explain a little more?
    thanks

Similar Threads

  1. grading system calculator
    By javacodeaddict in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 14th, 2014, 11:14 AM
  2. [SOLVED] Grading Program
    By greg2186 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: September 6th, 2012, 10:36 AM
  3. grading program help
    By devilhanzou in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 14th, 2011, 09:01 AM
  4. Grading
    By BuhRock in forum Java Theory & Questions
    Replies: 5
    Last Post: April 18th, 2010, 05:05 PM
  5. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM