I am making a program to calculate the cost of carpet fitting. I am struggling to write a setter getter message for the following variables... labourCharge, price and postCode.
Asked
Active
Viewed 438 times
0
-
You should initiallize variables in constructor right? – Shubham Dixit Nov 24 '19 at 10:51
-
Share the class definition and attributs please – azro Nov 24 '19 at 10:51
-
@GeorgeZ. this seems yo be a setter, or change the name to getLabourCharge – azro Nov 24 '19 at 10:53
-
@azro lol, I did not notice. I just saw that and focused there. You are absolutely right. Plus, there are no arguments... – George Z. Nov 24 '19 at 10:55
3 Answers
1
This is how you should write your code. Assuming that your class is CarpetCostEstimator. The constructor takes the three values of labourCharge, postCode and price and sets them as follows:
public CarpetCostEstimator(double labourCharge, String postCode, double
price)
{
this.price = price;
this.postCode = postCode;
this.labourCharge = labourCharge;
}
public double getLabourCharge()
{
return this.labourCharge ;
}
public void setLabourCharge(double labourCharge){
this.labourCharge = labourCharge
}
In the above code, I've showed you how to put a setter and getter for labourCharge, you can do the same for the other properties of price and postCode.

Marwa Eldawy
- 191
- 2
- 5
0
You need to create a getter/setter inside the class only
public class CarpetCostEstimator {
double price = -1.0;
String postCode = "XXX-XXX";
double labourCharge = 1.0;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public double getLabourCharge() {
return labourCharge;
}
public void setLabourCharge(double labourCharge) {
this.labourCharge = labourCharge;
}
}

Damodhar
- 1,219
- 8
- 17
-
hi, many thanks for your answer. so instead of this.price, this.postCode so on and so forth I should change it to the above...double price, string postcode.public class CarpetCostEstimator { // instance variables - replace the example below with your own double price; double labourCharge; String postCode; – Nov 24 '19 at 13:27
-
You can access in a way CarpetCostEstimator obj = new CarpetCostEstimator(); obj.setPrice(10.90) and get using getter Double priceis = obj.getPrice() – Damodhar Nov 25 '19 at 04:38
-
hi how would I test it. for example how would I create an object of the class CarpetCostEstimator, passing it the value 4.0 for the labour charge. thanks – Nov 25 '19 at 12:59
-
-
-
CarpetCostEstimator obj = new CarpetCostEstimator(); obj.setPrice(10.90) and get using getter Double priceis = obj.getPrice() – Damodhar Nov 25 '19 at 14:12
-
so this is what I have so far and when I execute it it says, error class expected. CarpetCostEstimator obj = new CarpetCostEstimator(); obj.labourCharge(4.0); obj.getlabourCharge(double); – Nov 25 '19 at 17:10
-
obj.labourCharge(4.0); this function not in class there setLabourCharge method for setting labour charges – Damodhar Nov 25 '19 at 18:55
-
0
In intellij idea it is convenient to generate getter and setter by means of a combination alt+insert, at the expense of other studios I do not know

Илья Сумков
- 25
- 5