JavaFX
JavaFX
1. The following Java applications contain errors. Point out the statement(s) that contain errors. Explain
what each of the errors is, and how it can be fixed.
EX 1.1.
public class OOPExercises { Point out the error(s) and how they can be fixed.
public static void main(String[] args) {
A objA = new A(); a là thuộc tính private nên ko thể gọi trực tiếp.
System.out.println("in main(): "); Fix: dùng phương thức getA(), setA();
System.out.println("objA.a = "+objA.a);
objA.a = 222; System.out.println("objA.a = "+objA.getA());
} objA.setA()= 222;
}
public class A {
private int a = 100;
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
EX 1.2.
public class OOPExercises { Point out the error(s) and how they can be fixed.
public static void main(String[] args) {
System.out.println("in main(): "); Chưa khai báo object của class A nên ko thể dùng phương thức
System.out.println("objA.a = "+getA() ); của class A
setA(123); Fix: khai báo thêm obj của class A sau hàm main:
} A obj = new A();
} // sửa GetA() thành obj.getA() và setA(123); thành
//obj.setA(123);
System.out.println("objA.a = "+obj.getA() );
public class A {
obj.setA(123);
private int a = 100;
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
Page 1
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 1.3.
public class OOPExercises { Point out the error(s) and how they can be fixed.
public static void main(String[] args) {
A objA = new A( ); Ko có lỗi
double result;
result = objA.getA( );
System.out.println("objA.a = "+ result);
}
}
public class A {
private int a = 100;
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
EX 1.4.
public class B extends A { Point out the error(s) and how they can be fixed.
private int a = 222;
hàm main là static nên ko thể xài thuộc tính non-static. Thuộc
public static void main(String[] args) { tính a ở class A vừa là private vừa là non-static nên cũng ko thể
System.out.println("in main(): "); dùng được.
System.out.println("a = "+a ); Fix: thêm static vào trước thuộc tính a ở class B thành static
a = 123; private int a=222; hoặc thêm static vào thuộc tính a
} trong class A và sửa access modifier thành protected hoặc
public hoặc default
}
public class A {
private int a = 100;
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
Page 2
Java: Exercises on OOP, Inheritance, and Polymorphism
public class A {
int a = 100;
public A() {
System.out.println("in the constructor of class A: ");
System.out.println("a = "+a);
a = 333;
System.out.println("a = "+a);
}
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
public class B {
double b = 123.45;
public B() {
System.out.println("-----in the constructor of class B: ");
System.out.println("b = "+b);
b = 3.14159;
System.out.println("b = "+b);
}
public void setB( double value) {
b = value;
}
public double getB() {
return b;
}
} //class B
Page 3
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 2.2.
public class A {
int a = 100;
public A() {
System.out.println("in the constructor of class A: ");
System.out.println("a = "+a);
a = 333;
System.out.println("a = "+a);
}
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class A
public class B extends A {
double b = 123.45;
public B() {
System.out.println("-----in the constructor of class B: ");
System.out.println("b = "+b);
b = 3.14159;
System.out.println("b = "+b);
}
public void setB( double value) {
b = value;
}
public double getB() {
return b;
}
} //class B
Page 4
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 2.3.
Page 5
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 2.4.
Page 6
Java: Exercises on OOP, Inheritance, and Polymorphism
3. UML Diagrams
EX 3.1. Draw a UML class diagram (with associations) to show the design of the Java application in
EX 2.2.
Page 7
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 3.2. The partial design of a Java application for a child care center is given in the following UML
diagram. Note that the diagram is not complete. How do you represent the following
relationships in the design: father, mother, and guardian? Revise the diagram to include those
relationships in the design.
Person
- lastName: String
- firstName: String
- father: Person
- mother: Person
+ setLastName(String)
+ getLastName( ): String
.
.
.
Child
- guardian: Person
- age: int
- height: int
- weight: double
+ setGuardian(Person)
+ getGuardian( ): Person
.
.
.
Page 8
Java: Exercises on OOP, Inheritance, and Polymorphism
EX 3.3. Implement the design in EX 3.2 as a Java application. Add the set and get methods for each
of the attributes. Note that Child is a subclass of Person.
class Person{
Person father,mother;
return lastName;
this.lastName = lastName;
return firstName;
this.firstName = firstName;
return father;
this.father = father;
return mother;
Page 9
Java: Exercises on OOP, Inheritance, and Polymorphism
this.mother = mother;
return guardian;
this.guardian = guardian;
return age;
this.age = age;
return height;
this.height = height;
Page 10
Java: Exercises on OOP, Inheritance, and Polymorphism
return weight;
this.weight = weight;
Page 11