Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
{ ++numOfCinemas; }
public Cinema(int Cid, String Cname)
{ id = Cid;
name = Cname;
++numOfCinemas; }
public Cinema(int Cid, String Cname, String Caddress)
{ id = Cid; // Set the ID of Cinema
name = Cname; // Set the name of Cinema
address = Caddress; // Set the address of Cinema
++numOfCinemas; }
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.
{
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.
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
{
private Cinema [] a;
private Label IDLabel, NameLabel, AddrLabel;
private TextField IDText, NameText, AddrText;
private Button button;
private int curNum = 0; // current cinema's sequence no.
{ id = Cid;
name = Cname;
address = Caddress;
numOfTheaters = CtNum;
t = new Theater[CtNum];
++numOfCinemas; }
{
// (other variables are declared as in lab sheet)
private Movie mov = null; // Assume that there is only 1 movie for each theater at a time
public Theater()
{ ++numOfTheaters;}
public Theater(int tcid, int tid, String tname)
{ cinemaId = tcid; // Set the CinemaID
id = tid; // Set the Theater ID
name = tname; // Set the Theater Name
++numOfTheaters; } // increment the total number of Theaters
public void createrMovie(int movieId, String movieName, String startDate, String endDate, int
duration)
{ mov = new Movie(movieId, movieName, startDate, endDate, duration); }
}
{
// ( variables are declared as in lab sheet)
public Movie()
{ ++numOfMovies;}
public Movie(int mId, String mname)
{ id = mId;
name = mname; }
public Movie(int mId, String mname, String mstartDate, String mendDate, int mduration)
{ id = mId; // Set Movie ID
name = mname; // Set Movie name
startDate = mstartDate; // Set Movie start date
endDate = mendDate; // Set Movie end date
duration = mduration; } // Set Movie duration
}
Apart from the constructors and “get” methods, no other methods is needed in
Movie class.
Part 5.4
{
private Cinema [] a;
private Movie [] m;
private int curCNum = 0; // current cinema's sequence no.
private int curMNum = 0; // current movie’s sequence no.
private TextField CinemaIDText, CinemaNameText, CinemaAddrText;
private TextField MovieIDText, MovieNameText, StartDateText, EndDateText, DurationText;
private Button NextCinema, NextMovie;
curMNum++;
if (curMNum == 7) curMNum = 0;
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.