Skip to content

Instantly share code, notes, and snippets.

@kryvoboker
Created August 5, 2020 19:02
Show Gist options
  • Save kryvoboker/13f8e35461c23048f37675d8b1b41eba to your computer and use it in GitHub Desktop.
Save kryvoboker/13f8e35461c23048f37675d8b1b41eba to your computer and use it in GitHub Desktop.
HomeWork6 (java.lang.Object)(Stack)
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) + '}';
}
}
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;
}
}
}
}
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 + '}';
}
}
public class Main {
public static void main(String[] args) {
BaseOfGroups bog = new BaseOfGroups();
Group gp = new Group();
Students st = new Students();
Human h = new Human();
Stack k = new Stack(Object.class, 10);
k.push(st);
k.push(bog);
k.push(gp);
k.push(h);
System.out.println(k);
System.out.println();
System.out.println("Deleted Object: " + k.pop());
System.out.println(k);
System.out.println("Show Object: " + k.peek());
}
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.EmptyStackException;
/**
*
* @author kamaz
*/
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 void push(final T object) {
if (size >= maxSize) {
throw new RuntimeException();
}
data[size++] = object;
}
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) + '}';
}
}
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 super.toString(); //To change body of generated methods, choose Tools | Templates.
}
@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