CSM02 – Labs 2, Solution

1. a) i

public class Rectangle

{

public double length

public double width;

public double area()

{

return length*width;

}

public double circumference()

{

return 2*(width+length);

}

}

public class PlaneRectangle extends Rectangle

{

public double x;

public double y;

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle one = new PlaneRectangle();

double length = 5;

double width = 10;

int shift_point_x = Integer.parseInt(args[0]);

int shift_point_y = Integer.parseInt(args[1]);

one.length = length;

one.width = width;

double area = one.area();

double circum = one.circumference();

one.x = 2;

one.y = 2;

System.out.println("Welcome to the Java Rectangle Program\n");

System.out.println("The default rectangle in this program has length "+one.length+" and width "+one.width+".\n");

System.out.println("The Centre point is located at
("+one.x+","+one.y+") by default");

System.out.println("It has area: "+area+"\n");

System.out.println("It has circumference: "+circum+"\n");

one.x = one.x + shift_point_x;

one.y = one.y + shift_point_y;

System.out.println("When moved by ("+shift_point_x+","+shift_point_y+"), the new centre point is located at ("+one.x+","+one.y+")");

}

}

1. b) i

public class Rectangle

{

public int length;

public int width;

public int area()

{

return length*width;

}

public int circumference()

{

return (2*length)+(2*width);

}

}

public class PlaneRectangle extends Rectangle

{

public int x;

public int y;

public int a;

public int b;

public int isx;

public int isy;

public boolean isinside()

{

if ((((width / 2) + x) >= a) & ((x - (width / 2)) <= a))

isx = 1;

if ((((length / 2) + y) >= b) & ((y - (length / 2)) <= b))

isy = 1;

if ((isx == 1) & (isy == 1))

return true;

else

return false;

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle r = new PlaneRectangle();

int length = 4;

int width = 4;

int insidex = Integer.parseInt(args[0]);

int insidey = Integer.parseInt(args[1]);

r.length = length;

r.width = width;

r.a = insidex;

r.b = insidey;

r.x = 0;

r.y = 0;

System.out.println("Creating Rectangle 1 - standby...\n\n");

System.out.println("Rectangle 1 consisting of length "+r.length+" and width "+r.width+" has been made by Java.\nCentre ("+r.x+","+r.y+")\n\n");

boolean result = r.isinside();

if (result == true)

System.out.println("The point ("+insidex+","+insidey+") lies INSIDE the rectangle.");

else

System.out.println("The point ("+insidex+","+insidey+") lies OUTSIDE the rectangle.");

}

}

1. c) i

public class Rectangle

{

public double length;

public double width;

public double area()

{

return length*width;

}

public double circumference()

{

return (2*length)+(2*width);

}

}

public class PlaneRectangle extends Rectangle

{

public int x;

public int y;

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle ra = new PlaneRectangle();

PlaneRectangle rb = new PlaneRectangle();

int xone = 0;

int xtwo = 0;

int yone = 0;

int ytwo = 0;

ra.length = 5;

ra.width = 10;

ra.x = 0;

ra.y = 0;

rb.length = Double.parseDouble(args[0]);

rb.width = Double.parseDouble(args[1]);

rb.x = 0;

rb.y = 0;

System.out.println("Rectangle A of length "+ra.length+" and width "+ra.width+" has been created.\nCentre ("+ra.x+","+ra.y+")\n\n");

System.out.println("Rectangle B of length "+rb.length+" and width "+rb.width+" has been created.\nCentre ("+rb.x+","+rb.y+")\n\n");

if((ra.x+(ra.length/2))>=(rb.x+(rb.length/2))) xone = 1;

if((ra.x-(ra.length/2))<=(rb.x-(rb.length/2))) xtwo = 1;

if((ra.y+(ra.width/2))>=(rb.y+(rb.width/2))) yone = 1;

if((ra.y-(ra.width/2))<=(rb.y-(rb.width/2))) ytwo = 1;

if((xone+xtwo+yone+ytwo)==4) System.out.println("Rectangle B fits completely inside Rectangle A.");

else System.out.println("Rectangle B does not fit completely inside Rectangle A.");

}

}

1. a) ii

public class Rectangle

{

public double length;

public double width;

public Rectangle()

{

length = 1.0;

width = 1.0;

}

public double area()

{

return length*width;

}

public double circumference()

{

return (2*length)+(2*width);

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

Rectangle r = new Rectangle();

System.out.println("A rectangle of length "+r.length+" and width "+r.width+" has been created.");

}

}

1. b) ii

public class Rectangle

{

public double length;

public double width;

public Rectangle()

{

length = 1.0;

width = 1.0;

}

public Rectangle(double length, double width)

{

this.length = length;

this.width = width;

}

public double area()

{

return length*width;

}

public double circumference()

{

return (2*length)+(2*width);

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

Rectangle y = new Rectangle();

Rectangle r = new Rectangle();

r.length = Integer.parseInt(args[0]);

r.width = Integer.parseInt(args[1]);

System.out.println("A default rectangle of length "+y.length+" and width "+y.width+" has been made.");

System.out.println("A rectangle of length "+r.length+" and width "+r.width+" has been created.");

}

}

1. a) iii

public class Rectangle

{

public double length;

public double width;

public Rectangle()

{

length = 1.0;

width = 1.0;

}

public double area()

{

return length*width;

}

public double circumference()

{

return (2*length)+(2*width);

}

}

public class PlaneRectangle extends Rectangle

{

public double x;

public double y;

public PlaneRectangle()

{

super();

x = 0.0;

y = 0.0;

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle r = new PlaneRectangle();

System.out.println("A rectangle of length "+r.length+" and width "+r.width+" has been created.\nOrigin=("+r.x+","+r.y+").");

}

}

1. b) iii

public class Rectangle

{

public double length;

public double width;

public Rectangle()

{

length = 1.0;

width = 1.0;

}

public double area()

{

return length*width;

}

public double circumference()

{

return (2*length)+(2*width);

}

}

public class PlaneRectangle extends Rectangle

{

public double x;

public double y;

public PlaneRectangle()

{

super();

x = 0.0;

y = 0.0;

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle r = new PlaneRectangle();

r.length = Double.parseDouble(args[0]);

r.width = Double.parseDouble(args[1]);

r.x = Double.parseDouble(args[2]);

r.y = Double.parseDouble(args[3]);

System.out.println("A rectangle of length "+r.length+" and width "+r.width+" has been created.\nOrigin=("+r.x+","+r.y+").");

}

}

2.

public class Rectangle

{

public double length1;

public double width1;

public double length2;

public double width2;

public Rectangle()

{

length1 = 1.0;

width1 = 1.0;

}

{

length2 = 1.0;

width2 = 1.0;

}

}

public class PlaneRectangle extends Rectangle

{

public double x1;

public double y1;

public double x2;

public double y2;

public PlaneRectangle()

{

super();

x1 = 0.0;

y1 = 0.0;

x2 = 0.0;

y2 = 0.0;

}

}

public class MakeRectangle

{

public static void main(String[] args)

{

PlaneRectangle r = new PlaneRectangle();

r.length1 = Double.parseDouble(args[0]);

r.width1 = Double.parseDouble(args[1]);

r.x1 = Double.parseDouble(args[2]);

r.y1 = Double.parseDouble(args[3]);

System.out.println("A rectangle of length "+r.length1+" and width "+r.width1+" has been created.\nOrigin=("+r.x1+","+r.y1+").");

r.length2 = Double.parseDouble(args[4]);

r.width2 = Double.parseDouble(args[5]);

r.x2 = Double.parseDouble(args[6]);

r.y2 = Double.parseDouble(args[7]);

System.out.println("A rectangle of length "+r.length2+" and width "+r.width2+" has been created.\nOrigin=("+r.x2+","+r.y2+").");

if ((r.x1-(0.5*length1))<(r.x2-(0.5*length2))) & ((r.x1+(0.5*length1))>(r.x2+(0.5*length2))) & ((r.y1-(0.5*width1))<(r.y2-(0.5*width2))) & ((r.y1+(0.5*width1))>(r.y2+(0.5*width2)))

{

System.out.println("Rectangle 2 is completely inside rectangle 1.");

}

if ((r.x2-(0.5*length2))<(r.x1-(0.5*length1))) & ((r.x2+(0.5*length2))>(r.x1+(0.5*length1))) & ((r.y2-(0.5*width2))<(r.y1-(0.5*width1))) & ((r.y2+(0.5*width2))>(r.y1+(0.5*width1)))

{

System.out.println("Rectangle 2 is completely inside rectangle 1.");

}

else

{

System.out.println("Neither rectangle is completely inside the other.")

}

}