Assignment 1
Assignment 1
NOTE: Submit the assignment in a single pdf file. You just have to copy-paste your source code and
give screenshots of your output in an organized manner. For example, Ans:1 sourceCode, Output
screenshot, Ans2 sourceCode, Ans2 Output screenshot, ….. , etc. (Ans 4 doesn’t need any sourceCode
or output screenshot because it has theoretical questions only).
Do not upload any zip file or seperate notepad files unlike I told you in class. Only upload a single pdf
file with all the sourceCode/output screenshots in an organized manner. Upload the pdf file in canvas
assignment section (which Sir will create having due date November 1st).
Also, print the pdf file as hardcopy and submit it to Sir. I will check the softcopy only and give your
marks on Canvas. But you must also print the pdf file using a printer & stapler and then submit the
hardcopy to Sir as Proof of your work (which might be sent to department).
Write a program to display the population for each of the next five years using PRECISE
COMPUTATIONAL MODELS SUCH AS CUMULATIVE GROWTH. Assume that the current
population is 172,954,319 (Est. 2023)
2) Java has inbuilt string class which makes your life easier but build your own string class
called ‘MyString1’ which will have the following methods:
Write a test class which will implement all the above-mentioned methods. Also, take your NSU
ID as a test string. If the 8th digit of your NSU ID is 6, print “8th digit of my NSU ID is 6. I must
make it 0 before graduating from NSU”. Else print “8th digit of my NSU ID is already 0.”
Sample output:
4) Read the code and answer the following questions. Also, give brief reasons for your
answer.
class SuperClass
{
protected int x = 0;
public SuperClass(int x)
{
this.x = x;
}
private void increment() { x++; }
protected final void add(int y)
{
x += y;
}
public void display()
{
System.out.println(x);
}
}
public class SubClass extends SuperClass
{
public SubClass(int x)
{
super(x); }
public void display()
{
add(2);
super.display();
}
public static void main(String [] args)
{
SuperClass sc = new SuperClass(3);
sc.display();
sc = new SubClass(3);
sc.display();
}
}
(b) name the methods that are visible in subclasses of SuperClass (i.e. methods that can be
called
directly).
(c) name the methods that may not be overridden by any subclasses of SuperClass.
(d) Suppose there is another subclass of SuperClass called SubClass2; and a further subclass of
SubClass called NSU.
a) GeometricObject.java
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
// Construct a default geometric object
protected GeometricObject() {
dateCreated = new java.util.Date();
}
// Construct a geometric object with color and filled value
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
// Return color
public String getColor() {
return color;
}
// Set a new color
public void setColor(String color) {
this.color = color;
}
// Return filled. Since filled is boolean,
// the get method is named isFilled
public boolean isFilled() {
return filled;
}
// Set a new filled
public void setFilled(boolean filled) {
this.filled = filled;
}
// Get dateCreated
public java.util.Date getDateCreated() {
return dateCreated;
}
// Return a string representation of this object
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
// Abstract method getArea
public abstract double getArea();
// Abstract method getPerimeter
public abstract double getPerimeter();
}
b) Circle.java
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
c) Rectangle.java
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}
d) TestGeometricObject.java
public class TestGeometricObject {
/** Main method */
public static void main(String[] args) {
// Declare and initialize two geometric objects
GeometricObject geoObject1 = new Circle(3);
GeometricObject geoObject2 = new Rectangle(3, 4);
System.out.println("The two objects have the same area? " +
equalArea(geoObject1, geoObject2));
// Display circle
displayGeometricObject(geoObject1);
// Display rectangle
displayGeometricObject(geoObject2);
Object object1 = new Circle(4);
Object object2 = new Rectangle(3, 4);
System.out.println();
System.out.println("The is result from next case");
// Display circle and rectangle
displayObject(object1);
displayObject(object2);
}
/** A method for comparing the areas of two geometric objects */
public static boolean equalArea(GeometricObject object1,
GeometricObject object2) {
return object1.getArea() == object2.getArea();
}
/** A method for displaying a geometric object */
public static void displayGeometricObject(GeometricObject object) {
System.out.println();
System.out.println("The area is " + object.getArea());
System.out.println("The perimeter is " + object.getPerimeter());
}
public static void displayObject(Object object) {
if (object instanceof Circle) {
System.out.println("The circle area is " +
((Circle)object).getArea());
System.out.println("The circle perimeter is " +
((Circle)object).getPerimeter());
}
else if (object instanceof Rectangle) {
System.out.println("The rectangle area is " +
((Rectangle)object).getArea());
System.out.println("The rectengle perimeter is " +
((Rectangle)object).getPerimeter());
}
}
}
Part#2:
i) Using MyGeometricObject.java, MyCircle.java, ExceptionInterface.java
MyRectangle.java, modify MyRectangle.java program to use Interface declared
in ExceptionInterface.java and write another java program
called MyTestRectangle.java that can show exception whether height or width is
negative or not, and then calculate area and perimeter of a rectangle using height
and width. For help, you can follow MyTestCircle.java to write
your MyestRectangle.java.
ii) This program has Interface characteristics of Object Oriented Programming (OOP).
Draw a flowchart using your class lecture to show the relationship between
MyGeometricObject.java, ExceptionInterface.java and Rectangle.java. Also, describe
how interface and exception work in your modified program.
a) MyGeometricObject.java
public abstract class MyGeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
protected MyGeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with color and filled value */
protected MyGeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
* the get method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + "\ncolor: " + color +
" and filled: " + filled;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
b) MyCircle.java
import java.io.IOException;
public class MyCircle extends MyGeometricObject implements
ExceptionInterface {
private double radius;
public MyCircle() {
}
public MyCircle(double radius) {
try {
setRadius(radius);
}
catch (IllegalArgumentException e) {
System.out.println(e);
}
}
/** Return radius */
public double getRadius() {
return radius;
}
public void setRadius(double newRadius)
throws IllegalArgumentException {
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
public double getArea() {
return radius * radius * Math.PI;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the Mycircle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
c) MyRectangle.java
public class MyRectangle extends GeometricObject {
private double width;
private double height;
public MyRectangle() {
}
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}
d) ExceptionInterface.java
import java.io.IOException;
public interface ExceptionInterface {
void setRadius(double newRadius) throws IllegalArgumentException;
}
e) MyTestCircle.java
public class MyTestCircle {
/** Main method */
public static void main(String[] args) {
MyGeometricObject geoObject1 = new MyCircle(-5);
MyGeometricObject geoObject2 = new MyCircle(5);
displayGeometricObject(geoObject1);
displayGeometricObject(geoObject2);
}
public static void displayGeometricObject(MyGeometricObject object)
{
System.out.println();
System.out.println("The area is " + object.getArea());
System.out.println("The perimeter is " + object.getPerimeter());
}
}