/    Sign up×
Community /Pin to ProfileBookmark

Inheritance problem

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.)

[CODE]import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class M006JApplet extends JApplet
implements ActionListener
{
private Button myBtn;
private Circle myCircle;
private Square mySquare;
public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());

myBtn = new Button(“Change”);
c.add(myBtn);
myBtn.addActionListener(this);

myCircle = new Circle(20, 20, 50);
mySquare = new Square(20, 100, 50);

}
public void actionPerformed(ActionEvent event)
{
myCircle.changeSize();
mySquare.changeSize();
repaint();

}
public void paint(Graphics g)
{
myCircle.drawCircle(g);
mySquare.drawSquare(g);
}

}

class Shape
{
private int x, y;
private int diameter;
private boolean grow = true;

public Shape(int xValue, int yValue, int dia)
{
x = xValue;
y = yValue;
diameter = dia;

}
public void setX(int xValue)
{
x = xValue;
}

public int getX()
{

return x;
}
public void setY(int yValue)
{
y = yValue;
}

public int getY()
{

return y;
}

public void setDiameter(int dia)
{
diameter = dia;

}
public int getDiameter()
{

return diameter;
}
public void changeSize()
{ if (grow)
{
radius += 10;
if (radius > 100)
{ grow = false;
}
}
else
{
radius -= 10;
if (radius < 20)
{ grow = true;
}
}
}

}

class Circle extends Shape
{
public Circle(int a, int b, int w)
{
super(a, b, w);

}

public void drawCircle(Graphics g)

{
g.setColor(Color.red);
g.fillOval(super.getX(), super.getY(), super.getDiameter(), super.getDiameter());
g.setColor(Color.black);
g.drawOval(super.getX(), super.getY(), super.getDiameter(), super.getDiameter());

}

}

class Square extends Shape
{

public Square(int a, int b, int w)
{
super(a, b, w);

}

public void drawSquare(Graphics g)

{
g.setColor(Color.green);
g.fillRect(super.getX(), super.getY(), super.getDiameter(), super.getDiameter());

g.setColor(Color.black);
g.drawRect(super.getX(), super.getY(), super.getDiameter(), super.getDiameter());

}

}

[/CODE]

Any help greatly apprciated. Again this is a home work problem and i am not asking anyone to do it for me, just a little help as to what logic i would use to get the change button to work as to get the required output.

thanx,
kanaka

to post a comment
Java

3 Comments(s)

Copy linkTweet thisAlerts:
@buntineJun 14.2005 — It looks like you will need to set the diameter of each shape from the changeSize method.

When repaint is called, your daimeter has not changed. You will need something like this:
<i>
</i>public void changeSize()
{ <br/>
if (grow)
{ <br/>
radius += 10;
setDiameter(getDiameter() + 10);
if (radius &gt; 100)
grow = false; <br/>
}
else
{ <br/>
radius -= 10; <br/>
setDiameter(getDiameter() - 10);
if (radius &lt; 20)
grow = true; <br/>
}
} <br/>

Regards.
Copy linkTweet thisAlerts:
@kanakatamauthorJun 14.2005 — It looks like you will need to set the diameter of each shape from the changeSize method.

When repaint is called, your daimeter has not changed. You will need something like this:
<i>
</i>public void changeSize()
{ <br/>
if (grow)
{ <br/>
radius += 10;
setDiameter(getDiameter() + 10);
if (radius &gt; 100)
grow = false; <br/>
}
else
{ <br/>
radius -= 10; <br/>
setDiameter(getDiameter() - 10);
if (radius &lt; 20)
grow = true; <br/>
}
} <br/>

Regards.[/QUOTE]

thanx, buntine.

Actually my code works this way, i click the change button, and both square and circle grows but i want circle to grow and square to shrink when i click change button and when the shapes become too small (20 pixels) or too big (100 pixels) then it should reverse that is, circle should shrink and square should grow.

this is where i have problem

help appreciated,

kanaka
Copy linkTweet thisAlerts:
@ExuroJun 14.2005 — I think you need to add another method to your [FONT=Courier New]Shape[/FONT] class, something like [FONT=Courier New]setGrowing(boolean)[/FONT]. It'd just be a mutator for the private field [FONT=Courier New]grow[/FONT]. That way, with that method, you could set the square's [FONT=Courier New]grow[/FONT] field to false initally, and the circle's to true. You could also change the constructors for you classes to accept a value for the [FONT=Courier New]grow[/FONT] field.
×

Success!

Help @kanakatam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...