您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Apply Index
Apply Page # 1

Activity 1 ?MyCircle and MySquare extensions of MyShape

A class has been defined called MyShape. This class has the following variables and methods:  

 
class MyShape
 
Variables
Methods
Public
int xPos  
Public
int yPos  
Public
  MyShape()
Public
  void setX( int newX )
Public
  void setY( int newY )
See the listing of MyShape.java.

Look at this listing for ShapeApplet.java:

import java.applet.Applet;
import java.awt.*;
public class ShapeApplet extends Applet
{
  MyCircle shape1;
  MySquare shape2;

  public void paint( Graphics g )
  {
    shape1 = new MyCircle();
    shape1.setX(100);
    shape1.draw( g );
    shape2 = new MySquare();
    shape2.setY(20);
    shape2.draw( g );
   }
 }

You need to define 2 new classes, called MyCircle and MyClass that both are direct subclasses of MyShape . Each class should have a constructor method (setting initial values to appropriate instance variable such as radius for the circle and sideLenth for the square), and should also define a draw( Garphics g ) method that makes use of the inherited xPos and yPos instance variables.

You can use the file Shape.html to load the applet into the appletviewer.

Discussion/Suggested solution to Activity 1
Back to content

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg