473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Student
  3. {
  4.     Scanner input = new Scanner(System.in);
  5.     // declare instance data 
  6.     private String studentName;
  7.     private int test1;
  8.     private int test2;
  9.  
  10.  
  11.  
  12.     //constructor
  13.     public Student(String sName)
  14.     {
  15.         sName = studentName; // add body of constructor
  16.     }
  17.  
  18.     // inputGrades: prompt for and read in student's grades for test1 and test2.
  19.     // Use name in prompts, e.g., "Enter's Joe's score for test1".
  20.     public void inputGrades()
  21.     {
  22.         System.out.println("Enter "+studentName+"'s score for test #1");
  23.         test1 = input.nextInt();
  24.         while(test1<0 || test1>100)
  25.         {
  26.             System.out.println("Invalid number. Enter "+studentName+"'s score for test #1");
  27.         }
  28.         System.out.println("Enter "+studentName+"'s score for test #2");
  29.         test2 = input.nextInt();
  30.         while(test2<0 || test2>100)
  31.         {
  32.             System.out.println("Invalid number. Enter "+studentName+"'s score for test #2");
  33.         }
  34.         // add body of inputGrades
  35.    }
  36.  
  37.     // getAverage: compute and return the student's test average as a double
  38.  
  39.     public double getAverage(int test1, int test2) // add header for getAverage
  40.     {
  41.  
  42.         double average = (test1+test2)/2.0;  // add body of getAverage
  43.         return average;
  44.     }
  45.  
  46.     // printName: print the student's name
  47.  
  48.     public void printName()   // add header for printName
  49.     {
  50.         System.out.print(studentName);// add body of printName
  51.     }
  52.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Grades
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.     Student student1 = new Student("Mary");
  8.     Student student2 = new Student("Mike");//create student2, "Mike"
  9.  
  10.     student1.inputGrades();//input grades for Mary
  11.     System.out.print("The average for Mary is " +student1.getAverage(test1, test2)); //print average for Mary
  12.  
  13.     System.out.println();
  14.  
  15.     //student2.inputGrades();//input grades for Mike
  16.     System.out.print("The average for Mike is " +student2.inputGrades().getAverage(test1, test2)); //print average for Mike
  17.  
  18.     }
  19. }
Oct 27 '08 #1
3 2392
Nepomuk
3,112 Expert 2GB
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?
[...]
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class Grades
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.     //...
  8.     System.out.print("The average for Mary is " +student1.getAverage(test1, test2)); //print average for Mary
  9.  
  10.     //...
  11.     System.out.print("The average for Mike is " +student2.inputGrades().getAverage(test1, test2)); //print average for Mike
  12.  
  13.     }
  14. }
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:
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Student
  3. {
  4.     Scanner input = new Scanner(System.in);
  5.     // declare instance data 
  6.     private String studentName;
  7.     private int test1;
  8.     private int test2;
  9.  
  10.     //constructor
  11.     public Student(String sName)
  12.     {
  13.         sName = studentName; // add body of constructor
  14.     }
  15.  
  16. //...
  17.  
  18.     // getAverage: compute and return the student's test average as a double
  19.  
  20.     public double getAverage(int test1, int test2) // add header for getAverage
  21.     {
  22.  
  23.         double average = (test1+test2)/2.0;  // add body of getAverage
  24.         return average;
  25.     }
  26. ///...
  27.  
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
Oct 27 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1. public class Grades extends Student
  2.     public Grades(String sName) {
  3.         super(sName);
  4.     }
  5.  
  6.     public static void main(String[] args) 
  7.     { 
  8.      Student student1 = new Student("Mary"); 
  9.      Student student2 = new Student("Mike");//create student2, "Mike"
  10.  
  11.      student1.inputGrades();//input grades for Mary 
  12.     System.out.println("Name: "+student1.toString()+" Test #1: "+test1+" Test#2: "+test2); 
  13.  
  14.      System.out.print("The average for Mary is " +student1.getAverage());  //average for Mary 
  15.  
  16.      System.out.println(); 
  17.  
  18.      student2.inputGrades();//input grades for Mike 
  19.      System.out.println("Name: "+student2.toString()+" Test #1: "+test1+" Test#2: "+test2); 
  20.      System.out.print("The average for Mike is " +student2.getAverage());  //average for Mike 
  21.  
  22.     } 
  23. }
  24.  


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Student
  4. {
  5.     Scanner input = new Scanner(System.in);
  6.     // declare instance data 
  7.     private String studentName;
  8.     static int test1 = 0;
  9.     static int test2 = 0;
  10.  
  11.     //constructor
  12.     public Student(String sName)
  13.     {
  14.         sName = studentName; // add body of constructor
  15.     }
  16.  
  17.     // inputGrades: prompt for and read in student's grades for test1 and test2.
  18.     // Use name in prompts, e.g., "Enter's Joe's score for test1".
  19.     public void inputGrades()
  20.     {
  21.         System.out.println("Enter " + studentName + "'s score for test #1");
  22.         test1 = input.nextInt();
  23.         while(test1<0 || test1>100)
  24.         {
  25.             System.out.println("Invalid number. Enter "+studentName+"'s score for test #1");
  26.         }
  27.         System.out.println("Enter "+studentName+"'s score for test #2");
  28.         test2 = input.nextInt();
  29.         while(test2<0 || test2>100)
  30.         {
  31.             System.out.println("Invalid number. Enter "+studentName+"'s score for test #2");
  32.         }
  33.         // add body of inputGrades
  34.    }
  35.  
  36.     // getAverage: compute and return the student's test average as a double
  37.  
  38.     public double getAverage() // add header for getAverage
  39.     {
  40.         double average = (test1+test2)/2.0;  // add body of getAverage
  41.         return average;
  42.     }
  43.  
  44.     // printName: print the student's name
  45.  
  46.     public String toString()   // add header for printName
  47.     {
  48.         //System.out.print(studentName);// add body of printName
  49.         return studentName;
  50.     }
  51.  
  52. }
  53.  
Oct 29 '08 #3
Nepomuk
3,112 Expert 2GB
Hi again!
Your error is in the constructor:
Expand|Select|Wrap|Line Numbers
  1. //constructor
  2. public Student(String sName)
  3. {
  4.     sName = studentName; // add body of constructor
  5. }
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
Oct 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Catherine | last post by:
I am having a problem viewing asp pages on iis version 5.1 xp pro. HTML pages are viewable on http://localhost but .asp pages are not. I have created a test program called timetest.asp with the...
7
by: dhnriverside | last post by:
Hi guys I've got a web application on a Win2k IIS5 server. I've been coding it using anonymous access, and have just come to test the AD stuff - trouble is - all i get is a 402.1 error ("not...
3
by: Brian Henry | last post by:
every time i try to run my asp.net 2.0 app it says Server Application Unavailable The web application you are attempting to access on this web server is currently unavailable. Please hit the...
0
by: Gancy | last post by:
Hi, I have a .net setup project, on building throws following error An error occurred while validating. HRESULT = '80004002' I followed many threads on similar issues on google groups, MS...
5
by: john_aspinall | last post by:
http://www.ainewmedia.co.uk/test.htm Im learning CSS based layout and Im pulling my hair out as to why wont my banner DIV float to the right of my login box? It works fine in IE. Any help...
3
Frinavale
by: Frinavale | last post by:
Hi there, I am currently behind a firewall that wont let me send emails. As a result my smtpClient.send(emailMessage) wont work on my IIS but this method does work through the VisualStudio...
2
by: samadams_2006 | last post by:
Hello, I have a problem that I'm hoping someone will be able to help me resolve. 1) I have a C# Web Site in which I connect to the database: "Install Microsoft SQL Server 2005 Express...
7
by: jamaicaboy | last post by:
This is the code that i need some help with........... #include<iostream.h> #include<iomanip.h> class test { public: void store( int);
6
by: gopalsd | last post by:
Hi Friends, Can anyone pls help me to resolve my school test in PERL................ as follows..... #!/usr/bin/perl @data=N; $sum=0; print"enter the required No. of numbers to be inputed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.