I'm still learning Java at University trying to do the exercises the teacher gives us (uncorrected, so feel free to tell me if there is a website where I would be able to practice Java)
I'm still studying collections and I was wondering, following the following, code how to write an instance c1 of Concret named "compListe" including s1 and an element instance of c1 of Atome of value 14?
Class Composant is abstract and defined the characteristics of a named given composant. It is characterized by a name and realize two interfaces Named and Valuable defined below. Two composants are equals if they have same name and same value.
Class Atome is a composant characterized by a name and a double value. An atome is equal to another atome if they have same value. The name of an atome is not given at creation. it is formed by its parent's name. followed by "_" and its value.
Abstract class Composite is defined by the characteristics of a complex composant having a collection of named elements of type Atome or Coposite. The class composite forbidden dupplicates. It realizes the interface ComposantManagement. Two objects are equals if they have same name and same elements.
Class Concret is a sublass of Composite which forbidden doublon and which is characterized by a number of double type. (by default = 1). Class Concret sort out the elements by their value and by increasing rate. The value of an instance of Concret is the maximal value of its element weighted by by the number of instanciation.
It seems to be an overly complicated example, but maybe I just feel that way because of all the French names. In any case, I also can't tell if adding an Atome to a Concret is logically correct following the assignment, so I'll stick to usage problems.
In your code:
... you use Concret in lowercase. Java is case sensitive and the class is defined (as it should be) as Concret, so you have to use it that way. The comment on addAll() method says it's coming from Collection, but I don't see that Concret inherits from Collection in any way, so that's not going to work. Also, what is s1? The problem statement mentions it, but doesn't explain what it is. Your code uses it, but doesn't show where it came from.
Thank you for your answer!
s1 is an instance of Concret of typeComposantManagement, named "simpleListe" and composed by five five elements instances of Atome which values are respectively 4, 6, 8, 10
I changed a bit my code with your remarks, but yet I still don't know how to add s1...
That's better, but you still use Concret as concret in one other place on line 1. Also, you declare the variable as c1, which is good because variables should be lowercase. However, you use it on line 3 as C1, which is uppercase. It's vital to stay consistent with casing. If you don't, your code won't even compile, which is very frustrating. The Java compiler considers C1 and c1 to be two completely different things, only one of which you've defined. To Java, they're as different as apples and oranges.