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 2 of 2

Thread: Java Grade Caculator

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Grade Caculator

    I need some help here I would like to remove repeated coding for a cleaner look but I am not sure how to do this at this point I already entered a if else to distinguish what will happen with code but I alos had to repeat code to do this is there a way to return the correct output based on the user choice of Graduate or Undergrad?
                    if (choice == 1) {
                        do {//do while block
                            try {//try catch block for exception handeling
                                // collect grades of tests
                                for (int i = 0; i < noTests; i++) {
                                    System.out.printf("Enter grade number for %s %s %d: ", grad.getFirstname(), grad.getLastname(), i + 1);
                                    grad.setGrades(scan.nextInt());                           
                                flag = false;
                                }
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {//do while block
                            try {//try catch block for exception handeling
                                System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname());
                                grad.setmidtermgrad(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {
                            try {//try catch block for exception handeling
                                System.out.printf("Enter final grade for: %s %s ", grad.getFirstname(), grad.getLastname());
                                grad.setfinalgrade(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loopp                  
                        //collect information and display results
                        System.out.printf("Average grade for: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getGradeAvg());
                        System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getFinalGrade());
                    } else if (choice == 2) {//begin else statement
                        do {//do while block
                            try {//try catch block for exception handeling
                                // collect grades of tests
                                for (int i = 0; i < noTests; i++) {
                                    System.out.printf("Enter grade number %d: ", i + 1);
                                    Grad.setGrades(scan.nextInt());
                                }
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        do {
                            try {//try catch block for exception handeling
                                System.out.printf("Enter final grade for: %s %s ", Grad.getFirstname(), Grad.getLastname());
                                Grad.setfingrade(scan.nextInt());
                                flag = false;
                            } catch (InputMismatchException IME) {//catch begin
                                System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
                                scan.nextLine();                            
                                flag = true;//return to top if ture is false
                            }
                        } while (flag);//End do loop
                        System.out.printf("Average grade for: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getGradeAvg());
                        System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getFinGrade());
                    }
     
        } //End Main Method
    } //End Class Egingras_week13
    Last edited by jps; December 5th, 2012 at 04:52 AM. Reason: Added code tags


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Java Grade Caculator

    For reusability, you have to think about it. Which part of the whole program needs to be executed again and again, make it as a function and so on.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. How to display grade using graph?
    By Wai Wai in forum Java Theory & Questions
    Replies: 1
    Last Post: August 4th, 2012, 07:06 AM
  2. Two-Dimensional Array to Grade Students?
    By Java Neil in forum What's Wrong With My Code?
    Replies: 37
    Last Post: March 22nd, 2011, 03:47 PM
  3. Grade Array Trouble
    By kite98765 in forum Collections and Generics
    Replies: 3
    Last Post: January 7th, 2010, 08:14 PM
  4. Help with JAVA (Grade Book)
    By Sara_21 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2009, 10:16 PM