• Post Reply Bookmark Topic Watch Topic
  • New Topic

toy demo class  RSS feed

 
Vamsi Kankipati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:-
Create Class Toy with below attributes
name
category
price
discount

Create Class ToyDemo with main method. Create four toy objects with relevant data. Two objects should have category as “fruits” and other two objects should have category as “anmial”. Write a method in this class which will take all four toy objects as input. Additionally, it will also take category name as input. This method should return least price toy name (considering the discount as well) for specified category. If there is no specific category (e.g category as “fruits”, method should return message “no category found”.

Follow class outline diagram as given below. Ensure class attributes are private and other methods are public. Use package “com” to build your solution.

NOTE: Use .equals for comparing String values instead of == operator. Search on net and find out why. Else discuss among colleagues.

Class Outline:
content_3.png
[Thumbnail for content_3.png]
Outline Diagram
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like homework. Give it a shot and post where you get stuck. See DoYourOwnHomework for more on why it is important to work on it yourself.
 
Vamsi Kankipati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:This looks like homework. Give it a shot and post where you get stuck. See DoYourOwnHomework for more on why it is important to work on it yourself.



I got stucked here;
I am unable to instantiate the objects through the user inputs

Code:-

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags, which you should always use, to your post. Doesn't it look better
Unfortunately it shows some stylistic problems. You should always have a blank line between successive methods.
You also have four fields in the Toy class but only three setXXX and getXXX methods. Why? Also give the Toy class a toString method so you can write
System.out.println(myToy);
and you will get something you can actually read. Apart from that, don't change anything else in the Toy class.

You have correctly encapsulated your Toys by making all fields private. That means you cannot access them from outside the class. You have also correctly given it a constructor meaning you must enter all four pieces of data before you can create an instance. Then in the ToyDemo class, you are trying to access those fields and trying to create a Toy without passing all four fields. The compiler will never let you do that.
Never use the assignment operator twice as in line 13. Of course the types are wrong, and access is prohibited, so the compiler will not permit such code.

You will have to remove most of the code from the main method. Also remove the least price method, which looks confused. Create a Toy object using the constructor and print it out. Then add another Toy object. If you use a Scanner to enter the data you may need to call input.nextLine(); and do nothing with the returned value after each Toy object. This old post explains why.
Only when you have got all that lot working will you be able to look at the least price method.
 
Vamsi Kankipati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the outiline figure in the question, all the getXxx and setXxx methods are fulfilled and no extra methods are required. Am i correct ?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the class diagram a minimum? Aren't you allowed to add methods to it?
 
Vamsi Kankipati
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Isn't the class diagram a minimum? Aren't you allowed to add methods to it?



Only the methods in the outline should be used in the program.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vamsi Kankipati wrote:
I got stucked here;
I am unable to instantiate the objects through the user inputs


From a very quick look at your code, it is obviously that you are encountering compiler errors. Generally, it is a good idea to tell us what the compiler errors are ... and what you don't understand about the error message.

Henry
 
Christopher Schneider
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I copy/pasted the code into an IDE, and there are many problems with the code as is.

First, are these two classes in separate files? If not, they need to be. The class name needs to match the file name, or you can use inner classes. I would recommend that you put the classes in separate files.

The Toy class is fine as-is. It's only holding data: getters and setters. The ToyDemo is where everything breaks down. You have no default constructor for Toy, so on line 10 you cannot instantiate a new Toy(). Remember, your constructor for a Toy is:



Therefore, when you say it doesn't work because expects a name, category, price, and discount. You instantiated a toy correctly on lines 15, 16, and 17:



because they match the constructor with a name, category, price, and discount.

There may be confusion here with the default constructor. If you do not supply a constructor, the compiler generates a default constructor that accepts no arguments. However, you've created a constructor for Toy, so no default constructor is generated.

Now the elephant in the room is line 13:


I have absolutely no idea what's going on here. What are you trying to do?
 
muralikrishna meda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got stucked here .. can you please help me

 
Carey Brown
Saloon Keeper
Posts: 3313
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit your post and put in Code tags as previously requested.

It is not clear where exactly you are stuck. Does it compile? Does it run? Does it throw an exception? ...
 
Carey Brown
Saloon Keeper
Posts: 3313
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You repeat this pattern multiple times

Your code would be more readable if your Toy class had an additional method called getDiscountedPrice() that did this.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New user, who probably doesn't know about code tags yet. I shall add them, and break the worst of the long lines, so he can see how to do it. Doesn't it look better now

Can you put the arguments into an array/list or similar? That would be far better for finding lowest price than all those if statements. Maybe the ... operator would help create the array.

And,welcome to the Ranch
 
Carey Brown
Saloon Keeper
Posts: 3313
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 22 you have
The problem with this is that "one" may or may not be of the correct Category.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!