Dear
all,
I hope by now all of
you have received the result of your first lab. Regarding your third lab, if you
still have difficulties, you can ask questions to me through this e-mail. Okay,
now I’ll talk a bit about the programming exercise I gave in yesterday’s class.
I see some of you managed to do it well and can complete the program in the
class but some of you still struggling, especially with the concept of Class in
Java. I’ve uploaded the solution of the programming exercise
here:
http://www.comp.nus.edu.sg/~johannes/ex1answer.zip
Okay, I’ll give some
explanations about the solution:
1.
The first
class which you need to implement is Jam Class. There are three data members in
this class, i.e.: typeOfFruit,
cannedDate, and size. Your first task is actually to fill
in the default constructor with any default values and then determine whether
this class needs another constructor or not. Actually, this class is better if
you add another constructor:
public
Jam (String type, String date, int size) {
this.typeOfFruit = type;
this.cannedDate = date;
this.size = size;
}
If you
didn’t add this constructor, when you create an instance of this class, you need
to do the following:
Jam j1 = new
Jam (); // calling the default constructor
// then you
need to change the data members using the mutator
j1.setTypeOfFruit(“Blueberry”);
j1.setCannedDate(“1
September 2007”);
j1.setSize(10);
But, if
you add the constructor above, your life will be easier. You only need one line
to do the above operations:
Jam j1 = new
Jam (“Blueberry”, “1 September 2007”, 10);
2.
After you
implement the constructors, you need to complete all the class’ accessors and
mutators:
public
String getTypeOfFruit () {
return this.typeOfFruit;
}
public
String getCannedDate () {
return this.cannedDate;
}
public
int getSize () {
return this.size;
}
public
void setTypeOfFruit (String type) {
this.typeOfFruit = type;
}
public
void setCannedDate (String date) {
this.cannedDate = date;
}
public
void setSize (int size) {
this.size = size;
}
3.
After
that, implement the isEmpty() method and spread(int fluidOz)
method
For
isEmpty() method, this method should return true if size == 0, and false
otherwise
For
spread(…) method, this method should return true if the amount of jam to be
spread is larger or equal with the current amount of jam, and false
otherwise:
public
boolean spread ( int fluidOz ) {
if (!isEmpty()) {
// if the jam is not empty, we need to check the current amount
against the amount to be spread
if (fluidOz <= size) {
size = size - fluidOz;
return true;
} else {
// not enough jam to be spread
return false;
}
} else
// if the jam is empty, of course we can’t do this operation and
just return false directly
return false;
}
4.
Yup,
that’s all for Jam Class. After that, your task is to finish the implementation
of Pantry Class. Pantry Class will store
three jars of jam, therefore inside Pantry class, there are three
objects of Jam class.
You first
need to implement the second constructor, to set all the jams in the Pantry
class:
public
Pantry (Jam jam1, Jam jam2, Jam jam3) {
this.jam1 = jam1;
this.jam2 = jam2;
this.jam3 = jam3;
}
5.
After
you’ve finished implementing the constructor, you need to complete all the
class’ accessors and mutators, just like the one you did inside Jam
class
6.
Then, you
need to implement select(int jamNumber) method. Since this class simulates the
behavior of real life pantry, before you can spread jams over a bread, you need
to select which jam you want to use right? Then you use select (int jamNumber)
method to simulate this operation:
public
void select (int jamNumber) {
switch (jamNumber) {
// here, you use switch-case statement
case 1: this.selectedJam = jam1; break;
case 2: this.selectedJam = jam2; break;
case 3: this.selectedJam = jam3; break;
default: this.selectedJam = jam1;
}
}
7.
Lastly,
implement the spread() method. Basically, this method do the spread method in
the selected jam:
public
boolean spread (int fluidOz) {
return selectedJam.spread(fluidOz);
}
8.
After
you’ve finished implementing Jam and Pantry classes, implement the main method
in Ex1Main.java to run the simulation J
Okay, that’s for the
programming exercise. If you still have any questions, you can ask me. If you
need a special consultation time with me (I mean, meeting face-to-face), you can
ask me to arrange one for you. My Wednesday and Friday are free
J
Now, let’s talk about
your upcoming Term Test #1. You can find the information about this here: http://www.comp.nus.edu.sg/~cs1101x/3_ca/termtests.html
Basically, the format
should be MCQ, but I don’t know how many questions are there for this semester.
In that website as well, you can find the term test questions and answers from
previous years. You can try those as an exercise; don’t look at the answer first! If you
don’t know why the answer is like that, you can ask me for explanation. In the
next discussion (1 October 2007), I’ll go through some of difficult questions in
the past years term tests. For your own practice, you can try to do all the
questions in your textbook until
Chapter 7. Try to do them and then if you are not sure about your answer, you
also can ask me. So, ask me as many as you wish since I’m your tutor for this
moduleJ.
Oh yes, one more thing,
for your own self exercise, try to do the second and third exercise in Lab 3 as
well.
Yup, that’s all from
me. I think I’ve written a very long e-mail this time
:p.
Best
regards,
School of
Computing
e-mail:
johannes@nus.edu.sg