• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to write an instance c1 of a subclass including another instance

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Morning!

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.



I tried:



Am I right ading such an Atome?

Thanks in advance!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Antoine, welcome to Java Ranch!

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.
 
Antoine Compagnie
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
and 12.

I changed a bit my code with your remarks, but yet I still don't know how to add s1...

 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic