0

I have an abstract class Element:

public abstract class Element{
    String nom;
    int prix;

    // constructor
    public Element(String n, int p) {
        this.nom = n;
        this.prix = p;
    }

    //getters
    public String getNom() {
        return nom;
    }

    public int getPrix() {
        return prix;
    }
}

And I have a subclass Bag which extends from class Element and is characterised by its weight (poids) and color (couleur). But also a Bag contains a list of Objects (For the Object class).

So what I'm trying to do is create a arrayList<superclass> in my subclass Bag, for then being able to write code to represent these objects: an Apple (price of an apple: 7 rubies), a Banana (price = 5 rubies) and a Fish (price = 20 rubies), also objects can be added or removed to the bag.

My question is: How to assign the inherited variables to the object arraylist objectsInBag of my "Bag" subclass?

I'm using the code from this question if is good what I'm trying, how should the constructor of the subclass be created, for include the arrayList objectsInBag?

The code I've so far:

import java.util.ArrayList;

public class Bag extends Element{

// atributtes propres a Bag
private String couleur;
private Integer poids;

ArrayList<Object[]> objectsInBag = new ArrayList<>();


// constructor

public Bag(String n, Integer p, String couleur, Integer poids ) {
    
    super(n, p);
    this.couleur = couleur;
    this.poids = poids;
}

// adding object into the bag
public void addObjects(String n, Integer p){
    objectsInBag.add(new Object[]{getNom(), getPrix()});
}

// getters and setters

public String getCouleur(){
    return this.couleur;
}

public void setCouleur(String couleur){
    this.couleur = couleur;
}

public Integer getPoids(){
    return this.poids;
}

public void setPoids(Integer poids){
    this.poids = poids;
}
3
  • 1
    Why you put Object[] inside ArrayList? Commented Jan 8, 2021 at 21:21
  • Also, why exactly superclass have to be abstract? It does not have any abstract methods. Commented Jan 8, 2021 at 21:27
  • I can not modify the abstract class (is a rule of the exercice)... I'm using the Object[] cause it allows to set more than one argument to the arrayList, given that am aim to get a arrayList of the type [ {banana: 5} , {apple: 7} , {fish: 20}], and then being able to add or removed element in the arrayList Commented Jan 8, 2021 at 21:34

4 Answers 4

3

As far as I understand, you're trying to have two things:

  1. Superclass Objet (misleads a lot, better to rename to Thing or something abstract and distinct from standard java Object) with its successors: Apple, Banana, Fish
  2. Bag that is a container that can store a collection of things.

To do that, you have to have a hierarchy:

abstract class Thing{
  private final String nom;
  private final int prix;
  //+ constructor, getters
}

class Apple extends Thing {
 //+constructor that just calls `super(nom, prix)`
}

class Banana extends Thing {
 //+constructor that just calls `super(nom, prix)`
}

class Fishextends Thing {
 //+constructor that just calls `super(nom, prix)`
}

And the container Bag will be generized with type Thing just like that:

class Bag<T> {
  // atributtes propres a Bag
  private final String couleur;
  private final Integer poids;
  //+ constructor, getters
  private final List<T> list = new ArrayList<>();

  addItem(T item){
    list.add(item);
  }

  removeItem(T item){
    list.remove(item);
  }
}

You can use it like that:

Bag<Thing> bag = new Bag<>("couleur", 5);
bag.addItem(new Apple("rubies", 7));
bag.addItem(new Banana ("rubies", 5));
bag.addItem(new Fish("rubies", 20));
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but is it necessary to create a class for each thing? if then I've to create a Coconut and delete a Banana? it should be necessary to create another class for the coconut?
Not necessary. You can probably have Thing not abstract and add name field to it, so that you will call new Thing("Apple", "rubies", 7)
am working your code cause it seems to be so appropriate, anyway I'm getting this error when I "addItem" to the bag: Bag is a raw type. References to generic type Bag<T> should be parameterized
Yeah, I seem to have a typo: it should be Bag<Thing> bag = new Bag<>("couleur", 5);. Sorry :)
That is good now!, but if I want to iterate the bag, for know for example the length or for print the items inside the bag, what's the right way to do it? Am using System.out.println("Size of ArrayList = " + bag.size()); Or also: for (String item : bag){ System.out.println("Thing: " + item); } but am getting: Can only iterate over an array or an instance of java.lang.Iterable
|
1

It seems that you need to modify class Bag to have a list of BagItem instances instead of List<Object[]>. BagItem should be a superclass for Apple, Banana, Fish etc. and may have a field associating the bag item with a specific bag.

public class Bag extends Objet {

    // atributtes propres a Bag
    private String couleur;
    private Integer poids;

    List<BagItem> objectsInBag = new ArrayList<>();

    public Bag(String n, Integer p, String couleur, Integer poids ) {
        super(n, p);
        this.couleur = couleur;
        this.poids = poids;
    }

    // adding object into the bag
    public boolean addItem(BagItem item) {
        item.setBag(this);
        return objectsInBag.add(item);
    }

    // remove object from bag
    public boolean removeItem(BagItem item) {
        Objects.requireNonNull(item);
        boolean res = objectsInBag.remove(item);
        if (res) {
            item.setBag(null); // clear reference to bag if removal succeeded
        }
        return res;
    }
}
public class BagItem extends Objet {
    private Bag bag;

    public BagItem(String n, Integer p) {
        this(n, p, null); // unknown bag
    }

    public BagItem(String n, Integer p, Bag bag) {
        super(n, p);
        if (null != this.bag) {
            this.bag.addItem(this);
        }
    }

    public void setBag(Bag bag) {
        this.bag = bag;
    }
}

Example class extending a BagItem -- only constructors may need to be redefined:

public class Apple extends BagItem {
    public Apple(String n, Integer p) {
        this(n, p, null);
    }

    public Apple(String n, Integer p, Bag bag) {
        super(n, p, bag);
    }
}

Usage:

  • Create a Bag instance
  • Create concrete BagItem instances (optionally using the Bag instance)
Bag redBag = new Bag("myBag", 5, "Red", 10);
// added immediately
Apple apple1 = new Apple("Golden", 7, redBag);

// added via .addItem
Apple apple2 = new Apple("Jonagold", 8);
redBag.addItem(apple2);

System.out.println("remove golden apple: " + redBag.removeItem(apple1));

1 Comment

@MiguelGIS, updated the answer with a usage example
0

If I understood correctly you just want to include your array's initialization in your subclass's constructor. You can change you code like this:

public class Bag extends Element{

    // atributtes propres a Bag
    private String couleur;
    private Integer poids;

    // constructor
    public Bag(String n, Integer p, String couleur, Integer poids ) {
        super(n, p);
        this.couleur = couleur;
        this.poids = poids;
        ArrayList<Object[]> objectsInBag = new ArrayList<>();
    }

    // adding object into the bag
    public void addObjects(String n, Integer p){
        objectsInBag.add(new Object[]{n, p});
    }

    // getters and setters
    public String getCouleur(){
        return this.couleur;
    }

    public void setCouleur(String couleur){
       this.couleur = couleur;
    }

    public Integer getPoids(){
        return this.poids;
    }

    public void setPoids(Integer poids){
       this.poids = poids;
    }
}

Comments

0

I solved it using the answer of @MikhailKopylov and applying the principle of polymorphism:

So, the abstract class Element:

public abstract class Element{
    String nom;
    int prix;

    // constructor
    public Objet(String n, int p) {
        this.nom = n;
        this.prix = p;
    }

    //getters
    public String getNom() {
        return nom;
    }

    public int getPrix() {
        return prix;
    }

}

// the name and price of each object are already determined in the Apple, Banana and Fish classes

class Apple extends Element {
//+constructor that just calls `super(nom, prix)`
public Apple(String n, int p) {
    super("Pomme", 7);
}

}

class Banana extends Element {
//+constructor that just calls `super(nom, prix)`
public Banana(String n, int p) {
    super("Banane", 5);
}
}

class Fish extends Element {
//+constructor that just calls `super(nom, prix)`
public Fish(String n, int p) {
    super("Poisson", 20);
}
}

And the subclass Bag:

public class Bag{

// atributtes propres a Bag
private String couleur;
private Integer poids;

// constructor
public Bag(String couleur, Integer poids ) {
    this.couleur = couleur;
    this.poids = poids;
}

// getters and setters
public String getCouleur(){
    return this.couleur;
}

public void setCouleur(String couleur){
   this.couleur = couleur;
}

public Integer getPoids(){
    return this.poids;
}

public void setPoids(Integer poids){
   this.poids = poids;
}

// Polymorphism

//creation of the list of object types, which allows to go on the concept of polymorphism to access the attributes of the Object class, and to assign them to an arrayList of the Bag class

List<Element> objetos = new ArrayList<Element>();

public void addItem(Element item){
    objetos.add(item);
  }

public void removeItem(Element item){
    objetos.remove(item);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.