CSE215 Assignment
CSE215 Assignment
int birthsPerMinute = 1;
int deathsPerMinute = 2;
int netMigrantsPerMinute = 8;
int netMigrantsPerDay = 180;
population += populationChange;
@Override
public String toString() {
return data;
}
}
if (nsuID.charAt(7) == '6') {
System.out.println("8th digit of my NSU ID is 6. I must make it 0 before
graduating from NSU");
} else {
System.out.println("8th digit of my NSU ID is already 0.");
}
}
}
Output:
3)
class Time {
private int hour;
private int minute;
private int second;
public Time() {
long currentTimeMillis = System.currentTimeMillis();
setTime(currentTimeMillis);
}
Output:
4)
(a) add(int y), display()
(c) add(int y), The add method may not be overridden by any subclasses of
SuperClass because it is marked as final.
SuperClass
|
SubClass
|
SubClass2
|
NSU
ii) No, the statement NSU p = new SubClass() is not legal. This is because
NSU is a subclass of SubClass2, and SubClass2 is a subclass of SuperClass.
Therefore, an NSU object cannot be assigned to a SubClass variable.
iii) Yes, the statement NSU p = new SubClass2() is legal. This is because
NSU is a subclass of SubClass2, and a subclass can always be assigned to a
superclass variable.
● Circle.java
● Rectangle.java
● Triangle.java
@Override
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
● TestGeometricObject.java
System.out.println();
System.out.println("This is the result from the next case");
// Display circle, rectangle, and triangle
displayObject(object1);
displayObject(object2);
displayObject(object3);
}
● Example: The private fields like radius in the Circle class and width and height in the
Rectangle class are encapsulated within their respective classes.
2. Inheritance:
● Example: The Circle and Rectangle classes inherit from the GeometricObject class.
● Explanation: Inheritance allows the creation of new classes that are based on
existing classes, inheriting their attributes and behaviors. It promotes code reuse and
represents the "is-a" relationship between classes.
3. Polymorphism:
4. Abstraction:
● Explanation: Abstraction allows for the definition of common interfaces and abstract
methods without specifying their implementation details. It hides the complexity of the
implementation from the users of the class.
5. Class and Object:
● Explanation: OOP revolves around the concept of classes and objects. Classes
define the blueprint for objects, and objects represent real-world entities. They
encapsulate data and behavior within a single unit.
6. Method Overloading:
● Example: The program includes two methods named equalArea with different
parameter lists (one for two objects and one for three objects).
● Explanation: Method overloading allows multiple methods with the same name but
different parameters to be defined within a class. It provides a way to create methods
that perform similar tasks but with different inputs.
7. Instanceof Operator:
● Example: The instanceof operator is used in the displayObject method to check the
type of an object.
These characteristics of OOP help in creating a well-structured and modular program that
models real-world entities effectively and promotes code organization, reusability, and
maintainability.
Part#2:
● MyGeometricObject
● MyRectangle
// Implementation of setRadius is not necessary for rectangle but required by the interface.
// This can throw UnsupportedOperationException.
public void setRadius(double newRadius) {
throw new UnsupportedOperationException("Not applicable for rectangle");
}
}
● ExceptionInterface.java
import java.io.IOException;
public interface ExceptionInterface {
void setRadius(double newRadius) throws IllegalArgumentException;
}
● MyTestRectangle.java
public class MyTestRectangle {
public static void main(String[] args) {
MyGeometricObject rectangle1 = null;
MyGeometricObject rectangle2 = null;
try {
rectangle1 = new MyRectangle(-5, 10);
displayGeometricObject(rectangle1);
} catch (IllegalArgumentException e) {
System.out.println("Exception caught for rectangle1: " + e.getMessage());
}
try {
rectangle2 = new MyRectangle(5, 10);
displayGeometricObject(rectangle2);
} catch (IllegalArgumentException e) {
System.out.println("Exception caught for rectangle2: " + e.getMessage());
}
}