Jump to content

Help fix the output

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
450081592

450081592

    Newbie

  • Members
  • PipPip
  • 13 posts
The Policy Class

Define a public class called Policy which has a private static/class variable
called NEXT_POLICY_NUMBER which is an int (initially set to 1) representing
the number to be given to the next policy created. Create the following attributes
in the class as well:

• a private attribute called policyNumber of type int that identifies the policy with a unique integer.
• a protected attribute called amount that contains the amount (a float) of coverage for the policy.

Create a public constructor which takes a single float parameter and uses it to set the amount variable. The constructor sets the policyNumber such that each created policy has a unique number and also updates the class variable appropriately.

Create public get methods for your attributes as well as a public toString() method that returns a String with the following format (use String.format() for both the policy number and amount) :
Policy: 0001 amount: $320.00

Create a public instance method called isExpired() which always returns false. Now test your code with this program:

public class PolicyTestProgram {

    public static void main(String args[]) {

          System.out.println(new Policy(320)); // displays Policy: 0001 amount: $320.00

          System.out.println(new Policy(500.1f)); // displays Policy: 0002 amount: $500.10

          System.out.println(new Policy(0)); // displays Policy: 0003 amount: $0.00

          System.out.println(new Policy(320).isExpired()); // displays false

     }

}

Edited by 450081592, 22 November 2009 - 02:35 PM.
add code tags (the # button)


#2
450081592

450081592

    Newbie

  • Members
  • PipPip
  • 13 posts
Here is my code
public class Policy {
	private static int  NEXT_POLICY_NUMBER;
	private int  policyNumber;
	protected float  amount;
	
	//Constructors
	public Policy(float a) {}
	
	//Get method
	public static int getNEXT_POLICY_NUMBER() { return  NEXT_POLICY_NUMBER; }
	public int getPolicyNumber() { return  this.policyNumber; }
	public float getAmount() { return  this.amount; }
	
	//Set method
	public void setNEXT_POLICY_NUMBER(int NEXT_POLICY_NUMBER) { NEXT_POLICY_NUMBER = 1;}
	public void setPolicyNumber(int newPolicyNumber) { this.policyNumber = newPolicyNumber; }
	public void setAmount(float a) { this.amount = a; }
	
	//toString Method
	public String toString() {
		return ("Policy:" + this.policyNumber + "amount:" + "$" + this.amount);
	}
		
		
	public boolean isExpired(){
		return false;
	}	
		
}


and my output is

Policy:0amount:$0.0
Policy:0amount:$0.0
Policy:0amount:$0.0
false

Process completed.


I have no idea why the numbers are not changing, the don't know how to do the string format

Edited by WingedPanther, 20 November 2009 - 05:30 AM.
add code tags (the # button)


#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your constructor doesn't do anything.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
450081592

450081592

    Newbie

  • Members
  • PipPip
  • 13 posts

WingedPanther said:

Your constructor doesn't do anything.

ok I have fixed the constructor, this is my new outout

Policy:0amount:$320.0
Policy:0amount:$500.1
Policy:0amount:$0.0
false

Process completed.


still not displaying the policynumbeer, why? and help me with the formatting

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What's your constructor now?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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.
Wow I changed my sig!

#7
450081592

450081592

    Newbie

  • Members
  • PipPip
  • 13 posts
I don't understand the codes you wrote, makes me more confused, which one is the proper one?

#8
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
What do you mean "which one is the proper one"? I split everything up intentionally so you wouldn't be able to copy/paste the code. I want you to read it and learn from it, is there anything in particular that confuses you or are you simply not sure how to put everything together?
Wow I changed my sig!

#9
450081592

450081592

    Newbie

  • Members
  • PipPip
  • 13 posts

ZekeDragon said:

What do you mean "which one is the proper one"? I split everything up intentionally so you wouldn't be able to copy/paste the code. I want you to read it and learn from it, is there anything in particular that confuses you or are you simply not sure how to put everything together?

oh ok, I c, I fixed my code
public class Policy {
	private static int  NEXT_POLICY_NUMBER = 1;
	private int  policyNumber;
	protected float  amount;
	
	//Constructors
	public Policy(float a) {
		this.policyNumber = 0;
		this.amount = a;
        this.policyNumber = NEXT_POLICY_NUMBER;  
        NEXT_POLICY_NUMBER++;	 	
	}
	
	//Get method
	public int getPolicyNumber() { return  this.policyNumber; }
	public float getAmount() { return  this.amount; }
	

	
	//Set method
	public void setPolicyNumber(int newPolicyNumber) { this.policyNumber = newPolicyNumber; }
	public void setAmount(float a) { this.amount = a; }
	
	
	//toString Method
	public String toString() {
		return ("Policy:" + String.format("%,04i", this.policyNumber) + "amount:" + "$" + String.format ("%,3.2f", this.amount));
	}
		
		
	public boolean isExpired(){
		return false;
	}
		
now, it display the output but the format gerenates UnknownFormatConversionException

Edited by Jaan, 21 November 2009 - 05:57 AM.
Please use code tags when you are posting your codes!