Please Use JAVA for this and make your code as simple as possible. assigne dprob
ID: 3847669 • Letter: P
Question
Please Use JAVA for this and make your code as simple as possible.
assigne dproblem:
A class object of your choice (Full points ONLY if invariants are checked properly everywhere. "Checked properly" includes throwing an exception when necessary)
(2 Points) A constructor
(2 Points) An overloaded constructor
(2 Points) At least two properties (and the corresponding getters/setters)
(2 Points) having at least one array type property
(2 Points) having at least one non-array reference type property
(2 Points) A method that takes in a parameter and returns a value (other than getters/setters)
(2 Points) A method that takes in at least two parameters and doesn't return a value (modifies the class properties)
(2 Points) A method that loops through an array to manipulate or gather data from the array
(2 Points) a toString() method
(2 Points) an equals() method
A driver for your class object
(2 Points) for testing creating valid and invalid instances of the class object
(4 Points) for testing every public method (including getters and setters) with valid and invalid values (i.e. test the invariants)
(4 Points) for testing the boundary conditions (lower and upper boundaries) for every public methods in the class (including getters and setters)
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Objects;
public class Test{
private int[] arr;
private PrintWriter out;
public Test(int[] arr) {
this.arr = arr;
out = new PrintWriter(System.out);
}
public Test(int size) {
arr = new int[size];
out = new PrintWriter(System.out);
}
public int[] getArr() {
return arr;
}
public void setArr(int[] arr) {
this.arr = arr;
}
public PrintWriter getOut() {
return out;
}
public void setOut(PrintWriter out) {
this.out = out;
}
public Test minumumSum(Test other){
int sumThis = 0;
int sumOther = 0;
for (int i = 0; i < arr.length; i++)
sumThis += arr[i];
for (int i = 0; i < other.arr.length; i++)
sumOther += other.arr[i];
return sumThis<sumOther?this:other;
}
public void printDifference(int[] otherArr, int k) {
for (int i = 0; i < arr.length; i++) {
boolean flag = true;
for (int j = 0; j < k; j++)
if (arr[i] == otherArr[j])
flag = false;
if (flag)
out.println(arr[i]);
}
}
public int getMinimum() {
int min = arr[0];
for (int i = 0; i < arr.length; i++)
if (arr[i] < min)
min = arr[i];
return min;
}
@Override
public String toString() {
return "Test{" + "arr=" + Arrays.toString(arr) + '}';
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Test other = (Test) obj;
if (!Arrays.equals(this.arr, other.arr)) {
return false;
}
if (!Objects.equals(this.out, other.out)) {
return false;
}
return true;
}
}
class TestDriver {
public static void main(String[] args) throws FileNotFoundException {
Test obj = new Test(5);
int[] a = {1,3,5,2,3,6,0};
Test obj1 = new Test(a);
obj1.setOut(new PrintWriter("JavaRocks,txt"));
obj.printDifference(a, a.length);
obj1.printDifference(obj.getArr(), obj.getArr().length);
obj1.setOut(obj.getOut());
Test obj2 = obj.minumumSum(obj1);
if (obj2.equals(obj))
System.out.println("Obj is minmum");
else if (obj2.equals(obj1))
System.out.println("Obj1 is minimum");
System.out.println(obj2.toString());
}
}
Here is a random class Test that satisfies all the requirments given above. I have not commented the code because the function of teh class is random. If incase you need explanation, let me know through the comments. Shall update them
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.