0

Okay so don't get turned off by the fact that I want homework help. Anyways, I don't want to explain the entire project for this little bit of help, but I'll just list some rules, and what I did with them. They're in weird order and they don't make complete sense. And there are many ways to do this probably, and I simply don't know what is what.

So here's the rules I'm confused about:

Three fields, a String for name of the purchase, int for units purchased, and a double for cost per unit.

So I made this:

private String purchase = "";
private int unitsPurchased = 0;
private double costPerUnit = 0;

Then, next rule: Standard accessors and modifier methods for each field, so I made this:

//Accessors
public String purchase(){
    return purchase;
}
public int unitsPurchased(){
    return unitsPurchased;
}
public double costPerUnit(){
    return costPerUnit;
}
//Modifiers
public void setPurchase(String purchase){
    this.purchase = purchase;
}

public void setunitsPurchased(int unitsPurchased){
    this.unitsPurchased = unitsPurchased;
}

public void setCostPerUnit(double costPerUnit){
    this.costPerUnit = costPerUnit;
}

Then this: Negative values are not allowed, so change those to zero in all cases.

Wasn't sure if that meant to do anything, so I continued on. Then this: Constructor to initialize these three fields (String, int, double) in that order.

So I did this:

public Purchase(String initialPurchase, int initialUnitsPurchased, double initialCostPerUnit){
purchase = initialPurchase;
unitsPurchased = initialUnitsPurchased;
costPerUnit = initialCostPerUnit;

}

Then this rule: Constructor overload, (String, double) assumes the int quantity is zero.

I didn't know if that meant anything, so once again I skipped it

Then this rule: Default constructor that assumes name is “” and numbers are zero, must call the three argument constructor.'

Now I'm just confused. So first of all, I would like to know if my code seems right. I don't think I need to explain the backstory of the program to do that. Then, I would love to know what to do about that last rule, because it says "must call the three argument constructor" am I supposed to use "this"? I didn't know where to go and I've tried a couple ideas but I don't think it works. I can't test to see if it's right either, given there's not really anything to test. Thanks so much to anyone who helps.

Here's just everything I've written:

    public class Purchase {
private String purchase = "";
private int unitsPurchased = 0;
private double costPerUnit = 0;
//Accessors
public String purchase(){
    return purchase;
}
public int unitsPurchased(){
    return unitsPurchased;
}
public double costPerUnit(){
    return costPerUnit;
}
//Modifiers
public void setPurchase(String purchase){
    this.purchase = purchase;
}

public void setunitsPurchased(int unitsPurchased){
    this.unitsPurchased = unitsPurchased;
}

public void setCostPerUnit(double costPerUnit){
    this.costPerUnit = costPerUnit;
}
//Default constructor
public Purchase(){
    purchase = "";
    unitsPurchased = 0;
    costPerUnit = 0;
}
//first constructor
public Purchase(String initialPurchase, int initialUnitsPurchased, double       initialCostPerUnit){
    purchase = initialPurchase;
    unitsPurchased = initialUnitsPurchased;
    costPerUnit = initialCostPerUnit;
}
//constructor overload
//Default constructor



}

You don't really need that but just in case. That's all a mess, not sure what I'm writing. But thanks to anyone that helps.

2
  • 1
    "Then this: Negative values are not allowed, so change those to zero in all cases. Wasn't sure if that meant to do anything, so I continued on." Yes it was, go back and implement it. Nothing's going to work until you do Commented Mar 14, 2016 at 0:50
  • You need to learn what constructor overloading means and how it works. Commented Mar 14, 2016 at 0:50

3 Answers 3

1

Starting with the first rule you didn't understand:

Negative values are not allowed, so change those to zero in all cases.

This is referring to the setter methods you wrote, and it means that if someone calls setUnitsPurchased with a negative number as a parameter, you only set unitsPurchased = 0.

You'll probably want to add an if statement (or ternary, if you're familiar with those) to setUnitsPurchased and setCostPerUnit checking for values below zero.


Wasn't sure if that meant to do anything, so I continued on. Then this: Constructor to initialize these three fields (String, int, double) in that order.

Instead of directly setting the values (like you did):

purchase = initialPurchase;
unitsPurchased = initialUnitsPurchased;
costPerUnit = initialCostPerUnit;

You should probably call your setters, so you don't have to repeat your checks:

this.setUnitsPurchased(initialUnitsPurchased);
// etc.

Constructor overload, (String, double) assumes the int quantity is zero.

If a class has an overloaded constructor, it means that you can initialize it with different amounts and/or types of parameters. You've already overloaded the constructor with an empty constructor and one that takes three arguments.

Simply make another constructor, but with this signature:

public Purchase(String initialPurchase, double initialCostPerUnit)

Default constructor that assumes name is "" and numbers are zero, must call the three argument constructor.

Instead of the default constructor you implemented, you must call the three argument constructor. To call another constructor, use the this keyword, and invoke it like a method, passing in the correct parameters:

this("", 0, 0);

Happy Coding!

Sign up to request clarification or add additional context in comments.

Comments

1

Couldn't be more self-explanatory. It writes itself if you can read:

public class Purchase {
    private String purchase;
    private int numUnits; 
    private double costPerUnit;

    public Purchase() {
        this("", 0, 0.0);
    }

    public Purchase(String purchase, double costPerUnit) {
        this(purchase, 0, costPerUnit);
    }

    public Purchase(String purchase, int numUnits, double costPerUnit) {
        this.purchase = purchase;
        this.numUnits = (numUnits < 0) ? 0 : numUnits; 
        this.costPerUnit = (costPerUnit < 0.0) ? 0.0 : costPerUnit;
    }

    // Leave the rest for you.
}

Comments

1
  1. Three fields, a String for name of the purchase, int for units purchased, and a double for cost per unit.

You got it correct, though there is no need for assigning any values to the data members.

private String purchase;
private int unitsPurchased;
private double costPerUnit;
  1. Standard accessors and modifier methods for each field

You got this correct too.

  1. Negative values are not allowed, so change those to zero in all cases.

You need to change your code to convert neg values to zero. e.g.

public void setunitsPurchased(int unitsPurchased){
    if(unitsPurchased < 0)
        unitsPurchased = 0;   
    this.unitsPurchased = unitsPurchased;
}
  1. Constructor to initialize these three fields (String, int, double) in that order.

You got this correct. Though you might want to have this keyword for data members. It just makes it more readable.

public Purchase(String initialPurchase, int initialUnitsPurchased, double initialCostPerUnit){
this.purchase = initialPurchase;
this.unitsPurchased = initialUnitsPurchased;
this.costPerUnit = initialCostPerUnit;
}
  1. Constructor overload, (String, double) assumes the int quantity is zero.

This is simple. you want a constructor with just 2 arguments. It also specifies to have unitsPurchased set to 0.

public Purchase(String initialPurchase, double initialCostPerUnit){
this.purchase = initialPurchase;
this.unitsPurchased = 0;
this.costPerUnit = initialCostPerUnit;
}
  1. Default constructor that assumes name is “” and numbers are zero, must call the three argument constructor.'

Now you know a default constructor does not take any arguments. but the question asks to call the three argument constructor from within this constructor. that can be done using the this() as follows:

public Purchase(){
    this("", 0, 0.0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.