Skip to content

Instantly share code, notes, and snippets.

@P0huber
P0huber / Correct.java
Created March 16, 2018 17:37
Interface and abstract class. Интерфейс и абстрактный класс. [Java]
package com.javarush.task.task13.task1314;
import java.awt.*;
import static java.awt.Color.GRAY;
/*
Большая лисица — это такая лисица
*/
public class Correct {
@P0huber
P0huber / MultipleInheritance.java
Created December 2, 2017 17:54
Multiple inheritance from interfaces. Множественное наследование интерфейсом от нескольких интерфейсов. Java
public class MultipleInheritance {
public static void main(String[] args) throws Exception {}
interface Person {
void use(Person person);
void startToWork();}
interface Workable {
boolean wantsToGetExtraWork();}
@P0huber
P0huber / BeerInBar.java
Created December 2, 2017 15:01
Interfaces. Интерфейсы. Java
public class BeerInBar {
public static void main(String[] args) throws Exception {}
public interface Drink {
void askMore(String message);
void sayThankYou();
boolean isReadyToGoHome();}
public interface Alcohol extends Drink {
boolean READY_TO_GO_HOME = false;
@P0huber
P0huber / MinAndIndex.java
Created November 23, 2017 03:32
Finding Min digit and its index. Нахождение минимального числа массива и его индекс [Java]
public class MinAndIndex {
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) {
if (array == null || array.length == 0) {
return new Pair<Integer, Integer>(null, null);}
@P0huber
P0huber / Implementation.java
Created November 20, 2017 16:27
OOP. The implementations of interfces. ООП. Реализации интерфейсов [Java]
public class Implementation {
public static void main(String[] args) {}
public interface Fly {
public void fly();}
public interface Move {
public void move();}
public interface Eat {
public void eat();}
@P0huber
P0huber / InheritanceFromAbstractClass.java
Created November 18, 2017 17:24
Inheritance from an abstract class. Наследование от абстрактного класса [Java]
public class InheritanceFromAbstractClass {
public static void main(String[] args) {}
public static abstract class Pet {
public abstract String getName();
public abstract Pet getChild();}
public static class Cat extends Pet{ // inheritance from abstract class and implementations of methods
public String getName(){return "Timmy";}
public Pet getChild(){return new Cat();}}
@P0huber
P0huber / AbstractWork.java
Created November 18, 2017 16:11
Practice of using abstract classes. Практика использования абстрактных классов [Java]
public class AbstractWork {
public static void main(String[] args) {}
public static abstract class Animal { // is subclassed by class Cow
public abstract String getName();} // is implemented in class Cow
public static class Cow extends Animal{ // class inheritance occurs
public String getName(){
return "I`m a cow Marry almost :)";} // the implementation occurs
}
@P0huber
P0huber / ThreeMAX.java
Created November 13, 2017 01:16
OOP. ООП [Java]
public class ThreeMAX {
public static void main(String[] args) {
}
public static int max(int a, int b){return Math.max(a, b);}
public static long max(long a, long b){return Math.max(a, b);}
public static double max(double a, double b){return (a > b)? a : b;}
}
/*Три метода и максимум
Написать public static методы: int max(int, int), long max(long, long), double max(double, double).
@P0huber
P0huber / MethodOverloading.java
Created November 11, 2017 22:23
Method overloading. Метод перегрузки [Java]
public class MethodOverloading {
public static void main(String[] args) {
Integer b = 1000;
print(10);
print(b); // method overloading with Integer parameter
}
public static void print(int a){}
public static void print(Integer b){}
}
/*Int и Integer
@P0huber
P0huber / PolymorphismInstanceof.java
Created November 11, 2017 20:46
Polymorphism. Instanceof. Полиморфизм. Оператор принадлежности экземпляру Instanceof [Java]
public class PolymorphismInstanceof {
public static void main(String[] args) {
System.out.println(getObjectType(new Cow()));
System.out.println(getObjectType(new Dog()));
System.out.println(getObjectType(new Whale()));
System.out.println(getObjectType(new Pig()));}
public static String getObjectType(Object o) {
if(o instanceof Cow) return "Корова"; // we`re checking "o" instances of "Cow" or no
if (o instanceof Dog) return "Собака";