Created
November 12, 2014 20:42
-
-
Save alexshavelev/8ca77d48058a076fd968 to your computer and use it in GitHub Desktop.
JavaRushLesson12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.bonus03; | |
/* Задача по алгоритмам | |
Написать метод, который возвращает минимальное число в массиве и его позицию (индекс). | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5}; | |
Pair<Integer, Integer> result = getMinimumAndIndex(data); | |
System.out.println("Minimum is " + result.x); | |
System.out.println("Index of minimum element is " + result.y); | |
} | |
public static Pair<Integer, Integer> getMinimumAndIndex(int[] array) | |
{ | |
int min = array[0]; | |
int index = 0; | |
if (array == null || array.length == 0) | |
{ | |
return new Pair<Integer, Integer>(null, null); | |
} | |
else { | |
for (int i = 1; i < array.length; i++){ | |
if (array[i]< min) { | |
min = array[i]; | |
index = i; | |
} | |
} | |
} | |
//Напишите тут ваше решение | |
return new Pair<Integer, Integer>(min, index); | |
} | |
public static class Pair<X, Y> | |
{ | |
public X x; | |
public Y y; | |
public Pair(X x, Y y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home01; | |
/* Метод getName в классе Cat | |
Переопредели метод getName в классе Cat так, чтобы программа выдавала на экран надпись | |
«Я - кот». | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
Pet pet = new Cat(); | |
System.out.println(pet.getName()); | |
} | |
public static class Pet | |
{ | |
public String getName() | |
{ | |
return "Я - пушистик"; | |
} | |
} | |
public static class Cat extends Pet | |
{ | |
public String getName(){ | |
return "Я - кот"; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home02; | |
/* Метод setName в классе Cat | |
Переопредели метод setName в классе Cat так, чтобы программа выдавала на экран надпись | |
«Я - кот». | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
Pet pet = new Cat(); | |
pet.setName("Я - пушистик"); | |
System.out.println(pet.getName()); | |
} | |
public static class Pet | |
{ | |
protected String name; | |
public Pet() | |
{ | |
} | |
public String getName() | |
{ | |
return name; | |
} | |
public void setName(String name) | |
{ | |
this.name = name; | |
} | |
} | |
public static class Cat extends Pet | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home07; | |
/* Fly, Run, Swim для классов Duck, Penguin, Toad | |
Есть интерфейсы Fly(летать), Swim(плавать), Run(бегать). | |
Добавь эти интерфейсы классам Duck(утка), Penguin(пингвин), Toad(жаба) | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
} | |
public interface Fly | |
{ | |
public void fly(); | |
} | |
public interface Run | |
{ | |
public void run(); | |
} | |
public interface Swim | |
{ | |
public void swim(); | |
} | |
public class Duck implements Swim, Run, Fly | |
{ | |
public void swim() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
{} | |
public void run() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
{} | |
public void fly() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
{} | |
} | |
public class Penguin implements Run, Swim | |
{ | |
public void run() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
public void swim() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
} | |
public class Toad implements Swim | |
{ | |
public void swim() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home08; | |
/* Интерфейсы к классу Human | |
Добавь как можно больше интерфейсов к классу Human, но чтобы он не стал абстрактным классом. | |
Добавлять методы в класс Human запрещается. | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
Human human = new Human(); | |
System.out.println(human); | |
} | |
public static interface Worker | |
{ | |
public void workLazy(); | |
} | |
public static interface Businessman | |
{ | |
public void workHard(); | |
} | |
public static interface Secretary | |
{ | |
public void workLazy(); | |
} | |
public static interface Miner | |
{ | |
public void workVeryHard(); | |
} | |
public static class Human implements Secretary, Businessman, Worker | |
{ | |
public void workHard() | |
{ | |
} | |
public void workLazy() | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home09; | |
/* Родитель класса CTO | |
Добавь такой класс-родитель к классу CTO(технический директор), чтобы класс перестал быть абстрактным. | |
Добавлять/реализовывать методы в классе CTO запрещается. | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
CTO cto = new CTO(); | |
System.out.println(cto); | |
} | |
public static interface Businessman | |
{ | |
public void workHard(); | |
} | |
public static class CTOW{ | |
public void workHard() | |
{ | |
//To change body of created methods use File | Settings | File Templates. | |
} | |
} | |
public static class CTO extends CTOW implements Businessman | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level12.lesson12.home10; | |
/* Метод, который выводит на экран число 10 | |
Добавь еще один метод, чтобы программа выводила на экран число 10. | |
Подсказка: используй перегрузку методов. | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
Integer i = 5; | |
int x = transformValue(i); | |
System.out.println(x); | |
} | |
public static int transformValue(int i) | |
{ | |
return i*i; | |
} | |
//@Override | |
public static int transformValue(Integer i){return i*2;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment