• Апреля 05, 2020, 06:36:02 am
• Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Не получили письмо с кодом активации?
Новости: Вебинар по выбору IT профессии: http://bit.ly/2S4kcC6

Автор Тема: Java on-line_pobut  (Прочитано 311 раз)

Mykola72

  • Пользователи
  • *
  • Сообщений: 39
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #45 : Марта 26, 2020, 06:31:34 pm »
Домашнее задание.
Программа для группы  из 10-ти студентов (подкласс Human).
Class Main
package com.gmail.ua.pobut;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] groupOne = new Student[10];
Group group = new Group(groupOne);
Student student1=new Student( "ivanov","ivan","ivanovych",true,20,4.8);
Student student2=new Student( "petrov","petr","petrovych",true,20,4.2);
Student student3=new Student( "sidorov","sidor","sidorovych",true,20,5.0);
Student student4=new Student( "janov","ivan","ivanovych",true,20,4.8);
Student student5=new Student( "debrov","petr","petrovych",true,20,4.2);
Student student6=new Student( "baulorov","sidor","sidorovych",true,20,5.0);
Student student7=new Student( "hrenov","ivan","ivanovych",true,20,4.8);
Student student8=new Student( "rebrov","petr","petrovych",true,20,4.2);
Student student9=new Student( "fedorov","sidor","sidorovych",true,20,5.0);
String choiceAct = "null";
Scanner sc = new Scanner(System.in);
groupOne[0]=student3;
groupOne[1]=student2;
groupOne[2]=student1;
groupOne[3]=student4;
groupOne[4]=student5;
groupOne[5]=student6;
groupOne[6]=student7;
groupOne[7]=student8;
groupOne[8]=student9;
group.toString(groupOne);
for (; !choiceAct.equals("e");) {
System.out.println("If you want to add new student press-a, search-s, delete-d, see group-p, exit-e");
choiceAct = sc.nextLine();
if (choiceAct.equals("a")) {
try {
group.groupFull(groupOne);
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Can not add 11-th student. Group is full");
continue;
}
group.addStudent(groupOne);
}
if (choiceAct.equals("s")) {
group.searchStudent(groupOne);

}
if (choiceAct.equals("d")) {
group.delStudent(groupOne);
}
if (choiceAct.equals("p")) {
group.toString(groupOne);
}
}
sc.close();
}

}
Class Group
package com.gmail.ua.pobut;

import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Group {
private Student[] groupOne = new Student[10];
Scanner sc = new Scanner(System.in);

public Group() {
super();
// TODO Auto-generated constructor stub
}

public Group(Student[] groupOne) {
super();
this.groupOne = groupOne;
}

public Student[] getGroup() {
return this.groupOne;
}

public void setGroup(Student[] groupOne) {
this.groupOne = groupOne;
}

public void addStudent(Student[] groupOne) {

System.out.println("Input surnem");
String surName = sc.nextLine();
System.out.println("Input first name");
String firstName = sc.nextLine();
System.out.println("Input patronymic");
String patroNymic = sc.nextLine();
System.out.println("Input sex");
boolean sex = sc.nextBoolean();
System.out.println("Input age");
int age = sc.nextInt();
double midMark = 0;
for (;;) {
try {
midMark = Double.valueOf(JOptionPane.showInputDialog("Input double mid mark"));
break;
} catch (NumberFormatException e) {
JOptionPane.showInputDialog(null, "Error number format");
} catch (NullPointerException e) {
JOptionPane.showInputDialog(null, "Canseled. Set default value");

break;
}
}
sc.nextLine();
Student studentNew = new Student(surName, firstName, patroNymic, sex, age, midMark);
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] == null) {
groupOne[i] = studentNew;
i = groupOne.length;
}
}
}
public void groupFull (Student [] groupOne) throws ArrayIndexOutOfBoundsException {
int a = 1;
for (Student student : groupOne) {
if (student!=null) {
a=a+1;
}
}
if (a>10) {
throw new ArrayIndexOutOfBoundsException();
}
}
public Student searchStudent(Student[] groupOne) {
System.out.println("Input student's surname");
String surNameSearch = sc.nextLine();
Student studentSerch = new Student();
for (Student student : groupOne) {
if (student != null) {
if (student.getSurName().equals(surNameSearch)) {
studentSerch = student;
continue;
}
}
}
System.out.print(String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
System.out.println(studentSerch.toString());
return studentSerch;
}

public void delStudent(Student[] groupOne) {
Student studentDel = new Student();
studentDel = searchStudent(groupOne);
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] != null) {
if (groupOne[i].equals(studentDel)) {
groupOne[i] = null;
}
}
}
for (int i = 0; i < groupOne.length - 1; i++) {
if (groupOne[i] == null) {
groupOne[i] = groupOne[i + 1];
groupOne[i + 1] = null;
}
}
}

public void toString(Student[] groupOne) {
System.out.print(String.format("%-4s", "num")+String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
int size = 0;
for (Student student : groupOne) {
if (student != null) {
size = size + 1;
}
}
Student[] groupSupport = new Student[size];

for (int i = 0; i < size; i++) {
groupSupport[i] = groupOne[i];
}
Arrays.sort(groupSupport, Student.StudentSurNameComparator);
for (int i = 0; i < size; i++) {
groupOne[i] = groupSupport[i];
System.out.println(String.format("%-4s", +(i+1))+groupOne[i].toString());
}
}

@Override
public String toString() {
return "Group [groupOne=" + Arrays.toString(this.groupOne) + "]";
}

}
Class Student
package com.gmail.ua.pobut;

import java.util.Comparator;

public class Student extends Human implements Comparable<Student> {
private double midMark;

public Student(String surName, String firstName, String patroNymic, boolean sex, int age, double midMark) {
super(surName, firstName, patroNymic, sex, age);
this.midMark = midMark;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public double getMidMark() {
return this.midMark;
}
public void setMidMark(double midMark) {
this.midMark = midMark;
}
@Override
public String toString() {
return super.toString() + String.format("%-7s", this.midMark);
}
@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
return this.getSurName().compareTo(getSurName());
}
public static Comparator<Student> StudentSurNameComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
String surName1 = student1.getSurName().toUpperCase();
String surtName2 = student2.getSurName().toUpperCase();
return surName1.compareTo(surtName2);
}
};
}
Class Human
package com.gmail.ua.pobut;

public class Human {
private String surName;
private String firstName;
private String patroNymic;
private boolean sex;
private int age;

public Human(String surName, String firstName, String patroNymic, boolean sex, int age) {
super();
this.surName = surName;
this.firstName = firstName;
this.patroNymic = patroNymic;
this.sex = sex;
this.age = age;
}

public Human() {
super();
// TODO Auto-generated constructor stub
}


public String getSurName() {
return this.surName;
}

public void setSurName(String surName) {
this.surName = surName;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getPatroNymic() {
return this.patroNymic;
}

public void setPatroNymic(String patroNymic) {
this.patroNymic = patroNymic;
}

public boolean isSex() {
return this.sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

public int getAge() {
return this.age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return String.format("%-10s",this.surName)+String.format("%-12s",this.firstName)+String.format("%-14s",this.patroNymic)+String.format("%-7s",this.sex)+String.format("%-4s",this.age);
}

}



Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #46 : Марта 27, 2020, 12:21:27 am »

Mykola72

Зачем в метод передавать этот массив?
public void addStudent(Student[] groupOne) {Ведь метод имеет доступ к полю

        private Student[] groupOne = new Student[10];
Так, что для таких методов не стоило добавлять ссылку на этот массив

Так, что попробуйте реализовать все методы класса Группа, без этого параметра

Mykola72

  • Пользователи
  • *
  • Сообщений: 39
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #47 : Марта 27, 2020, 08:08:58 am »
Домашнее задание исправленное.
Класс Мейн
package com.gmail.ua.pobut;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] groupOne = new Student[10];
Group group = new Group(groupOne);
Student student1=new Student( "ivanov","ivan","ivanovych",true,20,4.8);
Student student2=new Student( "petrov","petr","petrovych",true,20,4.2);
Student student3=new Student( "sidorov","sidor","sidorovych",true,20,5.0);
Student student4=new Student( "janov","ivan","ivanovych",true,20,4.8);
Student student5=new Student( "debrov","petr","petrovych",true,20,4.2);
Student student6=new Student( "baulorov","sidor","sidorovych",true,20,5.0);
Student student7=new Student( "hrenov","ivan","ivanovych",true,20,4.8);
Student student8=new Student( "rebrov","petr","petrovych",true,20,4.2);
Student student9=new Student( "fedorov","sidor","sidorovych",true,20,5.0);
String choiceAct = "null";
Scanner sc = new Scanner(System.in);
groupOne[0]=student3;
groupOne[1]=student2;
groupOne[2]=student1;
groupOne[3]=student4;
groupOne[4]=student5;
groupOne[5]=student6;
groupOne[6]=student7;
groupOne[7]=student8;
groupOne[8]=student9;
group.toString(groupOne);
for (; !choiceAct.equals("e");) {
System.out.println("If you want to add new student press-a, search-s, delete-d, see group-p, exit-e");
choiceAct = sc.nextLine();
if (choiceAct.equals("a")) {
try {
group.groupFull();
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Can not add 11-th student. Group is full");
continue;
}
group.addStudent();
}
if (choiceAct.equals("s")) {
group.searchStudent();

}
if (choiceAct.equals("d")) {
group.delStudent();
}
if (choiceAct.equals("p")) {
group.toString(groupOne);
}
}
sc.close();
}

}
Класс Group
package com.gmail.ua.pobut;

import java.util.Arrays;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Group {
private Student[] groupOne = new Student[10];
Scanner sc = new Scanner(System.in);

public Group() {
super();
// TODO Auto-generated constructor stub
}

public Group(Student[] groupOne) {
super();
this.groupOne = groupOne;
}

public Student[] getGroup() {
return this.groupOne;
}

public void setGroup(Student[] groupOne) {
this.groupOne = groupOne;
}

public void addStudent() {

System.out.println("Input surnem");
String surName = sc.nextLine();
System.out.println("Input first name");
String firstName = sc.nextLine();
System.out.println("Input patronymic");
String patroNymic = sc.nextLine();
System.out.println("Input sex");
boolean sex = sc.nextBoolean();
System.out.println("Input age");
int age = sc.nextInt();
double midMark = 0;
for (;;) {
try {
midMark = Double.valueOf(JOptionPane.showInputDialog("Input double mid mark"));
break;
} catch (NumberFormatException e) {
JOptionPane.showInputDialog(null, "Error number format");
} catch (NullPointerException e) {
JOptionPane.showInputDialog(null, "Canseled. Set default value");

break;
}
}
sc.nextLine();
Student studentNew = new Student(surName, firstName, patroNymic, sex, age, midMark);
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] == null) {
groupOne[i] = studentNew;
i = groupOne.length;
}
}
}
public void groupFull () throws ArrayIndexOutOfBoundsException {
int a = 1;
for (Student student : groupOne) {
if (student!=null) {
a=a+1;
}
}
if (a>10) {
throw new ArrayIndexOutOfBoundsException();
}
}
public Student searchStudent() {
System.out.println("Input student's surname");
String surNameSearch = sc.nextLine();
Student studentSerch = new Student();
for (Student student : groupOne) {
if (student != null) {
if (student.getSurName().equals(surNameSearch)) {
studentSerch = student;
continue;
}
}
}
System.out.print(String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
System.out.println(studentSerch.toString());
return studentSerch;
}

public void delStudent() {
Student studentDel = new Student();
studentDel = searchStudent();
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] != null) {
if (groupOne[i].equals(studentDel)) {
groupOne[i] = null;
}
}
}
for (int i = 0; i < groupOne.length - 1; i++) {
if (groupOne[i] == null) {
groupOne[i] = groupOne[i + 1];
groupOne[i + 1] = null;
}
}
}

public void toString(Student[] groupOne) {
System.out.print(String.format("%-4s", "num")+String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
int size = 0;
for (Student student : groupOne) {
if (student != null) {
size = size + 1;
}
}
Student[] groupSupport = new Student[size];

for (int i = 0; i < size; i++) {
groupSupport[i] = groupOne[i];
}
Arrays.sort(groupSupport, Student.StudentSurNameComparator);
for (int i = 0; i < size; i++) {
groupOne[i] = groupSupport[i];
System.out.println(String.format("%-4s", +(i+1))+groupOne[i].toString());
}
}

@Override
public String toString() {
return "Group [groupOne=" + Arrays.toString(this.groupOne) + "]";
}

}

Mykola72

  • Пользователи
  • *
  • Сообщений: 39
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #48 : Марта 27, 2020, 10:37:31 pm »
Домашнее задание
Программа группа студентов расширенная с добавлением интерфейса военком.
Class Main
package com.gmail.ua.pobut;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] groupOne = new Student[10];
Group group = new Group(groupOne);
Student student1=new Student( "ivanov","ivan","ivanovych",true,21,4.8);
Student student2=new Student( "petrov","petr","petrovych",true,17,4.2);
Student student3=new Student( "sidorov","sidor","sidorovych",true,23,5.0);
Student student4=new Student( "janov","ivan","ivanovych",true,24,4.8);
Student student5=new Student( "debrova","irina","petrovych",false,16,4.2);
Student student6=new Student( "baulorov","sidor","sidorovych",true,32,5.0);
Student student7=new Student( "hrenova","gadya","ivanovna",false,22,4.8);
Student student8=new Student( "rebrov","petr","petrovych",true,28,4.2);
Student student9=new Student( "fedorov","sidor","sidorovych",true,19,5.0);
String choiceAct = "null";
Scanner sc = new Scanner(System.in);
groupOne[0]=student3;
groupOne[1]=student2;
groupOne[2]=student1;
groupOne[3]=student4;
groupOne[4]=student5;
groupOne[5]=student6;
groupOne[6]=student7;
groupOne[7]=student8;
groupOne[8]=student9;
group.toString(groupOne);
for (; !choiceAct.equals("e");) {
System.out.println("If you want to add new student press-a, search-s, delete-d, see group-p, voenkom - v, exit-e");
choiceAct = sc.nextLine();
if (choiceAct.equals("a")) {
try {
group.groupFull();
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Can not add 11-th student. Group is full");
continue;
}
group.addStudent();
}
if (choiceAct.equals("s")) {
group.searchStudent();

}
if (choiceAct.equals("d")) {
group.delStudent();
}
if (choiceAct.equals("p")) {
group.toString(groupOne);
}
if (choiceAct.equals("v")) {
Student [] groupVoencom = group.vperedSpesney();
group.toString(groupVoencom);
}
}
sc.close();
}

}
Interface Voencom
package com.gmail.ua.pobut;

public interface Voencom {
public Student [] vperedSpesney();
}
Class Group
package com.gmail.ua.pobut;

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Group implements Voencom {
private Student[] groupOne = new Student[10];
Scanner sc = new Scanner(System.in);

public Group() {
super();
// TODO Auto-generated constructor stub
}

public Group(Student[] groupOne) {
super();
this.groupOne = groupOne;
}

public Student[] getGroup() {
return this.groupOne;
}

public void setGroup(Student[] groupOne) {
this.groupOne = groupOne;
}

public void addStudent() {
Student studentNew = new Student();
String surName = null;
String firstName = null;
String patroNymic = null;
boolean sex = true;
int age = 0;
double midMark = 0;
for (;;) {
try {
System.out.println("Input surnem");
surName = sc.nextLine();
System.out.println("Input first name");
firstName = sc.nextLine();
System.out.println("Input patronymic");
patroNymic = sc.nextLine();
System.out.println("Input sex");
sex = sc.nextBoolean();
System.out.println("Input age");
age = sc.nextInt();
midMark = Double.valueOf(JOptionPane.showInputDialog("Input double mid mark"));
break;
} catch (NumberFormatException e) {
JOptionPane.showInputDialog(null, "Error number format");
} catch (NullPointerException e) {
JOptionPane.showInputDialog(null, "Canseled. Set default value");
} catch (InputMismatchException e) {
System.out.println("Invalid data, try again");
continue;

} finally {
sc.nextLine();
}
studentNew = new Student(surName, firstName, patroNymic, sex, age, midMark);
}
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] == null) {
groupOne[i] = studentNew;
i = groupOne.length;
}
}
}

public void groupFull() throws ArrayIndexOutOfBoundsException {
int a = 1;
for (Student student : groupOne) {
if (student != null) {
a = a + 1;
}
}
if (a > 10) {
throw new ArrayIndexOutOfBoundsException();
}
}

public Student searchStudent() {
System.out.println("Input student's surname");
String surNameSearch = sc.nextLine();
Student studentSerch = new Student();
for (Student student : groupOne) {
if (student != null) {
if (student.getSurName().equals(surNameSearch)) {
studentSerch = student;
continue;
}
}
}
System.out.print(String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
System.out.println(studentSerch.toString());
return studentSerch;
}

public void delStudent() {
Student studentDel = new Student();
studentDel = searchStudent();
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] != null) {
if (groupOne[i].equals(studentDel)) {
groupOne[i] = null;
}
}
}
for (int i = 0; i < groupOne.length - 1; i++) {
if (groupOne[i] == null) {
groupOne[i] = groupOne[i + 1];
groupOne[i + 1] = null;
}
}
}

@Override
public Student[] vperedSpesney() {
int sizeVoen = 0;
for(int i=0;i<groupOne.length; i++) {
if (groupOne[i]==null) {
continue;
}
if (groupOne[i].getAge()>=18& groupOne[i].isSex()) {
sizeVoen=sizeVoen+1;
}
}
Student [] groupVoencom = new Student [sizeVoen];
int j=0;
for(int i=0;i<groupOne.length; i++) {
if (groupOne[i]==null) {
continue;
}
if (groupOne[i].getAge()>=18& groupOne[i].isSex()) {
groupVoencom[j]=groupOne[i];
j=j+1;
}
}

return groupVoencom;
}

public void toString(Student[] groupOne) {

int size = 0;
for (Student student : groupOne) {
if (student != null) {
size = size + 1;
}
}
Student[] groupSupport = new Student[size];

for (int i = 0; i < size; i++) {
groupSupport[i] = groupOne[i];
}
System.out.println("What kind of sorting name-n, age-a, mid mark - m");
String kindSort = sc.nextLine();
System.out.println("If you want order of sorting  Z->A input - z");
String orderSort = sc.nextLine();
if (kindSort.equals("n") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentSurNameZaComparator);
}
if (kindSort.equals("n") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentSurNameAzComparator);
}

if (kindSort.equals("a") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentAgeZaComparator);
}
if (kindSort.equals("a") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentAgeAzComparator);
}

if (kindSort.equals("m") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentMidMarkZaComparator);
}
if (kindSort.equals("m") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentMidMarkAzComparator);
}

System.out.print(String.format("%-4s", "num") + String.format("%-10s", "surname")
+ String.format("%-12s", "first name") + String.format("%-14s", "patronymic")
+ String.format("%-7s", "sex") + String.format("%-4s", "age") + String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
for (int i = 0; i < size; i++) {
groupOne[i] = groupSupport[i];
System.out.println(String.format("%-4s", +(i + 1)) + groupOne[i].toString());
}
}

@Override
public String toString() {
return "Group [groupOne=" + Arrays.toString(this.groupOne) + "]";
}

}
Class Student
package com.gmail.ua.pobut;

import java.util.Comparator;

public class Student extends Human implements Comparable<Student> {
private double midMark;

public Student(String surName, String firstName, String patroNymic, boolean sex, int age, double midMark) {
super(surName, firstName, patroNymic, sex, age);
this.midMark = midMark;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public double getMidMark() {
return this.midMark;
}
public void setMidMark(double midMark) {
this.midMark = midMark;
}
@Override
public String toString() {
return super.toString() + String.format("%-7s", this.midMark);
}
@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
return this.getSurName().compareTo(getSurName());
}
public static Comparator<Student> StudentSurNameAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
String surName1 = student1.getSurName().toUpperCase();
String surName2 = student2.getSurName().toUpperCase();
return surName1.compareTo(surName2);
}
};
public static Comparator<Student> StudentSurNameZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
String surName1 = student1.getSurName().toUpperCase();
String surName2 = student2.getSurName().toUpperCase();
return surName2.compareTo(surName1);
}
};

public static Comparator<Student> StudentAgeAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
return student1.getAge()-student2.getAge();

}
};
public static Comparator<Student> StudentAgeZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
return student2.getAge()-student1.getAge();
}
};
public static Comparator<Student> StudentMidMarkAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
int st1=(int)(student1.getMidMark()*10);
int st2=(int)(student2.getMidMark()*10);
System.out.println(st1+" "+st2);
return st1-st2;

}
};
public static Comparator<Student> StudentMidMarkZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
int st1=(int)student1.getMidMark()*10;
int st2=(int)student2.getMidMark()*10;
return st2-st1;
}
};
}
Class Human
package com.gmail.ua.pobut;

public class Human {
private String surName;
private String firstName;
private String patroNymic;
private boolean sex;
private int age;

public Human(String surName, String firstName, String patroNymic, boolean sex, int age) {
super();
this.surName = surName;
this.firstName = firstName;
this.patroNymic = patroNymic;
this.sex = sex;
this.age = age;
}

public Human() {
super();
// TODO Auto-generated constructor stub
}


public String getSurName() {
return this.surName;
}

public void setSurName(String surName) {
this.surName = surName;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getPatroNymic() {
return this.patroNymic;
}

public void setPatroNymic(String patroNymic) {
this.patroNymic = patroNymic;
}

public boolean isSex() {
return this.sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

public int getAge() {
return this.age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return String.format("%-10s",this.surName)+String.format("%-12s",this.firstName)+String.format("%-14s",this.patroNymic)+String.format("%-7s",this.sex)+String.format("%-4s",this.age);
}

}

Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #49 : Марта 27, 2020, 11:02:02 pm »
Mykola72
И опять такой метод:
        public void toString(Student[] groupOne) {
Ну зачем тут массив Студентов в качестве параметра? И этот метод должен возвращать строку, а у вас он вообще ничего не возвращает.
В остальном решение верное



Mykola72

  • Пользователи
  • *
  • Сообщений: 39
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #50 : Марта 28, 2020, 03:07:55 pm »
Исправленное домашнее задание
Class Main
package com.gmail.ua.pobut;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Student[] groupOne = new Student[10];
Group group = new Group(groupOne);
Student student1=new Student( "ivanov","ivan","ivanovych",true,21,4.8);
Student student2=new Student( "petrov","petr","petrovych",true,17,4.2);
Student student3=new Student( "sidorov","sidor","sidorovych",true,23,5.0);
Student student4=new Student( "janov","ivan","ivanovych",true,24,4.8);
Student student5=new Student( "debrova","irina","petrovych",false,16,4.2);
Student student6=new Student( "baulorov","sidor","sidorovych",true,32,5.0);
Student student7=new Student( "hrenova","gadya","ivanovna",false,22,4.8);
Student student8=new Student( "rebrov","petr","petrovych",true,28,4.2);
Student student9=new Student( "fedorov","sidor","sidorovych",true,19,5.0);
String choiceAct = "null";
Scanner sc = new Scanner(System.in);
groupOne[0]=student3;
groupOne[1]=student2;
groupOne[2]=student1;
groupOne[3]=student4;
groupOne[4]=student5;
groupOne[5]=student6;
groupOne[6]=student7;
groupOne[7]=student8;
groupOne[8]=student9;
group.toString();
for (; !choiceAct.equals("e");) {
System.out.println("If you want to add new student press-a, search-s, delete-d, see group-p, sort group-sg, voenkom - v, exit-e");
choiceAct = sc.nextLine();
if (choiceAct.equals("a")) {
try {
group.groupFull();
} catch (ArrayIndexOutOfBoundsException e){
System.out.println("Can not add 11-th student. Group is full");
continue;
}
group.addStudent();
group.sortStudent();
group.toString();
}
if (choiceAct.equals("s")) {
group.searchStudent();
}
if (choiceAct.equals("sg")) {
group.sortStudent();
group.toString();
}
if (choiceAct.equals("d")) {
group.delStudent();
}
if (choiceAct.equals("p")) {
group.sortStudent();
group.toString();
}
if (choiceAct.equals("v")) {
group.sortStudent();
group.vperedSpesney();
}
}
sc.close();
}

}
Interface Voencom
package com.gmail.ua.pobut;

public interface Voencom {
public void vperedSpesney();
}
Class Group
package com.gmail.ua.pobut;

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Group implements Voencom {
private Student[] groupOne = new Student[10];
Scanner sc = new Scanner(System.in);

public Group() {
super();
}

public Group(Student[] groupOne) {
super();
this.groupOne = groupOne;
}

public Student[] getGroup() {
return this.groupOne;
}

public void setGroup(Student[] groupOne) {
this.groupOne = groupOne;
}

public void addStudent() {
Student studentNew = new Student();
String surName = null;
String firstName = null;
String patroNymic = null;
boolean sex = true;
int age = 0;
double midMark = 0;
for (;;) {
try {
System.out.println("Input surnem");
surName = sc.nextLine();
System.out.println("Input first name");
firstName = sc.nextLine();
System.out.println("Input patronymic");
patroNymic = sc.nextLine();
System.out.println("Input sex");
sex = sc.nextBoolean();
System.out.println("Input age");
age = sc.nextInt();
midMark = Double.valueOf(JOptionPane.showInputDialog("Input double mid mark"));
studentNew = new Student(surName, firstName, patroNymic, sex, age, midMark);
System.out.print(String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex")
+ String.format("%-4s", "age") + String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
System.out.println(studentNew.toString());
break;
} catch (NumberFormatException e) {
JOptionPane.showInputDialog(null, "Error number format");
} catch (NullPointerException e) {
JOptionPane.showInputDialog(null, "Canseled. Set default value");
} catch (InputMismatchException e) {
System.out.println("Invalid data, try again");
continue;
} finally {
sc.nextLine();
}
}
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] == null) {
groupOne[i] = studentNew;
i = groupOne.length;
}
}
}

public void groupFull() throws ArrayIndexOutOfBoundsException {
int a = 1;
for (Student student : groupOne) {
if (student != null) {
a = a + 1;
}
}
if (a > 10) {
throw new ArrayIndexOutOfBoundsException();
}
}

public Student searchStudent() {
System.out.println("Input student's surname");
String surNameSearch = sc.nextLine();
Student studentSerch = new Student();
int b= 0;
for (Student student : groupOne) {
if (student != null) {
if (student.getSurName().equals(surNameSearch)) {
studentSerch = student;
b=b+1;
break;
}
}
}
if (b>0) {
System.out.print(String.format("%-10s", "surname") + String.format("%-12s", "first name")
+ String.format("%-14s", "patronymic") + String.format("%-7s", "sex") + String.format("%-4s", "age")
+ String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
System.out.println(studentSerch.toString());
} else {
System.out.println("Invalid surname");
}
return studentSerch;
}

public void delStudent() {
Student studentDel = new Student();
studentDel = searchStudent();
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] != null) {
if (groupOne[i].equals(studentDel)) {
groupOne[i] = null;
}
}
}
for (int i = 0; i < groupOne.length - 1; i++) {
if (groupOne[i] == null) {
groupOne[i] = groupOne[i + 1];
groupOne[i + 1] = null;
}
}
}

@Override
public void vperedSpesney() {

System.out.print(String.format("%-4s", "num") + String.format("%-10s", "surname")
+ String.format("%-12s", "first name") + String.format("%-14s", "patronymic")
+ String.format("%-7s", "sex") + String.format("%-4s", "age") + String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] == null) {
continue;
}
if (groupOne[i].getAge() >= 18 & groupOne[i].isSex()) {
System.out.println(String.format("%-4s", +(i + 1)) + groupOne[i].toString());
}
}
}

public void sortStudent() {
for (int i = 0; i < groupOne.length - 1; i++) {
if (groupOne[i] == null) {
groupOne[i] = groupOne[i + 1];
groupOne[i + 1] = null;
}
}
int size = 0;
for (Student student : groupOne) {
if (student != null) {
size = size + 1;
}
}
Student[] groupSupport = new Student[size];

for (int i = 0; i < size; i++) {
groupSupport[i] = groupOne[i];
}
System.out.println("What kind of sorting name-n, age-a, mid mark - m");
String kindSort = sc.nextLine();
System.out.println("If you want order of sorting  Z->A input - z");
String orderSort = sc.nextLine();
if (kindSort.equals("n") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentSurNameZaComparator);
}
if (kindSort.equals("n") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentSurNameAzComparator);
}

if (kindSort.equals("a") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentAgeZaComparator);
}
if (kindSort.equals("a") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentAgeAzComparator);
}

if (kindSort.equals("m") & orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentMidMarkZaComparator);
}
if (kindSort.equals("m") & !orderSort.equals("z")) {
Arrays.sort(groupSupport, Student.StudentMidMarkAzComparator);
}
for (int i = 0; i < size; i++) {
groupOne[i] = groupSupport[i];
}
}

public String toString() {

System.out.print(String.format("%-4s", "num") + String.format("%-10s", "surname")
+ String.format("%-12s", "first name") + String.format("%-14s", "patronymic")
+ String.format("%-7s", "sex") + String.format("%-4s", "age") + String.format("%-7s", "midMark"));
System.out.println();
System.out.print("-------------------------------------------------------");
System.out.println();
for (int i = 0; i < groupOne.length; i++) {
if (groupOne[i] != null) {
System.out.println(String.format("%-4s", +(i + 1)) + groupOne[i].toString());
}
}
return "Group [groupOne=" + Arrays.toString(this.groupOne) + "]";
}

}
Class Student
package com.gmail.ua.pobut;

import java.util.Comparator;

public class Student extends Human implements Comparable<Student> {
private double midMark;

public Student(String surName, String firstName, String patroNymic, boolean sex, int age, double midMark) {
super(surName, firstName, patroNymic, sex, age);
this.midMark = midMark;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public double getMidMark() {
return this.midMark;
}
public void setMidMark(double midMark) {
this.midMark = midMark;
}
@Override
public String toString() {
return super.toString() + String.format("%-7s", this.midMark);
}
@Override
public int compareTo(Student student) {
// TODO Auto-generated method stub
return this.getSurName().compareTo(getSurName());
}
public static Comparator<Student> StudentSurNameAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
String surName1 = student1.getSurName().toUpperCase();
String surName2 = student2.getSurName().toUpperCase();
return surName1.compareTo(surName2);
}
};
public static Comparator<Student> StudentSurNameZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
String surName1 = student1.getSurName().toUpperCase();
String surName2 = student2.getSurName().toUpperCase();
return surName2.compareTo(surName1);
}
};

public static Comparator<Student> StudentAgeAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
return student1.getAge()-student2.getAge();

}
};
public static Comparator<Student> StudentAgeZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
return student2.getAge()-student1.getAge();
}
};
public static Comparator<Student> StudentMidMarkAzComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
int st1=(int)(student1.getMidMark()*10);
int st2=(int)(student2.getMidMark()*10);
return st1-st2;

}
};
public static Comparator<Student> StudentMidMarkZaComparator = new Comparator<Student>() {
public int compare(Student student1, Student student2) {
int st1=(int)student1.getMidMark()*10;
int st2=(int)student2.getMidMark()*10;
return st2-st1;
}
};
}
Class Human
package com.gmail.ua.pobut;

public class Human {
private String surName;
private String firstName;
private String patroNymic;
private boolean sex;
private int age;

public Human(String surName, String firstName, String patroNymic, boolean sex, int age) {
super();
this.surName = surName;
this.firstName = firstName;
this.patroNymic = patroNymic;
this.sex = sex;
this.age = age;
}

public Human() {
super();
// TODO Auto-generated constructor stub
}


public String getSurName() {
return this.surName;
}

public void setSurName(String surName) {
this.surName = surName;
}

public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getPatroNymic() {
return this.patroNymic;
}

public void setPatroNymic(String patroNymic) {
this.patroNymic = patroNymic;
}

public boolean isSex() {
return this.sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}

public int getAge() {
return this.age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public String toString() {
return String.format("%-10s",this.surName)+String.format("%-12s",this.firstName)+String.format("%-14s",this.patroNymic)+String.format("%-7s",this.sex)+String.format("%-4s",this.age);
}

}

Alexander Ts

  • Преподаватели
  • Пользователи
  • *
  • Сообщений: 13429
    • Просмотр профиля
    • E-mail
Re: Java on-line_pobut
« Ответ #51 : Марта 28, 2020, 11:49:11 pm »
Mykola72
Так решение уже более работоспособно. Хотя не стоит особенно сильно увлекаться консольным выводом. Java приложения работаю на стороне сервера, и этот вывод никто не увидит. Так, что лучше возвращать хорошо оформленную строку (ее можно отправить по сети и т. д.)