Results 1 to 15 of 15
Thread: Help with classes
- 10-10-2008, 01:22 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Help with classes
I'm learning how to make classes in school and everything works fine and dandy over there but when I try to make classes at home and use them at my house i get an error
cannot find symbol constructor EmployeeGriggs(java.lang.String,int)
I have saved both files and they are in the same folder if that even matters any help?
- 10-10-2008, 01:37 AM #2
More info...
Please post the code so we can see where the problem is. Remember to wrap the text that has code section using the "CODE Tag" (third icon above from right to left).
CJSL
- 10-10-2008, 01:41 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
This is the class
This is the driverJava Code:/** *Chris Griggs *October 9 */ public class EmployeeGriggs { public String Employee;//These two variables can be used by all the methods public Double Salary; public void EmployeeGriggs(String EmployeeName, Double CurrentSalary) { Employee = EmployeeName;//takes the employees name and puts into the employee variable for use by the methods Salary = CurrentSalary;//takes the salary and puts in the salary variable for use by the methods } public String GetName(){ return Employee;//sends out whatever is in the variable at the time when called on } public double GetSalary(){ return Salary;//sends out whatever is in the variable at the time when called on } public void raiseSalary(double percent){ percent = percent/100; //divides the variabe percent by a hundred to make it into a decimal double NewSalary = Salary * percent;//creates a new variable and finds how much the increase will be Salary = NewSalary * Salary;//adds the increase and current salary together } }
Java Code:/** *Chris Griggs *October 9 */ public class EmployeeTesterGriggs { public static void main(String[] args) { EmployeeGriggs harry = new EmployeeGriggs("Harry Hacker", 50000);//intializes the EmployeeGriggs class harry.raiseSalary(10);//Raises the salary by 10 percent System.out.println(harry.GetName());//Calls on GetName to get a name System.out.println(harry.GetSalary());//Calls on GetSalary to get a salary } }
- 10-10-2008, 02:42 AM #4
Where is the error message? It has the source line number and text.
What are the types of args to the Constructor?
what types are you passing to the constructor?
Hint: 50000 is not a Double.
-
this:
is not a constructor as it has a return type of "void". A true constructor has no return type. So try removing the void and see what happens.Java Code:public void EmployeeGriggs(String EmployeeName, Double CurrentSalary)
-
- 10-10-2008, 02:51 AM #7
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Fubarable - I think you may mean casting
I did take out the void and still the same error
"cannot find symbol constructor EmployeeGriggs(java.lang.String,int)"
it is also at line 10, could there be a problem with the IDE (JCreator) that I need to do something in the options?
- 10-10-2008, 02:52 AM #8
Not sure, but maybe
Could it be that you're calling the class with the wrong type arguments? The second argument of your class has an int when it should be a double.
CJSL
- 10-10-2008, 02:56 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Ok well I changed 50000 to 50000.0 because it has a decimal point and it compiles and runs but when I call for the salary its kinda ridiculous and comes out at 2.5E8
- 10-10-2008, 02:57 AM #10
Try 50000.0
- 10-10-2008, 02:58 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
OOOOO that problem may have something to do with the salary but that's a different problem. Thanks for the help guys.
- 10-10-2008, 03:00 AM #12
It is better to use interger data types for money to prevent rounding errors. However for student exercises it doesn't matter.
Add some println() statements to the raise method to show intermediate values so you'll know what your code is doing.\
Reading the coment looks like this is the problemSalary = NewSalary * Salary;//adds ...
-
OK, it's actually called "implicit casting" and should cause no problem. The allowable implicit casts are: byte or char → short → int → long → float → double.
Again, so using an int as a parameter value will cause no problem.
for any non-believers:
Java Code:public class ImplicitCast { public ImplicitCast(double myDouble) { System.out.println("this works as myDouble has been automatically implicitly cast: " + myDouble); } public static void main(String[] args) { ImplicitCast myObj = new ImplicitCast(5000); } }
- 10-10-2008, 03:22 AM #14
Member
- Join Date
- Oct 2008
- Posts
- 34
- Rep Power
- 0
Okie dokie then now all is well thanks.
- 10-10-2008, 02:29 PM #15
The arg was Double not double. Here's what I see:
Java Code:public class TestCast { public static void method(Double d) { // code } public static void main(String[] args) { TestCast.method(1); } }/*Running: C:\Program Files\Java\jdk1.5.0_04\bin\javac.exe -cp D:\JavaDevelopment\Testing\JavaForum\;D:\JavaDevelopment -deprecation -g -Xlint TestCast.java TestCast.java:6: method(java.lang.Double) in TestCast cannot be applied to (int) TestCast.method(1); ^ 1 error Running: D:\Java\jdk1.6.0_02\bin\javac.exe -Xlint -g -deprecation -classpath D:\JavaDevelopment\;. D:\JavaDevelopment\Testing\JavaForum\TestCast.java D:\JavaDevelopment\Testing\JavaForum\TestCast.java:6: method(java.lang.Double) in TestCast cannot be applied to (int) TestCast.method(1); ^ 1 error */
Similar Threads
-
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Help with classes
By freswood in forum New To JavaReplies: 5Last Post: 04-21-2008, 03:28 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks