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? -
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
-
}
-
-
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
-
-
}
-
}
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?
[...] -
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: -
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
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: -
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
-
-
}
-
}
-
-
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;
-
}
-
-
}
-
Nepomuk 3,112
Recognized Expert Specialist
Hi again!
Your error is in the constructor: -
//constructor
-
public Student(String sName)
-
{
-
sName = studentName; // add body of constructor
-
}
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
|
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...
|
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...
|
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
|
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
| |
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...
|
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
|
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);
|
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";
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |