Name: David Age: 31
In the previous example you cannot call the no-argument constructor. If you want to instantiate objects from the class Student by calling the no-argument constructor, you need to add the following constructor to the code.
// no-argument constructor public Student()
{ }
Calling a constructor within another constructor
You can define one or more constructors in one class. It is also possible to call a constructor from another constructor. To call a constructor, within another constructor we use the keyword this. In the following example, we call the constructor with two arguments from the constructor with one argument by using the statement this(name, 20);. If you call the one-argument constructor to create an object, the value of 20 is assigned to the variable age. See the following program:
Example 6
public class Student {
String name;
int age;
// one-argument constructor public Student(String name) {
// calling the two-argument constructor this(name, 20);
}
// two-argument constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Student st = new Student("Emma");
System.out.println("Name: " + st.name);
System.out.println("Age: " + st.age);
} }
If you compile an run the program, the following is written to the standard output.
Name: Emma Age: 20
Quiz 1: Instantiating objects
What happens when the following program is compiled and run?
public class Employee {
String name = "Anna";
int age = 22;
public static void main(String[] args) {
Employee emp = new Employee();
Employee emp2 = new Employee();
emp.name = "John";
emp.age = 20;
System.out.print(emp.name + " ");
System.out.print(emp2.age + " ");
} }
Select the correct answer:
a. This code writes "Anna 22" to the standard output.
b. This code writes "John 22" to the standard output.
c. This code writes "John 20" to the standard output.
d. This code writes "Anna 20" to the standard output.
e. This code writes nothing to the standard output.
Explanation
The statement Employee emp = new Employee(); create the object emp.
The statement emp.name = "John"; assigns the value John to the name variable.
The statement Employee emp2 = new Employee(); create the object emp2.
By default, the name and age of all the objects are assigned to Anna and 22.
That is why emp2.age prints 22 to the standard output.
The correct answer is b.
Assignments
1. We need to know extra information about each employee namely, their phone numbers and the city where they live.
2. Create a new employee object called employee; name is Emma, age 25, phone number 00233-786854 and she lives in New York.
3. Add a piece of code to write all the information (including phone number and city) of "Emma" to the standard output.
4. Add a piece of code to write all the information about "John" to the standard output. John lives in California and assume that his phone number is 00383-384833
Quiz 2: Objects and object references
What happens when the following program is compiled and run?
public class MyClass
{ int x;
int y = 7;
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.x = 5;
mc.y = 8;
MyClass mc2 = new MyClass();
MyClass mc3 = mc;
System.out.print(mc.x + ", " + mc2.x + ", " + mc3.y);
} }
Select the correct answer:
a. This code writes "5, 0, 8" to the standard output.
b. This code writes "5, 5, 7" to the standard output.
c. This code writes "5, 0, 7" to the standard output.
d. This code writes "5 5 8" to the standard output.
e. This code writes nothing to the standard output.
Explanation
MyClass contains two variables namely x & y.
The statement MyClass mc = new MyClass(); instantiates the object mc.
mc.x = 5; assigns the value of 5 to x.
The statement MyClass mc2 = new MyClass(); instantiates the object mc2.
The default value of x is 0, because x is not initialized.
mc2.x = 0;
The statement MyClass mc3 = mc; creates a reference mc3 to the object mc.
mc3.y is the same as mc.y, because mc3, mc are both references to the same object.
mc.y = 8, and that is why mc3.y is also equal to 8.
The correct answer is a.
Assignments
Create an object called myClass and refer it to the object mc3. Add a statement to the program to write the variables x and y of the object myClass to the standard output.
Quiz 3: Three constructors in one class
What happens when the following program is compiled and run?
public class MyClass {
int x = 2;
int y = 5;
// no-argument constructor MyClass()
{ }
// one-argument constructor MyClass(int x)
{
this.y = x;
}
// two-arguments constructor MyClass(int x, int y)
{
this.x = x;
this.y = y;
}
public static void main(String[] args) {
MyClass mc = new MyClass();
MyClass mc2 = new MyClass(7);
MyClass mc3 = new MyClass(9, 3);
System.out.print(mc.y + ", " + mc2.y + ", " + mc3.x);
} }
Select the correct answer:
a. This code writes "5, 7, 2" to the standard output.
b. This code writes "5, 2, 9" to the standard output.
c. This code writes "5, 2, 2" to the standard output.
d. This code writes "5, 7, 9" to the standard output.
e. This code does not compile.
Explanation
The statement MyClass mc = new MyClass(); creates the object mc.
mc.y = 5 the value of y is equal to 5.
The statement MyClass mc2 = new MyClass(7); creates the object mc2 using the one-argument constructor.
The statement this.y = x; assigns 7 to the value of y.
The statement MyClass mc3 = new MyClass(9,3); uses the two-argument constructor.
the value of x assigned to the first arg, which is 9.
this.x = x;
The correct answer is d.
Assignments
1. Call the third (two-argument) constructor within the first constructor so that all the new created objects by default have x = 6 and y = 3.
2. Instantiate a new object called myObject using the first (no-argument) constructor.
3. Add a statement to write both variables x and y of your object myObject
to the standard output.
The x value of your object should be 6 and the y value 3.
Quiz 4: Calling a constructor within a constructor
What happens when the following program is compiled and run?
public class Staff {
String name = "Ron";
double salary = 400.0;
Staff(String name) {
this(name, 780.0);
}
Staff(String name, double salary) {
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
Staff st = new Staff("Ben");
System.out.print(st.name + ", " + st.salary);
} }
Select the correct answer:
a. This code writes "Ben, 780.0" to the standard output.
b. This code writes "Ron, 400.0" to the standard output.
c. This code writes "Ben, 400.0" to the standard output.
d. This code writes "Ron, 780.0 Ron 400.0" to the standard output.
e. This code does not compile.
Explanation
The statement Staff st = new Staff("Ben"); instantiate the object st.
By calling the one-argument constructor the statement this(name,780.0); calls the two-argument constructor.
which assigns new values to the variables name and salary.
name = "Ben";
salary = 780.0;
The correct answer is a.
Assignments
1. Create n staff object called "staffObject" for Mary, her salary is 2000.55. Use the (two-argument) constructor.
2. Add a statement to the program to write the name and the salary of the staffObject to the standard output.
Compile and run the program to check out your answer.
Quiz 5: Calling a constructor within another constructor
What happens when the following program is compiled and run?
public class MyClass {
int x = 3;
int y = 5;
MyClass() {
this(4, 6);
}
MyClass(int x, int y) {
this.y = y;
}
public static void main(String[] args) {
MyClass mc = new MyClass();
MyClass mc2 = new MyClass(9, 7);
System.out.print(mc.x + ", " + mc.y + ", " + mc2.x + ", " + mc2.y);
} }
Select the correct answer:
a. This code writes "4, 6, 9, 7" to the standard output.
b. This code writes "3, 5, 9, 7" to the standard output.
c. This code writes "4, 6, 3, 7" to the standard output.
d. This code writes "3, 6, 3, 7" to the standard output.
e. This code does not compile.
Explanation
The statement MyClass mc = new MyClass(); instantiates the mc object.
The no-argument constructor calls the two-argument constructor by using the statement this(4,6);
The two-argument constructor assigns the y parameter to the value of y by using the statement this.y = y;
The value of x is not assigned to the x parameter, and that is why the x value remains the initial value 3.
mc.x = 3;
The statement this(4,6); assigns the value of y to 6. mc.y = 6
The statement MyClass mc2 = new MyClass(9,7); assigns the value of y to 7, but x remains 3.
mc2.x = 3;
mc2.y = 7;
The result is 3, 6, 3, 7
The correct answer is d.
Assignments
1. Add an uninitialized integer variable "z" to the class MyClass.
2. Add a three-argument constructor to MyClass, and pass all the variable x, y, z to it.
3. Use your own created constructor to instantiate an object of MyClass called mc3, and pass the values 7, 8, 9 to it.
4. Add a statement to write the values of the variables to the standard output.
Compile and run the program to check out your expectation.
Assignment chapter 5: Create a class Employee with and without a constructor
1. Create a class Employee.
2. We want the following information about the employees: name, salary, and the country of origin. Most employees have a standard monthly salary of $
2400.55 and most of them are from France.
3. Create the employee object for Olivia who is from Canada and her salary is $ 3100.45.
4. Repeat step 3 for Daniel, his salary is $ 2400.55 and he is from France.
5. Add a piece of code to the program to write all the data of Olivia and Daniel to the standard output. There are two methods to achieve the goal. Write the program first by using a constructor and also without a constructor. The result would be something like the following.
--- Employees ---
Name: Olivia Salary: $ 3100.45 Country: Canada
---Name: James Salary: $ 2400.55 Country: France