Results 1 to 19 of 19

Thread: [RESOLVED]Printing...simple <<just newb

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    [RESOLVED]Printing...simple <<just newb

    This is just a small project that I am working on for class, and for my own benefit. Basically, what I want to do is...when I "printHighandLow()" I want it to print out the name as well as the grade the person has rather then just the high and low grade. Did I explain this well enough, anyone know a solution?
    VB Code:
    1. import java.util.ArrayList;
    2.  
    3. public class course
    4. {
    5.     // instance variables
    6.       ArrayList students;
    7.       ArrayList grades;
    8.       private InputReader reader;
    9.       double sum;
    10.       double totalgrades;
    11.       double avg;
    12.       double highgrade;
    13.       double lowgrade;
    14.      
    15.     /**
    16.      * Constructor for objects of class course
    17.      */
    18.     public course()
    19.     {
    20.         //
    21.         students = new ArrayList();
    22.         grades = new ArrayList();
    23.         reader = new InputReader();
    24.         highgrade = highgrade;
    25.         lowgrade = 999999;
    26.              
    27.     }
    28.  
    29.     /**
    30.      * An example of a method - replace this comment with your own
    31.      *
    32.      * @param  y   a sample parameter for a method
    33.      * @return     the sum of x and y
    34.      */
    35.     public void addStudnet()
    36.     {
    37.        info newstudent;
    38.        String name;
    39.        boolean finished = false;
    40.        String gradeSt;
    41.        double grade;
    42.      
    43.        welcome();
    44.        while(!finished)
    45.        {    
    46.        System.out.println(" Enter the name of the student");
    47.        name = reader.getInput();    
    48.        if(name.startsWith("bye"))
    49.        {
    50.             finished = true;
    51.             System.out.println("Good Bye");    
    52.        }
    53.        else
    54.        {
    55.        
    56.        System.out.println(" Enter a grade");
    57.        gradeSt = reader.getInput();
    58.        System.out.println("Done");
    59.              
    60.        grade = Double.parseDouble(gradeSt);
    61.        newstudent = new info(name , grade);
    62.        students.add(newstudent);
    63.        // totals up the grades(used in the Average method)
    64.        sum = sum + grade;  
    65.      
    66.           if (grade > highgrade)
    67.             {    
    68.               highgrade = grade;
    69.             }
    70.            
    71.              if (grade < lowgrade)
    72.             {    
    73.              lowgrade = grade;
    74.             }
    75.            
    76.            
    77.           /*if (gradeSt.compareTo(lowgrade) < 0)
    78.             {
    79.               lowgrade.equals(newstudent);
    80.             }*/
    81.            
    82.            
    83.           // totals up the number of grades.
    84.             totalgrades++;
    85.        }
    86.        }
    87.  
    88.     }
    89.    
    90.     public void welcome()
    91.     {
    92.        
    93.         System.out.println("Type bye to leave");
    94.     }
    95.    
    96.     public void printStudents()
    97.     {
    98.         int ndx;
    99.         for (ndx =0; ndx< students.size(); ndx++)
    100.         System.out.println(students.get(ndx));
    101.        
    102.     }
    103.    
    104.     public void printHighandLow()
    105.     {
    106.         System.out.println(highgrade);
    107.         System.out.println(lowgrade);
    108.     }
    109.    
    110.    
    111.     public void getAverage()
    112.     {
    113.      avg = sum/totalgrades;
    114.      System.out.println(" Average = "+ avg );
    115.     }
    116.        
    117.        
    118.    
    119. }
    Last edited by stinkoman; Mar 4th, 2006 at 12:25 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Printing...simple <<just newb

    Welcome to the forums!

    To print the name as well as the marks you have to do something like you already have done in the getAverage method. What part more specifically are you having trouble with?


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    Im new to this...and im just confused on how I would print the highest grade along with the name that goes along with it.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Printing...simple <<just newb

    Is that all the code you have? Cause you don't seem to be calling the method anywhere. I'm a little confused...


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    sorry, im also using...
    VB Code:
    1. import java.io.BufferedReader;
    2. import java.io.InputStreamReader;
    3.  
    4. public class InputReader
    5. {
    6.     private BufferedReader reader;
    7.  
    8.     /**
    9.      * Create a new InputReader that reads text from the text terminal.
    10.      */
    11.     public InputReader()
    12.     {
    13.         reader = new BufferedReader(new InputStreamReader(System.in));;
    14.     }
    15.  
    16.     /**
    17.      * Read a line of text from standard input (the text terminal), and
    18.      * return it as a String.
    19.      *
    20.      * @return  A String typed by the user.
    21.      */
    22.     public String getInput()
    23.     {
    24.         System.out.print("> ");                // print prompt
    25.         String inputLine = readInputLine();
    26.  
    27.         return inputLine;
    28.     }
    29.  
    30.     /**
    31.      * Read one line of input and return it as a String.
    32.      *
    33.      * @return  A String representing the input, or an empty String
    34.      *          if an error occurs.
    35.      */
    36.     private String readInputLine()
    37.     {
    38.         String line = "";
    39.  
    40.         try {
    41.             line = reader.readLine();
    42.         }
    43.         catch(java.io.IOException exc) {
    44.             System.out.println ("Read error: " + exc.getMessage());
    45.         }
    46.         return line;
    47.     }
    48. }

    and


    VB Code:
    1. public class info
    2. {
    3.     // instance variables - replace the example below with your own
    4.     private String name;
    5.     private double grade;
    6.  
    7.     /**
    8.      * Constructor for objects of class info
    9.      */
    10.     public info(String nam, double grad)
    11.     {
    12.         name = nam;
    13.         grade = grad;
    14.     }
    15.  
    16.     /**
    17.      * An example of a method - replace this comment with your own
    18.      *
    19.      * @param  y   a sample parameter for a method
    20.      * @return     the sum of x and y
    21.      */
    22.     public String toString()
    23.     {
    24.         return name + "  " + grade;
    25.     }
    26. }

    sorry

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    I guess your code is missing the "main" so:

    Code:
    /**
       * Main Program
       *
       * @param args String[]
       */
      public static void main (String args[]) {
        course c = new course() ;
        c.addStudnet() ;
        c.getAverage() ;
        c.printStudents() ;
        c.printHighandLow() ;
      }
    Last edited by ComputerJy; Mar 2nd, 2006 at 10:02 PM.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    But I'm still wondering why all this complex code
    most of it is useless
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    its what is required of me, is why I have all this code...I don't know haha.
    Thanks for the help.
    so what would I do next...

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    compile and run!

    your code is ready to be compiled
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    well I know that, but I don't need to change anything else? That didn't quite work....what im trying to do is print out the HighandLow with the owners of the HighandLow grades right next to each other. So I know who had that high and low.
    Last edited by stinkoman; Mar 3rd, 2006 at 11:01 AM.

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    somehow I need to associate the grades with the names in the array...so I can print them both. I need to see the name next to the grade so that you can see who had the grades.

  12. #12
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    Then you'll have to replace the ArrayList of students and the ArrayList of grades with one ArrayList of infos
    Then you can use the following method to get high and low:

    Code:
    private info highestInfo () {
        //infos is supposed to be the ArrayList that contains info objects
        info tmp = (info) infos.get(0) ;
        Iterator i = infos.iterator() ;
        //visit all members in the ArrayList
        while (i.hasNext()) {
          info tmp2 = (info) i.next() ;
          if (tmp2.getGrade() > tmp.getGrade()) {
            tmp = tmp2 ;
          }
        }
        return tmp ;
      }
    This one get the high, write another one to get the low
    p.s. Add getGrade() to info class
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    <--- Really confused, im trying to figure this out....Not only am I new to this, I have been away from it for a while. Thank you for the help though so far
    Last edited by stinkoman; Mar 3rd, 2006 at 07:20 PM.

  14. #14
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    Quote Originally Posted by stinkoman
    <--- Really confused, im trying to figure this out....Not only am I new to this, I have been away from it for a while.
    What's confusing you?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    Quote Originally Posted by ComputerJy
    What's confusing you?
    Everything, I don't know exactly what I should do with the code you gave me...Ive just been messing with it..I can't figure it out....I feel newb haha

  16. #16
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    The code doesn't need any changing
    just apply the previous instructions and write a similar method to get the lowest info given
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    so you saying it should work if I just paste that in? and where do I need to put the getGrade()

  18. #18
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Printing...simple <<just newb

    I've changed a few things in your code
    if you have any problem understanding it don't be shy to ask
    Attached Files Attached Files
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  19. #19

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    10

    Re: Printing...simple <<just newb

    Thank you, I am going over it right now...Thanks for all the help.

Posting Permissions

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



Click Here to Expand Forum to Full Width