Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JavaFX

The document provides exercises on Java focusing on Object-Oriented Programming concepts such as OOP, inheritance, and polymorphism. It includes examples of Java applications with errors, requiring identification and correction, as well as outputs for certain applications. Additionally, it features UML diagrams and design implementations for a child care center application involving relationships between classes.

Uploaded by

package.alamre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JavaFX

The document provides exercises on Java focusing on Object-Oriented Programming concepts such as OOP, inheritance, and polymorphism. It includes examples of Java applications with errors, requiring identification and correction, as well as outputs for certain applications. Additionally, it features UML diagrams and design implementations for a child care center application involving relationships between classes.

Uploaded by

package.alamre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java: Exercises on OOP, Inheritance, and Polymorphism

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

2. Show the output of the following applications.


EX 2.1.

public class OOPExercises { Output:


public static void main(String[] args) {
A objA = new A(); in the constructor of class A:
B objB = new B(); a = 100
System.out.println("in main(): "); a = 333
System.out.println("objA.a = "+objA.getA()); -----in the constructor of class B:
System.out.println("objB.b = "+objB.getB()); b = 123.45
objA.setA (222); b = 3.14159
in main():
objB.setB (333.33);
objA.a = 333
System.out.println("objA.a = "+objA.getA());
objB.b = 3.14159
System.out.println("objB.b = "+objB.getB()); objA.a = 222
} objB.b = 333.33
}

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 OOPExercises { Output:


public static void main(String[] args) { in the constructor of class A:
//A objA = new A(); a = 100
B objB = new B(); a = 333
System.out.println("in main(): "); -----in the constructor of class B:
//System.out.println("objA.a = "+objA.getA()); b = 123.45
System.out.println("objB.b = "+objB.getB()); b = 3.14159
//objA.setA (222); in main():
objB.b = 3.14159
objB.setB (333.33);
objB.b = 333.33
//System.out.println("objA.a = "+objA.getA());
System.out.println("objB.b = "+objB.getB());
}
}

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.

public class OOPExercises { Output:


static int a = 555; in main():
a = 555
public static void main(String[] args) { objB.a = 2222
A objA = new A(); After roll back -----
B objB = new B(); a = 444
objA.a = 77777
System.out.println("in main(): ");
objB.a = 333
System.out.println("a = "+a);
a = 444;
System.out.println("objB.a = "+objB.getA());
objA.setA (77777);
objB.rollBackA();
System.out.println("After roll back -----");
System.out.println("a = "+a);
System.out.println("objA.a = "+objA.getA());
System.out.println("objB.a = "+objB.getA());
}
}
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 {
private int a = 123;
public B() {
a = 2222;
}
public void rollBackA () {
a = super.getA();
}
public void setA( int value) {
a = value;
}
public int getA() {
return a;
}
} //class B

Page 5
Java: Exercises on OOP, Inheritance, and Polymorphism

EX 2.4.

public class OOPExercises { Output:


static int a = 555;
a in A = 100
public static void main(String[] args) { a in B = 123
A objA = new A(); a in B = 123
B objB1 = new B(); a in C = 543
a in C = 543
A objB2 = new B();
a in C = 543
C objC1 = new C();
B objC2 = new C();
A objC3 = new C();
objA.display();
objB1.display();
objB2.display();
objC1.display();
objC2.display();
objC3.display(); }
}
public class A {
int a = 100;
public void display() {
System.out.printf("a in A = %d\n", a);
}
} //class A
public class B extends A {
private int a = 123;
public void display() {
System.out.printf("a in B = %d\n", a);
}
} //class B
public class C extends B {
private int a = 543;
public void display() {
System.out.printf("a in C = %d\n", a);
}
} //class C

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{

private String lastName;

private String firstName;

Person father,mother;

public String getLastName() {

return lastName;

public void setLastName(String lastName) {

this.lastName = lastName;

public String getFirstName() {

return firstName;

public void setFirstName(String firstName) {

this.firstName = firstName;

public Person getFather() {

return father;

public void setFather(Person father) {

this.father = father;

public Person getMother() {

return mother;

Page 9
Java: Exercises on OOP, Inheritance, and Polymorphism

public void setMother(Person mother) {

this.mother = mother;

class Child extends Person{

private Person guardian;

private int age, height;

private double weight;

public Person getGuardian() {

return guardian;

public void setGuardian(Person guardian) {

this.guardian = guardian;

public int getAge() {

return age;

public void setAge(int age) {

this.age = age;

public int getHeight() {

return height;

public void setHeight(int height) {

this.height = height;

Page 10
Java: Exercises on OOP, Inheritance, and Polymorphism

public double getWeight() {

return weight;

public void setWeight(double weight) {

this.weight = weight;

Page 11

You might also like