overloading methods and testing them in the same class

a school project for my java course on overloading methods

Page 1 of 1

10 Replies - 1416 Views - Last Post: 28 March 2010 - 07:27 PM Rate Topic: -----

#1 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

overloading methods and testing them in the same class

Posted 27 March 2010 - 12:26 PM

I had to modify one of my school projects for another project, which means basically, I added another method to it. I am receiving one error and it is giving me headaches...can someone tell me what the problem is with my code please?

public class Commission2
{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	public static void main(String[] args)
	{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	Commission q = new Commission();
	q.computeCommission(salesFigure);
	q.computeCommission(salesFigure, commissionRateD);
	q.computeCommission(salesFigure, commissionRateI);
	}
	public void computeCommission(double salesFigure, double commissionRateD)
	{
	Results = ( salesFigure * commissionRateD);
	System.out.println("the result is " + Results );
	}
	
	public void computeCommission(double salesFigure, int commissionRateI)
	{
	commissionA = (commissionRateI/100.0);
	commissionB = (commissionA * salesFigure);
	System.out.println("The result of this procedure is " + commissionB);
	}
	public void computeCommission(double salesFigure)
	{
	commissionC = (salesFigure * 7.5%);
	System.out.println("The result of this procedure is " + commissionC);
	}
}
		



The error I am receiving is

Commission2.java:40: illegal start of expression


This puzzles me, because I was not starting an expression...I was actually ending one. I do not understand why I received this error, nor do I know how to correct it

Is This A Good Question/Topic? 0
  • +

Replies To: overloading methods and testing them in the same class

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 12:40 PM

% is the modulus operator, and must take two operands. Example:
// remainder is 0, number is even
if (a % 2 == 0)
   System.out.println("Even number!");
// etc..


More here: http://java.sun.com/.../operators.html
So, I guess you want to do something like that:
commissionC = (salesFigure * 7.5) * 100;


Is this the intent? Represent comissionC as percent.
Was This Post Helpful? 1
  • +
  • -

#3 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 01:09 PM

Okay, I fixed that error, but now I have a different one...again, I do not understand why, so I look to you more experienced java users for guidance...here is my code...

public class Commission2
{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	public static void main(String[] args)
	{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	Commission q = new Commission();
	q.computeCommission(salesFigure);
	q.computeCommission(salesFigure, commissionRateD);
	q.computeCommission(salesFigure, commissionRateI);
	}
	public void computeCommission(double salesFigure, double commissionRateD)
	{
	Results = ( salesFigure * commissionRateD);
	System.out.println("the result is " + Results );
	}
	
	public void computeCommission(double salesFigure, int commissionRateI)
	{
	commissionA = (commissionRateI/100.0);
	commissionB = (commissionA * salesFigure);
	System.out.println("The result of this procedure is " + commissionB);
	}
	public void computeCommission(double salesFigure)
	{
	commissionC = (salesFigure * 7.5) * 100;
	System.out.println("The result of this procedure is " + commissionC);
	}
}
		



here is the error I am receiving...

Commission2.java:22: cannot find symbol
symbol : method computeCommission (double)
location: class Commission
q.computeCommission(salesFigure)
 _



Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 01:21 PM

@sarmanu: Here commissionC = (salesFigure * 7.5) * 100;, this will not display as a percentage. Instead, you are multiplying saleFigure by 750. If you want to display it as a decimal, multiply it by .075. If you want it in percentage format, then multiply it by 7.5 (or .075 * 100).

@kweef19: I believe your problem comes from the fact that Commission objects do not have the computeCommission(double) method. I think you want to instantiate a Commission2 object, not a Commission object as you do here Commission2 q = new Commission2();.

Also, you are still missing the point of using classes and Objects. If you have a class with instance variables, you do not need to (and should not) declare the same variables in main(). Instead, work through your Commission2 object's instance variables. When you do this, you are saying that this Comission2 has this saleFigure, this commissionRateD, etc. When you re-declare these variables in main(), you are violating encapsulation, as those variables are not associated with the corresponding Commission2 object. While this may work for small programs, as you are working on now, as you need multiple instances of the same class, you will see how very tedious this becomes very quickly. You will soon digress to the parallel array model, which I discuss more in my tutorial on class design called Moving Away From Parallel Arrays.
public class Commission2 
{ 
        double salesFigure = 25.00; 
        double commissionRateD = 5.50; 
        int commissionRateI = 5; 
        double Results; 
        double commissionA; 
        double commissionB; 
        double commissionC; 
         
        public static void main(String[] args) 
        { 
        double salesFigure = 25.00; 
        double commissionRateD = 5.50; 
        int commissionRateI = 5; 
        double Results; 
        double commissionA; 
        double commissionB; 
        double commissionC; 


Was This Post Helpful? 1
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 01:34 PM

I literally LOL'd when I saw my mistake. Nothing more to say, *needs sleep*.
Was This Post Helpful? 0
  • +
  • -

#6 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 01:43 PM

Okay macos...I thought I had it worked out earlier, but everything u just said has thrown me for a loop. I redeclared my variables for one simple reason. The reason being that I knew it would work. I'm sure there are some etiquette issues with the way I have coded this tiny little program, but I am just doing my best to make it functional. If you can tell me a better way, I am certainly open to suggestions. You have mentioned instance variables before, but I am still unsure of how to create them and where they need to be located. I know I am pretty ignorant when it comes to java but I am really trying my best to understand it.
Here is my code revised...any help or suggestions is greatly appreciated.

public class Commission2
{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	public static void main(String[] args)
	{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	Commission2 q = new Commission();
	q.computeCommission(salesFigure);
	q.computeCommission(salesFigure, commissionRateD);
	q.computeCommission(salesFigure, commissionRateI);
	}
	public void computeCommission(double salesFigure, double commissionRateD)
	{
	Results = ( salesFigure * commissionRateD);
	System.out.println("the result is " + Results );
	}
	
	public void computeCommission(double salesFigure, int commissionRateI)
	{
	commissionA = (commissionRateI/100.0);
	commissionB = (commissionA * salesFigure);
	System.out.println("The result of this procedure is " + commissionB);
	}
	public void computeCommission(double salesFigure)
	{
	commissionC = (salesFigure * .075);
	System.out.println("The result of this procedure is " + commissionC);
	}
}
		


also if there is a tutorial on instance variables, I would love a link to it.

This post has been edited by kweef19: 27 March 2010 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 01:49 PM

also I found that by changing the commission object in the main method to commission2 I received a new error in reference to line 21 about incompatible types...this is all very confusing.

This post has been edited by kweef19: 27 March 2010 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 02:09 PM

You have actually declared instance variables. The variables below are instance variables. Notice how they are not local to a method, but are declared within the class without the keyword static. This means that each Commission2 object has the following variables associated with it. In simpler terms, each Commission2 variable can have unique values for the below variables.
public class Commission2 
{ 
        double salesFigure = 25.00; 
        double commissionRateD = 5.50; 
        int commissionRateI = 5; 
        double Results; 
        double commissionA; 
        double commissionB; 
        double commissionC; 




So to work with them, I reference them from the Commission2 object, like so:
Commission2 com = new Commission2();
com.salesFigure = 40;
com.Results = 3.5;



Really though, instance variables should be declared private, and you should provide getter and setter methods to interact with your variables. This is better practice and helps reduce the chance of unexpected errors. Like so:
class MyClass{
   private int someInstanceVar;
  
   //the constructors are used
   //to make sure all instance variables
   //are initialized before using them
   public MyClass(){
       someInstanceVar = 0;
   }

   //this is basically how getter methods work- they return the 
   //value of a variable or reference to an Object
   public int getSomeInstanceVar(){return someInstanceVar;}

    //setter methods are used to modify variables
    //here, I assign someInstanceVar the value stored in the param
   public void setSomeInstanceVar(int someVar){someInstanceVar = someVar;}
}



For more information on instance variables, check out Locke's tutorial on Class Design.

As for your error, can you post the error you are getting along with the code on line 21?
Was This Post Helpful? 0
  • +
  • -

#9 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 02:31 PM

Okay the part about instance variables makes sense, but when I only use instance variables without declaring them in the main() method as well I get several errors. as far as the error...i will state it below followed by the code I have revised so far.

here is the error...
Commission2.java:21: incompatible types
found: Commission
required: Commission2
     Commission2 q = new Commission();
                     _



and here is the code that caused this error...in full

public class Commission2
{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	public static void main(String[] args)
	{
	double salesFigure = 25.00;
	double commissionRateD = 5.50;
	int commissionRateI = 5;
	double Results;
	double commissionA;
	double commissionB;
	double commissionC;
	
	Commission2 q = new Commission();
	q.computeCommission(salesFigure);
	q.computeCommission(salesFigure, commissionRateD);
	q.computeCommission(salesFigure, commissionRateI);
	}
	public void computeCommission(double salesFigure, double commissionRateD)
	{
	Results = ( salesFigure * commissionRateD);
	System.out.println("the result is " + Results );
	}
	
	public void computeCommission(double salesFigure, int commissionRateI)
	{
	commissionA = (commissionRateI/100.0);
	commissionB = (commissionA * salesFigure);
	System.out.println("The result of this procedure is " + commissionB);
	}
	public void computeCommission(double salesFigure)
	{
	commissionC = (salesFigure * .075);
	System.out.println("The result of this procedure is " + commissionC);
	}
}



nevermind guys...I fixed this one on my own.
I simply needed to change the latter "Commission()" to "Commission2()
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10775
  • View blog
  • Posts: 40,123
  • Joined: 27-December 08

Re: overloading methods and testing them in the same class

Posted 27 March 2010 - 02:37 PM

How about q.computeCommission(q.salesFigure); instead of q.computeCommission(salesFigure);. This way, you can use the instance variables.
Was This Post Helpful? 1
  • +
  • -

#11 kweef19  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 108
  • Joined: 19-January 10

Re: overloading methods and testing them in the same class

Posted 28 March 2010 - 07:27 PM

Thanks Macos, your an amazing guy, to say the least. If I ever know half as much as you do about java I'll be completely satisfied.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1