diff --git a/src/com/company/Main.java b/src/com/company/Main.java index 9b37c5c..6fcb857 100644 --- a/src/com/company/Main.java +++ b/src/com/company/Main.java @@ -1,9 +1,23 @@ package com.company; +import java.time.LocalDate; +import java.time.Month; + +import com.company.week5.Car; +import com.company.week5.Wine; + public class Main { public static void main(String[] args) { - //YOUR code - } + Wine wine1 = new Wine("Fragollino", "Italy", LocalDate.of(2015, Month.DECEMBER, 31)); + Wine wine2 = new Wine("Yellow Tail", "Australia", LocalDate.of(2013, Month.APRIL, 13)); + System.out.println(wine1.toString() + "\n" + wine2.toString()); + System.out.println("Ageing of wine1 = " + wine1.calculateAgeingOfWine() + " years"); + Car car1 = new Car(2010, "Mazda"); + Car car2 = new Car(2015, "Audi"); + + car1.showResultOfChangeSpeedFewTimes(5, "+"); + car1.showResultOfChangeSpeedFewTimes(5, "-"); + } } diff --git a/src/com/company/MarathonComparableComparator/Main.java b/src/com/company/MarathonComparableComparator/Main.java new file mode 100644 index 0000000..2944132 --- /dev/null +++ b/src/com/company/MarathonComparableComparator/Main.java @@ -0,0 +1,25 @@ +package com.company.MarathonComparableComparator; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class Main extends RunnersList { + public static void main(String[] args) { + ArrayList m = fillListWithMarathonResults(); + Collections.sort(m, Comparator.comparing(Marathon::getName).thenComparing(Marathon::getTimeInMins)); + /* Collections.sort(m, new Comparator() { + @Override + public int compare(Marathon o1, Marathon o2) { + if (o1.getName().compareTo(o2.getName()) != 0) + return o1.getName().compareTo(o2.getName()); + else + return (int)(o1.getTimeInMins() - o2.getTimeInMins()); + } + });*/ + printList(m); + sortList(m); + System.out.println("3 Marathon winners with the results: "); + printList(findWinners(m)); + } +} diff --git a/src/com/company/MarathonComparableComparator/Marathon.java b/src/com/company/MarathonComparableComparator/Marathon.java new file mode 100644 index 0000000..e45fd4c --- /dev/null +++ b/src/com/company/MarathonComparableComparator/Marathon.java @@ -0,0 +1,32 @@ +package com.company.MarathonComparableComparator; + +public class Marathon implements Comparable { + private String name; + private long timeInMins; + + public Marathon (){ } + + public Marathon (String name, long timeInMins) { + setName(name); + setTimeInMins(timeInMins); + } + + public void setName(String name) { this.name = name; } + + public String getName() { return name; } + + public void setTimeInMins(long timeInMins) { this.timeInMins = timeInMins; } + + public long getTimeInMins() { return timeInMins; } + + @Override + public String toString() { + return "{" + + "name='" + name + '\'' + + ", timeInMins=" + timeInMins + + '}'; + } + + @Override + public int compareTo(Marathon o) { return (int) (this.timeInMins - o.getTimeInMins()); } // return (int) (this.getTimeInMins() - o.getTimeInMins()); +} diff --git a/src/com/company/MarathonComparableComparator/RunnersList.java b/src/com/company/MarathonComparableComparator/RunnersList.java new file mode 100644 index 0000000..9b0244b --- /dev/null +++ b/src/com/company/MarathonComparableComparator/RunnersList.java @@ -0,0 +1,34 @@ +package com.company.MarathonComparableComparator; + +import java.util.ArrayList; +import java.util.Collections; + +public class RunnersList extends Marathon { + + public static ArrayList fillListWithMarathonResults() { + ArrayList m = new ArrayList<>(); + m.add(new Marathon("Inga",255)); + m.add(new Marathon("Inga",350)); + m.add(new Marathon("Alisa",275)); + m.add(new Marathon("Rimma",263)); + m.add(new Marathon("Alisa",215)); + return m; + } + + public static void sortList(ArrayList m) { Collections.sort(m); } + + public static void printList (ArrayList RunnersList) { + for (Marathon runner: RunnersList) { + System.out.println(runner.toString()); + } + } + + public static ArrayList findWinners (ArrayList marathonList) { + int winnersQuantity = 3; + ArrayList winnersList = new ArrayList<>(); + for (int i = 0; i < winnersQuantity; i++) { + winnersList.add(marathonList.get(i)); + } + return winnersList; + } +} diff --git a/src/com/company/countries/CountriesMap.java b/src/com/company/countries/CountriesMap.java new file mode 100644 index 0000000..750dd06 --- /dev/null +++ b/src/com/company/countries/CountriesMap.java @@ -0,0 +1,28 @@ +package com.company.countries; + +import java.util.HashMap; +import java.util.Map; + +public class CountriesMap { + Map countries; + + public CountriesMap() { + this.countries = new HashMap<>(); + } + + public void printCountries(){ + for(Map.Entry m:countries.entrySet()){ + System.out.println(m.getKey()+" "+m.getValue()); + } + } + + public void addCountry(Country country) { + countries.put(country.name, country); + } + + public Country findCountry(String countryName){ + if (countries.containsKey(countryName)) + return countries.get(countryName); + else throw new IllegalArgumentException("Country " + countryName + " was not found"); + } +} diff --git a/src/com/company/countries/Country.java b/src/com/company/countries/Country.java new file mode 100644 index 0000000..555bdd7 --- /dev/null +++ b/src/com/company/countries/Country.java @@ -0,0 +1,19 @@ +package com.company.countries; + +public class Country { + public String name; + public Long population; + + public Country(String name, Long population) { + this.name = name; + this.population = population; + } + + @Override + public String toString() { + return "Country{" + + "name='" + name + '\'' + + ", population=" + population + + '}'; + } +} diff --git a/src/com/company/countries/Main.java b/src/com/company/countries/Main.java new file mode 100644 index 0000000..be464de --- /dev/null +++ b/src/com/company/countries/Main.java @@ -0,0 +1,39 @@ +package com.company.countries; + +import java.util.Scanner; + +public class Main { + public static void main(String args[]) { + CountriesMap map; + map = addCountries(); + map.printCountries(); + + Scanner s = new Scanner(System.in); + String input; + + do { + System.out.println("\nEnter country input you want to find: "); + input = s.next(); + try { + System.out.println(map.findCountry(input).toString()); + } + catch (IllegalArgumentException e) { + System.out.println(e.getMessage()); + } + System.out.println("\nDo you want to continue search ? Press Y or N"); + input = s.next(); + if (input.equals("N")) break; + } + while (true); + } + + public static CountriesMap addCountries() { + CountriesMap map = new CountriesMap(); + map.addCountry(new Country("Andorra", 11111111111L)); + map.addCountry(new Country("Ukraine", 22222222L)); + map.addCountry(new Country("USA", 333333333L)); + map.addCountry(new Country("Spain", 55555L)); + map.addCountry(new Country("Ukraine", 999999999L)); + return map; + } +} diff --git a/src/com/company/flight/Flight.java b/src/com/company/flight/Flight.java new file mode 100644 index 0000000..9a5c82a --- /dev/null +++ b/src/com/company/flight/Flight.java @@ -0,0 +1,40 @@ +package com.company.flight; + +public class Flight { + private String flightNumber; + private String company; + private Plane plane; + private Double price; + private Double distance; + + public Flight(String flightNumber, String company, Plane plane, Double price, Double distance) { + this.flightNumber = flightNumber; + this.company = company; + this.plane = plane; + this.price = price; + this.distance = distance; + } + + public String getFlightNumber() { return flightNumber; } + + public Double getPrice() { return price; } + + public String getCompany() { return company; } + + public Plane getPlane() { return plane; } + + public Double getDistance() { return distance; } + + public String getPlaneName() {return getPlane().toString(); } + + @Override + public String toString() { + return "Flight{" + + "flightNumber='" + flightNumber + '\'' + + ", company='" + company + '\'' + + ", plane=" + plane + + ", price=" + price + + ", distance=" + distance + + '}'; + } +} \ No newline at end of file diff --git a/src/com/company/flight/FlightsInfo.java b/src/com/company/flight/FlightsInfo.java new file mode 100644 index 0000000..7c91ab3 --- /dev/null +++ b/src/com/company/flight/FlightsInfo.java @@ -0,0 +1,89 @@ +package com.company.flight; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class FlightsInfo { + Set flights = new HashSet<>(); + + public void addFlight(Flight flight) { + flights.add(flight); + } + + public List sort(SORT_BY sortBy){ + List list = new ArrayList(); + list.addAll(flights); + switch (sortBy){ + case PRICE: + Collections.sort(list, Comparator.comparing(Flight::getPrice)); + break; + case DISTANCE: + Collections.sort(list, Comparator.comparing(Flight::getDistance)); + break; + case COMPANY_AND_PRICE: + Collections.sort(list, Comparator.comparing(Flight::getCompany).thenComparing(Flight::getPrice)); + break; + case DISTANCE_AND_PLANE: + Collections.sort(list, Comparator.comparing(Flight::getDistance).thenComparing(Flight::getPlaneName)); + break; + } + return list; + } + + public enum SORT_BY{ + PRICE, DISTANCE, COMPANY_AND_PRICE, DISTANCE_AND_PLANE + } + + public List findFlightByNumber(String flightNumber) { + return flights.stream() + .filter(o->o.getFlightNumber().equals(flightNumber)) + .collect(Collectors.toList()); + } + + public List findFlightByCompany(String company) { + return flights.stream() + .filter(o->o.getCompany().equals(company)) + .collect(Collectors.toList()); + } + + public List findFlightByPrice(Double price) { + return flights.stream() + .filter(o->o.getPrice().equals(price)) + .collect(Collectors.toList()); + } + + public void deleteFlightByNumber(String flightNumber) { + Iterator iter = flights.iterator(); + while (iter.hasNext()) { + Flight flight = iter.next(); + if (flight.getFlightNumber().equals(flightNumber)) + iter.remove(); + } + } + + public void deleteFlightByPlane(String word) { + Iterator iter = flights.iterator(); + while (iter.hasNext()) { + Flight flight = iter.next(); + if (flight.getPlaneName().contains(word)) + iter.remove(); + } + } + + @Override + public String toString() { + return "FlightsInfo{" + + "flights=" + flights + + '}'; + } + + public void print() { + flights.stream().forEach(System.out::println); + } +} diff --git a/src/com/company/flight/Main.java b/src/com/company/flight/Main.java new file mode 100644 index 0000000..c2ff834 --- /dev/null +++ b/src/com/company/flight/Main.java @@ -0,0 +1,49 @@ +package com.company.flight; + +import java.util.List; + +public class Main { + public static void main(String[] args) { + + FlightsInfo flights = addFlightsToSet(); + + System.out.println("Sorted by Price:"); + printFlightsList(flights.sort(FlightsInfo.SORT_BY.PRICE)); + System.out.println("Sorted by Company and Price:"); + printFlightsList(flights.sort(FlightsInfo.SORT_BY.COMPANY_AND_PRICE)); + System.out.println("Sorted by Distance:"); + printFlightsList(flights.sort(FlightsInfo.SORT_BY.DISTANCE)); + System.out.println("Sorted by Distance and Plane:"); + printFlightsList(flights.sort(FlightsInfo.SORT_BY.DISTANCE_AND_PLANE)); + + System.out.println("Find flight by company: "); + printFlightsList(flights.findFlightByCompany("ok")); + System.out.println("Find flight by flight number: "); + printFlightsList(flights.findFlightByNumber("55")); + System.out.println("Find flight by flight price: "); + printFlightsList(flights.findFlightByPrice(5.99)); + + flights.deleteFlightByNumber("122"); + System.out.println("After removal: "); + flights.print(); + flights.deleteFlightByPlane("BOIENG"); + System.out.println("After removal: "); + flights.print(); + } + + public static FlightsInfo addFlightsToSet() { + FlightsInfo flights = new FlightsInfo(); + flights.addFlight(new Flight("222", "dss", new Plane(Producer.AIRBUS, Model.AIRBUS.A330),45.99, 150.00)); + flights.addFlight(new Flight("567", "ab", new Plane(Producer.AIRBUS, Model.AIRBUS.A320), 4.99, 10.00)); + flights.addFlight(new Flight("122", "ok", new Plane(Producer.BOIENG, Model.BOIENG.b767), 5.99, 150.00)); + flights.addFlight(new Flight("905", "ok", new Plane(Producer.BOIENG, Model.BOIENG.b737), 99.99, 70.00)); + flights.addFlight(new Flight("55", "dss", new Plane(Producer.AIRBUS, Model.AIRBUS.A310), 15.99, 10.00)); + return flights; + } + + public static void printFlightsList(List flights){ + for(Flight flight: flights){ + System.out.println(flight); + } + } +} \ No newline at end of file diff --git a/src/com/company/flight/Model.java b/src/com/company/flight/Model.java new file mode 100644 index 0000000..9179a69 --- /dev/null +++ b/src/com/company/flight/Model.java @@ -0,0 +1,8 @@ +package com.company.flight; + +public enum Model {; + + enum AIRBUS {A310, A320, A330} + enum BOIENG {b737, b747, b767, b777} + enum EMBRAER {e175, e190, e195} +} diff --git a/src/com/company/flight/Plane.java b/src/com/company/flight/Plane.java new file mode 100644 index 0000000..6580657 --- /dev/null +++ b/src/com/company/flight/Plane.java @@ -0,0 +1,20 @@ +package com.company.flight; + +public class Plane { + + public Enum producer; + public Enum model; + + public Plane(Enum producer, Enum model){ + this.producer = producer; + this.model = model; + } + + @Override + public String toString() { + return "Plane{" + + "producer=" + producer + + ", model=" + model + + '}'; + } +} diff --git a/src/com/company/flight/Producer.java b/src/com/company/flight/Producer.java new file mode 100644 index 0000000..79360de --- /dev/null +++ b/src/com/company/flight/Producer.java @@ -0,0 +1,3 @@ +package com.company.flight; + +enum Producer{AIRBUS, BOIENG, EMBRAER} diff --git a/src/com/company/shopping/Human.java b/src/com/company/shopping/Human.java new file mode 100644 index 0000000..a25797d --- /dev/null +++ b/src/com/company/shopping/Human.java @@ -0,0 +1,15 @@ +package com.company.shopping; + +import java.util.ArrayList; + +public class Human { + protected ArrayList purchaseList = new ArrayList<>(); + + public ArrayList getPurchaseList() { + return purchaseList; + } + + public void setPurchaseList(ArrayList purchaseList) { + this.purchaseList = purchaseList; + } +} diff --git a/src/com/company/shopping/Husband.java b/src/com/company/shopping/Husband.java new file mode 100644 index 0000000..4f985f7 --- /dev/null +++ b/src/com/company/shopping/Husband.java @@ -0,0 +1,64 @@ +package com.company.shopping; + +import java.util.ArrayList; + +public class Husband extends Human { + + private ArrayList foundGoods = new ArrayList<>(); + + private ArrayList totalListOfGoods = new ArrayList<>(); + + public void goShopping(ArrayList shops) { + for (Shop shop: shops) { + goToShop(shop); + } + } + + public void goToShop(Shop shop) { + addGoodsFromShopToTotalListOfGoods(shop); + findNeededGoods(shop.getItems()); + } + + public void printTotalListOfGoodsInStores() { + for (Item item: getGoodsFromAllShops()) { + System.out.println("Total list: \n" + item.toString()); + } + } + + public void findNeededGoods(ArrayList list) { + for(String item: purchaseList) { + for (Item good: list) { + if (item.equals(good.getName())) { + foundGoods.add(good); + break; + } + } + } + } + + public void printItems(ArrayList list) { + for(Item item: list) { + System.out.println(item.toString()); + } + } + + public double getTotalPrice() { + double totalPrice = 0; + for(Item item: foundGoods) { + totalPrice += item.getPrice(); + } + return totalPrice; + } + + private void addGoodsFromShopToTotalListOfGoods(Shop shop) { + totalListOfGoods.addAll(shop.getItems()); + } + + public ArrayList getGoodsFromAllShops() { + return totalListOfGoods; + } + + public ArrayList getFoundGoods() { + return foundGoods; + } +} diff --git a/src/com/company/shopping/Item.java b/src/com/company/shopping/Item.java new file mode 100644 index 0000000..c4d2e0f --- /dev/null +++ b/src/com/company/shopping/Item.java @@ -0,0 +1,25 @@ +package com.company.shopping; + +public class Item { + private String name; + private double price; + + public Item(String name, double price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } + + @Override + public String toString() { + return "name='" + getName() + '\'' + + ", price=" + getPrice(); + } +} diff --git a/src/com/company/shopping/Main.java b/src/com/company/shopping/Main.java new file mode 100644 index 0000000..4a78eaf --- /dev/null +++ b/src/com/company/shopping/Main.java @@ -0,0 +1,50 @@ +package com.company.shopping; + + +import java.util.ArrayList; + +public class Main { + public static void main(String[] args) { + final String purchaseList[] = {"Sony", "Lenovo", "Bread", "Onion"}; + + Husband husband = new Husband(); + Wife wife = new Wife(husband); + wife.addListOfProducts(purchaseList); + wife.giveListOfProductsToHusband(); + + husband.goShopping(getListOfShopsToGo()); + wife.printInfoAboutPurchaseReceivedFromHusband(); + } + + public static ArrayList getListOfShopsToGo() { + ArrayList shops = new ArrayList<>(); + Shop Comfy = new Shop(Shop.ShopKind.TECHNICS); + shops.add(Comfy); + Shop Silpo = new Shop(Shop.ShopKind.FOOD); + shops.add(Silpo); + addItemsToStore(Comfy); + addItemsToStore(Silpo); + return shops; + } + + public static void addItemsToStore(Shop shop) { + switch (shop.shopKind) { + + case FOOD: + shop.addItem("Bread", 110.0); + shop.addItem("Sausage", 80.0); + shop.addItem("Butter", 70.0); + shop.addItem("Milk", 300.0); + shop.addItem("Potato", 40.0); + break; + + case TECHNICS: + shop.addItem("Sony", 100.0); + shop.addItem("Samsung", 80.0); + shop.addItem("Sharp", 70.0); + shop.addItem("Apple", 300.0); + shop.addItem("Acer", 40.0); + break; + } + } +} \ No newline at end of file diff --git a/src/com/company/shopping/Shop.java b/src/com/company/shopping/Shop.java new file mode 100644 index 0000000..786bfa4 --- /dev/null +++ b/src/com/company/shopping/Shop.java @@ -0,0 +1,31 @@ +package com.company.shopping; + +import java.util.ArrayList; + +public class Shop { + + public enum ShopKind { + FOOD, TECHNICS + } + + public ShopKind shopKind; + private ArrayList listOfItems = new ArrayList<>(); + + public Shop(ShopKind kind) { + this.shopKind = kind; + } + + public void addItem(String name, double price) { + listOfItems.add(new Item(name, price)); + } + + public ArrayList getItems() { + return listOfItems; + } + + public void printItems() { + for(Item item: listOfItems) { + System.out.println(item.toString()); + } + } +} diff --git a/src/com/company/shopping/Wife.java b/src/com/company/shopping/Wife.java new file mode 100644 index 0000000..3aa6319 --- /dev/null +++ b/src/com/company/shopping/Wife.java @@ -0,0 +1,34 @@ +package com.company.shopping; + +import java.util.Collections; + +public class Wife extends Human { + + public Husband husband; + + public Wife(Husband husband){ + this.husband = husband; + } + + public void giveListOfProductsToHusband() { + husband.setPurchaseList(getPurchaseList()); + } + + public void addProduct(String name){ + purchaseList.add(name); + } + + public void addListOfProducts(String [] values) { + Collections.addAll(getPurchaseList(), values); + } + + public void printInfoAboutPurchaseReceivedFromHusband() { + System.out.println("Purchase list received from wife: " + husband.getPurchaseList()); + System.out.println("\nList of products available for purchase: "); + husband.printItems(husband.getGoodsFromAllShops()); + System.out.println("\nFound goods from list: "); + husband.printItems(husband.getFoundGoods()); + System.out.println("\nTotal price of found goods: "); + System.out.println(husband.getTotalPrice()); + } +} diff --git a/src/com/company/week1/javaProblems.java b/src/com/company/week1/javaProblems.java new file mode 100644 index 0000000..604aa4b --- /dev/null +++ b/src/com/company/week1/javaProblems.java @@ -0,0 +1,22 @@ +package com.company.week1; + +import java.util.Scanner; + +public class javaProblems { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.println("Enter any number"); + int number = scan.nextInt(); + for (int i = 1; i <= number; i++) { + int n = i, digit; + while (n > 0) { + digit = n % 10; + if ((digit == 0) || ((i % digit) != 0)) + n=-10; + n /= 10; + } + if (n == 0) + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/src/com/company/week2/javaProblems2_1.java b/src/com/company/week2/javaProblems2_1.java new file mode 100644 index 0000000..fa8ef6d --- /dev/null +++ b/src/com/company/week2/javaProblems2_1.java @@ -0,0 +1,17 @@ +package com.company.week2; + +public class javaProblems2_1 { + public static void main(String[] args) { + int n = 0; + int [] arr = new int[50]; + System.out.println("Odd numbers in ascending order: "); + for (int i = 1; i < 100; i += 2) { + arr[n] = i; + System.out.print(arr[n++] + " "); + } + System.out.println("\nOdd numbers in descending order: "); + for (int i = arr.length - 1; i >= 0; i--) { + System.out.print(arr[i] + " "); + } + } +} diff --git a/src/com/company/week2/javaProblems2_10.java b/src/com/company/week2/javaProblems2_10.java new file mode 100644 index 0000000..f455f7b --- /dev/null +++ b/src/com/company/week2/javaProblems2_10.java @@ -0,0 +1,29 @@ +package com.company.week2; + +import java.util.Arrays; +import java.util.Random; + +public class javaProblems2_10 { + public static void main(String[] args) { + int min = 0; + int [] [] array = new int [10] [5]; + Random r = new Random(); + for (int i = 0; i max) { + max = array[i][j]; + } + } + if ((max <= min) || (min == 0)) + min = max; + } + System.out.println("Minimum value from maximum in rows = " + min); + } +} diff --git a/src/com/company/week2/javaProblems2_2.java b/src/com/company/week2/javaProblems2_2.java new file mode 100644 index 0000000..c56c2e0 --- /dev/null +++ b/src/com/company/week2/javaProblems2_2.java @@ -0,0 +1,20 @@ +package com.company.week2; + +import java.util.Random; + +public class javaProblems2_2 { + public static void main(String[] args) { + int [] arr2 = new int[15]; + int count = 0; + Random r = new Random(); + System.out.println("Array with random digits from 0 to 9: "); + for (int i = 0; i < arr2.length; i++){ + arr2[i] = r.nextInt(10); + System.out.print(arr2[i] + " "); + if (arr2[i] % 2 == 0) + count++; + } + System.out.println(); + System.out.println("Quantity of even numbers in array: " + count); + } +} diff --git a/src/com/company/week2/javaProblems2_3.java b/src/com/company/week2/javaProblems2_3.java new file mode 100644 index 0000000..22e7431 --- /dev/null +++ b/src/com/company/week2/javaProblems2_3.java @@ -0,0 +1,20 @@ +package com.company.week2; + +import java.util.Random; + +public class javaProblems2_3 { + public static void main(String[] args) { + int [] arr3 = new int[8]; + Random r = new Random(); + for (int i = 0; i < arr3.length; i++){ + arr3[i] = r.nextInt(10) + 1; + System.out.print(arr3[i] + " "); + } + System.out.println("\nModified array where elements with odd indexes were changed to 0:"); + for (int i = 0; i < arr3.length; i++){ + if ((i % 2) == 1) + arr3[i] = 0; + System.out.print(arr3[i] + " "); + } + } +} diff --git a/src/com/company/week2/javaProblems2_4.java b/src/com/company/week2/javaProblems2_4.java new file mode 100644 index 0000000..9ae69eb --- /dev/null +++ b/src/com/company/week2/javaProblems2_4.java @@ -0,0 +1,33 @@ +package com.company.week2; + +import java.util.Random; + +public class javaProblems2_4 { + public static void main(String[] args) { + int [] a4, b4; + a4 = b4 = new int[5]; + float average1, average2; + average1 = average2 = 0f; + Random r = new Random(); + for (int i = 0; i < a4.length; i++){ + a4[i] = r.nextInt(6); + System.out.print(a4[i] + " "); + average1 += a4[i]; + } + System.out.println(); + for (int i = 0; i < b4.length; i++){ + b4[i] = r.nextInt(6); + System.out.print(b4[i] + " "); + average2 += b4[i]; + } + if ((average1/a4.length) == (average2/b4.length)){ + System.out.println("\nArithmetical average value in both arrays are equal"); + } + else{ + if ((average1/a4.length) > (average2/b4.length)) + System.out.println("\nAverage1 is bigger than average2"); + else + System.out.println("\nAverage2 is bigger than average1"); + } + } +} diff --git a/src/com/company/week2/javaProblems2_5.java b/src/com/company/week2/javaProblems2_5.java new file mode 100644 index 0000000..389c44f --- /dev/null +++ b/src/com/company/week2/javaProblems2_5.java @@ -0,0 +1,14 @@ +package com.company.week2; + +public class javaProblems2_5 { + public static void main(String[] args) { + int [] arr = new int[20]; + arr[0] = arr[1] = 1; + System.out.println("20 first elements of Fibonacci: "); + System.out.print(arr[0] + " " + arr[1]); + for (int i = 2; i < arr.length; i++){ + arr[i] = arr[i-1] + arr[i-2]; + System.out.print(" " + arr[i]); + } + } +} diff --git a/src/com/company/week2/javaProblems2_6.java b/src/com/company/week2/javaProblems2_6.java new file mode 100644 index 0000000..606be42 --- /dev/null +++ b/src/com/company/week2/javaProblems2_6.java @@ -0,0 +1,20 @@ +package com.company.week2; + +import java.util.Random; + +public class javaProblems2_6 { + public static void main(String[] args) { + int [] arr = new int[12]; + int index = 0, max = arr[0]; + Random r = new Random(); + for (int i = 0; i < arr.length; i++){ + arr[i] = r.nextInt(31)-15; + if (arr[i] >= max) { + max = arr[i]; + index = i; + } + System.out.print(arr[i] + " "); + } + System.out.println("\nMaximum element = " + max + " and it's last index is: " + index); + } +} diff --git a/src/com/company/week2/javaProblems2_7.java b/src/com/company/week2/javaProblems2_7.java new file mode 100644 index 0000000..3b48635 --- /dev/null +++ b/src/com/company/week2/javaProblems2_7.java @@ -0,0 +1,25 @@ +package com.company.week2; + +import java.util.Arrays; +import java.util.Random; + +public class javaProblems2_7 { + public static void main(String[] args) { + int quantity = 0, length = 10; + int [] mas1 = new int[length]; + int [] mas2 = new int[length]; + double [] mas3 = new double[length]; + Random r = new Random(); + for (int i = 0; i < length; i++){ + mas1[i] = r.nextInt(9) + 1; + mas2[i] = r.nextInt(9) + 1; + mas3[i] = (double) mas1[i] / mas2[i]; + if ((mas1[i] % mas2[i]) == 0) + quantity++; + } + System.out.println(Arrays.toString(mas1)); + System.out.println(Arrays.toString(mas2)); + System.out.println(Arrays.toString(mas3)); + System.out.println("Quantity of integer values from array 3: " + quantity); + } +} diff --git a/src/com/company/week2/javaProblems2_8.java b/src/com/company/week2/javaProblems2_8.java new file mode 100644 index 0000000..4c8868f --- /dev/null +++ b/src/com/company/week2/javaProblems2_8.java @@ -0,0 +1,36 @@ +package com.company.week2; + +import java.util.Arrays; +import java.util.Random; + +public class javaProblems2_8 { + public static void main(String[] args) { + int [] arr = new int[11]; + boolean flag = true; + Random r = new Random(); + int b = 0, c = 1, ctmp = 0; + for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(3) - 1; + } + Arrays.sort(arr); + int a = arr[0]; + System.out.println(Arrays.toString(arr)); + for (int i = 1; i < arr.length; i++) { + if (arr[i] == a) + c++; + else { + c = 1; + a = arr[i]; + } + if (c > ctmp) { + ctmp = c; + b = a; + flag = true; + } + else if (c == ctmp) + flag = false; + } + if (flag) + System.out.println("element " + b + " meets " + ctmp + " times"); + } +} diff --git a/src/com/company/week2/javaProblems2_9.java b/src/com/company/week2/javaProblems2_9.java new file mode 100644 index 0000000..b1586d8 --- /dev/null +++ b/src/com/company/week2/javaProblems2_9.java @@ -0,0 +1,35 @@ +package com.company.week2; + +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + +public class javaProblems2_9 { + public static void main(String[] args) { + int input, sum2 = 0, sum1 = 0; + Scanner s = new Scanner(System.in); + do { + System.out.println("Please enter positive even number"); + input = s.nextInt(); + } + while (((input % 2) != 0 ) || (input <= 0)); + int [] randomArray = new int[input]; + Random r = new Random(); + for (int i = 0; i < input; i++) { + randomArray[i] = r.nextInt(11) - 5; + sum2 += Math.abs(randomArray[i]); + } + System.out.println(Arrays.toString(randomArray)); + for (int i = 0; i < input/2; i++){ + sum1 += Math.abs(randomArray[i]); + } + sum2 -= sum1; + if (sum1 > sum2) + System.out.println("Summ of absolute values of 1st half is bigger"); + else + if (sum1 < sum2) + System.out.println("Summ of absolute values of 2nd half is bigger"); + else + System.out.println("Summs of absolute values of both halfs are equal"); + } +} diff --git a/src/com/company/week3/binarySearchDigits.java b/src/com/company/week3/binarySearchDigits.java new file mode 100644 index 0000000..1068819 --- /dev/null +++ b/src/com/company/week3/binarySearchDigits.java @@ -0,0 +1,39 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class binarySearchDigits { + public static void main(String[] args) { + int findMe = 5; + boolean foundIt = false; + int[] arr = new int[10]; + Random r = new Random(); + for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(10); + } + Arrays.sort(arr); + System.out.println(Arrays.toString(arr)); + int i = 0; + int start = 0, end = arr.length - 1; + while ((!foundIt) && ((end - start) > 1)) { + int mid = (start + end) / 2; + if ((arr[start] == findMe) || (arr[end] == findMe) || arr[mid] == findMe) { + foundIt = true; + } + else if (findMe < (arr[mid])) { + end = mid; + } + else { + start = mid; + } + i++; + } + if (foundIt) { + System.out.println("Found " + findMe + " with iterations = " + i); + } + else { + System.out.println("Not found " + findMe + " with iterations = " + i); + } + } +} \ No newline at end of file diff --git a/src/com/company/week3/binarySearchStrings.java b/src/com/company/week3/binarySearchStrings.java new file mode 100644 index 0000000..f9cea67 --- /dev/null +++ b/src/com/company/week3/binarySearchStrings.java @@ -0,0 +1,53 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class binarySearchStrings { + public static void main(String[] args) { + String findMe = "v"; + int stringsQuantity = 100, maxWordLength = 2; + String [] arr = generateRandomStringArray(stringsQuantity, maxWordLength); + System.out.println(Arrays.toString(arr)); + boolean result = doBinarySearch(findMe, arr); + if (result) { + System.out.println("Found " + findMe); + } + else { + System.out.println("Not found " + findMe); + } + } + + public static boolean doBinarySearch (String findMe, String [] arr) { + boolean foundIt = false; + int i = 0; + int start = 0, end = arr.length - 1; + while ((!foundIt) && ((end - start) > 1)) { + int mid = (start + end) / 2; + if ((arr[start].equals(findMe)) || (arr[end].equals(findMe)) || arr[mid].equals(findMe)) { + foundIt = true; + } + else if (findMe.compareTo(arr[mid]) < 0) { + end = mid; + } + else { + start = mid; + } + } + return foundIt; + } + + public static String [] generateRandomStringArray (int arrayLength, int wordMaxLength) { + String [] arr = new String[arrayLength]; + Random r = new Random(); + for (int i = 0; i < arrayLength; i++) { + String word = ""; + int wordLength = r.nextInt(wordMaxLength) + 1; + for (int j = 0; j < wordLength; j++) { + word += (char)(r.nextInt(26) + 97); + } + arr[i] = word; + } + return arr; + } +} \ No newline at end of file diff --git a/src/com/company/week3/bubbleSortDigits.java b/src/com/company/week3/bubbleSortDigits.java new file mode 100644 index 0000000..121dc62 --- /dev/null +++ b/src/com/company/week3/bubbleSortDigits.java @@ -0,0 +1,32 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class bubbleSortDigits { + public static void main(String[] args) { + int[] arr = new int[10]; + Random r = new Random(); + for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(10); + } + System.out.println("Array before sort: " + Arrays.toString(arr)); + int count = 0; + for (int it = 0; it < arr.length - 1; it++) { + boolean sorted = true; + for (int i = 0; i < arr.length - 1 - it; i++) { + if (arr[i] > arr[i + 1]) { + sorted = false; + int tmp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = tmp; + } + count++; + } + if (sorted) + break; + } + System.out.println("Sorted array: " + Arrays.toString(arr)); + System.out.println("How many iterations? " + count); + } +} \ No newline at end of file diff --git a/src/com/company/week3/bubbleSortOverload.java b/src/com/company/week3/bubbleSortOverload.java new file mode 100644 index 0000000..31e2a92 --- /dev/null +++ b/src/com/company/week3/bubbleSortOverload.java @@ -0,0 +1,53 @@ +package com.company.week3; + +import java.util.Arrays; + +public class bubbleSortOverload { + public static void main(String[] args) { + int arrayLength = 10, randomBound = 10; + int [] arr = linearSearchOverload.generateRandomIntArray(arrayLength,randomBound); + System.out.println("Array of integer before sort: " + Arrays.toString(arr)); + doBubbleSorting(arr); + System.out.println("Array of integer after sort: " + Arrays.toString(arr)); + + int wordLength = 3; + String [] arrS = linearSearchOverload.generateRandomStringArray(arrayLength,wordLength); + System.out.println("Array of strings before sort: " + Arrays.toString(arrS)); + doBubbleSorting(arrS); + System.out.println("Array of integer after sort: " + Arrays.toString(arrS)); + } + + public static void doBubbleSorting (int [] a){ + for (int index = 0; index < a.length - 1; index++) { + boolean sorted = true; + for (int i = 0; i < a.length - 1 - index; i++) { + if (a[i] > (a[i + 1])) { + sorted = false; + int tmp = a[i]; + a[i] = a[i + 1]; + a[i + 1] = tmp; + } + } + if (sorted) { + break; + } + } + } + + public static void doBubbleSorting (String [] arr){ + for (int it = 0; it < arr.length - 1; it++) { + boolean sorted = true; + for (int i = 0; i < arr.length - 1 - it; i++) { + if (arr[i].compareTo(arr[i + 1]) > 0 ) { + sorted = false; + String tmp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = tmp; + } + } + if (sorted) { + break; + } + } + } +} \ No newline at end of file diff --git a/src/com/company/week3/bubbleSortStrings.java b/src/com/company/week3/bubbleSortStrings.java new file mode 100644 index 0000000..8efd2b2 --- /dev/null +++ b/src/com/company/week3/bubbleSortStrings.java @@ -0,0 +1,11 @@ +package com.company.week3; + +import java.util.Arrays; + +public class bubbleSortStrings { + public static void main(String[] args) { + String [] arr = {"d", "b", "c"}; + bubbleSortOverload.doBubbleSorting(arr); + System.out.println("Sorted array: " + Arrays.toString(arr)); + } +} diff --git a/src/com/company/week3/linearSearchDigits.java b/src/com/company/week3/linearSearchDigits.java new file mode 100644 index 0000000..d544323 --- /dev/null +++ b/src/com/company/week3/linearSearchDigits.java @@ -0,0 +1,28 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class linearSearchDigits { + public static void main(String[] args) { + int findMe = 5; + int position = -1; + int [] arr = new int[10]; + Random r = new Random(); + for (int i = 0; i < arr.length; i++) { + arr[i] = r.nextInt(10); + } + System.out.println(Arrays.toString(arr)); + int i = 0; + while ((i < arr.length) && (position == -1)) { + if (arr[i] == findMe) { + position = i; + } + i++; + } + if (position > -1) + System.out.println("We found number " + findMe + " in position " + position); + else + System.out.println("There's no " + findMe + " in array"); + } +} diff --git a/src/com/company/week3/linearSearchOverload.java b/src/com/company/week3/linearSearchOverload.java new file mode 100644 index 0000000..ca80307 --- /dev/null +++ b/src/com/company/week3/linearSearchOverload.java @@ -0,0 +1,82 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class linearSearchOverload { + public static void main(String[] args) { + int findDigit = 5, arrayLength = 10, randomValueBound = 10; + + // generate random array with int and find there findDigit; + int [] arrayInt = generateRandomIntArray(arrayLength,randomValueBound); + System.out.println(Arrays.toString(arrayInt)); + int position = doLinearSearch(arrayInt,findDigit); + printResult(position,findDigit); + + String findString = "v"; + int wordLength = 5; + //generate random array with strings and find there findString; + String [] arrayString = generateRandomStringArray(arrayLength, wordLength); + System.out.println(Arrays.toString(arrayString)); + position = doLinearSearch(arrayString, findString); + printResult(position,findString); + } + + public static int doLinearSearch (int [] arr, int findMe) { + int i = 0, position = -1; + while ((i < arr.length) && (position == -1)) { + if (arr[i] == findMe) { + position = i; + } + i++; + } + return position; + } + + public static int doLinearSearch (String [] arr, String findMe) { + int position = -1; + for (int i = 0; i < arr.length; i++){ + if (arr[i].equals(findMe)) { + position = i; + } + } + return position; + } + + public static int [] generateRandomIntArray (int arrayLength, int randomBound) { + int [] arr = new int[arrayLength]; + Random r = new Random(); + for (int i = 0; i < arrayLength; i++) { + arr[i] = r.nextInt(randomBound); + } + return arr; + } + + public static String [] generateRandomStringArray (int arrayLength, int wordMaxLength) { + String [] arr = new String[arrayLength]; + Random r = new Random(); + for (int i = 0; i < arrayLength; i++) { + String word = ""; + int wordLength = r.nextInt(wordMaxLength) + 1; + for (int j = 0; j < wordLength; j++) { + word += (char)(r.nextInt('z'-'a') + 'a'); + } + arr[i] = word; + } + return arr; + } + + public static void printResult (int position, int findDigit) { + if (position > -1) + System.out.println("We found number " + findDigit + " in position " + position); + else + System.out.println("There's no " + findDigit + " in array"); + } + + public static void printResult (int position, String findString) { + if (position > -1) + System.out.println("We found string: " + findString + " in position " + position); + else + System.out.println("There's no string: " + findString + " in strings array"); + } +} diff --git a/src/com/company/week3/linearSearchStrings.java b/src/com/company/week3/linearSearchStrings.java new file mode 100644 index 0000000..c1124cc --- /dev/null +++ b/src/com/company/week3/linearSearchStrings.java @@ -0,0 +1,31 @@ +package com.company.week3; + +import java.util.Arrays; +import java.util.Random; + +public class linearSearchStrings { + public static void main(String[] args) { + String findMe = "v"; + String [] arr = new String[10]; + Random r = new Random(); + for (int i = 0; i < arr.length; i++) { + String word = ""; + int wordLength = r.nextInt(5) + 1; + for (int j = 0; j < wordLength; j++) { + word += (char)(r.nextInt(26) + 97); + } + arr[i] = word; + } + System.out.println(Arrays.toString(arr)); + boolean fountIt = false; + for (int i = 0; i < arr.length; i++){ + if (arr[i].equals(findMe)) { + fountIt = true; + } + } + if (fountIt) + System.out.println("We found string " + findMe); + else + System.out.println("There's no " + findMe + " in array"); + } +} \ No newline at end of file diff --git a/src/com/company/week4/guessLetter.java b/src/com/company/week4/guessLetter.java new file mode 100644 index 0000000..82086b1 --- /dev/null +++ b/src/com/company/week4/guessLetter.java @@ -0,0 +1,45 @@ +package com.company.week4; + +import java.util.Random; +import java.util.Scanner; + +public class guessLetter { + public static void main(String[] args) { + String letter = String.valueOf(generateRandomLetter()); + String input; + System.out.println("Please enter any latin letter"); + Scanner s = new Scanner(System.in); + int count = 0; + do { + input = s.next().toUpperCase(); + if (checkForWrongInput(input)) { + System.out.println("Please make sure you enter latin letter and only 1 letter at a time"); + } + else { + printHintForUserIfNotGuessed(input, letter); + } + count++; + } + while (!input.equals(letter)); + System.out.println("You won in " + count + " attempts"); + } + + public static char generateRandomLetter() { + return (char)(new Random().nextInt('Z'-'A') + 'A'); + } + + public static boolean checkInputContainsWrongCharacter(String input) { + return (input.compareTo("A") < 0) || (input.compareTo("Z") > 0); + } + + public static boolean checkForWrongInput (String input) { + return (input.length() != 1) || checkInputContainsWrongCharacter(input); + } + + public static void printHintForUserIfNotGuessed(String input, String letter) { + if (input.compareTo(letter) < 0) + System.out.println("Too Low"); + else if (input.compareTo(letter) > 0) + System.out.println("Too High"); + } +} \ No newline at end of file diff --git a/src/com/company/week5/Car.java b/src/com/company/week5/Car.java new file mode 100644 index 0000000..08d1a8c --- /dev/null +++ b/src/com/company/week5/Car.java @@ -0,0 +1,70 @@ +package com.company.week5; + + +public class Car { + private int yearModel; + private String make; + private int speed; + int speedUnit = 5; + + public Car (int yearModel, String make) { + setYearModel(yearModel); + setMake(make); + setSpeed(0); + } + + public void setYearModel(int yearModel) { + this.yearModel = yearModel; + } + + public void setMake(String make) { + this.make = make; + } + + public void setSpeed(int speed) { + this.speed = speed; + } + + public int getYearModel() { + return yearModel; + } + + public String getMake() { + return make; + } + + public int getSpeed() { + return speed; + } + + public void addAccelerationToCar () { + setSpeed(speed + speedUnit); + } + + public void makeCarSlowDown () { + if (speed > speedUnit) { + setSpeed(speed - speedUnit); + } + else setSpeed(0); + } + + public void showSpeed () { + System.out.println("Speed of car after change: " + getSpeed()); + } + + public void showResultOfChangeSpeedFewTimes (int times, String speedChange) { + for (int i = 1; i <= times; i++) { + changeSpeed(speedChange); + showSpeed(); + } + } + + public void changeSpeed (String speedChange) { + if (speedChange.equals("+")) { + addAccelerationToCar(); + } + else if (speedChange.equals("-")) { + makeCarSlowDown(); + } + } +} diff --git a/src/com/company/week5/Wine.java b/src/com/company/week5/Wine.java new file mode 100644 index 0000000..92bfc3f --- /dev/null +++ b/src/com/company/week5/Wine.java @@ -0,0 +1,74 @@ +package com.company.week5; + +import java.time.LocalDate; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.util.Comparator; + +public class Wine implements Comparable { + private String name; + private String country; + private LocalDate dateOfProduction; + + public Wine(String name, String country, LocalDate dateOfProduction) { + setName(name); + setCountry(country); + setDateOfProduction(dateOfProduction); + } + + public void setName(String name) { + this.name = name; + } + + public void setCountry(String country) { + this.country = country; + } + + public void setDateOfProduction(LocalDate dateOfProduction) { + this.dateOfProduction = dateOfProduction; + } + + public String getName() { + return name; + } + + public String getCountry() { + return country; + } + + public LocalDate getDateOfProduction() { + return dateOfProduction; + } + + public int calculateAgeingOfWine() { + return Period.between(dateOfProduction, LocalDate.now()).getYears(); + } + + public String getDateString(LocalDate dateOfProduction) { + return dateOfProduction.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")); + } + + @Override + public String toString() { + return "Wine{" + + "name='" + name + '\'' + + ", country='" + country + '\'' + + ", dateOfProduction=" + getDateString(dateOfProduction) + + '}'; + } + + public static Comparator WineDateProdComparator = new Comparator() { + @Override + public int compare(Wine o1, Wine o2) { + int ageing1 = o1.calculateAgeingOfWine(); // вот здесь имеет смысл обойтись без переменных + int ageing2 = o2.calculateAgeingOfWine(); + return ageing1 - ageing2; // return o1.calculateAgeingOfWine() - o2.calculateAgeingOfWine(); + } + }; + + @Override + public int compareTo(Wine o) { + return this.calculateAgeingOfWine() - o.calculateAgeingOfWine(); + } +} + diff --git a/src/com/company/week6/WineCollectionComparable.java b/src/com/company/week6/WineCollectionComparable.java new file mode 100644 index 0000000..8ba85a2 --- /dev/null +++ b/src/com/company/week6/WineCollectionComparable.java @@ -0,0 +1,24 @@ +package com.company.week6; + +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.Collections; + +import com.company.week5.Wine; + +public class WineCollectionComparable { + public static void main(String[] args) { + ArrayList winesSet = new ArrayList<>(); + winesSet.add(new Wine("Merlo", "France", LocalDate.of(2010, Month.DECEMBER, 11))); + winesSet.add(new Wine("Bordo", "France", LocalDate.of(2013, Month.APRIL, 22))); + winesSet.add(new Wine("Chardonnay", "France", LocalDate.of(2015, Month.MAY, 3))); + Wine youngestWine = Collections.min(winesSet); + + for (Wine wine : winesSet) { + if (youngestWine.compareTo(wine) == 0) { + System.out.println(wine.toString()); + } + } + } +} diff --git a/src/com/company/week6/WineList.java b/src/com/company/week6/WineList.java new file mode 100644 index 0000000..af4ca03 --- /dev/null +++ b/src/com/company/week6/WineList.java @@ -0,0 +1,49 @@ +package com.company.week6; + +import java.time.LocalDate; +import java.time.Month; +import java.util.ArrayList; +import java.util.Collections; + +import com.company.week5.Wine; + +public class WineList { + public static void main(String[] args) { + ArrayList wine = getWineList(); // wine - список объектов класса Wine... Может wines? Или SetOfWines// Их же несколько + printWineList(getAllWinesOfSomeYear(wine, getMinimumAgeingOfWineInList(wine))); + } + + public static ArrayList getWineList () { + ArrayList wines = new ArrayList<>(); + wines.add(new Wine("Muskat", "Ukraine", LocalDate.of(2017, Month.NOVEMBER, 17))); + wines.add(new Wine("Fragollino", "Italy", LocalDate.of(2015, Month.DECEMBER, 31))); + wines.add(new Wine("Yellow Tail", "Australia", LocalDate.of(2013, Month.APRIL, 13))); + wines.add(new Wine("Muskat", "Moldova", LocalDate.of(2017, Month.DECEMBER, 17))); + wines.add(new Wine("Riesling", "Germany", LocalDate.of(2017, Month.NOVEMBER, 13))); + return wines; + } + + public static void printWineList (ArrayList wines) { + for (Wine w : wines) { + System.out.println(w.toString()); + } + } + + public static void sortArrayList (ArrayList wines) { + Collections.sort(wines,Wine.WineDateProdComparator); // Перемудрили. Во вторник объясню + } + + public static int getMinimumAgeingOfWineInList (ArrayList wines) { + sortArrayList(wines); + return wines.get(0).calculateAgeingOfWine(); + } + + public static ArrayList getAllWinesOfSomeYear (ArrayList wines, int age) { // ..OfSomeAge? + ArrayList tmp = new ArrayList<>(); + for (Wine w : wines) { + if (w.calculateAgeingOfWine() == age) + tmp.add(w); + } + return tmp; + } +} diff --git a/src/com/company/week7/Main.java b/src/com/company/week7/Main.java new file mode 100644 index 0000000..cff5a5f --- /dev/null +++ b/src/com/company/week7/Main.java @@ -0,0 +1,14 @@ +package com.company.week7; + + +public class Main { + public static void main(String[] args) { + MovablePointSet set = new MovablePointSet(); + System.out.println("Created points set"); + set.printPoints(); + for(int i = 0; i < 5; i++) { + System.out.println("Points after " + (i+1) + " move"); + set.movePoints().printPoints(); + } + } +} diff --git a/src/com/company/week7/MovablePoint.java b/src/com/company/week7/MovablePoint.java new file mode 100644 index 0000000..4e16bde --- /dev/null +++ b/src/com/company/week7/MovablePoint.java @@ -0,0 +1,49 @@ +package com.company.week7; + +public class MovablePoint extends Point { + private float xSpeed; + private float ySpeed; + public Pair speed; + + public MovablePoint() { + super(); + setSpeed(0.0f, 0.0f); + } + public MovablePoint(float xSpeed, float ySpeed) { + super(); + setSpeed(xSpeed, ySpeed); + } + + public MovablePoint(float x, float y, float xSpeed, float ySpeed) { + super(x,y); + setSpeed(xSpeed, ySpeed); + } + public void setxSpeed(float xSpeed) { this.xSpeed = xSpeed; } + + public float getxSpeed() { return xSpeed; } + + public void setySpeed(float ySpeed) { this.ySpeed = ySpeed; } + + public float getySpeed() { return ySpeed; } + + public MovablePoint setSpeed(float xSpeed, float ySpeed) { + setxSpeed(xSpeed); + setySpeed(ySpeed); + return this; + } + + public MovablePoint getSpeed() { + return new MovablePoint(speed.x, speed.y); + } + + public MovablePoint move() { + x += xSpeed; + y += ySpeed; + return this; + } + + @Override + public String toString() { + return super.toString() + "Speed=(" + getxSpeed() + "," + getySpeed() + ")"; + } +} diff --git a/src/com/company/week7/MovablePointSet.java b/src/com/company/week7/MovablePointSet.java new file mode 100644 index 0000000..9e4782b --- /dev/null +++ b/src/com/company/week7/MovablePointSet.java @@ -0,0 +1,46 @@ +package com.company.week7; + +import java.util.ArrayList; +import java.util.Random; + +public class MovablePointSet { + private Random r = new Random(); + private ArrayList set = new ArrayList<>(); + private int pointQuantity = 5; + + public MovablePointSet() { + createMovablePoint(this.pointQuantity); + } + + public MovablePointSet(int pointQuantity) { + createMovablePoint(pointQuantity); + } + + private float getRandomNumber(int from, int to) { + return (float)(r.nextInt(to - from + 1) + from); + } + + private void createMovablePoint(int pointQuantity) { + for(int i = 0; i < pointQuantity; i++) { + set.add(new MovablePoint(getRandomNumber(10,35), + getRandomNumber(10,35), + getRandomNumber(0,10), + getRandomNumber(0,10))); + } + } + + public MovablePointSet movePoints() { + for(MovablePoint mp: set) { + mp.setSpeed(setRandomSpeed(),setRandomSpeed()).move(); + } + return this; + } + + private float setRandomSpeed() { + return getRandomNumber(-3, 3); + } + + public void printPoints() { + System.out.println(set.toString()); + } +} diff --git a/src/com/company/week7/Pair.java b/src/com/company/week7/Pair.java new file mode 100644 index 0000000..7b92b4f --- /dev/null +++ b/src/com/company/week7/Pair.java @@ -0,0 +1,6 @@ +package com.company.week7; + +public class Pair { + public float x; + public float y; +} diff --git a/src/com/company/week7/Point.java b/src/com/company/week7/Point.java new file mode 100644 index 0000000..08e6de6 --- /dev/null +++ b/src/com/company/week7/Point.java @@ -0,0 +1,39 @@ +package com.company.week7; + +public class Point { + public Pair coordinate = new Pair(); + public float x; + public float y; + + public Point(float x, float y) { + this.x = x; + this.y = y; + } + + public Point() { + setX(0.0f); + setY(0.0f); + } + + public void setX(float x) { this.x = x; } + + public float getX() { return x; } + + public void setY(float y) { this.y = y; } + + public float getY() { return y; } + + public Point getXY() { + return new Point(coordinate.x, coordinate.y); + } + + public void setXY(float x, float y) { + setX(x); + setY(y); + } + + @Override + public String toString() { + return "(" + getX() + "," + getY() +")"; + } +} diff --git a/src/com/company/week8/Animal.java b/src/com/company/week8/Animal.java new file mode 100644 index 0000000..51e2950 --- /dev/null +++ b/src/com/company/week8/Animal.java @@ -0,0 +1,16 @@ +package com.company.week8; + +public class Animal implements Eat, Sleep { + + public Animal(){ + System.out.println("A new animal has been created!"); + } + + public void sleep() { + System.out.println("An animal sleeps..."); + } + + public void eat() { + System.out.println("An animal eats..."); + } +} diff --git a/src/com/company/week8/Bird.java b/src/com/company/week8/Bird.java new file mode 100644 index 0000000..9e43c0a --- /dev/null +++ b/src/com/company/week8/Bird.java @@ -0,0 +1,18 @@ +package com.company.week8; + +public class Bird extends Animal implements Eat, Sleep { + + public Bird() { + System.out.println("A new bird has been created!"); + } + + @Override + public void sleep() { + System.out.println("A bird sleeps..."); + } + + @Override + public void eat() { + System.out.println("A bird eats..."); + } +} \ No newline at end of file diff --git a/src/com/company/week8/Dog.java b/src/com/company/week8/Dog.java new file mode 100644 index 0000000..b5b890f --- /dev/null +++ b/src/com/company/week8/Dog.java @@ -0,0 +1,18 @@ +package com.company.week8; + +public class Dog extends Animal implements Eat, Sleep { + + public Dog() { + System.out.println("A new dog has been created!"); + } + + @Override + public void sleep() { + System.out.println("A dog sleeps..."); + } + + @Override + public void eat() { + System.out.println("A dog eats..."); + } +} \ No newline at end of file diff --git a/src/com/company/week8/Eat.java b/src/com/company/week8/Eat.java new file mode 100644 index 0000000..bd58fc2 --- /dev/null +++ b/src/com/company/week8/Eat.java @@ -0,0 +1,5 @@ +package com.company.week8; + +public interface Eat { + void eat(); +} diff --git a/src/com/company/week8/MainClass.java b/src/com/company/week8/MainClass.java new file mode 100644 index 0000000..9ace2bf --- /dev/null +++ b/src/com/company/week8/MainClass.java @@ -0,0 +1,26 @@ +package com.company.week8; + +public class MainClass { + public static void main(String[] args) { + Animal animal = new Animal(); + Bird bird = new Bird(); + Dog dog = new Dog(); + + System.out.println(); + + animal.sleep(); + animal.eat(); + + bird.sleep(); + bird.eat(); + + dog.sleep(); + dog.eat(); + + Animal a1 = new Dog(); + Animal a2 = new Bird(); + + a1.eat(); + a2.sleep(); + } +} diff --git a/src/com/company/week8/Sleep.java b/src/com/company/week8/Sleep.java new file mode 100644 index 0000000..0dfecaf --- /dev/null +++ b/src/com/company/week8/Sleep.java @@ -0,0 +1,5 @@ +package com.company.week8; + +public interface Sleep { + void sleep(); +}