0% found this document useful (0 votes)
26 views

Exercises JavaFX

The document contains exercises focused on Object-Oriented Programming (OOP) concepts in Java, specifically on inheritance and polymorphism. It includes code examples demonstrating the behavior of classes A and B, their constructors, and methods for setting and getting values. Additionally, it features UML diagram exercises for designing class relationships in a Java application for a child care center.

Uploaded by

package.alamre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Exercises JavaFX

The document contains exercises focused on Object-Oriented Programming (OOP) concepts in Java, specifically on inheritance and polymorphism. It includes code examples demonstrating the behavior of classes A and B, their constructors, and methods for setting and getting values. Additionally, it features UML diagram exercises for designing class relationships in a Java application for a child care center.

Uploaded by

package.alamre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java: Exercises on OOP, Inheritance, and Polymorphism

1.

Page 1
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();
B objB = new B();
System.out.println("in main(): ");
System.out.println("objA.a =
"+objA.getA());
System.out.println("objB.b =
"+objB.getB());
objA.setA (222);
objB.setB (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 {
double b = 123.45;
public B() {
Sys
}
} //class B

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

EX 2.2.

public class OOPExercises { Output:


public static void main(String[] args) {
//A objA = new A();
B objB = new B();
System.out.println("in main(): ");
//System.out.println("objA.a =
"+objA.getA());
System.out.println("objB.b =
"+objB.getB());
//objA.setA (222);
objB.setB (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 3
Java: Exercises on OOP, Inheritance, and Polymorphism

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

EX 2.3.

public class OOPExercises { Output:


static int a = 555;

public static void main(String[] args) {


A objA = new A();
B objB = new B();
System.out.println("in main(): ");
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;

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

}
} //class B

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

EX 2.4.

public class OOPExercises { Output:


static int a = 555;

public static void main(String[] args) {


A objA = new A();
B objB1 = new B();
A objB2 = new B();
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 7
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 8
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 9
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.

Page 10

You might also like