Q 1: Access class members from the same package Note: ClassA and ClassB in this question are in the same package. public class ClassB { public int x = 1; int y = 2; private int z =3; } ملاحظة: عرف كل كلاس في ملف لانه لا يجوز ان يكون كلاسين public في نفس الملف. public class ClassA { public static void main(String[] args) { ClassB cb = new ClassB(); // System.out.print(cb.x); /* 1 */ // System.out.print(cb.y); /* 2 */ // System.out.print(cb.z); /* 3 */ } } 1.1- Which statement(s) are true? Choose all that apply. If statement 1 is uncommented, the code prints "1" to the output. If statement 2 is uncommented, the code prints "2" to the output. If statement 3 is uncommented, the code prints "3" to the output. This code does not compile. 1.2- Try to do the following: Declare a character type variable myChar in the class ClassB, the variable myChar shouldn't be accessible through the object cb. Add a statement to the classA to test whether myChar is accessible.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Q 1: Access class members from the same package
Note: ClassA and ClassB in this question are in the same package.
public class ClassB {
public int x = 1;
int y = 2;
private int z =3;
}
ملاحظة: عرف كل كلاس في ملف لانه لا يجوز ان يكون كلاسين public في نفس الملف.
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
// System.out.print(cb.x); /* 1 */
// System.out.print(cb.y); /* 2 */
// System.out.print(cb.z); /* 3 */
}
}
1.1- Which statement(s) are true? Choose all that apply.
- If statement 1 is uncommented, the code prints "1" to the output.
- If statement 2 is uncommented, the code prints "2" to the output.
- If statement 3 is uncommented, the code prints "3" to the output.
- This code does not compile.
1.2- Try to do the following:
- Declare a character type variable myChar in the class ClassB, the variable myChar shouldn't be accessible through the object cb.
- Add a statement to the classA to test whether myChar is accessible.
Q2: Different packages and access modifiers
Note: ClassA and ClassB in this question are in two different packages, while in the previous question, they were in the same package.
package package_02;
public class ClassB {
public int x = 1;
int y = 2;
private int z =3;
}
package package_01;
import package_02.ClassB;
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
// System.out.print(cb.x); /* 1 */
// System.out.print(cb.y); /* 2 */
// System.out.print(cb.z); /* 3 */
}
}
2.1- Which statement(s) are true? Choose all that apply.
- If statement 1 is uncommented, the code prints "1" to the output.
- If statement 2 is uncommented, the code prints "2" to the output.
- If statement 3 is uncommented, the code prints "3" to the output.
- This code does not compile.
2.2- Try to do the following:
- Declare an integer variable called myInt in the ClassB; the variable myInt should be accessible through the object cb.
- Write a statement in ClassA to check out the accessibility of your variable.
Q 3: Different packages and access modifiers
package package_02;
public class ClassB {
public int x = 1;
int y = 2;
private int z = 3;
public void myMethod() {
System.out.print(x);
System.out.print(y);
System.out.print(z);
}
}
package package_01;
import package_02.ClassB;
public class ClassA {
public static void main(String[] args) {
ClassB cb = new ClassB();
cb.myMethod();
}
}
3.1- Which statement(s) are true? Choose all that apply.
- This code prints "123" to the output.
- If you remove the "public" modifier from the ClassB, the code writes "123" to the output.
- If you remove the "public" modifier from the method myMethod, the code prints "123" to the output.
- This code does not compile
3.2- What do you expect if you remove the public keyword from the ClassB (public class ClassB{..})?
Q4. The classes DateInfo and Student in different packages
- Create two packages, calendar, and personal_data
- Create a class called DateInfo inside the package calendar
- The class DateInfo has only three variables, day (type integer), month (integer), year (integer).
- The three variables day, month and year, are invisible outside the class DateInfo.
- Define a constructor for the class DateInfo with three-arguments to initialize the variables day, month and year.
- Write a method called getDateFormat, that returns a date format example; 19/4/2016 (the method returns a String type for simplification)
- Create a class called Person inside the package personal_data.
- The Person class has only two variables, name (type String) and birthDate (type DateInfo).
- Create two Person objects for Omar and Rami, Omar's birthDate is 28/8/1998 and Rami's birthDate is 13/9/1996
Step by step
Solved in 3 steps