Results 1 to 17 of 17
  1. #1
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Post Passing Grades Help!

    Hello again everyone!

    Can anyone help me with this program

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    /////////////////////////////////////////////////////////////////////////
    class Hw07
    {
    //-----------------------------------------------------------------------
       public static void main (String [] args) throws Exception
       {
          Scanner kb = new Scanner(System.in);
    
          System.out.print("\nEnter name of a file of Students: ");
          String filename = kb.nextLine();
    
          Section sec = new Section(filename);
    
          System.out.println("\nIn forward order:");
          sec.print(System.out);
    
          System.out.println("\nThat Section contains " +
                             sec.howMany() +
                             " Student(s).");
    
          System.out.println("\nThat Section contains " +
                             sec.nPassing() +
                             " passing Student(s).");
    
          System.out.println("\nThe highest grade is " +
                             sec.highestGrade() + ".");
       } 
    //-----------------------------------------------------------------------
    } // end class Hw07
    /////////////////////////////////////////////////////////////////////////
    class Section
    {
       private Student [] a;
       private int used;
       private static final int INIT_SIZE = 20;
    //-----------------------------------------------------------------------
       public Section ( String filename ) throws Exception
       {
          Scanner sc = new Scanner(new File(filename));
    
          a = new Student[INIT_SIZE];
          used = 0;
    
          while ( sc.hasNext() )
          {
             Student s = Student.read(null,sc);
    
             if ( used == a.length )
             {
                Student[] newA = new Student[2*a.length+1];
                for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
                newA[used] = s;
                used++;
                a = newA;
             }
             else
             {
                a[used] = s;
                used++;
             }
          }
       }
    //-----------------------------------------------------------------------
       public void print ( PrintStream ps )
       {
          for ( int i = 0 ; i < used ; i++ )
          {
             ps.println(a[i]);
          }
       }
    //-----------------------------------------------------------------------
       public int howMany() { return used; }
    //-----------------------------------------------------------------------
       public double average ()
       {
          int sum = 0;
    
          for ( int i = 0 ; i < used ; i++ )
          {
             Student s = a[i];
             sum += s.getGrade();
          }
    
          return 1.0 * sum / used;
       }
    //-----------------------------------------------------------------------
    } // end class Section
    /////////////////////////////////////////////////////////////////////////
    class Student
    {
       private String name;
       private int grade;
    //-----------------------------------------------------------------------
       public Student ( String name, int grade )
       {
          this.name = name;
          this.grade = grade;
       }
    //-----------------------------------------------------------------------
       public String toString ()
       {
          return name + " (" + grade + ")";
       }
    //-----------------------------------------------------------------------
       public String getName() { return name; }
    //-----------------------------------------------------------------------
       public int getGrade() { return grade; }
    //-----------------------------------------------------------------------
       public void setGrade( int newGrade ) { grade = newGrade; }
    //-----------------------------------------------------------------------
       public static Student read ( PrintStream ps, Scanner sc ) 
       {
          if ( ps != null ) ps.println("Reading a Student record ...");
          if ( ps != null ) ps.print("Enter the name: ");
          String name = sc.nextLine();
          if ( ps != null ) ps.print("Enter the grade: ");
          int grade = sc.nextInt(); sc.nextLine();
          return new Student(name,grade);
       }
    //-----------------------------------------------------------------------
    } // end class Student
    /////////////////////////////////////////////////////////////////////////
    I need to write the method n.Passing and a method highestGrade to make this program work.

    i'm having trouble figuring out what the program is doing, i.e., how it's all working and how i can get it to run properly with these two new methods.

    Thanks to everyone.
    Last edited by JojoDiaz; 10-14-2011 at 07:08 AM.

  2. #2
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    the <div> and </div> isn't in my code!

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    Can you please put your code in the code brackets. Makes it easier for us to read. Then I will go through it

  4. #4
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Java Code:
    // Modified by YOUR NAME HERE.
    import java.io.*;
    import java.util.Scanner;
    /////////////////////////////////////////////////////////////////////////
    class Hw07
    {
    //-----------------------------------------------------------------------
       public static void main (String [] args) throws Exception
       {
          Scanner kb = new Scanner(System.in);
    
          System.out.print("\nEnter name of a file of Students: ");
          String filename = kb.nextLine();
    
          Section sec = new Section(filename);
    
          System.out.println("\nIn forward order:");
          sec.print(System.out);
    
          System.out.println("\nThat Section contains " +
                             sec.howMany() +
                             " Student(s).");
    
          System.out.println("\nThat Section contains " +
                             sec.nPassing() +
                             " passing Student(s).");
    
          System.out.println("\nThe highest grade is " +
                             sec.highestGrade() + ".");
       } 
    //-----------------------------------------------------------------------
    } // end class Hw07
    /////////////////////////////////////////////////////////////////////////
    class Section
    {
       private Student [] a;
       private int used;
       private static final int INIT_SIZE = 20;
    //-----------------------------------------------------------------------
       public Section ( String filename ) throws Exception
       {
          Scanner sc = new Scanner(new File(filename));
    
          a = new Student[INIT_SIZE];
          used = 0;
    
          while ( sc.hasNext() )
          {
             Student s = Student.read(null,sc);
    
             if ( used == a.length )
             {
                Student[] newA = new Student[2*a.length+1];
                for ( int i = 0 ; i < used ; i++ ) newA[i] = a[i];
                newA[used] = s;
                used++;
                a = newA;
             }
             else
             {
                a[used] = s;
                used++;
             }
          }
       }
    //-----------------------------------------------------------------------
       public void print ( PrintStream ps )
       {
          for ( int i = 0 ; i < used ; i++ )
          {
             ps.println(a[i]);
          }
       }
    //-----------------------------------------------------------------------
       public int howMany() { return used; }
    //-----------------------------------------------------------------------
       public double average ()
       {
          int sum = 0;
    
          for ( int i = 0 ; i < used ; i++ )
          {
             Student s = a[i];
             sum += s.getGrade();
          }
    
          return 1.0 * sum / used;
       }
    //-----------------------------------------------------------------------
    } // end class Section
    /////////////////////////////////////////////////////////////////////////
    class Student
    {
       private String name;
       private int grade;
    //-----------------------------------------------------------------------
       public Student ( String name, int grade )
       {
          this.name = name;
          this.grade = grade;
       }
    //-----------------------------------------------------------------------
       public String toString ()
       {
          return name + " (" + grade + ")";
       }
    //-----------------------------------------------------------------------
       public String getName() { return name; }
    //-----------------------------------------------------------------------
       public int getGrade() { return grade; }
    //-----------------------------------------------------------------------
       public void setGrade( int newGrade ) { grade = newGrade; }
    //-----------------------------------------------------------------------
       public static Student read ( PrintStream ps, Scanner sc ) 
       {
          if ( ps != null ) ps.println("Reading a Student record ...");
          if ( ps != null ) ps.print("Enter the name: ");
          String name = sc.nextLine();
          if ( ps != null ) ps.print("Enter the grade: ");
          int grade = sc.nextInt(); sc.nextLine();
          return new Student(name,grade);
       }
    //-----------------------------------------------------------------------
    } // end class Student
    /////////////////////////////////////////////////////////////////////////
    hopefully this works
    Last edited by JojoDiaz; 10-14-2011 at 07:08 AM.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    I'll go through it, but what you do is put "code" your code "/code" ...replace the quotes with brackets

  6. #6
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Ok got it, sorry about that. Will do from now on. Thanks for your help.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Passing Grades Help!

    Quote Originally Posted by JojoDiaz View Post
    Ok got it, sorry about that. Will do from now on. Thanks for your help.
    Don't "do it from now on". Please edit your posts above and place the tags in now.

    the tag [code] goes above your code and the tag [/code] goes below your code block.

  8. #8
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Thanks, way easier to read!

  9. #9
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    I'm sure I could go through each line of your code and figure it out, but you could please explain what it is you are doing, and the problems that you are having?

  10. #10
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Yeah what i need to do is write a method n.Passing and a method highestGrade

    I guess my first question is: Do I need to make it
    Java Code:
    public static n.Passing
    or public int?

    also im not even sure where i need to put these methods.. below public static student?

    A walkthrough would be great, just really need help doing it
    Last edited by JojoDiaz; 10-14-2011 at 06:34 AM.

  11. #11
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    My guess is to do
    while grade is <=60 counter ++
    and then return counter?

  12. #12
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    Not static, and they need to go in your Section class. As in Hw07 class you
    Java Code:
    sec.highestGrade() + ".");

  13. #13
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    Do you by chance have a file made with the students listed?

  14. #14
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Yes i have 3 files that yield different results when the program is complete.

  15. #15
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    Why doesn't this work?

    Java Code:
    //-----------------------------------------------------------------------
       public int nPassing()
       {
           int counter=0;
           for (int i =0;i<=used;i++)
           {
               if(a[i].getGrade() >60)
                   counter++;
           }
           return counter;
       }
    //-----------------------------------------------------------------------
       public int highestGrade()
       {
           int highest =a[0].getGrade();
           if(used==0)
           {
               throw new Error
                       ("Attempt to find the maximum of an empty section");
           }
           else 
               for(int i=0;i<used;i++)
               {
                   if (a[i].getGrade() > highest)
                       highest = a[i].getGrade();
               }
           return highest;
       }
    //-----------------------------------------------------------------------
    } // end class Section
    /////////////////////////////////////////////////////////////////////////

  16. #16
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Passing Grades Help!

    Are you reading from an empty file? Or is the initial file "System.out.print("\nEnter name of a file of Students: ");" a list of Student names w/ grades? I mean if it's empty then you will be getting a few errors in your other methods as well.

  17. #17
    JojoDiaz is offline Member
    Join Date
    Sep 2011
    Posts
    29
    Rep Power
    0

    Default Re: Passing Grades Help!

    No the files are not empty they have 1 student, 3 students, and 10 students

Similar Threads

  1. Project with Arrays and Grades
    By rochla16 in forum New To Java
    Replies: 13
    Last Post: 03-29-2011, 12:40 AM
  2. Passing a value..
    By Lagarto in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 05:58 PM
  3. calculate average letter grades...
    By Christ90 in forum New To Java
    Replies: 14
    Last Post: 01-25-2010, 12:59 AM
  4. Passing value....
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-03-2010, 11:19 AM
  5. Array help counting # of grades
    By speaknspell in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 10:09 PM

Posting Permissions

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