*
The moose likes Beginning Java and the fly likes Inheritance problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance problem" Watch "Inheritance problem" New topic
Author

Inheritance problem

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
This is a problem that i am working on is based on inheritance concept. I am having trouble in the action performed portion of the code. The problem is to create an applet that has a button (JButton) labeled �Change�, a red circle and a green square (each 50 pixels across). Each time the button is pressed, the circle grows by 10 pixels and the square shrinks by 10 pixels. Whenever a shape gets too small (less than or equal to 20 pixels across) or too big (greater than or equal to 100 pixels across) it reverses its growth pattern (i.e., if it was shrinking, it now grows, or vice versa.)Define a �super� class called Shapes for the common items shared by both the Circle and Square �sub-classes�.
I was successful in using the inheritance concept and create the code. But onlky the action performed code is where i am stuck.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Hi! I realize you're new to Java (relatively, anyway), so I am going to give you a few tips. Don't take it the wrong way!!

First of all, you are defeating the purpose of inheritance by using methods such as drawCircle and drawSquare. You should notice that any Shape should know how to draw itself! So, why not have a method draw() in your Shape class, and each subclass could tweak it for its own special circumstances? The result of this change would be, instead of calling drawCircle or drawSquare for each Shape of your program, you would only need to put them all into some form of collection of Shape objects, and then call draw() on each Shape.

This would also allow you to come up with totally innovative new Shapes later that you didn't envision now, put them into the collection, and with no changes to your code, it would work - just like magic!!!

This concept - polymorphism - is the foundation of object oriented programming and the number 1 reason for inheritance in the first place!! I hope it's not too much at once, but when used correctly, it can be a very powerful resource!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Here's what I would do:

1) Give Shape a shrink(int pixels) method and a grow(int pixels) method. These methods have to change the member variable that controls the size of the shape.
2) In actionPerformed(), call the appropriate methods on the appropriate objects, then call repaint().


[Jess in Action][AskingGoodQuestions]
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Ernest Friedman-Hill:
Here's what I would do:

1) Give Shape a shrink(int pixels) method and a grow(int pixels) method. These methods have to change the member variable that controls the size of the shape.
2) In actionPerformed(), call the appropriate methods on the appropriate objects, then call repaint().


Please forgive me if i have done this without understanding what you meant .Right now i have put this code inside shape class


then inside action performed i called these methods on circle and square shapes respectively.

It still doesn't work as expected. In the sense, i have a change button and a square and a circle that doesn't change. Please don't mistake me, i am trying harder and couldn't get it.
Any help appreciated,
Kanaka
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

That looks great -- except that you're calling both grow() and shrink() on each shape, so that the net effect is... nothing! You want to just grow the circle, and shrink the square, right?
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Ernest Friedman-Hill:
That looks great -- except that you're calling both grow() and shrink() on each shape, so that the net effect is... nothing! You want to just grow the circle, and shrink the square, right?

Ernest,
Thank you for you reply. But the problem is
Each time the button is pressed, the circle should grow by 10 pixels and the square shrink by 10 pixels. Whenever a shape gets too small (less than or equal to 20 pixels across) or too big (greater than or equal to 100 pixels across) it reverses its growth pattern (i.e., if it was shrinking, it now grows, or vice versa.)
This is where i have problem and so that's why i called shrink and grow methods on both of them.
I tried doing it with just
myCircle.grow();
mySquare.shrink(); in the action performed. I am not able to see any change. I am very surprised that nothing happened to the shapes.
could you please identify what am i doing wrong?
Kanaka
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Actually, only one (or none) of the two operations grow() or shrink() actually changes anything (What happens if size == 50?). Maybe a single method would be better: a boolean saying whether it grows or shrinks



P.S. Would someone edit the original post to fix the code tags??? On my monitor it's like 200 inches wide!
[ June 14, 2005: Message edited by: Timmy Marks ]
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Timmy Marks:
Actually, only one (or none) of the two operations grow() or shrink() actually changes anything (What happens if size == 50?). Maybe a single method would be better: a boolean saying whether it grows or shrinks



P.S. Would someone edit the original post to fix the code tags??? On my monitor it's like 200 inches wide!

[ June 14, 2005: Message edited by: Timmy Marks ]


Marks,
Thank you. I appreciate your help. But the problem is different, actually when change button is pressed, circle should grow by 10 pixels and square should shrink by 10 pixels and when they both become too small or too large then they reverse that is circle shrinks and square grows.
Right now both grows at the same time and shrinks at the same time. As you can see that i am a novice, I am going nuts over this.ou and Ernest have been very helpful and trust me i am trying hard to get the sloution. Any help greatly appreciated.
Kanaka
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
The size of both your circle and square are determined by a single variable radius..as it looks from the code.
so if u reduce the radius..both are minimizing and if you increase the radius both will magnify..
Define seperate variables to control the dimensions for circle/square
example circleRadius and squareSide..
Now u can put some logic in if statements to change these dimensions in grow/shrink.. else u can simply define 4 distinct functions
growcircle()
{
circleRadius+=10;
}
similarly define growSquare(),shrinkCircle and ShrinkSquare().
Now everytime button is clicked.. check the sizes and your other conditions and call grow function for one shape and shrink for the other.


Hope this helps you..all the best


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Sorry if I wasn't clear, the grow field and the radius should be members of the individual classes, and the changeSize() method is a method of the Shape class, overridden in the subclasses. Actually, the name radius is maybe misleading, since it is more associated with circles, and not with squares.

Since they are both growing at the same time, start one of them with grow = true and the other with grow = false!
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Timmy Marks:
Sorry if I wasn't clear, the grow field and the radius should be members of the individual classes, and the changeSize() method is a method of the Shape class, overridden in the subclasses. Actually, the name radius is maybe misleading, since it is more associated with circles, and not with squares.

Since they are both growing at the same time, start one of them with grow = true and the other with grow = false!


Thanx Marks. I kind of figured that out and fixed it. As always i am so greatful to this forum for helping me to do java programming which when i started made me feel very scared.
Kanaka
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance problem
 
Similar Threads
Why does it not show the new coordinates?!!??!!
Polymorphism 2!?!!??!
How to achieve runtime polymorphism in Java
Polymorphism!?!!??!
painting custom components without extending jcomponent