In: Computer Science
Lab 4 Specifications
Inside each of the arithmetic classes (Add, Subtract, Multiply, Divide, Modulo), make sure EACH class contains the following:
Two private fields
// Example Fields:
private int n1;
private int n2;
A constructor that passes two values that can be saved in the private fields
// Example Constructor:
public Add(int n1, int n2){
this.n1 = n1;
this.n2 = n2;
}
The following three methods must be supported by ALL arithmetic classes:
public int getResult(); // This should return the result of n1 arithmetic operator n2 // n1 + n2 for add
public void changeVals(int n1, int n2); // This should change or “reset” the values in the
// private class fields
public String toString(); // This returns a string that displays the equation with result
// e.g. “3 + 5 = 8”
Inside the “Lab04.java” file should contain the following:
All package and import statements needed to reference the other packages and classes.
Inside the main method, create class objects for each of arithmetic classes
// Example:
Add a = new Add(5, 3);
For each class object, make sure to ALSO print out the toString method of each to verify the current equation, call the method to change the values, and print out the toString method again to verify the changes.
// Example:
System.out.println(a.toString());
a.changeVals(4, 4);
System.out.println(a.toString());
Verify, by running the program, that everything works as expected.
The Java programs for your requirement given below,
Project structure:
Add.java
package add;
public class Add {
private int n1;
private int n2;
public Add(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getResult() {
return n1 + n2;
}
public void changeVals(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public String toString() {
return n1 + "+" + n2 + "=" + (n1 +
n2);
}
}
Subtract.java
package subtract;
public class Subtract {
private int n1;
private int n2;
public Subtract(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getResult() {
return n1 - n2;
}
public void changeVals(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public String toString() {
return n1 + "-" + n2 + "=" + (n1 -
n2);
}
}
Multiply.java
package multiply;
public class Multiply {
private int n1;
private int n2;
public Multiply(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getResult() {
return n1 * n2;
}
public void changeVals(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public String toString() {
return n1 + "*" + n2 + "=" + (n1 *
n2);
}
}
Divide.java
package divide;
public class Divide {
private int n1;
private int n2;
public Divide(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getResult() {
return n1 / n2;
}
public void changeVals(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public String toString() {
return n1 + "/" + n2 + "=" + (n1 /
n2);
}
}
Modulo.java
package modulo;
public class Modulo {
private int n1;
private int n2;
public Modulo(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public int getResult() {
return n1 % n2;
}
public void changeVals(int n1, int n2) {
this.n1 = n1;
this.n2 = n2;
}
public String toString() {
return n1 + "%" + n2 + "=" + (n1 %
n2);
}
}
Lab04.java
package Main;
import add.Add;
import divide.Divide;
import modulo.Modulo;
import multiply.Multiply;
import subtract.Subtract;
public class Lab04 {
public static void main(String args[]) {
// addition
Add a = new Add(5, 3);
System.out.println(a.toString());
a.changeVals(4, 4);
System.out.println(a.toString());
//
subtraction
Subtract s = new Subtract(5,
3);
System.out.println(s.toString());
s.changeVals(4, 4);
System.out.println(s.toString());
//
multiplication
Multiply m = new Multiply(5,
3);
System.out.println(m.toString());
m.changeVals(4, 4);
System.out.println(m.toString());
// division
Divide d = new Divide(5, 3);
System.out.println(d.toString());
d.changeVals(4, 4);
System.out.println(d.toString());
// modulo
Modulo mo = new Modulo(5, 3);
System.out.println(mo.toString());
mo.changeVals(4, 4);
System.out.println(mo.toString());
}
}
Sample
output: