1. Mark what new material is covered and what is not covered in Chapter 9:
2-4. In some European countries such as Czech Republic, March 28 is celebrated as a Teachers' Day.
2. Define a class Teacher whose elements are teachers; each creature is characterized by its first name, last name, and the number of students this teacher teaches. Your class should contain a constructor method, accessor methods, mutator methods, and a method for computing the number of credit hours generated by this teacher (let us assume that it is number of students multiplied by 3.0).
Solution:
public class Teacher{
private String firstName;
private String lastName;
private int numberOfStudents;
public Teacher(String fn, String ln, int nos){
firstName = fn, lastName = ln; numberOfStudents = nos;}
public String getFirstName(){return firstName;}
public String getLastName(){return lastName;}
public int getNumberOfStudents(){return numberOfStudents;}
public void setFirstName(String newfn){firstName = newfn;}
public void setLastName(String newln){lastName = newln;}
public void setNumberOfStudents(String newnos)
{numberOfStudents = newnos;}
public double creditHours(){return 3.0 * numberOfStudents;}
public double area(){return Math.Pi * radius * radius;}
}
3. Let us plan for the future. Use your class in the main
method to define a new teacher that represents the future you -- this
teacher
has your name and originally teaches 75 students. Compute and
print the number of credit hours that you generated. Then, suppose
that you were so good that 15 more students signed for your class.
Show how to increase the number of students by 15, and compute and
print the new number of credit hours.
Solution (on the example of Julio Urenda):
Teacher julio = new Teacher("Julio", "Urenda", 75);
int credits = julio.creditHours();
System.out.println(credits);
int students = julio.getNumberOfStudents();
students += 15;
julio.setNumberOfStudents(students);
credits = julio.creditHours();
System.out.println(credits);
4. Trace your program step-by-step.
Solution (in brief):
on the first line, we call a constructor method:
--------------
| ---------- |
| | Julio | |
| ---------- |
| fn |
| ---------- |
| | Urenda | |
| ---------- |
| ln |
| ---------- |
| | 75 | |
| ---------- |
| nos |
--------------
| |
| |
| |
| |
| |
| |
| |
| |
--------------
Teacher
after the first line, we get a new object
------------------
| ---------- |
| | Julio | |
| ---------- |
| firstName |
| ---------- |
| | Urenda | |
| ---------- |
| lastName |
| ---------- |
| | 75 | |
| ---------- |
| numberOfStudents |
------------------
julio
after the second line, we get
----------
| 225 |
----------
credits
at the third line, we print 225 to the screen
after the fourth line, we get
----------
| 75 |
----------
students
after the fifth line, this value changes:
----------
| 90 |
----------
students
after the sixth line, the object changes to:
------------------
| ---------- |
| | Julio | |
| ---------- |
| firstName |
| ---------- |
| | Urenda | |
| ---------- |
| lastName |
| ---------- |
| | 90 | |
| ---------- |
| numberOfStudents |
------------------
julio
after the 7th line, we change the value of the variable credits:
----------
| 270 |
----------
credits
on the 8th line, we print 270 to the screen