Basically I have been tasked with tackling the following scenario:
When you are designing your class/es, you have to decide what attributes and methods you will include in. For example, if you decide to work in millimeters, your size variable (length) could be of type integer (but later, when calculating the cost, you will have to convert the volume into square inches, because the cost is given per cubic inch of plastic (Table 2 and Table 3 of the coursework)). The volume of plastic material used will be the difference between the outer and inner volume of a pipe. If you decided to prompt the length in meters, then the type should be double, or float, etc.
Once you have validated the user order, your program should determine, based on Table 1, what is the type of the ordered pipe.
Table 1. Types of plastic pipes available.
Type Plastic’s grade Colour print Inner insulation Outer reinforcement Chemical resistance
0 1 2
I 1 – 3 YES NO NO NO NO YES/NO
II 2 – 4 NO YES NO NO NO YES/NO
III 2 – 5 NO NO YES NO NO YES/NO
IV 2 – 5 NO NO YES YES NO YES/NO
V 3 – 5 NO NO YES YES YES YES/NO
That’s all fine but the part that is getting me is this bit here:
Say in your main class you have determined that client’s order is a pipe of type I, then you can create an object of TypeI and for this object you can call the cost() method to calculate the cost and to show it to the user.
It is basically asking to not instantiate any objects before figuring out which one you need to instantiate, which is hard when it classes a big if statement in the verification as a ‘Brute force method’.
Here is what I have so far.
Main
public class Cw1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
// TODO code application logic here
Grade g1 = new Grade(1,3,true,false,false,false,false);
Grade g2 = new Grade(2,4,false,true,false,false,false);
Grade g3 = new Grade(2,5,false,false,true,false,false);
Grade g4 = new Grade(2,5,false,false,true,true,false);
Grade g5 = new Grade(3,5,false,false,true,true,true);
pipeList.add(g1);
pipeList.add(g2);
pipeList.add(g3);
pipeList.add(g4);
pipeList.add(g5);
for (Pipe p: pipeList)
{
p.setGrade(1);
p.setColour0(false);
p.setColour1(false);
p.setColour2(true);
p.setIns(true);
p.setReinf(true);
p.validate();
}
}
}
Grade (It must have abstracting in the solution)
public class Grade extends Pipe {
public Grade(int minGrade, int maxGrade, boolean hasColour0, boolean hasColour1, boolean hasColour2, boolean hasIns, boolean hasReinf) {
super(minGrade, maxGrade, hasColour0, hasColour1, hasColour2, hasIns, hasReinf);
}
}
And pipe
public abstract class Pipe {
public boolean isChemRes() {
return chemRes;
}
public void setChemRes(boolean chemRes) {
this.chemRes = chemRes;
}
public boolean isColour0() {
return colour0;
}
public void setColour0(boolean colour0) {
this.colour0 = colour0;
}
public boolean isColour1() {
return colour1;
}
public void setColour1(boolean colour1) {
this.colour1 = colour1;
}
public boolean isColour2() {
return colour2;
}
public void setColour2(boolean colour2) {
this.colour2 = colour2;
}
public double getDiameter() {
return diameter;
}
public void setDiameter(double diameter) {
this.diameter = diameter;
}
public boolean isIns() {
return ins;
}
public void setIns(boolean ins) {
this.ins = ins;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public boolean isReinf() {
return reinf;
}
public void setReinf(boolean reinf) {
this.reinf = reinf;
}
public Pipe(int minGrade, int maxGrade, boolean hasColour0, boolean hasColour1, boolean hasColour2, boolean hasIns, boolean hasReinf) {
this.minGrade = minGrade;
this.maxGrade = maxGrade;
this.hasColour0 = hasColour0;
this.hasColour1 = hasColour1;
this.hasColour2 = hasColour2;
this.hasIns = hasIns;
this.hasReinf = hasReinf;
}
public Pipe() {
}
//<editor-fold desc="Class variables">
private int grade;
private double length, diameter;
private boolean colour0, colour1, colour2, ins, reinf, chemRes;
private int minGrade, maxGrade;
private boolean hasColour0, hasColour1, hasColour2, hasIns, hasReinf;
// </editor-fold>
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
//<editor-fold desc="Public Methods">
public double calcVol()
{
return 0;
}
public double calcCost()
{
return 0;
}
public void validate()
{
if ((grade >= minGrade && grade <= maxGrade) & (colour0 == true && hasColour0 || colour1 == true && hasColour1 || colour2 == true && hasColour2) && (ins == hasIns) && (reinf == hasReinf))
{
System.out.print("True");
}
else
{
System.out.print("False");
}
}
// </editor-fold>
}
So basically, I don’t understand how I could achieve the same result without instantiating the objects before hand and validating them?
The class isn’t high level, we have only just learned polymorphism.
Usually, the data which tells you which objects to create comes from an external source: a file, a socket, another object etc. In your case, you could use a text file. Create the
Gradeinstances passing the values you read to the constructor and then callvalidateandcoston each.