Skip to content

Instantly share code, notes, and snippets.

@ad-m
Created October 13, 2015 16:31
Show Gist options
  • Select an option

  • Save ad-m/c396b09dcb26a0e410bf to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/c396b09dcb26a0e410bf to your computer and use it in GitHub Desktop.
public interface IZbior {
public void wypisz();
public void dodaj(int nowy);
public void usun(int element);
public int rozmiar();
public boolean czyZawiera(int element);
public void wczytaj(String filename);
public boolean czyRowne(IZbior drugi);
public IZbior suma(IZbior drugi);
public IZbior roznica(IZbior drugi);
public IZbior przeciecie(IZbior drugi);
public IZbior roznicaSymetryczna(IZbior drugi);
public int getElement(int pos);
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Zbior implements IZbior {
private int memory[] = new int[25];
private int size = 0;
public Zbior() {
}
@Override
public void wypisz() {
if(this.size == 0){
return;
}
System.out.print("[");
for(int i=0; i<this.size-1; i++){
System.out.print(this.memory[i]);
System.out.print(", ");
};
System.out.print(this.memory[this.size-1]);
System.out.print("]\n");
}
private int extends_memory(){
int[] temp = this.memory.clone();
this.memory = new int[this.memory.length*2];
System.arraycopy(temp, 0, this.memory, 0, temp.length);
return this.memory.length;
}
@Override
public void dodaj(int nowy) {
if(this.czyZawiera(nowy)){
return;
}
if(this.size == this.memory.length){
this.extends_memory();
}
this.memory[this.size]=nowy;
this.size+=1;
return;
}
@Override
public void usun(int element) {
int position = -1;
for(int i=0; i<this.size; i++){
if(this.memory[i] == element){
position = i;
}
}
if(position > 0){
for(int i=this.size; i>position; i--){
this.memory[i] = this.memory[i-1];
}
this.size--;
}
}
@Override
public int rozmiar() {
return this.size;
}
@Override
public boolean czyZawiera(int element) {
for(int i=0; i<this.size; i++){
if(this.memory[i] == element){
return true;
}
}
return false;
}
@Override
public void wczytaj(String filename) {
File fp = new File(filename);
Scanner sc;
try {
sc = new Scanner(fp);
while(true){
sc.nextInt();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean czyRowne(IZbior drugi) {
IZbior nowy = this.suma(drugi);
if(nowy.rozmiar() == drugi.rozmiar() && nowy.rozmiar() == this.rozmiar()){
return true;
}
return false;
}
@Override
public IZbior suma(IZbior drugi) {
Zbior nowy = new Zbior();
for(int i=0; i<this.size; i++){
nowy.dodaj(this.memory[i]);
}
for(int i=0; i<drugi.rozmiar(); i++){
nowy.dodaj(drugi.getElement(i));
}
return nowy;
}
@Override
public IZbior roznica(IZbior drugi) {
Zbior nowy = new Zbior();
IZbior suma = this.suma(drugi);
for(int i=0; i<suma.rozmiar(); i++){
int liczba = suma.getElement(i);
if(this.czyZawiera(liczba) && !drugi.czyZawiera(liczba)){
nowy.dodaj(liczba);
}
}
return nowy;
}
@Override
public IZbior przeciecie(IZbior drugi) {
Zbior nowy = new Zbior();
IZbior suma = this.suma(drugi);
for(int i=0; i<=suma.rozmiar(); i++){
int liczba = suma.getElement(i);
if(this.czyZawiera(liczba) && drugi.czyZawiera(liczba)){
nowy.dodaj(liczba);
}
}
return nowy;
}
@Override
public IZbior roznicaSymetryczna(IZbior drugi) {
return this.suma(drugi).roznica(this.przeciecie(drugi));
}
public static void main(String[] args) {
System.out.print("Start: \n");
Zbior a = new Zbior();
Zbior b = new Zbior();
a.dodaj(1);
a.dodaj(2); b.dodaj(2);
a.dodaj(3); b.dodaj(3);
b.dodaj(4);
assert(a.suma(b).rozmiar() == 4);
System.out.print("Suma: "); a.suma(b).wypisz();
assert(a.roznica(b).rozmiar() == 1);
System.out.print("Roznica: "); a.roznica(b).wypisz();
assert(a.przeciecie(b).rozmiar() == 2);
System.out.print("Przeciecie: "); a.przeciecie(b).wypisz();
assert(a.roznicaSymetryczna(b).rozmiar() == 2);
System.out.print("roznicaSymetryczna: "); a.roznicaSymetryczna(b).wypisz();
System.out.print("\nEnd");
}
@Override
public int getElement(int pos) {
return this.memory[pos];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment