aspose file tools*
The moose likes Beginning Java and the fly likes another newbie needs help...:-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "another newbie needs help...:-(" Watch "another newbie needs help...:-(" New topic
Author

another newbie needs help...:-(

Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
hi ....i need help in the following.
this is a question from my assignment i have done it as much as i can but am unsure of the answers...can someone pls check and tell me if i have done rite ???..........
here is the question..
Create a class named itemtype that following attributes as members
Attributes are ? item_name of type string
cost of type float
qty of type int
i.Is it possible to create an instance of itemtype without a constructor implemented?
Explain why for not; otherwise

my answer: No . it is because there would be nothing to take in the values.

ii.Implement a constructor that takes necessary arguments for member variables to be initialized
my answer
iii.Show you can overload the constructor by implementing another constructor in itemtype class.
my answer : however this does not work when i compile it...i get the following error
F:\j2sdk1.4.0_03\bin>javac itemtype.java
itemtype.java:15: possible loss of precision
found : double
required: float
price = 5.00;
^
itemtype.java:26: cannot resolve symbol
symbol : constructor itemtype (java.lang.String)
location: class itemtype
products = new itemtype("shit");
^
2 errors
F:\j2sdk1.4.0_03\bin>
can somebody please guide me.......thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay, let's take this a step at a time...
First, this question:

Is it possible to create an instance of itemtype without a constructor implemented?
Explain why for not; otherwise
my answer: No . it is because there would be nothing to take in the values.

That's true, if you don't implement a constructor, you can't pass any values in, but could you still create an itemtype object? How about this code:

Try compiling that and see what happens. Does it fail? Does it succeed? Why do you think it did what it did?
Okay, for now, let's go forward...

ii.Implement a constructor that takes necessary arguments for member variables to be initialized
my answer


You're awfully close. One thing to remember is that, when you perform an assignment, you assign the value of the variable on the right hand side to the variable on the left hand side. Do you see your mistake?

iii.Show you can overload the constructor by implementing another constructor in itemtype class.
my answer :


Okay, again you've got some of the same problems as above - look at what is being assigned to what. Think of it like this:
setThisVariable = toTheValueOfThisOne;
Now, let's look at the errors you're getting:

F:\j2sdk1.4.0_03\bin>javac itemtype.java
itemtype.java:15: possible loss of precision
found : double
required: float
price = 5.00;
^
itemtype.java:26: cannot resolve symbol
symbol : constructor itemtype (java.lang.String)
location: class itemtype
products = new itemtype("fruit");
^
2 errors

The first error is stating something about a loss of precision. The reason for this is that Java considers any decimal literal (in your case, 5.00) to be a double. A double doesn't fit into a float, so a compiler error is thrown. Try adding a 'f' to the end of the number, like this: 5.00f so that the compiler knows that you want that value to be a float, not a double.
The second error is because you're trying to invoke a constructor that takes just a String as a parameter. Do you have any of those? Hmmmm...
Try some of those things and come back with what you have.
Best of luck,
Corey


SCJP Tipline, etc.
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
hithere thanks for the help..
i) it does compile w/o errors....but it does not do anything except create another object of itself ....rite??...what is the use of this??? (i see no use why someone would implement this..)
conclusion : i guess that an itemtype object can be created if a constructor is not implemented.
ii) is this the proper way....??

iii) Got rid of the float error!! thanks!!! i get this part..wherever i use a float..i must use and f to indicate its a float...
however, i dun get the following..
The second error is because you're trying to invoke a constructor that takes just a String as a parameter. Do you have any of those? Hmmmm...
i've tried to change the code but i got too many errors so i changed back..could you kinldy pls explain this in more detail... thank you
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
i) it does compile w/o errors....but it does not do anything except create another object of itself ....rite??...what is the use of this??? (i see no use why someone would implement this..)
conclusion : i guess that an itemtype object can be created if a constructor is not implemented.

When you don't provide a constructor, the compiler inserts a "default" constructor for you. The default constructor takes no parameters and has an empty method body, but it allows you to construct an instance of the object.
In your case, in which you want to initialize an object with some information the second you create it, this may seem like a silly thing to do. However, you don't always know all of the information you're going to put into an object when you create it. In a case such as that, it might be helpful to create an "empty" object and then add data to it later using "setter" methods (A setter method simply takes a value and assigns it to an instance variable).
Anyway, I really just wanted to demonstrate the default constructor. Also note that the compiler will not insert a default constructor if you declare one yourself. It is only inserted if no constructor is provided.

ii)
is this the proper way....??

Yup - looks better.

i've tried to change the code but i got too many errors so i changed back..could you kinldy pls explain this in more detail...

In the original code snippet, you had these two lines:

This code tells the JVM to create a new variable of type itemtype and then it attempts to create an instance of that type when you use the keyword "new". However, you supply just one parameter, a String ("fruit"). The JVM then looks for a constructor that takes a single string as a parameter. Unfortunately, you don't have one. You have these constructors:

The first constructor takes a String, but it also takes a float and an int. The second one takes and float and an int. Nowhere have you defined a constructor that takes a single String. To fix this, one of two things must occur:
1. Provide additional parameters when you attempt to construct your object.
2. Create a constructor that takes only a single String as a parameter.
Does that help?
Corey
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Tabz, I forgot to ask you last time to review JavaRanch's naming policy. Would you please do that, cos we like to feel that we are helping real live people.
Thanks - Barry (also known by lots of other name, but that's my own).
[ March 19, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
ok Barry..its done....thanks for telling me...
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tabrez:
ok Barry..its done....thanks for telling me...

Tabrez,
You're close. If you look back the the Javaranch Naming Policy, you'll see that names are requried to be in the form "first_name last_name" (notice a space between them). You can change your publicly displayed name (here) to comply with our unique rule. Thank you.
Corey
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
Yahoooo!!! thanks...you people r GREAT!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Only one problem, it's gotta be two names. We'll call you by the first name up front. If you don't want to use your real second name, then make up something that looks real. Thanks Tabrez!
How did you get along with that reverse assignment?
Did the lecturer fall over?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Tabrez:
Yahoooo!!! thanks...you people r GREAT!!

Glad you got it working, Tabrez.
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
thank you corey....as for my assignment..i have yet to show it to my lect..my class is on thursday..and i will talk to him then....i will get back to u about his response ....thanks barry......
Tabrez Muchhala
Greenhorn

Joined: Mar 16, 2003
Posts: 18
btw..this is my full name...no kidding...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thanks and good luck Tabz!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
One tip:

That code works fine, nothing wrong with it. But it isn't
x, y, z is ok for algebra and stuff, but it's not so good in computer programs that other people have got to read.
This is cooler:

Notice I used this which means the variable belongs to this object I'm now making ( or constructing ). If you don't use this the compiler gets confused because the same names are being used as parameters to the method.
An alternative could be:

Corrected according to Layne's post below, sorry Tabz!
[ March 19, 2003: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do you still need this constructor?


I asked because you do not use it, and the Apple parameter is never used. And it does not initialize an itemtype object correctly.
Zap it! Simplify to the Max.
[ March 19, 2003: Message edited by: Barry Gaunt ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Barry Gaunt:
This is cooler:


Sorry to nitpick, but I think you need to change the code to corespond with your new parameter names...


Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thanks Layne,
Will do that, but it's funny I thought I already did that. Guess I hit the "back" button at some time. I can't be that dumb... or...?
Actually, that convinces me not to post code until I have at least compiled it myself. I didn't do it this time because I don't have Java on this particular PC.
[ March 19, 2003: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another newbie needs help...:-(