Results 1 to 2 of 2
Thread: I Need Help >_<
- 04-26-2009, 07:55 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 1
- Rep Power
- 0
I Need Help >_<
Hello. I need help.
Ill go straight to the point:
I have 3 classes:
The first one, AssembledPart, is a sub- class of Part. In this, please pay attention to the 3 static variables and method.public class AssembledPart extends Part
{
static Part part0 = new Part(null,null,0, 0);
static Part part1 = new Part(null,null, 0, 0);
public AssembledPart(String ID,String name, int stockLevel,double unitPrice,Part p0,Part p1)
{
super( ID,name,stockLevel,unitPrice);
part0=p0;
part1=p1;
}
static public int getAvailForAssembly()
{
int min;
if(part0.getStockLevel() >= part1.getStockLevel())
min=part1.getStockLevel();
else
min=part0.getStockLevel();
return min;
}
}
The second one is the super class, Part:
Please note the supply method, in which used the static method and variable in the AssembledPart class. And I think it is necessary.public class Part{
private String ID;
private String name;
private int stockLevel;
private double unitPrice;
/**
* Constructor
*/
public Part()
{
ID = null;
name=null;
stockLevel = 1;
unitPrice=1.0;
}
public Part(String ID, String name, int stockLevel, double unitPrice){
this.ID = ID;
this.name = name;
this.stockLevel = stockLevel;
this.unitPrice = unitPrice;
}
public String getID(){
return ID;
}
public String getName(){
return name;
}
public int getStockLevel(){
return stockLevel;
}
public double getUnitPrice(){
return unitPrice;
}
/**
* Replenish the inventory
*/
public void replenish(int qty){
stockLevel += qty;
}
/**
* Suppply.
* Return the cost of the supply if there's enough in stock
* otherwise return -1.0
*/
public double supply(int qty){
if (qty <= stockLevel){
stockLevel = stockLevel - qty;
return (qty * unitPrice);
}
else if (qty <= stockLevel+AssembledPart.getAvailForAssembly()){
AssembledPart.part0.stockLevel= AssembledPart.part0.stockLevel - qty + stockLevel;
AssembledPart.part1.stockLevel= AssembledPart.part1.stockLevel - qty + stockLevel;
stockLevel=0;
return (qty * (unitPrice + AssembledPart.part0.unitPrice + AssembledPart.part1.unitPrice));
}
else
return -1;
}
}
It will have no problem if I dont have the 3rd class:
The problem is here: When I declare an array for Assembled Part, please notice the last 2 arguments, which are P[0] and P[1], then P[2] and P[3]. because of static variable I declared earlier in AssembledPart class, when I declare AP[1] with P[2] and P[3], AP[0]'s 2 last arguments, which suppose to be P[0] and P[1], change in to P[2] and P[3].public class Main {
public static void main(String[] args) {
Part[] P = new Part[4];
P[0] = new Part("p101", "Crank", 218, 12.20);
P[1] = new Part("p102", "Pedal", 320, 14.30);
P[2] = new Part("p103", "HandleBar", 120, 35.50);
P[3] = new Part("p104", "Stem", 90, 20.00);
AssembledPart[] AP = new AssembledPart[2];
AP[0] = new AssembledPart("p183", "Crank-Pedal", 80, 3.50, P[0], P[1]);
AP[1] = new AssembledPart("p184", "HandleBar-Stem", 30, 1.50, P[2], P[3]);
......
}
}
Now, I dont know what to do. If I dont declare variable and method in AssembledPart as static, I will be able to do Main class, but unable to pass the method and variable to Part class. If I do, then the Main class will not run.
HELP ME PLEASE!! This is urgent.
MANY thanks.
- 04-27-2009, 04:14 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
your program seems containing logic problem...
super class use subclass object in its method...?
you better reconstruct your program...
if you dont...
i suggest to use non-static variable and non-static method in AssembledPart
and supply method in Part, you add to accept 2 more input parameter Part p1, Part p2


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks