aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Java/lab6
diff options
context:
space:
mode:
Diffstat (limited to 'csci1913/Java/lab6')
-rw-r--r--csci1913/Java/lab6/Polygon.java32
-rw-r--r--csci1913/Java/lab6/lab6_strap012.java19
-rw-r--r--csci1913/Java/lab6/tests6.java28
3 files changed, 79 insertions, 0 deletions
diff --git a/csci1913/Java/lab6/Polygon.java b/csci1913/Java/lab6/Polygon.java
new file mode 100644
index 0000000..7f025fe
--- /dev/null
+++ b/csci1913/Java/lab6/Polygon.java
@@ -0,0 +1,32 @@
+package lab6;
+
+class Polygon
+{
+ private int[] sideLengths;
+
+ public Polygon(int sides, int ... lengths)
+ {
+ int index = 0;
+ sideLengths = new int[sides];
+ for (int length: lengths)
+ {
+ sideLengths[index] = length;
+ index += 1;
+ }
+ }
+
+ public int side(int number)
+ {
+ return sideLengths[number];
+ }
+
+ public int perimeter()
+ {
+ int total = 0;
+ for (int index = 0; index < sideLengths.length; index += 1)
+ {
+ total += side(index);
+ }
+ return total;
+ }
+}
diff --git a/csci1913/Java/lab6/lab6_strap012.java b/csci1913/Java/lab6/lab6_strap012.java
new file mode 100644
index 0000000..e2c96e4
--- /dev/null
+++ b/csci1913/Java/lab6/lab6_strap012.java
@@ -0,0 +1,19 @@
+package lab6;
+
+class Rectangle extends Polygon {
+ public Rectangle(int l, int w) {
+ super(4, l, w, l, w);
+ }
+ public int area() {
+ return this.side(0)*this.side(1);
+ }
+}
+
+class Square extends Polygon {
+ public Square(int l) {
+ super(4, l, l, l, l);
+ }
+ public int area() {
+ return this.side(0)*this.side(1);
+ }
+} \ No newline at end of file
diff --git a/csci1913/Java/lab6/tests6.java b/csci1913/Java/lab6/tests6.java
new file mode 100644
index 0000000..fc98648
--- /dev/null
+++ b/csci1913/Java/lab6/tests6.java
@@ -0,0 +1,28 @@
+package lab6;
+
+// SHAPES. Public tests for the classes RECTANGLE and SQUARE. Comments show
+// what each test must print, and how many points it is worth.
+
+class Shapes
+{
+ public static void main(String[] args)
+ {
+ Rectangle wreck = new Rectangle(3, 5);
+
+ System.out.println(wreck.side(0)); // 3 1 point.
+ System.out.println(wreck.side(1)); // 5 1 point.
+ System.out.println(wreck.side(2)); // 3 1 point.
+ System.out.println(wreck.side(3)); // 5 1 point.
+ System.out.println(wreck.area()); // 15 1 point.
+ System.out.println(wreck.perimeter()); // 16 1 point.
+
+ Square nerd = new Square(7);
+
+ System.out.println(nerd.side(0)); // 7 1 point.
+ System.out.println(nerd.side(1)); // 7 1 point.
+ System.out.println(nerd.side(2)); // 7 1 point.
+ System.out.println(nerd.side(3)); // 7 1 point.
+ System.out.println(nerd.area()); // 49 1 point.
+ System.out.println(nerd.perimeter()); // 28 1 point.
+ }
+}