0% found this document useful (0 votes)
4 views10 pages

I2211 Spring2024 Session1

This document is a final exam for an Object Oriented Programming course focused on Java, scheduled for June 11, 2024. It includes UML diagrams, Java code examples, and questions regarding the output of various code blocks related to interfaces and classes. Additionally, it requires the creation of a 'ShapeCollection' class with specific functionalities for managing an array of Shape objects.

Uploaded by

m91717981
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)
4 views10 pages

I2211 Spring2024 Session1

This document is a final exam for an Object Oriented Programming course focused on Java, scheduled for June 11, 2024. It includes UML diagrams, Java code examples, and questions regarding the output of various code blocks related to interfaces and classes. Additionally, it requires the creation of a 'ShapeCollection' class with specific functionalities for managing an array of Shape objects.

Uploaded by

m91717981
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

INFO I2211-E Final Exam

Object Oriented Programming June 11, 2024


Java Duration: 1h30

NO QUESTIONS ARE ALLOWED, IF YOU HAVE A DOUBT, WRITE DOWN AN ASSUMPTION


AND SOLVE.

Consider the following UML diagram that shows the relation between a set of interfaces and classes.
The corresponding Java code is shown below.

Interface 6

Interface 4 Interface 5

Interface 1 Interface 2 Interface 3

Shape ShapeCollection

Circle Quadrilateral

Square
public interface Interface6 {
String interfaceVariable = "It is Interface 6.";
static void display(){
System.out.println("This is a static method of Interface 6.");
} }
public interface Interface5 extends Interface6{
String interfaceVariable = "It is Interface 5.";
double getPerimetre();
}
public interface Interface4 extends Interface6 {
String interfaceVariable = "It is Interface 4.";
double getArea();
}
public interface Interface3 {
String interfaceVariable = "It is Interface 3.";
void DisplayInfo();
}
public interface Interface2 extends Interface4,Interface5{
String interfaceVariable = "It is Interface 2.";
String getType();
}
public interface Interface1 {
String interfaceVariable = "It is Interface 1.";
void Draw();
}
Page 1 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

public abstract class Shape implements Interface2,Interface3{


protected String name;
public Shape(String name) {this.name = name;}
public boolean isLargerThan(Shape otherObject) {
return this.getArea() > otherObject.getArea();}}

public class Circle extends Shape implements Interface1{


private double radius;
public Circle(String name, double radius){super(name);this.radius=radius;}
@Override
public double getArea() {return Math.PI * Math.pow(radius, 2);}
public String getType(){return "Circle";}
public double getPerimetre(){return 2 * Math.PI * radius;}
@Override
public void DisplayInfo() {
System.out.println("Shape: Circle, Radius: " + radius);}
@Override
public void Draw() {System.out.println(" Drawing Circle");}}

public class Quadrilateral extends Shape{


protected double side1, side2,side3,side4;
public Quadrilateral(String name, double side1, double side2, double side3,
double side4) {super(name);
this.side1 = side1;this.side2 = side2;
this.side3 = side3;this.side4 = side4;}
@Override
public double getArea() {return side1 * side2;}
public String getType(){return "Quadrilateral";}
public double getPerimetre(){return side1 + side2 + side3 + side4;}
@Override
public String toString(){
return "Shape: Quadrilateral, Sides: " + side1 + ", " + side2 + ", " + side3 +
"," + side4;}
@Override
public void DisplayInfo() {
System.out.println("Shape: Quadrilateral, Sides: " + side1 + ", " + side2 +
", " + side3 + ", " + side4);
}}

public class Square extends Quadrilateral{


protected double sideLength;
public Square(String name, double sideLength) {
super(name,sideLength, sideLength, sideLength, sideLength);
this.sideLength = sideLength;}
public double getArea() {return Math.pow(sideLength, 2);}
public String getType(){return "Square";}
public double getPerimetre(){return 4 * sideLength;}
@Override
public String toString() {
return "Square{" + "sideLength=" + sideLength + '}';}
@Override
public void DisplayInfo() {
System.out.println("Shape: Square, Side Length: "
+sideLength);}}
Note: The ShapeCollection Class will be implementend in Section B
Page 2 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

Section A (45 points): For each of the following blocks of code specify what the output is. If the
instruction causes an error YOU HAVE to specify its reason. NOTE that the blocks are
INDEPENDENT. If a specific block generates an error, you have to IGNORE it when interpreting
the remaining blocks.

Block 1:

Interface5 S=new Square(“Square 1”, 6.0);


System.out.println(((Square) S).getType());

Answer:
Block 2:

Quadrilateral Q=new Square(“Square 2”, 5.0);


System.out.println(Q.getType());

Answer:
Block 3:

Square S=new Square (“Square 3”, 5.0);


S. DisplayInfo();

Answer:
Block 4:

Shape S1=new Square(“Square 4”, 5.0);


Shape S2=new Square(“Square 5”, 7.0);
System.out.println(S1. isLargerThan(S2));
S1. DisplayInfo();

Answer:
Block 5:

Circle C1=new Circle(“Circle 1”, 5.0);


Circle C2=new Circle(“Circle 1”, 5.0);
System.out.println(C1.equals(C2));

Answer:

Page 3 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

Block 6:

Shape S=new Circle(“Circle 2”, 5.0);


System.out.println(S.toString());

Answer:
Block 7:

Interface4 I=new Circle(“Circle 2”, 2.0);


System.out.println(((Circle) I).getPerimetre());

Answer:
Block 8:

System.out.println(Interface5.interfaceVariable);

Answer:
Block 9:

Interface6.display();

Answer:
Block 10:

Square S=new Quadrilateral (“Quadrilateral1”, 5.0,10.0,5.0,10.0);


S. DisplayInfo();

Answer:
Block 11:

Interface3 S=new Circle(“Circle 3”, 6.0);


System.out.println(((Square) S).getType());

Answer:

Page 4 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

Block 12:

Interface1 S=new Square(“Square 6”, 6.0);


System.out.println(((Square) S).getType());

Answer:

Block 13:

Interface5.display();

Answer:

Block 14:

Quadrilateral Q=new Quadrilateral(“Quadrilateral 2” , 10.0,5.0,10.0,5.0);


System.out.println(Q.interfaceVariable);

Answer:

Block 15:

Shape S1=new Circle(“Circle 4”, 5.0);


S1. Draw();

Answer:

Section B is in the next page!

Page 5 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

Section B (55 points):

Create a class in Java called “ShapeCollection” that contains an array of Shape objects. The
ShapeCollection class should manage the shapes and include detailed functionalities as specified below:

1. Instance Variables:

 name: The name of the shape collection.


 capacity: The maximum number of shapes the collection can hold.
 numberOfShapes: The current number of shapes in the collection.
 shapesArray[]: An array to store Shape objects.

2. Methods:

 A constructor that initializes the name and capacity of the shape collection to given arguments.
The constructor should set the size of the shapesArray[] to the specified capacity value.
 A method called addShape that adds a Shape object to the shapesArray if the collection capacity
is not reached. If the capacity is reached, this method should throw an
ArrayIndexOutOfBoundsException.
 A method called getShapesInformation that returns a string value containing the information of
all the shapes in the collection.
 A method called searchShapeByName that displays information about shapes having a specific
name. If no shape with the specified name is found, this method should throw an
IllegalArgumentException.
 A method called countShapesByArea that counts and returns the number of shapes having a
specific area.
 A method called countSquaresWithSideLength that counts and returns the number of squares
having a specific side length.

3. Main Method:

In the main method, demonstrate the functionalities of the ShapeCollection class by:
 Creating an instance of ShapeCollection.
 Adding shapes to the collection.
 Attempting to add a shape beyond the capacity to trigger an exception.
 Retrieving and displaying information about all shapes in the collection.
 Searching for a shape by name and handling the case where the shape is not found.
 Counting shapes by a specific area.
 Counting squares by a specific side length.
 Include exception handling to manage and display user-friendly error messages.

Note: Include exception handling in the main method to demonstrate the error handling functionality.

Page 6 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

public class ShapeCollection {

//Add the with-arguments constructor

//Add the addShape method

//Add the method getShapesInformation

Page 7 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

//Add the method searchShapeByName

//Add the method countShapebyArea

Page 8 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

//Add the method countSquaresWithSideLength

public static void main(String[] args) {

}//end of the main // end of the ShapeCollection class }

Page 9 of 10
INFO I2211-E Final Exam
Object Oriented Programming June 11, 2024
Java Duration: 1h30

Page 10 of 10

You might also like