Last active
August 8, 2020 13:53
-
-
Save kryvoboker/c5513ef51c62eac2ba8c5ca719bd81ef to your computer and use it in GitHub Desktop.
HomeWork6 (java.lang.Object)(Stack) + BlackList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class BaseOfGroups implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private Group[] groups = new Group[3]; | |
public BaseOfGroups() { | |
super(); | |
} | |
public Group[] getGroups() { | |
return groups; | |
} | |
public void setGroups(Group[] groups) { | |
this.groups = groups; | |
} | |
public void setGroup(Group gp) { | |
for (int i = 0; i < groups.length; i += 1) { | |
if (groups[i] == null) { | |
groups[i] = gp; | |
break; | |
} | |
} | |
} | |
@Override | |
public String toString() { | |
return "BaseOfGroups{" + "groups = " + Arrays.toString(groups) + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class BlackList { | |
private Class[] css = new Class[3]; | |
public BlackList() { | |
super(); | |
} | |
public Class[] getCss() { | |
return css; | |
} | |
public void setCss(Class[] css) { | |
this.css = css; | |
} | |
public void setList(Class cs) { | |
for (int i = 0; i < css.length; i += 1) { | |
if (css[i] == null) { | |
css[i] = cs; | |
break; | |
} | |
} | |
} | |
@Override | |
public String toString() { | |
return "BlackList{" + "css = " + Arrays.toString(css) + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Car { | |
private String model; | |
private int buildYear; | |
public Car(String model, int buildYear) { | |
this.model = model; | |
this.buildYear = buildYear; | |
} | |
public String getModel() { | |
return model; | |
} | |
public void setModel(String model) { | |
this.model = model; | |
} | |
public int getBuildYear() { | |
return buildYear; | |
} | |
public void setBuildYear(int buildYear) { | |
this.buildYear = buildYear; | |
} | |
@Override | |
public String toString() { | |
return "Car{" + "model=" + model + ", buildYear=" + buildYear + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cat { | |
private String name; | |
private int age; | |
public Cat(String name, int age) { | |
this.name = name; | |
this.age = age; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
@Override | |
public String toString() { | |
return "Cat{" + "name=" + name + ", age=" + age + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Group implements Serializable { | |
private int groupMarker; | |
private Students[] student = new Students[3]; | |
public Group() { | |
super(); | |
} | |
public Group(int groupMarker) { | |
this.groupMarker = groupMarker; | |
} | |
public int getGroupMarker() { | |
return groupMarker; | |
} | |
public void setGroupMarker(int groupMarker) { | |
this.groupMarker = groupMarker; | |
} | |
public Students[] getStudents() { | |
return student; | |
} | |
public void setStudents(Students[] student) { | |
this.student = student; | |
} | |
@Override | |
public String toString() { | |
return "Group{" + "groupMarker = " + groupMarker + Arrays.toString(student) + '}'; | |
} | |
public void setStudent(Students std, int number) { | |
for (int i = 0; i < student.length; i += 1) { | |
if (student[i] == null && number == groupMarker) { | |
student[i] = std; | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Human implements Serializable { | |
private int age; | |
private String sex; | |
private String name; | |
private String surname; | |
public Human(int age, String sex, String name, String surname) { | |
this.age = age; | |
this.sex = sex; | |
this.name = name; | |
this.surname = surname; | |
} | |
public Human() { | |
super(); | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public String getSex() { | |
return sex; | |
} | |
public void setSex(String sex) { | |
this.sex = sex; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
@Override | |
public String toString() { | |
return "Human{" + "age = " + age + ", sex = " + sex + ", name = " + name + ", surname = " + surname + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
BaseOfGroups bog = new BaseOfGroups(); | |
Group gp = new Group(); | |
Group g = new Group(); | |
Students st = new Students(); | |
Human h = new Human(); | |
Cat cat = new Cat("Murzik", 5); | |
Cat catTwo = new Cat("Musia", 4); | |
Car car = new Car("Mescedes", 1968); | |
Car carTwo = new Car("Shkoda", 2000); | |
Square sr = new Square(12, 48); | |
Square srTwo = new Square(25, 50); | |
BlackList bk = new BlackList(); | |
bk.setList(Cat.class); | |
bk.setList(Car.class); | |
bk.setList(Square.class); | |
System.out.println("bk = " + bk); | |
System.out.println(); | |
Stack k = new Stack(Object.class, 10); | |
k.push(st, bk); | |
k.push(bog, bk); | |
k.push(gp, bk); | |
k.push(h, bk); | |
k.push(cat, bk); | |
k.push(g, bk); | |
k.push(car, bk); | |
k.push(bog, bk); | |
k.push(sr, bk); | |
k.push(st, bk); | |
k.push(catTwo, bk); | |
k.push(h, bk); | |
k.push(carTwo, bk); | |
k.push(srTwo, bk); | |
System.out.println("Show Stack = " + k); | |
System.out.println(); | |
System.out.println("Deleted Object: " + k.pop()); | |
System.out.println(k); | |
System.out.println("Show Object: " + k.peek()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Square { | |
private int weight; | |
private int height; | |
public Square(int weight, int height) { | |
this.weight = weight; | |
this.height = height; | |
} | |
public int getWeight() { | |
return weight; | |
} | |
public void setWeight(int weight) { | |
this.weight = weight; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
@Override | |
public String toString() { | |
return "Square{" + "weight=" + weight + ", height=" + height + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Array; | |
import java.util.Arrays; | |
import java.util.EmptyStackException; | |
/** | |
* | |
* @author kamaz | |
* @param <T> | |
*/ | |
public class Stack<T> { | |
private final T[] data; | |
private int size; | |
private final int maxSize; | |
public Stack(Class<T> clazz, int maxSize) { | |
this.size = 0; | |
this.maxSize = maxSize; | |
data = (T[]) Array.newInstance(clazz, maxSize); | |
} | |
public T push(final T object, BlackList bk) { | |
for (int i = 0; i < bk.getCss().length; i += 1) { | |
if (bk.getCss()[i].isInstance(object)) { | |
System.out.println("ERROR " + object); | |
return null; | |
} | |
} | |
if (size >= maxSize) { | |
throw new RuntimeException(); | |
} | |
data[size++] = object; | |
return data[size]; | |
} | |
public T pop() { | |
if (size <= 0) { | |
throw new EmptyStackException(); | |
} | |
T result = data[--size]; | |
data[size] = null; | |
return result; | |
} | |
public T peek() { | |
if (size <= 0) { | |
throw new EmptyStackException(); | |
} | |
T result = data[size]; | |
for (;;) { | |
if (data[size] == null) { | |
result = data[--size]; | |
} | |
return result; | |
} | |
} | |
@Override | |
public String toString() { | |
return "Stack{" + "data=" + Arrays.toString(data) + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/** | |
* | |
* @author kamaz | |
*/ | |
public class Students extends Human implements Serializable { | |
public Students(int age, String sex, String name, String surname) { | |
super(age, sex, name, surname); | |
} | |
public Students() { | |
super(); | |
} | |
@Override | |
public String toString() { | |
return "Students{" + "age = " + getAge() + ", sex = " + getSex() + ", name = " + getName() + ", surname = " + getSurname() + '}'; | |
} | |
@Override | |
public void setSurname(String surname) { | |
super.setSurname(surname); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getSurname() { | |
return super.getSurname(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setName(String name) { | |
super.setName(name); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getName() { | |
return super.getName(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setSex(String sex) { | |
super.setSex(sex); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public String getSex() { | |
return super.getSex(); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public void setAge(int age) { | |
super.setAge(age); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int getAge() { | |
return super.getAge(); //To change body of generated methods, choose Tools | Templates. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment