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

Thread: How to combine these 3 void methods?..

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How to combine these 3 void methods?..

    My assignment is to make a programm to read student's name and tests from a file.
    1)method to calculate average.
    2)method to evaluate grade.

    This is my java code.
    import java.util.*;
    import java.io.*;
    public class average {
        public static void main(String[] args) throws FileNotFoundException{
        double test1, test2, test3, test4, test5;
        String student;
        Scanner inFile = new Scanner(new FileReader
                ("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.txt"));
        PrintWriter outFile = new PrintWriter
                ("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.out");
    outFile.println("Student    Test1 Test2 Test3 Test4 Test5 Average Grade");
                while (inFile.hasNext()){
    		student = inFile.next();
    		test1 = inFile.nextDouble();
    		test2 = inFile.nextDouble();
    		test3 = inFile.nextDouble();
    		test4 = inFile.nextDouble();
    		test5 = inFile.nextDouble();
    outFile.printf("%-9s %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %n",
    		student, test1, test2, test3, test4, test5);
                }
                    inFile.close();
    		outFile.close();
    	}
    ---->OR<---- But i dont think this apply to my assignment
    public class calaverage {
        public static void main(String[] args) throws FileNotFoundException {
        double test1, test2, test3, test4, test5;
        double average;
        String student;
        Scanner inFile = new Scanner
                (new FileReader("C:\\Users\\Owner\\Desktop\\CSCI 112\\Class.txt"));
        PrintWriter outFile = new PrintWriter ("testavg.out");
        student = inFile.next();
        outFile.println("Student Name: " + student);
        test1 = inFile.nextDouble();
        test2 = inFile.nextDouble();
        test3 = inFile.nextDouble();
        test4 = inFile.nextDouble();
        test5 = inFile.nextDouble();
        outFile.printf("Test scores: %5.2f %5.2f %5.2f %5.2f %5.2f %n"
               , test1, test2, test3, test4, test5);
        average = (test1 + test2 + test3 + test4 + test5) / 5.0;
        outFile.printf("Average test score: %5.2f %n", average);
        inFile.close();
        outFile.close();
        }
    }

    method to calculate average
    (wondering how i can turn this into a value returning method or combime with my first code)
    public static void calculateAverage(double average, double test1,
                    double test2, double test3, double test4, double test5)
    	{
    		average = (test1 + test2 + test3 + test4 + test5) / 5.0;
    	}
    method to evaluate the grade
    (wondering how i can turn this into a value returning method or combime with my first code)
    public static void calculateGrade(final int score, char grade){
                 if (score >= 90)
                grade = 'A';
                else if (score >= 80)
                grade = 'B';
                else if (score >= 70)
                grade = 'C';
                else if (score >= 60)
                grade = 'D';
                else
                grade = 'F';
      System.out.printf ("The grade is %c.%n", grade);
            }
    Last edited by helloworld922; February 6th, 2011 at 12:05 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to combine these 3 void methods?..

    You should declare a return type and return the value you want from that method. Also, I would recommend using an array to simplify your average calculations.

    // example implementation of calculateGrade
    public static char calculateGrade(final int score){
                 char grade;
                 if (score >= 90)
                grade = 'A';
                else if (score >= 80)
                grade = 'B';
                else if (score >= 70)
                grade = 'C';
                else if (score >= 60)
                grade = 'D';
                else
                grade = 'F';
      return grade;
            }

    Then, in your main method:

    System.out.println("The grade for test 1 was: " + calculateGrade(test1));

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    TimoElPrimo (February 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to combine these 3 void methods?..

    i need to read and print certian files, but i can not test my programm because my computer doesnt allow acess to my programm to run. I use netbeans and run it as administrator, but it doesnt work.

    This is the file class.text on my computer. The output most add average, grade and class avgerage
    Student Test1 Test2 Test3 Test4 Test5 Average Grade
    Johnson 85 83 77 91 76
    Aniston 80 90 95 93 48
    Cooper 78 81 11 90 73
    Gupta 92 83 30 69 87
    Blair 23 45 96 38 59
    Clark 60 85 45 39 67
    Kennedy 77 31 52 74 83
    Bronson 93 94 89 77 97
    Sunny 79 85 28 93 82
    Smith 85 72 49 75 63
    Last edited by TimoElPrimo; February 8th, 2011 at 03:22 PM.

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to combine these 3 void methods?..

    Thanks! HelloWorld922
    I think this is what my teacher is looking for:
    new improve code:

    import java.util.*;
    import java.io.*;
    public class average {
        public static double calculateAverage(double average, double test1,
                double test2, double test3, double test4, double test5){
             average = (test1 + test2 + test3 + test4 + test5) / 5.0;
    return average;}
        public static char calculateGrade(double score){
                 char grade;
                 if (score >= 90)
                grade = 'A';
                else if (score >= 80)
                grade = 'B';
                else if (score >= 70)
                grade = 'C';
                else if (score >= 60)
                grade = 'D';
                else
                grade = 'F';
    return grade;}
        public static void main(String[] args) throws FileNotFoundException{
        double test1, test2, test3, test4, test5, sAverage, classAverage;
        double average = 0;
        String student;
        char sAvgGrade;
        double sum = 0;
        double count = 0;
        Scanner inFile = new Scanner(new FileReader
                ("C:\\Class.txt"));
        PrintWriter outFile = new PrintWriter
                ("C:\\Class.out");
    outFile.println("Student   Test1 Test2 Test3 Test4 Test5 Average Grade");
                while (inFile.hasNext()){
    		student = inFile.next();
    		test1 = inFile.nextDouble();
    		test2 = inFile.nextDouble();
    		test3 = inFile.nextDouble();
    		test4 = inFile.nextDouble();
    		test5 = inFile.nextDouble();
     sAverage = calculateAverage(average, test1, test2, test3, test4, test5);
     sAvgGrade = calculateGrade(sAverage);
    outFile.printf("%-7s %5.0f %5.0f %5.0f %5.0f %5.0f %8.2f %3c %n",
    	student, test1, test2, test3, test4, test5,sAverage, sAvgGrade);
                 sum = sum + sAverage;
                 count++;}
    outFile.println("");
    outFile.println("Class Average = "+ sum/count);
                    inFile.close();
                    outFile.close(); } }

    i cant test this out tho so i have no clue if its working or not. T_T
    Last edited by TimoElPrimo; February 8th, 2011 at 03:13 PM.

Similar Threads

  1. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  2. Reverse character using void method
    By bgwilf in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 8th, 2010, 07:25 AM
  3. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  4. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  5. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 10:12 PM