Brian Brian - 3 years ago 160
Java Question

Get methods in my subclass are getting errors and i cannot solve them

Here is the primary class (I am trying to build a program to check items in a stock):

* Shows various details of an item.
* @author Brian Clarke
* @version 28 sept 2017
public class Item {
// instance variables
private String id;
private String descr;
private int quantity;
private double price;
private double discount;

* Constructor for objects of class Item
public Item(String id, String descr, int quantity, double price, double discount) {
// initialise instance variables
id = id;
descr = descr;
quantity = quantity;
price = price;
discount = discount;

* Get and set methods for variables.
public String getid(String id) {
return id;
public String getdescr(String descr) {
return descr;
public int getquantity(int quantity) {
return quantity;
public double getprice(double price) {
return price;
public double getdiscount(double discount) {
return discount;
public void setid(String id) { = id;
public void setdescr(String descr) {
this.descr = descr;
public void setquantity(int quantity) {
if (quantity < 0)
quantity = 0;
this.quantity = quantity;
public void setprice(double price) {
if (price < 0)
price = 0.0;
this.price = price;
public void setdiscount(double discount) {
if (discount < 0 || discount > 0.8)
discount = 0; = discount;
public String toString() {
return "Item1{" + "ID = " + id + ", description = " + descr + ", quantity = " + quantity + ", price = " + price + ", discount = " + discount + '}';
public double computeCost() {
return (quantity * price - quantity * price * discount); // Normal price minus price by the percentage discount should give the correct discounted price

And here is the test class where I am getting the errors in the last 5 lines, e.g ( method
in class
cannot be applied to given types; required:
found: no arguments reason: actual and formal argument lists differ in length).

I am not sure how to fix them and have tried editing:

* Test the class Item
* The code tests all methods in the class Item
* @author Brian Clarke
* @version 28 Sep 2017

public class ItemTest {
public static void main(String[] args) {
// Create two objects to represent two stock items
Item item1 = new Item("ZA423", "Refurbished smartphone", 14, 149.99, 0.3);
Item item2 = new Item("BD015", "New 40' TV", 8, 299.99, 0.8);


item1.setdescr("Refurbished smarthphone");


item2.setdescr("New 40' TV");

System.out.printf("\nItem2 ID is: %s\n", item2.getid());
System.out.printf("\nItem2 Description is: %s\n", item2.getdescr());
System.out.printf("\nQuantity is: %s\n", item2.getquantity());
System.out.printf("\nPrice is: %f\n", item2.getprice());
System.out.printf("\nDiscount is: %f\n", item2.getdiscount());

The code is not complete but this is most of it. I've searched a lot but couldn't find any answers. I am very new to Java and any help would be appreciated.

Answer Source

You have defined getter methods incorrectly. In your implementation you expect to pass a parameter to each getter method, which does not make much sense, since getter method is meant to return a value. Instead

public String getid(String id) {
  return id;

you should do:

public String getid() {
  return id;

Remove all parameters from getter methods and it will work.


Two Three more things worth mentioning:

  • constructor of Item class is incorrect. Assignments like id = id; won't work, you should do = id; instead (you have to explicitly say that the id on the left side is a field in the class and id on the right side is a value passed as a constructor parameter)
  • when you use a constructor that expects all fields to be passed you can mark all fields as final and remove setter methods to prevent an instance of this class from changing its state - this is a good rule worth following
  • consider using camel-case notation. If your field name is id then getter method should be called getId and setter - setId. This is a convention widely used in Java programs.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download