473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why wont Test 1 and Test 2 resolve?

18 New Member
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 2398
Nepomuk
3,112 Recognized Expert Specialist
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
NycCraze88
18 New Member
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 Recognized Expert Specialist
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
8008
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 following code: <html> <head> <title>Test ASP</title>
7
2234
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 authorised to view this page"). I've set <deny users="?"> in my <authorisation> bit of web.config, and ive got <authentication mode="Windows"> in...
3
3076
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 "Refresh" button in your web browser to retry your request. Administrator Note: An error message detailing the cause of this specific request...
0
1071
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 support I tried all suggested methods to resolve them
5
6338
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 will be greatly appreciated! Ive added coloured borders to try help with whats going on. Cheers...John
3
6266
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 development/test environment. Lately I've been testing the cases where my site encounters a fault to make sure everything is handled nicely. My only...
2
4945
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 Edition" via the link on: http://msdn2.microsoft.com/en-us/express/bb410792.aspx
7
1851
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
1476
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 : f";
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.