<< Chapter < Page Chapter >> Page >

Normally in an object-oriented program, all the fields of an object are private and an accessor method like getPrice() is used to access the values of the fields. If this is done, the computation of the price can be placed inthe accessor for the superclass and overridden in accessor for the subclass.

Check this by executing the code and ensuring that the discounted price is computed.

The disadvantage of this solution is that the computation is performed for each access of the field price .

Exercise Develop other solutions for this problem: (a) Call computePrice explicitly after the call to the constructor; (b) Modify getPrice to compute the value of price on the first call and save it for future calls. Summarize the advantages anddisadvantages of all the solutions for this problem.

Constructors with object parameters

Concept An object can contain fields of other user-defined objects, not just of primitive and predefined types. There is nodifference in the constructors, except that references to objects are passed as actual parameters and assigned to fields of the object.

Program: Constructor07.java

// Learning Object Constructor07 //    constructors with object parametersclass Song {     String name;    int seconds;     double pricePerSecond;      Song(String n, int s, double p) {        name = n;         seconds = s;        pricePerSecond = p;     }      public double computePrice() {        return seconds * pricePerSecond;     }}  class SongSet {     public Song track1, track2;      public SongSet(Song t1, Song t2) {        track1 = t1; track2 = t2;     }}  public class Constructor07 {     public static void main(/*String[] args*/) {         Song song1 = new Song("Waterloo", 164, 0.01);        Song song2 = new Song("Fernando", 253, 0.01);         SongSet set = new SongSet(song1, song2);        double price1 = set.track1.computePrice();         double price2 = set.track2.computePrice();    } }

Two objects of type Song are allocated and assigned to fields of another object of type SongSet which has two fields of type Song .

  • Execute the program until the two objects of type Song are allocated and their references assigned to the variables song1 and song2 . (You may want to select Animation / Run Until (ctrl-T) to skip the animation of these declarations.)
  • A variable set is allocated. An object of type SongSet is allocated with default null fields.
  • The constructor for SongSet is called and the references in the two variables song1 and song2 are passed as actual parameters. These references are stored in the two fields track1 and track2 .
  • The reference to the object of class SongSet is returned and stored in set .
  • The prices of the two objects are obtained and stored in the variables price1 and price2 . set is an object of type Songset , while set.track1 is an object of type Song and thus can be used to call the method computePrice .

Exercise Modify the program so that the variables song1 and song2 are not used; instead, the constructors for the songs are embedded within the constructor call for SongSet .

Exercise Modify the program so the constructors for the songs are call within the constructor for SongSet . Under what circumstances would this be done?

Constructors with subclass object parameters

Concept An object of a subclass is also an object of the type of the superclass. Therefore, it can be used when an actual parameter is expected.

Program: Constructor08.java

// Learning Object Constructor08 //    constructors with subclass object parametersclass Song {     String name;    int seconds;     double pricePerSecond;      Song(String n, int s, double p) {        name = n;         seconds = s;        pricePerSecond = p;     }      public double computePrice() {        return seconds * pricePerSecond;     }}  class DiscountSong extends Song {     double discount;      DiscountSong(String n, int s, double p, double d) {        super(n, s, p);         discount = d;    }      public double computePrice() {         return seconds * pricePerSecond * discount;    } }  class SongSet {    public Song track1, track2;      public SongSet(Song t1, Song t2) {         track1 = t1; track2 = t2;    } }  public class Constructor08 {    public static void main(/*String[] args*/) {        Song song1 = new Song("Waterloo", 164, 0.01);         DiscountSong song2 = new DiscountSong("Fernando", 253, 0.01, 0.8);        SongSet set = new SongSet(song1, song2);         double price1 = set.track1.computePrice();        double price2 = set.track2.computePrice();     }}

We allocate two objects, one of type Song and one of type DiscountSong , and use them as actual parameters in the constructor for an object of type SongSet that expects two parameters of type Song .

  • Execute the program until the two objects one of type Song the other of type DiscountSong are allocated and their references assigned to the variables song1 and song2 , respectively.(You may want to select Animation / Run Until (ctrl-T) to skip the animation of these declarations.)
  • The variable set is allocated, and an object of type SongSet is allocated with default null fields.
  • The constructor for SongSet is called and the references in the two variables song1 and song2 are passed as actual parameters. These references are stored in the two fields track1 and track2 .
  • The reference to the object of class SongSet is returned and stored in set .
  • The prices of the two objects are obtained and stored in the variables price1 and price2 . set is an object of type Songset , while set.track1 is an object of type Song and thus can be used to call the method computePrice of that class. Similarly for price2 , except that set.track2 is an object of type DiscountSong ; check that the method computePrice of this class is called.

Exercise Can s2 in the main method be declared to be of type Song ? Explain.

Questions & Answers

what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
preparation of nanomaterial
Victor Reply
how to synthesize TiO2 nanoparticles by chemical methods
Zubear
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask