-
(Newbie) Multiple class problem
I am trying to write a program which i have do to for my programming class. I this program i have to use both a switch and if statements (I know this program can be written with only an if statement, but the teacher explicitly wants both). I have written the programm but it generates couple of errors.
Can help me out with this, any suggestions, hints?
class Decision{
public static decide(int range, double sales){
double bonus;
switch(range){
case 1:
if (sales ==100000)
bonus = ((sales/100)*10);
return bonus;
break;
case 2:
if ((sales >100000)&&(sales<200000))
bonus = ((sales/100)*15);
return bonus;
break;
case 3:
if ((sales >200000)&&(sales<=500000))
bonus = ((sales/100)*20);
return bonus;
break;
case 4:
if ((sales >500000))
bonus = ((sales/100)*25);
return bonus;
break;
default:
System.out.println("Error");
}
double salary = 32000;
System.out.println("You earnings are: ");
System.out.println("Salary of: "+salary);
System.out.println("Sales bonuses: "+bonus);
System.out.println("Total of: "+(bonus+salary));
}
}
class sales{
public static void main(String []args){
int range = Integer.parseInt(args[0]);
double sales = Double.parseDouble(args[1]);
Decision first = new Decision();
first.decide(range,sales);
first.output();
}
}
-
Hi Wizard,
I've changed a few things around to get it to compile (code below)
1. I have changed the methods from static to just normal instance methods - you should be sharing the data between methods and I have added two instance variables for bonus and sales
2. Having both return and break statements in the select construct is not really good practice as you can never actually get to the break, and it is creating multiple points of return in the code which is usually frowned upon. I have rejigged it to have a single return point.
3. I'm not sure whether it is the way in which it was posted, but it is good practice to have a class in a separate java file, unless it is a inner class.
4. I added the output method which was missing.
Hope this helps
Graham
----------------------------------------
Decision.java
----------------------------------------
class Decision
{
private double bonus = 0;
private double sales = 0;
public double decide(int range, double sales)
{
this.sales = sales;
switch(range)
{
case 1:
if (sales ==100000)
bonus = ((sales/100)*10);
break;
case 2:
if ((sales >100000)&&(sales<200000))
bonus = ((sales/100)*15);
break;
case 3:
if ((sales >200000)&&(sales<=500000))
bonus = ((sales/100)*20);
break;
case 4:
if ((sales >500000))
bonus = ((sales/100)*25);
break;
default:
System.out.println("Error");
break;
}
return (bonus);
}
public void output()
{
double salary = 32000;
System.out.println("You earnings are: ");
System.out.println("Salary of: "+salary);
System.out.println("Sales bonuses: "+bonus);
System.out.println("Total of: "+(bonus+salary));
}
}
----------------------------------------
sales.java
----------------------------------------
class sales
{
public static void main(String []args)
{
int range = Integer.parseInt(args[0]);
double sales = Double.parseDouble(args[1]);
Decision first = new Decision();
first.decide(range,sales);
first.output();
}
}
-
Thank you very much
I have studied the changes you made to this code and it cleared some things out to me, but this also helped me to get a very high grade on a test I had today. Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks