i'm trying to call a method from a different class and i just cant get it to work....

code from method displayTutorGroup():

/** 
    * Displays the names and marks for the students 
    * in the tutor group in the Display Pane
    */ 
   public void displayTutorGroup()
   {
      for (String name: this.students.keySet())      // line 1
      {
        System.out.println("Student " + name + ":"); // line 2
        //this line should call the displayMarks() method from the class student, but whatever i try doesnt work
        System.out.println();
      }
      
      
   }

Student Class code:

import java.util.*;
/**
 * Class Student - Simple class representing an OU student and their marks
 * 
 * @author M255 Course Team 
 * @version 1.0
 */

public class Student
{
   /* instance variables */
   private String name;            // String representing student's name
   private List<Integer> tmaMarks; // list of student's TMA marks.
   private int examMark;           // student's exam mark. -1 indicates exam has not been taken
   private int substitutionScore;  // student's substitution score. -1 if not yet calculated
   private int finalOCAS;          // final continuous assessment score. -1 if not yet calculated
   
   

   /**
    * Constructor for objects of class Student
    */
   public Student(String aName)
   {
      // put your code here (see part (i))
      
      name = aName;
      List<Integer> tmaMarks = new ArrayList<Integer>();
      examMark = -1;
      substitutionScore = -1;
      finalOCAS = -1;
      
   }

   
   /* instance methods */
   
   /**
    * Returns the integer average (rounded down) of the integers
    * in the list given as the argument
    */
   // put your code for the class method average() here (see part (iii)(a))
   
   
        
   /**
    * Returns the name of the receiver
    */
   public String getName()
   {
      return this.name;
   }
   
   
   
   /**
    * Sets the tmaMarks of the receiver to someTmaMarks
    */
   public void setTmaMarks(List<Integer> someTmaMarks)
   {
      this.tmaMarks = someTmaMarks;
   }
   
   
   
   /**
    * Returns the tmaMarks of the receiver
    */
   public List<Integer> getTmaMarks()
   {
      return this.tmaMarks;
   }
   
   
     
   /**
    * Sets the examMark of the receiver to aMark
    */
   public void setExamMark (int aMark)
   {
      this.examMark = aMark;
   }
   
   
     
   /** 
    * Returns the examMark of the receiver
    */
   public int getExamMark()
   {
      return this.examMark;
   }
   
   
   
   /**
    * Sets the substitutionScore of the receiver to aScore
    */
   private void setSubstitutionScore (int aScore)
   {
      this.substitutionScore = aScore;
   }
     
   
   
   /**
    * Returns the substitutionScore of the receiver
    */
   public int getSubstitutionScore()
   {
      return this.substitutionScore;
   }
   
   
   
   /**
    * Sets the finalOCAS of the receiver to anOCAS
    */
   private void setFinalOCAS (int anOCAS)
   {
      this.finalOCAS = anOCAS;
   }
   
   
     
   /**
    * Returns the finalOCAS of the receiver
    */
   public int getFinalOCAS()
   {
      return this.finalOCAS;
   }
   
   
  
   /** 
    * Displays the name and set of marks of the receiver
    */
   public void displayMarks()
   {
      System.out.println("TMA marks are " + this.tmaMarks);
      System.out.println("Exam mark is " + this.getExamMark());
      System.out.println("Substitution score is " + this.getSubstitutionScore());
      System.out.println("Final OCAS is " + this.getFinalOCAS());
      
   }
   
   

   /**
    * Calculates and sets the substitutionScore of the receiver
    */
   public void calcSubstitutionScore()   
   {
      // put your code here (see part (iii)(b)
     
   }
   
   
   
   /**
    * Calculates and sets the finalOCAS of the receiver
    */
   public void calcFinalOCAS()
   {
      // put your code here (see part (iii)(c))
      
   }
  
   /**
    * calculates the average
    */
   public Integer average()
   {
      List<Integer> userData = new ArrayList<Integer>(); 
      userData = this.getTmaMarks();
      int total = 0;
      for (int eachElement : userData)
      {
         total = total + eachElement;
      }
      int avg = ((int)total) / userData.size();
      
      System.out.println("average = " + avg);
      return avg;
   }

}

i know this is something i should have learned early in the course but i cannot remember how to do it...can anyone help?

Recommended Answers

All 6 Replies

for (String name: this.students.keySet())      // line 1
  {
    System.out.println("Student " + name + ":"); // line 2
    //this line should call the displayMarks() method from the class student, but whatever i try doesnt work
    System.out.println();
  }

In order to call a method of the class Student, you must use an Object of type Student. You are iterating through a Collection of Strings ("name" is a String) so you can't use it to call the Student class. Change your for loop to iterate over Students...

i've just managed to get it to call the method but it doesn't pick up the data from the lists. what am i doing wrong now?

thanks for the help too.... i literaly got to where i am now as you posted!!

/** 
    * Displays the names and marks for the students 
    * in the tutor group in the Display Pane
    */ 
   public void displayTutorGroup()
   {
      for (String name: this.students.keySet())      // line 1
      {
        System.out.println("Student " + name + ":"); // line 2
        Student aStudent = new Student(name);
        aStudent.displayMarks();
        System.out.println();
      }
      
      
   }

should i be creating a new Student each time? and if so why isnt it using the 'name' from the keyset?

You're creating a new Student Object, when what you want to be doing is getting the data from an existing Student Object. What exactly is students.keySet()? I don't know what "students" is, nor do I know what the keySet method does, so I can't help you until you post the relevant code.

thanks again for the help on this, as i understand it the students.keyset() is the way of returning the key/values. but......the complete code for both classes:

import java.util.*;
/**
 * Class Student - Simple class representing an OU student and their marks
 * 
 * @author M255 Course Team 
 * @version 1.0
 */

public class Student
{
   /* instance variables */
   private String name;            // String representing student's name
   private List<Integer> tmaMarks; // list of student's TMA marks.
   private int examMark;           // student's exam mark. -1 indicates exam has not been taken
   private int substitutionScore;  // student's substitution score. -1 if not yet calculated
   private int finalOCAS;          // final continuous assessment score. -1 if not yet calculated
   
   

   /**
    * Constructor for objects of class Student
    */
   public Student(String aName)
   {
      // put your code here (see part (i))
      
      name = aName;
      List<Integer> tmaMarks = new ArrayList<Integer>();
      examMark = -1;
      substitutionScore = -1;
      finalOCAS = -1;
      
   }

   
   /* instance methods */
   
   /**
    * Returns the integer average (rounded down) of the integers
    * in the list given as the argument
    */
   // put your code for the class method average() here (see part (iii)(a))
   
   
        
   /**
    * Returns the name of the receiver
    */
   public String getName()
   {
      return this.name;
   }
   
   
   
   /**
    * Sets the tmaMarks of the receiver to someTmaMarks
    */
   public void setTmaMarks(List<Integer> someTmaMarks)
   {
      this.tmaMarks = someTmaMarks;
   }
   
   
   
   /**
    * Returns the tmaMarks of the receiver
    */
   public List<Integer> getTmaMarks()
   {
      return this.tmaMarks;
   }
   
   
     
   /**
    * Sets the examMark of the receiver to aMark
    */
   public void setExamMark (int aMark)
   {
      this.examMark = aMark;
   }
   
   
     
   /** 
    * Returns the examMark of the receiver
    */
   public int getExamMark()
   {
      return this.examMark;
   }
   
   
   
   /**
    * Sets the substitutionScore of the receiver to aScore
    */
   private void setSubstitutionScore (int aScore)
   {
      this.substitutionScore = aScore;
   }
     
   
   
   /**
    * Returns the substitutionScore of the receiver
    */
   public int getSubstitutionScore()
   {
      return this.substitutionScore;
   }
   
   
   
   /**
    * Sets the finalOCAS of the receiver to anOCAS
    */
   private void setFinalOCAS (int anOCAS)
   {
      this.finalOCAS = anOCAS;
   }
   
   
     
   /**
    * Returns the finalOCAS of the receiver
    */
   public int getFinalOCAS()
   {
      return this.finalOCAS;
   }
   
   
  
   /** 
    * Displays the name and set of marks of the receiver
    */
   public void displayMarks()
   {
      System.out.println("TMA marks are " + this.tmaMarks);
      System.out.println("Exam mark is " + this.getExamMark());
      System.out.println("Substitution score is " + this.getSubstitutionScore());
      System.out.println("Final OCAS is " + this.getFinalOCAS());
      
   }
   
   

   /**
    * Calculates and sets the substitutionScore of the receiver
    */
   public void calcSubstitutionScore()   
   {
      // put your code here (see part (iii)(b)
     
   }
   
   
   
   /**
    * Calculates and sets the finalOCAS of the receiver
    */
   public void calcFinalOCAS()
   {
      // put your code here (see part (iii)(c))
      
   }
  
   /**
    * calculates the average
    */
   public Integer average()
   {
      List<Integer> userData = new ArrayList<Integer>(); 
      userData = this.getTmaMarks();
      int total = 0;
      for (int eachElement : userData)
      {
         total = total + eachElement;
      }
      int avg = ((int)total) / userData.size();
      
      System.out.println("average = " + avg);
      return avg;
   }

}

and

import java.util.*;

/**
 * Class TutorGroup - Class representing an OU tutor group
 * 
 * @author M255 Course Team 
 * @version 1.0
 */

public class TutorGroup
{
   /* instance variables */
   private SortedMap<String, Student> students; 

   /**
    * Constructor for objects of class TutorGroup
    */
   public TutorGroup()
   {
      super();
      this.students = new TreeMap<String,Student>();
   }


   /* instance methods */

   /**
    * Adds a number of students to the tutorgroup
    */
   public void populate()
   {
      String [] nameArray = {"Rebecca", "Anne", "Kiran", "Lee", "Haruki", "Tony", "Hanif", "Dylan", "Katja", "Ana"};
      Integer [][] tmaScores = {{46, 75, 64, 0},{92, 89, 94, 85},
                             {71, 80, 75, 80},{97, 95,80, 81},
                             {55, 65, 60, 60},{96, 93, 0, 88},
                             {69, 75, 64, 47},{80, 81, 92, 65},
                             {85, 92, 0, 0},{83, 89, 90, 35}};
      Integer [] examScores = {37, 86, 65, 85, 48, 87, 55, 72, -1, 70};
      for (int count = 0; count < nameArray.length; count++)
      {
         Student aStudent = new Student(nameArray[count]);   
         aStudent.setTmaMarks(Arrays.asList(tmaScores[count])); 
         aStudent.setExamMark(examScores[count]);
         students.put(nameArray[count], aStudent);
      }      
   }
    /** 
    * Displays the names and marks for the students 
    * in the tutor group in the Display Pane
    */ 
   public void displayTutorGroup()
   {
      for (String name: this.students.keySet())      // line 1
      {
        System.out.println("Student " + name + ":"); // line 2
           
           Student aStudent = new Student(name);
           aStudent.displayMarks(); //this works but doesnt use tmaScores values....
           System.out.println();
      }
      
      
   }
   
   
   
   /** 
    * Removes students with an exam mark of -1 from the tutor group
    * INCORRECT VERSION (see parts (iv)(a) and (b))
    */
   public void removeDropouts() 
   {
     int examScore;
      for (String name: this.students.keySet())
      {
         examScore = this.students.get(name).getExamMark();
         if (examScore == -1)
         {
            this.students.remove(name);
         }
      }
   }

   // add methods for parts (iv)(c) and (d) here
   
}

Your TutorGroup class has a SortedMap that appears to contain student names as keys and Student objects as values. You can use this map to look up the Student object for any given name.
The map is private, so you can't just access it from anywhere else (quite rightly!), so you could add a new public method to TutorGroup to do the lookup public Student getStudent(String name) {...}

thanks for all the help on this peeps... i've cracked it! i knew it was something stoopidly easy that i'd missed....

in the for loop this.students.get(name).displayMarks(); was all i needed!

i'm sure i'll be back on with problems later but for now thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.