Національний технічний університет України
«Київський політехнічний інститут ім. Ігоря Сікорського»
Кафедра цифрових технологій в енергетиці
ЗВІТ з виконання лабораторної роботи №4з дисципліни «Програмування на мові Java»
«РОБОТА З КОЛЕКЦІЯМИ»
Варіант 13
Завдання 2
Код і скріншот
public class Task2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HashMap<String, Abonent> network = new HashMap<>(); network.put("+380973841234", new Abonent("Daniil", "Fundament", "Vinnytsia")); network.put("+380933843534", new Abonent("Nick", "Pepsi", "Kiyv")); network.put("+380912844344", new Abonent("George", "Camel", "Lviv")); network.put("+380990976234", new Abonent("Woodie", "Bond", "Odessa")); network.put("+380964368894", new Abonent("Rick", "Rolton", "Chernigiv")); System.out.println("Enter user phone to find it: "); String number = scan.nextLine(); Abonent currentUser = findAbonent(network, number); if(currentUser != null) { System.out.println(currentUser); } else { System.out.println("invalid user!"); } } public static Abonent findAbonent(HashMap<String, Abonent> network, String number) { if (network.containsKey(number)) { return network.get(number); } else { return null; } }}class Abonent { public String name; public String surname; public String address; public Abonent(String name, String surname, String address) { this.name = name; this.surname = surname; this.address = address; } @Override public String toString() { return surname + " " + name + ", address: " + address; }}
Робота коду:
/
Тести:
@org.junit.jupiter.api.Testvoid findAbonent1() { HashMap<String, Abonent> network = new HashMap<>(); network.put("+380973841234", new Abonent("Daniil", "Fundament", "Vinnytsia")); network.put("+380933843534", new Abonent("Nick", "Pepsi", "Kiyv")); network.put("+380912844344", new Abonent("George", "Camel", "Lviv")); network.put("+380990976234", new Abonent("Woodie", "Bond", "Odessa")); network.put("+380964368894", new Abonent("Rick", "Rolton", "Chernigiv")); Abonent correct = network.get("+380973841234"); Abonent result = Task2.findAbonent(network, "+380973841234"); assertEquals(correct, result);}@org.junit.jupiter.api.Testvoid findAbonent2() { HashMap<String, Abonent> network = new HashMap<>(); network.put("+380973841234", new Abonent("Daniil", "Fundament", "Vinnytsia")); network.put("+380933843534", new Abonent("Nick", "Pepsi", "Kiyv")); network.put("+380912844344", new Abonent("George", "Camel", "Lviv")); network.put("+380990976234", new Abonent("Woodie", "Bond", "Odessa")); network.put("+380964368894", new Abonent("Rick", "Rolton", "Chernigiv")); Abonent correct = null; Abonent result = Task2.findAbonent(network, "380973841234"); assertEquals(correct, result);}@org.junit.jupiter.api.Testvoid findAbonent3() { HashMap<String, Abonent> network = new HashMap<>(); network.put("+380973841234", new Abonent("Daniil", "Fundament", "Vinnytsia")); network.put("+380933843534", new Abonent("Nick", "Pepsi", "Kiyv")); network.put("+380912844344", new Abonent("George", "Camel", "Lviv")); network.put("+380990976234", new Abonent("Woodie", "Bond", "Odessa")); network.put("+380964368894", new Abonent("Rick", "Rolton", "Chernigiv")); Abonent correct = null; Abonent result = Task2.findAbonent(network, "+3845345"); assertEquals(correct, result);}@org.junit.jupiter.api.Testvoid findAbonent4() { HashMap<String, Abonent> network = new HashMap<>(); network.put("+380973841234", new Abonent("Daniil", "Fundament", "Vinnytsia")); network.put("+380933843534", new Abonent("Nick", "Pepsi", "Kiyv")); network.put("+380912844344", new Abonent("George", "Camel", "Lviv")); network.put("+380990976234", new Abonent("Woodie", "Bond", "Odessa")); network.put("+380964368894", new Abonent("Rick", "Rolton", "Chernigiv")); Abonent correct = null; Abonent result = Task2.findAbonent(network, ""); assertEquals(correct, result);}
Завдання 8
package org.example;// Створити застосунок для перегляду списку черговиків і зміни пріоритету черговиків в черзі.// Початкові записи в списку (5 записів) створюються в програмному коді і є об'єктом класу LinkedList.// Запис списку є об'єктом QueuePerson, що містить поля прізвища, імені та по батькові черговика (типу String),// поле типу AddressValue для адреси черговика і пріоритет черговика (типу int).// У свою чергу, поле типу AddressValue містить три поля типу String: найменування міста,// найменування вулиці та номер будинку, а також поле типу int - номер квартири// (якщо номер квартири дорівнює 0, будинок, в якому проживає черговик, не має квартир).// Записи в черзі упорядковуються відповідно до пріоритету, і черговик додається останнім в чергу свого пріоритету.import java.util.LinkedList;import java.util.Scanner;public class Task8 { public static void main(String[] args) { LinkedList<QueuePerson> queue = new LinkedList<>(); queue.add(new QueuePerson("Petro", "Ivanov", "Sergiyovych", new AddressValue("Kiyv", "Maydan Nezalejnosti", 104, 4), 1)); queue.add(new QueuePerson("Evgeniy", "Klopotenko", "Batkovych", new AddressValue("Kiyv", "Dniprovska", 145, 56), 2)); queue.add(new QueuePerson("Petro", "Poroshenko", "Roshenovich", new AddressValue("Vinnytsia", "Ovodova", 4, 1), 3)); queue.add(new QueuePerson("Volodymyr", "Petrov", "Ivanovych", new AddressValue("Kiyv", "Saksaganskogo", 57, 48), 4)); queue.add(new QueuePerson("Mykola", "Voytiuk", "Dmitrovych", new AddressValue("Vinnytsia", "Politechnichna", 78, 78), 5)); System.out.println("queu persons list:"); for (QueuePerson queuePerson : queue) { System.out.println(queuePerson); } Scanner scanner = new Scanner(System.in); System.out.println("enter surname to change priority:"); String surname = scanner.nextLine(); System.out.println("enter new priority:"); int newPriority = scanner.nextInt(); try { QueuePerson changedPriority = changePriority(queue, surname, newPriority); } catch (Exception e) { throw new RuntimeException(e); } System.out.println("queuePersons list after changes:"); for (QueuePerson queuePerson1 : queue) { System.out.println(queuePerson1); } } public static QueuePerson getPersonBySurname(LinkedList<QueuePerson> queue, String surname) { QueuePerson queuePerson = null; for (QueuePerson person : queue) { if (person.surname.equals(surname)) { queuePerson = person; break; } } return queuePerson; } public static QueuePerson getPersonByPriority(LinkedList<QueuePerson> queue, int priority) { QueuePerson queuePerson = null; for (QueuePerson person : queue) { if (person.priority == priority) { queuePerson = person; break; } } return queuePerson; } public static QueuePerson changePriority(LinkedList<QueuePerson> queue, String surname, int newPriority) throws Exception { QueuePerson queuePerson = getPersonBySurname(queue, surname); if (queuePerson == null) { System.out.println("Invalid surname"); throw new Exception("Invalid surname"); } QueuePerson oldPriorityPerson = getPersonByPriority(queue, newPriority); if (oldPriorityPerson == null) { System.out.println("Invalid surname"); throw new Exception("Invalid surname"); } oldPriorityPerson.priority = queuePerson.priority; queuePerson.priority = newPriority; queue.sort((a, b) -> a.priority - b.priority); return queuePerson; }}class AddressValue { public String city; public String street; public int houseNumber; public int flatNumber; public AddressValue(String city, String street, int houseNumber, int flatNumber) { this.city = city; this.street = street; this.houseNumber = houseNumber; this.flatNumber = flatNumber; } @Override public String toString() { if (flatNumber == 0) { return city + ", " + street + ", " + houseNumber; } else { return city + ", " + street + ", " + houseNumber + ", Flat: " + flatNumber; } }}class QueuePerson { public String name; public String surname; public String lastname; public AddressValue address; public int priority; public QueuePerson(String name, String surname, String lastname, AddressValue address, int priority) { this.name = name; this.surname = surname; this.lastname = lastname; this.address = address; this.priority = priority; } @Override public String toString() { return "Name: " + surname + " " + name + " " + lastname + ", Address: " + address + ", Priority: " + priority; }}
Робота коду:/
Тести:
@Testvoid getPersonBySurname1() { LinkedList<QueuePerson> queue = new LinkedList<>(); queue.add(new QueuePerson("Petro", "Ivanov", "Sergiyovych", new AddressValue("Kiyv", "Maydan Nezalejnosti", 104, 4), 1)); queue.add(new QueuePerson("Evgeniy", "Klopotenko", "Batkovych", new AddressValue("Kiyv", "Dniprovska", 145, 56), 2)); queue.add(new QueuePerson("Petro", "Poroshenko", "Roshenovich", new AddressValue("Vinnytsia", "Ovodova", 4, 1), 3)); queue.add(new QueuePerson("Volodymyr", "Petrov", "Ivanovych", new AddressValue("Kiyv", "Saksaganskogo", 57, 48), 4)); queue.add(new QueuePerson("Mykola", "Voytiuk", "Dmitrovych", new AddressValue("Vinnytsia", "Politechnichna", 78, 78), 5)); String surname = "Klopotenko"; QueuePerson correct = queue.get(1); QueuePerson result = Task8.getPersonBySurname(queue, surname); assertEquals(correct, result);}@Testvoid getPersonBySurname2() { LinkedList<QueuePerson> queue = new LinkedList<>(); queue.add(new QueuePerson("Petro", "Ivanov", "Sergiyovych", new AddressValue("Kiyv", "Maydan Nezalejnosti", 104, 4), 1)); queue.add(new QueuePerson("Evgeniy", "Klopotenko", "Batkovych", new AddressValue("Kiyv", "Dniprovska", 145, 56), 2)); queue.add(new QueuePerson("Petro", "Poroshenko", "Roshenovich", new AddressValue("Vinnytsia", "Ovodova", 4, 1), 3)); queue.add(new QueuePerson("Volodymyr", "Petrov", "Ivanovych", new AddressValue("Kiyv", "Saksaganskogo", 57, 48), 4)); queue.add(new QueuePerson("Mykola", "Voytiuk", "Dmitrovych", new AddressValue("Vinnytsia", "Politechnichna", 78, 78), 5)); String surname = ""; QueuePerson correct = null; QueuePerson result = Task8.getPersonBySurname(queue, surname); assertEquals(correct, result);}@Testvoid getPersonByPriority1() { LinkedList<QueuePerson> queue = new LinkedList<>(); queue.add(new QueuePerson("Petro", "Ivanov", "Sergiyovych", new AddressValue("Kiyv", "Maydan Nezalejnosti", 104, 4), 1)); queue.add(new QueuePerson("Evgeniy", "Klopotenko", "Batkovych", new AddressValue("Kiyv", "Dniprovska", 145, 56), 2)); queue.add(new QueuePerson("Petro", "Poroshenko", "Roshenovich", new AddressValue("Vinnytsia", "Ovodova", 4, 1), 3)); queue.add(new QueuePerson("Volodymyr", "Petrov", "Ivanovych", new AddressValue("Kiyv", "Saksaganskogo", 57, 48), 4)); queue.add(new QueuePerson("Mykola", "Voytiuk", "Dmitrovych", new AddressValue("Vinnytsia", "Politechnichna", 78, 78), 5)); int priority = 3; QueuePerson correct = queue.get(2); QueuePerson result = Task8.getPersonByPriority(queue, priority); assertEquals(correct, result);}@Testvoid getPersonByPriority2() { LinkedList<QueuePerson> queue = new LinkedList<>(); queue.add(new QueuePerson("Petro", "Ivanov", "Sergiyovych", new AddressValue("Kiyv", "Maydan Nezalejnosti", 104, 4), 1)); queue.add(new QueuePerson("Evgeniy", "Klopotenko", "Batkovych", new AddressValue("Kiyv", "Dniprovska", 145, 56), 2)); queue.add(new QueuePerson("Petro", "Poroshenko", "Roshenovich", new AddressValue("Vinnytsia", "Ovodova", 4, 1), 3)); queue.add(new QueuePerson("Volodymyr", "Petrov", "Ivanovych", new AddressValue("Kiyv", "Saksaganskogo", 57, 48), 4)); queue.add(new QueuePerson("Mykola", "Voytiuk", "Dmitrovych", new AddressValue("Vinnytsia", "Politechnichna", 78, 78), 5)); int priority = 6; QueuePerson correct = null; QueuePerson result = Task8.getPersonByPriority(queue, priority); assertEquals(correct, result);}
Завдання 14
Код і скріншот
public class Task14 { public static void main(String[] args) { Map<Integer, Member> members = new HashMap<>(); members.put(1, new Member("Ivan", "Petrov", "Kiyv")); members.put(3, new Member("Petro", "Ivanov", "Lviv")); members.put(2, new Member("Mykola", "Azirov", "Odessa")); members.put(4, new Member("Oleg", "Balashov", "Kharkiv")); members.put(5, new Member("Andriy", "Lampreht", "Dnipro")); System.out.println("List of members"); for (Map.Entry<Integer, Member> entry : members.entrySet()) { System.out.println("Number: " + entry.getKey() + " " + entry.getValue()); } System.out.println(); TreeMap<Member, Integer> sortedMembers = sortBySurname(members); System.out.println("List of members after sort by surname"); for (Map.Entry<Member, Integer> entry : sortedMembers.entrySet()) { System.out.println("Number: " + entry.getValue() + ", " + entry.getKey()); } System.out.println(); Scanner scanner = new Scanner(System.in); System.out.println("Please enter member number to delete:"); int number = scanner.nextInt(); Member removedMember = deleteMember(members, number); if (removedMember == null) { System.exit(0); } System.out.println(); System.out.println("List after member delete"); for (Map.Entry<Integer, Member> entry : members.entrySet()) { System.out.println("Number: " + entry.getKey() + " " + entry.getValue()); } } public static TreeMap<Member, Integer> sortBySurname(Map<Integer, Member> members) { TreeMap<Member, Integer> sortedMembers = new TreeMap<>(new SurnameComparator()); for (Map.Entry<Integer, Member> entry : members.entrySet()) { sortedMembers.put(entry.getValue(), entry.getKey()); } return sortedMembers; } public static Member deleteMember (Map<Integer, Member> members, int number) { Member removedMember = members.remove(number); return removedMember; }}class SurnameComparator implements Comparator<Member> { @Override public int compare(Member ab1, Member ab2) { return ab1.surname.compareTo(ab2.surname); }}class Member { public String name; public String surname; public String address; public Member(String name, String surname, String address) { this.name = name; this.surname = surname; this.address = address; } @Override public String toString() { return surname + " " + name + ", Address: " + address; }}
робота коду:
/
Тести:
@Testvoid deleteMember1() { Map<Integer, Member> members = new HashMap<>(); members.put(1, new Member("Ivan", "Petrov", "Kiyv")); members.put(3, new Member("Petro", "Ivanov", "Lviv")); members.put(2, new Member("Mykola", "Azirov", "Odessa")); members.put(4, new Member("Oleg", "Balashov", "Kharkiv")); members.put(5, new Member("Andriy", "Lampreht", "Dnipro")); int number = 3; Member correct = members.get(number); Member result = Task14.deleteMember(members, number); assertEquals(correct, result);}@Testvoid deleteMember2() { Map<Integer, Member> members = new HashMap<>(); members.put(1, new Member("Ivan", "Petrov", "Kiyv")); members.put(3, new Member("Petro", "Ivanov", "Lviv")); members.put(2, new Member("Mykola", "Azirov", "Odessa")); members.put(4, new Member("Oleg", "Balashov", "Kharkiv")); members.put(5, new Member("Andriy", "Lampreht", "Dnipro")); int number = 6; Member correct = null; Member result = Task14.deleteMember(members, number); assertEquals(correct, result);}
Завдання 28
Код і скріншот
public class Task28 { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<>(); students.add(new Student("Ivanov", "Ivan", "Petrovych", "TI-21", new BirthDate(2000, 1, 1), 65.5f)); students.add(new Student("Gaydash", "Oleg", "Valeriyovych", "TV-34", new BirthDate(2001, 2, 2), 78.6f)); students.add(new Student("Boyko", "Mykhaylo", "Mykolayovych", "TR-12", new BirthDate(2002, 3, 3), 94.3f)); students.add(new Student("Danilyuk", "Max", "Stepanovych", "RT-14", new BirthDate(2003, 4, 4), 86.2f)); students.add(new Student("Sharmalyuk", "Serhiy", "Volodymyrovych", "MM-15", new BirthDate(2004, 5, 5), 72.1f)); System.out.println("Students list:"); printStudents(students); System.out.println(); System.out.println("Adding a student:"); Scanner scanner = new Scanner(System.in); System.out.println("Enter surname:"); String surname = scanner.nextLine(); System.out.println("Enter name:"); String name = scanner.nextLine(); System.out.println("Enter lastname:"); String lastname = scanner.nextLine(); System.out.println("Enter group:"); String group = scanner.nextLine(); System.out.println("Enter year:"); int year = scanner.nextInt(); System.out.println("Enter month:"); int month = scanner.nextInt(); System.out.println("Enter day:"); int day = scanner.nextInt(); System.out.println("Enter avg drage:"); float averageGrade = scanner.nextFloat(); Student newStudent = addStudent(students, name, surname, lastname, group, year, month, day, averageGrade); System.out.println(); System.out.println("Students list:"); printStudents(students); System.out.println(); System.out.println("Enter index of student to delete:"); int index = scanner.nextInt(); Student removedStudent = deleteStudent(students, index); System.out.println(); System.out.println("Students list:"); printStudents(students); } public static void printStudents(ArrayList<Student> students) { for (Student student : students) { System.out.println(student); } } public static Student addStudent(ArrayList<Student> students, String name, String surname, String lastName, String group, int year, int month, int day, float averageGrade) { Student newStudent = new Student(surname, name, lastName, group, new BirthDate(year, month, day), averageGrade); students.add(newStudent); return newStudent; } public static Student deleteStudent(ArrayList<Student> students, int index) { Student student = students.get(index); students.remove(index); return student; }}class BirthDate { public int year; public int month; public int day; public BirthDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } @Override public String toString() { return String.format("%d.%d.%d", year, month, day); }}class Student { public String surname; public String name; public String lastName; public String group; public BirthDate birthDate; public float averageGrade; public Student(String surname, String name, String lastName, String group, BirthDate birthDate, float averageGrade) { this.surname = surname; this.name = name; this.lastName = lastName; this.group = group; this.birthDate = birthDate; this.averageGrade = averageGrade; } @Override public String toString() { return surname + " " + name + " " + lastName + " Group: " + group + " Date: " + birthDate + " Grade: " + averageGrade; }}
Робота коду:
/
/
Тести:
@Testvoid addStudent() { ArrayList<Student> students = new ArrayList<>(); String surname = "Fundament"; String name = "Daniil"; String lastname = "Dmitrovych"; String group = "TR-15"; int year = 2004; int month = 4; int day = 7; float grade = 100; Student newStudent = Task28.addStudent(students, name, surname, lastname, group,year, month, day, grade); Student correct = students.get(0); assertEquals(correct, newStudent);}@Testvoid deleteStudent() { ArrayList<Student> students = new ArrayList<>(); students.add(new Student("Ivanov", "Ivan", "Petrovych", "TI-21", new BirthDate(2000, 1, 1), 65.5f)); students.add(new Student("Gaydash", "Oleg", "Valeriyovych", "TV-34", new BirthDate(2001, 2, 2), 78.6f)); students.add(new Student("Boyko", "Mykhaylo", "Mykolayovych", "TR-12", new BirthDate(2002, 3, 3), 94.3f)); students.add(new Student("Danilyuk", "Max", "Stepanovych", "RT-14", new BirthDate(2003, 4, 4), 86.2f)); students.add(new Student("Sharmalyuk", "Serhiy", "Volodymyrovych", "MM-15", new BirthDate(2004, 5, 5), 72.1f)); int index = 2; Student correct = students.get(2); Student result = Task28.deleteStudent(students, index); assertEquals(correct, result);}