w4 by keralaguest


									IM110 Intro to Programming in Java                   2.2 Other methods are needed to make
Lecture Week 4                                       JTBoxWord complete. What might they be? Write
                                                     and demonstrate at least one.
Topics Creating new classes; aggregate classes.      2.3 Look at the Flag class code below and use it to
                                                     draw the flag of any country that uses the tricolor
Ref: Charatan & Kans ch5                             layout (eg green, white, yellow).
                                                     2.4 Create a new class JTCircleWord, like
1 Classes
                                                     JTBoxWord, which constructs a word inside a
New classes are often created as aggregates of       circle.
objects of existing classes.
                                                     (Hint see Learning Aid 4.1 Creating objects).
                                                     Code for download
1.1 Look through the Learning Aids 4.1 Creating      The following Java file contains the code needed
new classes and 4.2 Aggregation. What objects        for a JTFlag class (copy and paste it into a new
might be needed in a new class to represent the      JTFlag.java file inside a new JTApplication
bank account described in Learning Aid 4.1?          Project). A new User.java file in the
                                                     JTApplication Project will also be needed to
2 JTBoxWord                                          demonstrate the class.
Consider the need for a graphic label, like a word   JTFlag.java
inside a border. Call the class JTBoxWord and the
following line could be included within a            3. Submarine
User.java file, defined within a new
                                                     In Learning Aid 3.2 Repetition, there is an
JTApplication Project:
                                                     animation of a diving submarine. If we had a
JTBoxWord aWord = new                                JTSub class we could use it as follows in
JTBoxWord("word",0,0);                               User.java:

The class can be created using a JTText object and   JTSub sub1 = new JTSub();
the border by using a JTRectangle object. The        sub1.setSubPosition(50, 100);
objects are aggregated into a new class
                                                     We can define our own JTSub in a new file
                                                     JTSub.java, defined within a new JTApplication
This new class should be created in a new file       Project.
JTBoxWord.java, defined within the same
                                                     The sub can be moved using a newly defined
JTApplication Project, as follows:
                                                     method setSubPosition inside a while loop.
// JTBoxWord.java PChalk Oct02
import JTShapes.*;
public class JTBoxWord {                             3.1 Develop the sub program and animate its
JTText word;                                         movement (see section 3 of the week 3 notes or
JTRectangle box;                                     follow the tutorial inside Learning Aid 4.1
                                                     Creating new classes).
public JTBoxWord(String
       wordText, int x, int y) {                     Assessed Exercise for week 4
   box = new JTRectangle(x,y);                       Extend the code for the sub program so that the
   word = new JTText(wordText);                      sub dives to the bottom of the sea. (Hint follow
   word.setPosition(x,y+12);                         the tutorial inside Learning Aid 4.1 Creating new
       // JTText draws from                          classes). Email the .java file(s) to your tutor and
       // bottom left                                explain in your log what new things you have
} // end constructor
}// end class JTBoxWord                              Each week you must do the assessed exercise
                                                     within WebCT and make a note when you have
Tasks                                                done it in your bound hand-written log (exercise)
2.1 Copy the JTBoxWord code and run the              book.
program that demonstrates this new class.

To top