Chan Wai Fung 06085478D EIE320: Object-Oriented Programming and Design Lab: Java Applet Programming Part

1 Cinema Class Three constructors of the class Cinema:
public Cinema() { ++numOfCinemas; } public Cinema(int Cid, String Cname) { id = Cid; name = Cname; ++numOfCinemas; { id = Cid; name = Cname; address = Caddress; ++numOfCinemas; } } // Set the ID of Cinema // Set the name of Cinema // Set the address of Cinema public Cinema(int Cid, String Cname, String Caddress)

18-11-2007

Different constructor is defined to meet different necessities, .e.g. the third constructor will be called when three parameters id, name, address of the Cinema is input. The ++numOfCinemas; statement is to increment the variable numOfCinemas after a Cinema is constructed, and its purpose is to count the total amount of Cinema objects. getDetail() method:
public String getDetail() { return ("CinemaID: " + id + " Cinema Name: " + name + " Address: " + address); }

This is a method to print out all the information of a Cinema object, and the method will be used in Part 2. Part 2 Cinema Applet Class (Part of) CinemaApplet2 class:
public class CinemaApplet2 extends JApplet { private Cinema [] a;

public void init() { a = new Cinema [5]; // a Cinema-array is created (Size of array = 5) // Cinema objects are then created one by one a[0] = new Cinema(1, "UA TIMES SQUARE", "CAUSEWAY BAY"); a[1] = new Cinema(2, "UA SHATIN", "SHA TIN"); a[2] = new Cinema(3, "TSUEN WAN BROADWAY", "TSUEN WAN"); a[3] = new Cinema(4, "POLYU THEATRE", "HUNG HOM"); a[4] = new Cinema(5, "GV MONG KOK", "MONG KOK"); } public void paint(Graphics g) { g.drawString(a[0].getDetail(), 25, 25); // print the details of a Cinema to position (25,25) g.drawString(a[1].getDetail(), 25, 50); g.drawString(a[2].getDetail(), 25, 75); g.drawString(a[3].getDetail(), 25, 100); g.drawString(a[4].getDetail(), 25, 125); } }

The init() method will be called once the applet being executed. Inside init(), a Cinema-array, which will store 5 Cinema objects, will be created. After the first statement inside init(), each of the 5 positions inside the array will create new Cinema object separately, because different Cinema objects will have different properties. The paint() method is used to draw the required text information to the applet by calling the getDetail() method inside each Cinema object, Different positions will display the information of different Cinema object. Part 3 Cinema HTML The HTML file:
<html> <applet code = "CinemaApplet2.class" width = "300" height = "300"> </applet> </html>

Only some simple HTML codes is necessary for building the webpage, because the compiled applet class will do all the tasks, i.e. creating objects, displaying their

information, etc. Part 4 GUI (Part of) CinemaApplet4 class:
public class CinemaApplet4 extends Applet implements ActionListener { private Cinema [] a; private Label IDLabel, NameLabel, AddrLabel; private TextField IDText, NameText, AddrText; private Button button; private int curNum = 0; public void init() { IDLabel = new Label("Cinema ID: "); …… IDText = new TextField(10); …… button = new Button("ShowRecord"); // Set Button add(IDLabel); …… button.addActionListener(this); // Add ActionListener } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) // if the button is pressed, run the following code { // set the content of ID text field to ID of the Cinema object IDText.setText(Integer.toString(a[curNum].getId())); // set the content of Name text field to name of the Cinema object NameText.setText(a[curNum].getName()); // set the content of Address text field to address of the Cinema object AddrText.setText(a[curNum].getAddress()); // increment the index which states the current position of Cinema-array // Add all components to the applet // Set all TextField //Set all Label // current cinema's sequence no.

curNum++; // jump back to 0 when the index is larger than 4 if (curNum == 5) curNum = 0; // show the index button.setLabel("Next Record: " + (curNum+1) ); } } public void paint(Graphics g) { } g.drawString("Total number of cinema: 5", 120, 200); }

At the beginning, different components (Label, TextField and Button) are declared. When the init() method is called, the components are created with different properties and then added to the applet, while “button” is set as an ActionListener. The integer variable curNum states the current position of the Cinema-array. It is used to show the details of a Cinema object inside the array. The actionPerformed() method is called when the ActionListener (button) is pressed, and the text inside the text fields will change accordingly. Lastly, the paint() method draw the string to the applet. Part 5 Constructing a Simple Cinema Part 5.1 Cinema Class Another constructor should be added:
public Cinema(int Cid, String Cname, String Caddress, int CtNum) { id = Cid; name = Cname; address = Caddress; numOfTheaters = CtNum; t = new Theater[CtNum]; ++numOfCinemas; }

So a Theater-array (size = CtNum) will be created when a Cinema is created. The method createTheater() should be added like this:
public void createTheater(int theaterNum, int theaterId, String theaterName)

{

t[theaterNum] = new Theater(id, theaterId, theaterName);

}

The purpose of this method is to create a Theater object by calling the constructor (with 3 input parameters) inside the Theater class. Part 5.2 Theater Class Some parts of the code inside the class Theater:
public class Theater { // (other variables are declared as in lab sheet) private Movie mov = null; public Theater() { ++numOfTheaters;} public Theater(int tcid, int tid, String tname) { cinemaId = tcid; id = tid; name = tname; ++numOfTheaters; } // Set the CinemaID // Set the Theater ID // Set the Theater Name // increment the total number of Theaters // Assume that there is only 1 movie for each theater at a time

public void createrMovie(int movieId, String movieName, String startDate, String endDate, int duration) { } mov = new Movie(movieId, movieName, startDate, endDate, duration); }

The purpose of createMovie() method is to create a Movie object by calling the constructor (with 5 input parameters) inside the Movie class. Part 5.3 Movie Class
public class Movie { // ( variables are declared as in lab sheet) public Movie() { ++numOfMovies;}

public Movie(int mId, String mname) { id = mId; name = mname; { id = mId; name = mname; startDate = mstartDate; endDate = mendDate; duration = mduration; } } // Set Movie ID // Set Movie name // Set Movie start date // Set Movie end date } // Set Movie duration public Movie(int mId, String mname, String mstartDate, String mendDate, int mduration)

Apart from the constructors and “get” methods, no other methods is needed in Movie class. Part 5.4 (Part of) CinemaApplet5 class:
public class CinemaApplet5 extends Applet implements ActionListener { private Cinema [] a; private Movie [] m; private int curCNum = 0; private int curMNum = 0; // current cinema's sequence no. // current movie’s sequence no.

private TextField CinemaIDText, CinemaNameText, CinemaAddrText; private TextField MovieIDText, MovieNameText, StartDateText, EndDateText, DurationText; private Button NextCinema, NextMovie; public void init() { // (create Cinema-array and Cinema objects as in Part2) m = new Movie[7]; // Create a Movie-array (size of array= 7)

// Movie objects are then created one by one m[0] = new Movie(1, "Battle Royale 2", "2003/5/6", "2003/5/20", 141); m[1] = new Movie(2, "Heroic Duo", "2003/5/1", "2003/5/8", 101); m[2] = new Movie(3, "Good times. Bed times", "2003/6/7", "2003/6/15", 105);

m[3] = new Movie(4, "The Medallion", "2003/5/6", "2003/5/20", 88); m[4] = new Movie(5, "Sinbad Legend of the Seven Seas", "2003/5/6", "2003/5/20", 141); m[5] = new Movie(6, "Finding Nemo", "2003/5/6", "2003/5/20", 101); m[6] = new Movie(7, "Blue Gate Crossing", "2003/5/6", "2003/5/20", 84); // Create all components (TextField, Button) CinemaIDText = new TextField(15); …… NextCinema = new Button("Show Next"); …… TextField tf1 = new TextField(15); tf1.setText("Total number of cinema: 5"); TextField tf2 = new TextField(15); tf2.setText("Total number of movie: 7"); // Start setting up layout setLayout( new GridLayout(10, 2)); // Add components to the applet add( new Label("Cinema ID: ")); // add ActionListener to the two buttons NextCinema.addActionListener(this); NextMovie.addActionListener(this); } public void actionPerformed(ActionEvent e) { // (if the NextCinema button is pressed, run the code as in Part 4 // if the NextMovie button is pressed, run the following code if (e.getSource() == NextMovie) { MovieIDText.setText(Integer.toString(m[curMNum].getId())); MovieNameText.setText(m[curMNum].getName()); StartDateText.setText(m[curMNum].getStartDate()); EndDateText.setText(m[curMNum].getEndDate());

DurationText.setText(Integer.toString(m[curMNum].getDuration())); curMNum++; if (curMNum == 7) curMNum = 0; NextMovie.setLabel("Next Record: " + (curMNum+1) ); } } }

The statement “setLayout( new GridLayout(10, 2));” set a Layout with row number = 10 and column number = 2. Then the components are added to the layout one by one (with a certain sequence, otherwise the position of the components will be not as expected). The other code are just like that in Part 4, i.e. creating array and object in init(), adding one more ActionListener ( “NextMovie”, to control the showing details of Movie objects), and set the event when NextMovie button being pressed.