Override a superclass's methods with a subclass
Hey everyone, I am part of a first year computer science class, and my friend and I are working on a project where we have to override a superclass's methods with a subclass. We have tried numerous techniques, but we have run out of ideas. Thanks in advance for all of your help. -From our computer to yours- zach&kody
This is what I have so far
This is the superclass that I am trying to override.
Code:
// This class represents a walker with two feet.
import java.awt.Image;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Walker extends JPanel
{
public static final int PIXELS_PER_INCH = 6;
public Foot leftFoot, rightFoot;
public int stepLength;
public int stepsCount;
// Constructor
public Walker(int x, int y, Image leftPic, Image rightPic)
{
leftFoot = new Foot(x, y - PIXELS_PER_INCH * 4, leftPic);
rightFoot = new Foot(x, y + PIXELS_PER_INCH * 4, rightPic);
stepLength = PIXELS_PER_INCH * 12;
}
// Returns the left foot
public Foot getLeftFoot()
{
return leftFoot;
}
// Returns the right foot
public Foot getRightFoot()
{
return rightFoot;
}
// Makes first step, starting with the left foot
public void firstStep()
{
leftFoot.moveForward(stepLength);
stepsCount = 1;
}
// Makes next step
public void nextStep()
{
if (stepsCount % 2 == 0) // if stepsCount is even
leftFoot.moveForward(2 * stepLength);
else
rightFoot.moveForward(2 * stepLength);
stepsCount++; // increment by 1
}
// Stops this walker (brings its feet together)
public void stop()
{
if (stepsCount % 2 == 0) // if stepsCount is even
leftFoot.moveForward(stepLength);
else
rightFoot.moveForward(stepLength);
stepsCount++; // increment by 1
}
// Returns the distance walked
public int distanceTraveled()
{
return stepsCount * stepLength;
}
// Draws this walker
public void draw(Graphics g)
{
rightFoot.draw(g);
leftFoot.draw(g);
}
}
This is the subcass of which I am trying to override the superclass
Code:
// This class represents a subclass of Walker.
import java.awt.Image;
import java.awt.Graphics;
public class Bystander extends Walker
{
public static final int PIXELS_PER_INCH = 6;
private Foot lf, rf;
private int stepLength;
public int tapsCount;
// Constructor
public Bystander (int x, int y, Image leftPic, Image rightPic)
{
super(x, y, leftPic, rightPic);
Foot lf = getLeftFoot();
Foot rf = getRightFoot();
}
// Returns the left foot
public Foot getLeftFoot()
{
return lf;
}
// Returns the right foot
public Foot getRightFoot()
{
return rf;
}
// Makes first tap, starting with the left foot
public void firstStep()
{
lf.turn(-45);
tapsCount = 1;
}
// Makes next tap
public void nextStep()
{
if (tapsCount % 2 == 0) // if stepsCount is even
lf.turn(45);
else
lf.turn(-45);
tapsCount++; // increment by 1
}
// Stops this walker (brings its feet together)
public void stop()
{
if (tapsCount % 2 == 0) // if tapsCount is even
lf.turn(45);
else
rf.turn(0);
tapsCount++; // increment by 1
}
// Returns the distance walked
public int distanceTraveled()
{
return 0;
}
// Draws this walker
public void draw(Graphics g)
{
super.draw(g);
}
}