Alright, I'll go through your code and fix it, so long as everything I tell you you'll take to heart and use in your future program code. These things are important.
First, perform proper formatting on your code. What I mean by this is you shouldn't have just some kind of return X function after your functions, that makes it more time consuming to write changes and it's not as obvious to someone reading it what your code is doing. Observe below, all I did was improve the formatting:
[highlight=Java]public class Policy
{
private static int NEXT_POLICY_NUMBER;
private int policyNumber;
protected int amount; // Financial value should NEVER be measured with float!
//Constructors
public Policy(float a)
// If the constructor, or any function or loop, is intentionally blank,
// you should say so in the function or loop.
{ /* Intentionally blank */ }
//Get method
public static int getNEXT_POLICY_NUMBER()
{
// This should be a private function, since policyNumber is private.
return NEXT_POLICY_NUMBER;
}
public int getPolicyNumber()
{
return this.policyNumber;
}
public float getAmount()
{
return this.amount;
}
//Set method
public void setNEXT_POLICY_NUMBER(int numberToSet)
{
// You should NEVER be able to "set" the value of an ALLCAPS
// variable. Generally, that means the variable is const.
NEXT_POLICY_NUMBER = 1;
// Also, you had a NEXT_POLICY_NUMBER variable in your class,
// then named the variable as a parameter to this function the
// same thing, which while syntactically correct, is a BIG no-no.
// Unless, of course, you like code obfuscation. ALWAYS name
// variables something unique, you shouldn't have to ever explicitly
// say "this." to access a variable.
}
public void setPolicyNumber(int newPolicyNumber)
{
this.policyNumber = newPolicyNumber;
}
public void setAmount(float a)
{
// You really shouldn't have this function if you can go without.
this.amount = a;
}
//toString Method
@Override
public String toString()
{
return ("Policy:" + this.policyNumber + "amount:" + "$" + this.amount);
}
public boolean isExpired()
{
return false;
}
}[/highlight]
Alright, I commented the code as well. Notice how much easier it is to read this program code? You may notice that I also took the liberty to clear any unnecessary whitespace and change tabs to spaces. I'd suggest you always stick to spaces instead of tabs, since tabs can be of different sizes in different programs, and may not look right to other programmers if and when you work with them. Because of this, I'd suggest sticking to spaces unless you and the programmers you're working with have
explicitly decided to stick with tabs. Now, the first part of the actual program code I'll fix is the constructor, and I'll be adding a new constructor. :)
[highlight=Java] //Constructors
public Policy(double a)
{
/**
* This is the double/float version of the constructor. It should
* interpret everything after the decimal as change, not bills.
* The way it does this is by simply multiplying the value sent
* to this function by 100, then casting it as an int and sending
* that value to the other constructor.
*/
this((int) (a * 100));
}
public Policy(int a)
{
/**
* This is the int version of the constructor. It treats the int
* value as if it were all change, no bills. So that means that
* if a = 10000, then the value assigned to amount is also
* 10000, but when the value is printed, it should show "$100.00"
*/
this.amount = a;
this.policyNumber = getNEXT_POLICY_NUMBER();
this.expired = false; // You'll see where this comes in later...
}[/highlight]
The next thing that I'll need to address is your getNEXT_POLICY_NUMBER function, since it should increment the value of NEXT_POLICY_NUMBER each time it's run to get a unique policyNumber value for each object.
[highlight=Java] private int getNEXT_POLICY_NUMBER()
{
// This should be a private function, since policyNumber is private.
return NEXT_POLICY_NUMBER++;
}[/highlight]
Next, let's clear out some functions. I eliminated your setNEXT_POLICY_NUMBER function entirely, since I can find no good reason for you to have it. I also eliminated setPolicyNumber, since policyNumber is entirely managed internally of the class. If you want to change how policy numbers work, you should change the code.
Alright, now I'm going to make a few other minor changes, and I'll explain why:
// You should explicitly give NEXT_POLICY_NUMBER a starting value.
private static int NEXT_POLICY_NUMBER = 1;
private int policyNumber;
// I made amount private since you already have public getAmount
// and setAmount functions. You don't need access, so don't allow it.
private int amount;
// I added another value, to represent an expired policy.
private boolean expired;
public String getAmount()
{
// Let's change getAmount to return a string.
int aval = this.amount % 100;
String retstr = "$" + (this.amount / 100) + ".";
if (aval < 10)
return retstr + "0" + aval;
else
return retstr + aval;
}
public void setAmount(double a)
{
// We'll have two of these, a float and an int version, just like
// the constructor.
this.setAmount((int) (a * 100));
}
public void setAmount(int a)
{
this.amount = a;
}
public String getPolicyNumber()
{
// Let's change getPolicyNumber to return a string as well.
if (this.policyNumber / 10 == 0)
return "000" + this.policyNumber;
else if (this.policyNumber / 100 == 0)
return "00" + this.policyNumber;
else if (this.policyNumber / 1000 == 0)
return "0" + this.policyNumber;
else
return "" + this.policyNumber;
}
public boolean isExpired()
{
return this.expired;
}
Finally, we change the toString() method to reflect everything we've done.
[highlight=Java] @Override
public String toString()
{
return "Policy: " + this.getPolicyNumber() + "; amount: " + this.getAmount();
}[/highlight]
If you've followed along, the completed source code should be obvious. I won't post it, since you'll need to read what I said to find out, and I know it's long, but I'd much prefer you become a better programmer than simply have copy/paste at your disposal. Take a look at the above code and see if you can make sense of it.