Lesson 9 - More OOP
DIY - Creating Fib Class (10 minutes)

create a Fib
class that can run the following code:
Fib fib = new Fib();
fib.next(2);
System.out.println(fib);
System.out.println(fib.next(3));
System.out.println(fib.next().next());
Fib fib2 = new Fib(7).next().prev();
System.out.println(fib2);
System.out.println("are fibs equal? " + (fib.equals(fib2) ? "yes" : "no"));
Fib(2) = 3
Fib(5) = 13
Fib(7) = 34
Fib(7) = 34
are fibs equal? yes
Solution
public class Main {
public static void main(String[] args) {
Fib fib = new Fib();
fib.next(2);
System.out.println(fib);
System.out.println(fib.next(3));
System.out.println(fib.next().next());
Fib fib2 = new Fib(6).next();
System.out.println(fib2);
System.out.println("are fibs equal? " + (fib.equals(fib2) ? "yes" : "no"));
}
}
Fib(2) = 3
Fib(5) = 13
Fib(7) = 34
Fib(7) = 34
are fibs equal? yes
Creating an IntList

public class Main {
public static void main(String[] args) {
IntList list = new IntList(4, 10);
list.add(20).add(30);
System.out.println(list);
for (int i = 0; i < list.length() ; i++)
System.out.print(list.get(i) + " ,");
System.out.println("\n======= Equals =======\n");
IntList new_list = new IntList(list); // Calls copy constructor
System.out.println("new_list: " + new_list.toString());
System.out.println("are the list the same: " + new_list.equals(list));
new_list.add(100);
System.out.println("new_list: " + new_list);
System.out.println("are the list the same: " + new_list.equals(list));
new_list.remove_index(6);
System.out.println("new_list: " + new_list);
System.out.println("are the list the same: " + new_list.equals(list));
}
}
IntList{size=6, capacity=8, elements=[10, 10, 10, 10, 20, 30]}
10 ,10 ,10 ,10 ,20 ,30 ,
======= Equals =======
new_list: IntList{size=6, capacity=6, elements=[10, 10, 10, 10, 20, 30]}
are the list the same: true
new_list: IntList{size=7, capacity=12, elements=[10, 10, 10, 10, 20, 30, 100]}
are the list the same: false
new_list: IntList{size=6, capacity=12, elements=[10, 10, 10, 10, 20, 30]}
are the list the same: true
another example
public class Main {
public static void main(String[] args) {
IntList list = new IntList(4, 10).add(20);
System.out.println(list);
list.add(100,4).remove_index(4);
System.out.println(list);
}
}
IntList{size=5, capacity=8, elements=[10, 10, 10, 10, 20]}
IntList{size=9, capacity=16, elements=[10, 10, 10, 10, 100, 100, 100, 100, 100]}
Polygon (Point list)


Lets change a few properties of our IntList
and create a PointList
we will add a few fields
Color color
String name
and a method to convert the the x and y to a list of double
double[] convert2DoubleArray(String x_or_y)
public class Main {
public static void main(String[] args) {
Polygon list = new Polygon(3, new Point(1, 2)).add(new Point(30, 40));
System.out.println(list);
list.add(new Point(-100, -100),3).remove_index(4);
System.out.println(list);
Polygon new_list = new Polygon(list);
System.out.println("are the Polygons the same? " + list.equals(new_list));
list.remove_index(3);
System.out.println("are the Polygons the same? " + list.equals(new_list));
}
}
PointList{size=4, capacity=6, elements=[Point { x=1.0, y=2.0 }, Point { x=1.0, y=2.0 }, Point { x=1.0, y=2.0 }, Point { x=30.0, y=40.0 }]}
PointList{size=7, capacity=12, elements=[Point { x=1.0, y=2.0 }, Point { x=1.0, y=2.0 }, Point { x=1.0, y=2.0 }, Point { x=30.0, y=40.0 }, Point { x=-100.0, y=-100.0 }, Point { x=-100.0, y=-100.0 }, Point { x=-100.0, y=-100.0 }]}
are the Polygons the same? true
are the Polygons the same? false
Drawing the polygon

Lets now draw a Polygon (Point list with at least 3 points + the first point to close the loop)
The points in the polygon are (x[i]
, y[i]
). For example, the following code fragment draws a filled diamond with vertices (0.1, 0.2), (0.2, 0.3), (0.3, 0.2), and (0.2, 0.1):
double[] x = { 0.1, 0.2, 0.3, 0.2 };
double[] y = { 0.2, 0.3, 0.2, 0.1 };
StdDraw.filledPolygon(x, y);
so we need to add the following function to Polygon
private double[] convert2DoubleArray(String x_or_y) {
double[] list = new double[elements.length];
switch (x_or_y) {
case "x":
for (int i = 0; i < size; i++)
list[i] = elements[i].getX();
return list;
//break;
case "y":
for (int i = 0; i < size; i++)
list[i] = elements[i].getY();
return list;
default:
return new double[]{0., 0.};
}
}
public class Main {
public static void main(String[] args) {
Polygon polygon1 = new Polygon();
polygon1.add(new Point(20,20));
polygon1.add(new Point(80,20));
polygon1.add(new Point(50,70));
polygon1.add(new Point(20,20));
Polygon polygon2 = new Polygon();
polygon2.setColor(0,255,0);
polygon2.add(new Point(80,20));
polygon2.add(new Point(90,40));
polygon2.add(new Point(50,70));
polygon2.add(new Point(80,20));
StdDraw.setXscale(0, 100);
StdDraw.setYscale(0, 100);
polygon1.draw();
polygon2.draw();
}
}

or
public class Main {
public static void main(String[] args) {
Polygon[] polygons = new Polygon[3];
polygons[0] = new Polygon();
polygons[0].add(new Point(229,153));
polygons[0].add(new Point(160,218));
polygons[0].add(new Point(177,325));
polygons[0].add(new Point(249,268));
polygons[0].add(new Point(229,153));
polygons[1] = new Polygon();
polygons[1].setColor(0,255,0);
polygons[1].add(new Point(249,268));
polygons[1].add(new Point(177,325));
polygons[1].add(new Point(269,336));
polygons[1].add(new Point(349,283));
polygons[1].add(new Point(249,268));
polygons[2] = new Polygon().setName("redPolygon");
polygons[2].setColor(255,0,0);
polygons[2].add(new Point(349,283));
polygons[2].add(new Point(328,182));
polygons[2].add(new Point(229,153));
polygons[2].add(new Point(249,268));
polygons[2].add(new Point(349,283));
StdDraw.setXscale(0, 500);
StdDraw.setYscale(0, 500);
for (int i = 0; i < polygons.length; i++) {
polygons[i].draw();
System.out.println(polygons[i]);
}
}
}
polygon_1: Polygon{num of points: 5, capacity=10, elements=[Point { x=229.0, y=153.0 }, Point { x=160.0, y=218.0 }, Point { x=177.0, y=325.0 }, Point { x=249.0, y=268.0 }, Point { x=229.0, y=153.0 }]}
polygon_2: Polygon{num of points: 5, capacity=10, elements=[Point { x=249.0, y=268.0 }, Point { x=177.0, y=325.0 }, Point { x=269.0, y=336.0 }, Point { x=349.0, y=283.0 }, Point { x=249.0, y=268.0 }]}
redPolygon: Polygon{num of points: 5, capacity=10, elements=[Point { x=349.0, y=283.0 }, Point { x=328.0, y=182.0 }, Point { x=229.0, y=153.0 }, Point { x=249.0, y=268.0 }, Point { x=349.0, y=283.0 }]}

Last updated
Was this helpful?