[POO] aula06 ex3 added

This commit is contained in:
TiagoRG 2023-03-21 22:28:05 +00:00
parent 710c3d6f84
commit 0a08a6ce92
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,95 @@
package aula06;
public class Vector {
private int[] vector;
// Constructors and main getter
public Vector() {
this.vector = new int[0];
}
public Vector(int size) {
this.vector = new int[size];
}
public Vector(int[] vector) {
this.vector = vector;
}
public int[] getVector() {
return vector;
}
// Getters
public int size() {
return this.vector.length;
}
public boolean contains(int value) {
for (int n : this.vector)
if (n == value)
return true;
return false;
}
public int count(int value) {
int count = 0;
for (int n : this.vector)
if (n == value)
count++;
return count;
}
// Method to change values
public void insert(int value) {
if (this.contains(value)) return;
int[] aux = new int[this.size()+1];
System.arraycopy(this.vector, 0, aux, 0, this.size());
aux[this.size()] = value;
this.vector = aux;
}
public void remove(int value) {
int[] aux = new int[this.size()-this.count(value)];
int i = 0;
for (int n : this.vector) {
if (n == value)
continue;
aux[i] = n;
i++;
}
this.vector = aux;
}
public void empty() {
this.vector = new int[0];
}
// Interact with other vectors
public Vector add(Vector secondVector) {
Vector result = new Vector();
for (int n : this.vector)
if (!result.contains(n))
result.insert(n);
for (int n : secondVector.vector)
if (!result.contains(n))
result.insert(n);
return result;
}
public Vector subtract(Vector secondVector) {
Vector result = new Vector();
for (int n : this.vector)
if (!result.contains(n) && !secondVector.contains(n))
result.insert(n);
return result;
}
public Vector intersect(Vector secondVector) {
Vector result = new Vector();
for (int n : this.vector)
if (!result.contains(n) && secondVector.contains(n))
result.insert(n);
return result;
}
// ToString
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (int n : this.vector)
result.append(String.format("%d ", n));
return this.size() > 0 ? result.substring(0, result.length()-1) : result.toString();
}
}

View File

@ -0,0 +1,31 @@
package aula06;
public class VectorTest {
public static void main(String[] args) {
Vector c1 = new Vector();
c1.insert(4); c1.insert(7); c1.insert(6); c1.insert(5);
Vector c2 = new Vector();
int[] test = { 7, 3, 2, 5, 4, 6, 7};
for (int el : test) c2.insert(el);
c2.remove(3); c2.remove(5); c2.remove(6);
System.out.println(c1);
System.out.println(c2);
System.out.println("Número de elementos em c1: " + c1.size());
System.out.println("Número de elementos em c2: " + c2.size());
System.out.println("c1 contém 6?: " + ((c1.contains(6) ? "sim" : "não")));
System.out.println("c2 contém 6?: " + ((c2.contains(6) ? "sim" : "não")));
System.out.println("União:" + c1.add(c2));
System.out.println("Interseção:" + c1.intersect(c2));
System.out.println("Diferença:" + c1.subtract(c2));
c1.empty();
System.out.println("c1:" + c1);
}
}