• Post Reply Bookmark Topic Watch Topic
  • New Topic

constructors  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am doing an assignment for school and I would appreciate some help. They provided the Grocery class, and a fruits.csv file and I have to create a Fruit class. The questions are:

1. Write a Fruit class, as a sub-class of Grocery. It should have only one field. pricePerKilo, a double.
2. Write the following constructors for your Fruit class:

1. a constructor that takes 3 parameters. Use the super keyword
2. a constructor that only takes 2 parameters, that is it does not have a pricePerKilo parameter. use the keyword this, and set the value of pricePerKilo to -1


this is from the csv file


Name, Producer, Price
Apple, Sunny Farm, 8.25
Banana, Dale Farm, 6.30
Strawberry, Hertfordshire Farm, 12.70
Raspberry, Lancs Farm,
Lychee, Dorset Farm,
Pear, Dover Farm, 5.85
Mango, Surrey Farm, 15.20

Please see my codes and the error messages below:





Errors:

--------------------Configuration: <Default>--------------------
C:\Fruit.java:9: error: constructor Grocery in class Grocery cannot be applied to given types;
super();
^
required: String,String
found: no arguments
reason: actual and formal argument lists differ in length
C:\Fruit.java:12: error: constructor Grocery in class Grocery cannot be applied to given types;
public Fruit(String name, String producer) {
^
required: String,String
found: no arguments
reason: actual and formal argument lists differ in length
2 errors

Process completed.

 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for starters, you aren't following the instructions about what constructors to write:
Carolin Sha wrote:
1. a constructor that takes 3 parameters. Use the super keyword
2. a constructor that only takes 2 parameters, that is it does not have a pricePerKilo parameter. use the keyword this, and set the value of pricePerKilo to -1


Your answer is right there in the error message as well:

required: String,String
found: no arguments
reason: actual and formal argument lists differ in length


Look at the constructor that is provided in the Grocery class, and then look at the constructor you are invoking with super(). Note that super() is invoking a constructor and takes arguments just the same way a constructor does.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

thank you. see updated code. I am not understanding the error for the second constructor, not sure what I am missing.



Error:

--------------------Configuration: <Default>--------------------
C:Fruit.java:14: error: constructor Grocery in class Grocery cannot be applied to given types;
public Fruit(String name, String producer) {
^
required: String,String
found: no arguments
reason: actual and formal argument lists differ in length
1 error
 
Ahmed Bin S
Ranch Hand
Posts: 385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In



you do not call a superclass constructor. Therefore, Java automatically calls super() for you as the first line. However, you have no Grocery() constructor in Grocery.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, in your first constructor you are ignoring the input argument pricePerKilo.
 
Liutauras Vilda
Sheriff
Posts: 4925
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:
Make sure you use keyword "this" in the right context. You might need similar as below:
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I think you have a much more basic design problem, I am afraid
Carolin Sha wrote:. . .. . .
That code makes me suspicious, I am afraid. Surely the superclass has a price field, in which case the Fruit class should not have a price field. You can get all sorts of dangerous confusion if you have fields in the subclass of the same name as superclass fields. And the superclass field should have private access, so you should not be able to say this.price.

Let's have a look at KettlesThe electric kettle adds a kW field. I think that is dubious, adding new fields in subclasses. Note the gas kettle does not add any fields methods; they should all be implemented in the Kettle class. You can restrict instantiation of plainsimple kettels by making the Kettle class abstract. That still allows you to write:-You cannot use a getKW() method normally. This will work however:-
System.out.println(((ElectricKettle)k2).getKW());

Notice there is often no need to add fields or methods in subclasses; if you design your inheritance hierarchy properly you will be surprised how simple the subclasses are. They can be written with one constructor and nothing else.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys. please see my updated code now

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above is incorrect - it does nothing. What you want is
 
Liutauras Vilda
Sheriff
Posts: 4925
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before I go, I need to point out right away one three things.

1. pricePerKilo needs to be private - always private, unless you know exactly what you are doing.
2. line 12. There is mistake too. You don't use keyword this where it is needed.
3. line 19. Looks suspicious too. How you can have price per kilo -1 as an argument ? At least prove us that it is not wrong (the presumption of innocence cannot be applied here )
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No better, I am afraid. You are either duplicating fields or you haven't given the fields private access. If class X extends class Y then Y should have private fields, public getXXX and maybe setXXX methods, and there is no need for X to duplicate any of them. You simply call the superclass's constructors. Nothing else is necessary.
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused by your comments, Campbell.
The OP isn't duplicating any fields.

privePerKilo only exists on the Fruit class, as per the problem definition.
Grocery does not have a pricePerKilo.

Am I missing something?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:

Line 12 won't work. You have to use a different parameter name or use this on the left side of the assignment.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:. . . Grocery does not have a pricePerKilo.

Am I missing something?
No, I think I am. Thank you for pointing out my mistake.
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, I think I am. Thank you for pointing out my mistake.


No worries.
I never trust my own eyes not to miss things...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!