Why wont Test 1 and Test 2 resolve?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NycCraze88
    New Member
    • Dec 2007
    • 18

    #1

    Why wont Test 1 and Test 2 resolve?

    Working on some basic code and I dont get why Test 1 and Test 2 wont resolve when I call the getAverage function in the main method....here are my classes, can someone please tell me what the problem is? Or point me in the right direction?

    Code:
    import java.util.Scanner;
    public class Student
    {
        Scanner input = new Scanner(System.in);
        // declare instance data 
        private String studentName;
        private int test1;
        private int test2;
    
    
    
        //constructor
        public Student(String sName)
        {
    	    sName = studentName; // add body of constructor
        }
    
        // inputGrades: prompt for and read in student's grades for test1 and test2.
        // Use name in prompts, e.g., "Enter's Joe's score for test1".
        public void inputGrades()
        {
            System.out.println("Enter "+studentName+"'s score for test #1");
            test1 = input.nextInt();
            while(test1<0 || test1>100)
            {
                System.out.println("Invalid number. Enter "+studentName+"'s score for test #1");
            }
            System.out.println("Enter "+studentName+"'s score for test #2");
            test2 = input.nextInt();
            while(test2<0 || test2>100)
            {
                System.out.println("Invalid number. Enter "+studentName+"'s score for test #2");
            }
    	    // add body of inputGrades
       }
    
        // getAverage: compute and return the student's test average as a double
    
        public double getAverage(int test1, int test2) // add header for getAverage
        {
        	
        	double average = (test1+test2)/2.0;  // add body of getAverage
        	return average;
        }
        
        // printName: print the student's name
    
        public void printName()   // add header for printName
        {
        	System.out.print(studentName);// add body of printName
        }
    Code:
    import java.util.*;
    
    public class Grades
    {
        public static void main(String[] args)
        {
    	Student student1 = new Student("Mary");
    	Student student2 = new Student("Mike");//create student2, "Mike"
    
    	student1.inputGrades();//input grades for Mary
    	System.out.print("The average for Mary is " +student1.getAverage(test1, test2)); //print average for Mary
    
    	System.out.println();
    
    	//student2.inputGrades();//input grades for Mike
    	System.out.print("The average for Mike is " +student2.inputGrades().getAverage(test1, test2)); //print average for Mike
    
        }
    }
    Last edited by JosAH; Oct 27 '08, 07:33 PM. Reason: added [code] ... [/code] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by NycCraze88
    Working on some basic code and I dont get why Test 1 and Test 2 wont resolve when I call the getAverage function in the main method....here are my classes, can someone please tell me what the problem is? Or point me in the right direction?
    [...]
    Code:
    import java.util.*;
    
    public class Grades
    {
        public static void main(String[] args)
        {
    	//...
    	System.out.print("The average for Mary is " +student1.getAverage(test1, test2)); //print average for Mary
    
    	//...
    	System.out.print("The average for Mike is " +student2.inputGrades().getAverage(test1, test2)); //print average for Mike
    
        }
    }
    The problem here is, that your Grades class has no way of knowing, what "test1" and "test2" are - you declared them in a different class.
    Here, look at your Student class:
    Originally posted by NycCraze88
    Code:
    import java.util.Scanner;
    public class Student
    {
        Scanner input = new Scanner(System.in);
        // declare instance data 
        private String studentName;
        private int test1;
        private int test2;
    
        //constructor
        public Student(String sName)
        {
    	    sName = studentName; // add body of constructor
        }
    
    //...
    
        // getAverage: compute and return the student's test average as a double
    
        public double getAverage(int test1, int test2) // add header for getAverage
        {
        	
        	double average = (test1+test2)/2.0;  // add body of getAverage
        	return average;
        }
    ///...
    You declared test1 and test2 as private and global, however you call getAverage with two parameters. No need for that - it knows global parameters anyway. By doing this, you are temporarily overloading the names of those variables. Can you figure out, how to fix it now?

    Greetings,
    Nepomuk

    Comment

    • NycCraze88
      New Member
      • Dec 2007
      • 18

      #3
      Yes I solved the problem and everything works fine.....the only problem Im confused about is when i compile and run the program...its supposed to print out "Enter Marys score for test 1" but instead it says "Enter null's score for test 1"...I dont understand why its replacing the name with null....its nothing serious I suppose but I would like to know how to fix it...here's my updated code:

      Code:
      public class Grades extends Student
      { 
      	public Grades(String sName) {
      		super(sName);
      	}
      
      	public static void main(String[] args) 
          { 
           Student student1 = new Student("Mary"); 
           Student student2 = new Student("Mike");//create student2, "Mike"
      
           student1.inputGrades();//input grades for Mary 
      	System.out.println("Name: "+student1.toString()+" Test #1: "+test1+" Test#2: "+test2); 
           
           System.out.print("The average for Mary is " +student1.getAverage());  //average for Mary 
          
           System.out.println(); 
      
           student2.inputGrades();//input grades for Mike 
           System.out.println("Name: "+student2.toString()+" Test #1: "+test1+" Test#2: "+test2); 
           System.out.print("The average for Mike is " +student2.getAverage());  //average for Mike 
      
          } 
      }


      Code:
      import java.util.Scanner;
      
      public class Student
      {
          Scanner input = new Scanner(System.in);
          // declare instance data 
          private String studentName;
          static int test1 = 0;
          static int test2 = 0;
      
          //constructor
          public Student(String sName)
          {
      	    sName = studentName; // add body of constructor
          }
      
          // inputGrades: prompt for and read in student's grades for test1 and test2.
          // Use name in prompts, e.g., "Enter's Joe's score for test1".
          public void inputGrades()
          {
              System.out.println("Enter " + studentName + "'s score for test #1");
              test1 = input.nextInt();
              while(test1<0 || test1>100)
              {
                  System.out.println("Invalid number. Enter "+studentName+"'s score for test #1");
              }
              System.out.println("Enter "+studentName+"'s score for test #2");
              test2 = input.nextInt();
              while(test2<0 || test2>100)
              {
                  System.out.println("Invalid number. Enter "+studentName+"'s score for test #2");
              }
      	    // add body of inputGrades
         }
      
          // getAverage: compute and return the student's test average as a double
      
          public double getAverage() // add header for getAverage
          {
          	double average = (test1+test2)/2.0;  // add body of getAverage
          	return average;
          }
          
          // printName: print the student's name
      
          public String toString()   // add header for printName
          {
          	//System.out.print(studentName);// add body of printName
      		return studentName;
          }
      
      }

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Hi again!
        Your error is in the constructor: [CODE=java]
        //constructor
        public Student(String sName)
        {
        sName = studentName; // add body of constructor
        }[/CODE] To understand this, you should know that the left variable is the one being declared and the right one is the one being read. So, you get a String sName and give it the value of studentName - I guess, you wanted to do that the other way round, right? ^^

        Greetings,
        Nepomuk

        Comment

        Working...