Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need explanation about objects  RSS feed

 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all , I have this exercise and I need some one to explain for me some points :

here is the exercise :

The first class :

Develop a public class MyDate to the following specifications:
• The class 3 private integer instance variables day, month, year
• The class has a multi-argument constructor that sets the value of its
instance variables to given values.
• The class has a zero-argument constructor that sets the value of its
instance variables to their default values. It should invoke its multi -
argument constructor in the same class.
• The class has the setter method setMyDate for its instance variables.
• The class has the getter method getMyDate for its instance variables in
order to return a string representation of the MyDate instance variables.

And the second Class is :

Develop a public class TicketID to the following specifications:
• The class has 2 private string instance variables flightNo, bookingRef.
• --------------The class has 1 private instance variable of the class MyDate.-----------------------------------
• -----------The class has a multi-argument constructor that sets the value of its
instance variables to given values. It should create the object of MyDate
and set the values of day, month, and year using appropriate method.-----------------
• The class has a zero-argument constructor that sets the value of its
instance variables to their default values. It should invoke its multi -
argument constructor in the same class.
• -----------------The class has the setter method setTicketID for its instance variables.-----------------
• The class has the getter method getTicketID for its instance variables in
order to return a string representation of the TicketID instance variables.


_Q:____________________________________________________

At first does I need do inherits from the first class or not ? (He didn't mention it );

Now I know everything about the first Class and I already wrote it right ,but about the second class in the (---------) marks , that points I didn't understand what he mean by it , in this line he says :

"The class has 1 private instance variable of the class MyDate"

So should I write like this :



Or not ?

In the second line about the multi-argument constructor  , what he mean by :

"It should create the object of MyDate and set the values of day, month, and year using appropriate method" ?

So the constructor will be like this or not ?



And I have no idea what I do in the zero-argument constructor ...

And about the set,get Methods how this will work for the object date ... ?

I appreciate any help .
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james leeead wrote:
At first does I need do inherits from the first class or not ? (He didn't mention it );


Well, one class is for a Date representation, and the other class is for a Ticket representation for a flight. What kinda of inheritance relationship would there be?

Henry
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james micheal wrote:here is the exercise : ...
Develop a public class TicketID to the following specifications:
• ...
• -----------------The class has the setter method setTicketID for its instance variables.-----------------
• The class has the getter method getTicketID for its instance variables in
order to return a string representation of the TicketID instance variables.

Can you please cite your sources for this exercise? Is this from a book or is it something your teacher just gave? These specifications are not good; they don't conform to how you'd normally define setter and getter methods.

In general, this is poorly constructed exercise but we can discuss why that is later. For now, please give us a better idea of where this exercise came from.
 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . Is this from a book or is it something your teacher just gave? . . . In general, this is poorly constructed exercise but we can discuss why that is later. For now, please give us a better idea of where this exercise came from.
Something my teacher gave me to solve , it's have more question on it but some of them I knew the answer but for those that I wrote here are not clear for me , I just want any example to make things clear for me .

about setter & getter method , we write them like this :



This example it's like to make a list of my journey , So I need to make something like this in final :

0 Ticket Info: Flight No: TS 223 Booking Ref.: 143 LKE Date: 5 : 3 : 2017
Passenger Name:James Seat NO: 54A
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been taught about creating objects, and about the structure of a class? Have you not been taught how to write a constructor? Have you seen the Java™ Tutorials? The nearest I can find in the Java™ Tutorials to calling other constructors is this section. Search these fora for use of the keyword this.
I am afraid the constructor you showed in your first post is incorrect; I am not sure it will even compile.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minuste ago, I wrote:. . . I am afraid the constructor you showed in your first post is incorrect; I am not sure it will even compile.
It will probably not require much work to correct it however.
Your indentation doesn't look correct. Don't double space all your code. Have a look at our suggestions about formatting.
 
Junilu Lacar
Sheriff
Posts: 10929
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get a string representation of your object, the normal practice is to override the toString() method, not to write a method like getTicketID(). The specification for the setter method, setTicketID(...) implies that it will have more than one parameter. This very unusual. Normally, setters only have one parameter: the value that you want to assign to a field.
 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want any explain for the constructor .... and thanks all .
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have searched for tutorials and not found one which was any good, I am afraid. What have you been taught about constructors?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james micheal wrote:
So the constructor will be like this or not ?




Well, you haven't showed us the complete code, so we have no idea what are the flightNo, bookingRef, and x1 fields.  However, assuming that they are simply data fields. And in the case of x1, simple getters and setters. Then... line 8, 10, and 12, doesn't actually have any noticeable effect.

Henry

 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried Googling or similar: java invoke different constructor in the same class?
I think the instructions mean that the three‑arguments constructor is in the date class.
 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
james micheal wrote:
So the constructor will be like this or not ?




Well, you haven't showed us the complete code, so we have no idea what are the flightNo, bookingRef, and x1 fields.  However, assuming that they are simply data fields. And in the case of x1, simple getters and setters. Then... line 8, 10, and 12, doesn't actually have any noticeable effect.

Henry



Ok if you assume  I did the first Class exactly as he want .

And now I want to create the second Class and I only put these code inside it :




So my Question is that right ?
Is that what he want ?

I just want to know what he mean in this inside the multi-argument constructor  ("It should create the object of MyDate and set the values of day, month, and year using appropriate method.").

How can I do that ?

And when I create the zero-argument constructor , when I invoke the  multi-argument constructor  what I do for the object x?


How can I create set Method for the object x ? or I can't do that ?

 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james micheal wrote:
So my Question is that right ?
Is that what he want ?


Well, since those lines still doesn't have any noticeable effect, I am going to say ... this is unlikely what your instructor wants.

james micheal wrote:
I just want to know what he mean in this inside the multi-argument constructor  ("It should create the object of MyDate and set the values of day, month, and year using appropriate method.").


It means that your constructor should change the fields, via its setters, and not by manipulating the fields directly.

Henry
 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
james micheal wrote:
So my Question is that right ?
Is that what he want ?


Well, since those lines still doesn't have any noticeable effect, I am going to say ... this is unlikely what your instructor wants.

james micheal wrote:
I just want to know what he mean in this inside the multi-argument constructor  ("It should create the object of MyDate and set the values of day, month, and year using appropriate method.").


It means that your constructor should change the fields, via its setters, and not by manipulating the fields directly.

Henry


Thank you for you reply .

Ok that's what I did above , I use the setters methods ...
 
Carlos Reves
Ranch Hand
Posts: 116
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@James: I've been reading this book: Java How To Program (Early Objects) (10th Edition) and it has good explanations on how to make constructors (at least i find them good...). In chapter 3 it has the introduction to the constructors, setter and getter methods, and in chapter 8 it has a case study that talks about overloaded constructors, the same your teacher is asking you to do when he says to use the multi-argument constructor in the no argument one to set the default values.

If you can find this book i think it will help you to better understand the questions.

Best regards
Carlos
 
james micheal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos Reves wrote:@James: I've been reading this book: Java How To Program (Early Objects) (10th Edition) and it has good explanations on how to make constructors (at least i find them good...). In chapter 3 it has the introduction to the constructors, setter and getter methods, and in chapter 8 it has a case study that talks about overloaded constructors, the same your teacher is asking you to do when he says to use the multi-argument constructor in the no argument one to set the default values.

If you can find this book i think it will help you to better understand the questions.

Best regards
Carlos


Thanks bro , I download it now & for sure I will read it .
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!