Question: Exercise This week we will be practicing writing a class which implements an interface. Getting Started To start this exercise, you should: 1. Open eclipse
Exercise This week we will be practicing writing a class which implements an interface. Getting Started To start this exercise, you should: 1. Open eclipse and start a new Java project named Lab08 2. Add a Class (named Nat) to this project, and copy the contents of the Nat.java le provided into it. 3. Add a Class (named Mod8) to this project, and copy the contents of the Mod8.java le provided into it. 4. Add a Class (named Oct) to this project, the contents of which you will be writting from scratch. 5. Add a Class (named DriverL8) to this project, and copy the contents of the DriverL8.java le provided into it. Requirements Nat.java A very simple class which models some of the functionality of a Natural number. The Natrual numbers are the non-negative Integers: 0, ... This class is complete and must not be modi ed. 1. Why is setN() protected? Mod8.java A very simple interface which implies a the cyclic set of Natural numbers [0, 7]. This class is complete and must not be modi ed. 1. Why does the notion of an additive inverse not make sence for Natural numbers, but does make sence for the cyclic set [0, 7]? Oct.java A very simple class which extends the Nat class and implements the Mod8 interface. This class you must write, such that: 1. You include default, specifying and copy constructors 2. You override Nat's mutator. 3. You override Nat's clone method 4. You override Nat's equals method 5. You implement Mod8's inverse method For any x in [0, 7], its unique inverse i has the property that (x + i) % 8 == 0 6. You override Nat's increment, decrement and addition methods so that the underlying int value is always in the cyclic range [0, 7]. DriverL8.java A simple driver class to test all of the above classes / interface. This class is complete and must not be modi ed. 1. Identify how / where Polymorphism is being utilized. If you wrote your Oct class correctly then your output will look like: n1 = 0 n2 = 7 n3 = 7 n4 = 0 n5 = 0 no1 = 0 no2 = 7 no3 = 7 no4 = 0 no5 = 2 Good: Oct can not equal Nat Good: Oct.clone() works Good: Oct(Oct o) works n2 + n3 = 14 n1 = 0 n2 = 8 n3 = 0 no5 inverse = 6 no2 + no5 = 1 no2 - no5 = 5 no1 = 7 no2 = 0 no3 = 0
public interface Mod8 { int MODULUS = 8; Nat inverse(); }
public class Nat { private int n; protected void setN(int n) { if (n >= 0) this.n = n; else this.n = 0; }
public int getN() { return this.n; } public Nat() { this(0); } public Nat(int n) { super(); this.setN(n); } public Nat(Nat guest) { this(guest.getN()); } public Object clone() { return new Nat(this); } public boolean equals(Object guest) { if ((guest == null) || !(guest instanceof Nat)) return false; if (((Nat)guest).getN() != this.getN()) return false; return true; } public String toString() { return "" + this.getN(); } public void zero() { this.setN(0); } public void increment() { this.setN(this.getN() + 1); } void decrement() { if (this.getN() != 0) this.setN(this.getN() - 1); } public Nat addition(Nat guest) { return new Nat(this.getN() + guest.getN()); } }
public class DriverL8 { public static void main(String[] args) { Nat n1 = new Nat(); Nat n2 = new Nat(7); Nat n3 = new Nat(n2); Nat n4 = (Nat)n1.clone(); Nat n5 = new Nat(-2); System.out.println("n1 = " + n1); System.out.println("n2 = " + n2); System.out.println("n3 = " + n3); System.out.println("n4 = " + n4); System.out.println("n5 = " + n5); System.out.println(); Nat no1 = new Oct(); Nat no2 = new Oct(7); Nat no3 = new Oct((Oct)no2); Nat no4 = (Oct)no1.clone(); Nat no5 = new Oct(10); System.out.println("no1 = " + no1); System.out.println("no2 = " + no2); System.out.println("no3 = " + no3); System.out.println("no4 = " + no4); System.out.println("no5 = " + no5); System.out.println(); if (!no1.equals(n1)) System.out.println("Good: Oct can not equal Nat"); if (no1.equals(no4)) System.out.println("Good: Oct.clone() works"); if (no2.equals(no3)) System.out.println("Good: Oct(Oct o) works"); System.out.println(); System.out.println("n2 + n3 = " + n2.addition(n3)); System.out.println();
n1.decrement(); n2.increment(); n3.zero(); System.out.println("n1 = " + n1); System.out.println("n2 = " + n2); System.out.println("n3 = " + n3);
System.out.println(); System.out.println("no5 inverse = " + ((Oct)no5).inverse()); System.out.println("no2 + no5 = " + no2.addition(no5)); System.out.println("no2 - no5 = " + no2.addition(((Oct)no5).inverse())); System.out.println(); no1.decrement(); no2.increment(); no3.zero(); System.out.println("no1 = " + no1); System.out.println("no2 = " + no2); System.out.println("no3 = " + no3); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
