Assume that you are creating a class which defines a Garment object. Your class
ID: 3540050 • Letter: A
Question
Assume that you are creating a class which defines a Garment object. Your class has the following variables:
private int gType;
private int gColor;
a. Code the constructor for the Garment class which has two formal parameters whose values are assigned into the class variables.
b. Code a mutator method for the gColor variable.
c. Assume that the gType variable contains a 1 for shirt and a 2 for pants, no other values will be loaded into this variable. Likewise the gColor variable contains a 1, 2 or 3 for red, green and blue respectively. Code the toString method for the Garment class which returns a string of the form: color space type. For example if gType contains a 1 (for a shirt) and gColor contains a 2 (for green) the method should return: green shirt
Explanation / Answer
public class Garment{
private int gType;
private int gColor;
Garment(int t,int c){
gType=t;
gColor=c;
}
public void setgType(int type){
gType=type;
}
public void setgColor(int color){
gColor=color;
}
public String toString(){
String str;
if(gType==0) str="shirt";
else str="pants";
String str2;
if(gColor==0) str2="red";
if(gColor==1) str2="green";
else str2="blue";
return str2 +" "+str;
}
public static void main(String []args){
Garment a=new Garment(0,1);
System.out.println(a);
}
}