Write a class, Mod9, that performs modulo 9 arithmetic. What this means is that
ID: 3849167 • Letter: W
Question
Write a class, Mod9, that performs modulo 9 arithmetic. What this means is that a Mod9 object can only hold integers from 0 to 8 and the value is determined by taking the absolute value and applying '% 9' (the remainder operator followed by 9). When converting Mod9 numbers to strings (using the _____str _____function) surround the value with " ". Here is the UML for the class: -value: int +Mod9(value: int = 0) +add (n2: Mod9): Mod9 +subract(n2: Mod9): Mod9 +multiply(n2: Mod9): Mod9 +toString(): String The following test program: import java util. Scanner public class TestMod9{ public static void main (String[] args) { System out.println.("" + Mod9 (1) + "" + Mod9 (-14) + "" + Mod9 (234)): Mod9 m1 = new Mod9 (44): Mod9 m2 = new Mod9 (78): Mod9 m3 = m1. multiply (m2): Mod9 m4 = m2. subtract (m2): Mod9 m5 = m1. add (m2): System out.println (m1 "" + m2 + "" + m3 + "" + m4 + "" + m5): } } should give this output:Explanation / Answer
class Mod9 {
int value;
public Mod9(int value){
this.value = value % 9;
}
public Mod9 add(Mod9 ob){
return new Mod9(this.value + ob.value);
}
public Mod9 multiply(Mod9 ob){
return new Mod9(this.value * ob.value);
}
public Mod9 substract(Mod9 ob){
return new Mod9(this.value - ob.value);
}
@Override
public String toString() {
return String.format("<" + value + "> " );
}
}
public class HelloWorld{
public static void main(String[] args) {
Mod9 m1 = new Mod9(44);
Mod9 m2 = new Mod9(78);
Mod9 m3 = m1.multiply(m2);
Mod9 m4 = m1.substract(m2);
Mod9 m5 = m1.add(m2);
System.out.println( m1 + " " + m2 + " " + m3 + " " + m4 + " " + m5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.