Prove domande capitolo 7

This commit is contained in:
Fabio Scotto di Santolo
2018-02-05 15:31:31 +01:00
parent 624ff09ad8
commit ad11fe5454
14 changed files with 279 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
package it.oracle.associate.java8;
class Automobile {
private final String drive() {
return "Driving vehicle";
}
}
class Car extends Automobile {
protected String drive() {
return "Driving car";
}
}
public class ElectricCar extends Car {
@Override
public final String drive() {
return "Driving electric car";
}
public static void main(String[] args) {
final Car car = new ElectricCar();
System.out.println(car.drive());
}
}

View File

@@ -0,0 +1,18 @@
package it.oracle.associate.java8.test;
abstract class House {
protected abstract Object getSpace();
}
abstract class Room extends House {
abstract Object getSpace(Object list);
}
abstract public class Ballroom extends House {
@Override
protected abstract Object getSpace();
public static void main(String[] squareFootage) {
System.out.println("Let's start the party");
}
}

View File

@@ -0,0 +1,21 @@
package it.oracle.associate.java8.test;
abstract class Car {
static { System.out.println("1"); }
public Car(String name) {
super();
System.out.println("2");
}
{ System.out.println("3"); }
}
public class BlueCar extends Car {
{ System.out.println("4"); }
public BlueCar() {
super("blue");
System.out.println("5");
}
public static void main(String[] gears) {
new BlueCar();
}
}

View File

@@ -0,0 +1,16 @@
package it.oracle.associate.java8.test;
abstract class Book {
protected static String material = "papyrus";
public Book() {}
public Book(String material) { this.material = material; }
}
public class Encyclopedia extends Book {
public static String material = "cellulose";
public Encyclopedia() {super();}
public String getMaterial() { return super.material; }
public static void main(String[] pages) {
System.out.println(new Encyclopedia().getMaterial());
}
}

View File

@@ -0,0 +1,27 @@
package it.oracle.associate.java8.test;
import java.io.FileNotFoundException;
import java.io.IOException;
class School {
public int getNumberOfStudentsPerClassroom(String... students) throws IOException {
return 3;
}
public int getNumberOfStudentsPerClassroom() throws IOException {
return 9;
}
}
public class HighSchool extends School {
@Override
public int getNumberOfStudentsPerClassroom() throws FileNotFoundException {
return 2;
}
public static void main(String[] args) throws IOException {
School school = new HighSchool();
System.out.println(school.getNumberOfStudentsPerClassroom());
}
}

View File

@@ -0,0 +1,28 @@
package it.oracle.associate.java8.test;
import java.io.FileNotFoundException;
class Math {
public final double secret = 2;
}
class ComplexMath extends Math {
public final double secret = 4;
protected void dance() throws FileNotFoundException {
return;
}
}
public class InfiniteMath extends ComplexMath {
public final double secret = 8;
public static void main(String[] args) {
Math math = new InfiniteMath();
System.out.println(math.secret);
}
@Override
public final void dance() {
return;
}
}

View File

@@ -0,0 +1,21 @@
package it.oracle.associate.java8.test;
abstract class Triangle {
abstract String getDescription();
}
class RightTriangle extends Triangle {
@Override
protected String getDescription() { return "rt"; } // g1
}
public abstract class IsoscelesRightTriangle extends RightTriangle { // g2
@Override
public String getDescription() {
return "irt";
}
public static void main(String[] args) {
//final Triangle shape = new IsoscelesRightTriangle(); // g3
}
}

View File

@@ -0,0 +1,21 @@
package it.oracle.associate.java8.test;
interface SpeakDialogue { default int talk() { return 7; } }
interface SingMonologue { default int talk() { return 5; } }
public class Performance implements SpeakDialogue, SingMonologue {
public int talk(String... x) {
return x.length;
}
// obbligato a fare l'override
@Override
public int talk() {
// TODO Auto-generated method stub
return SingMonologue.super.talk();
}
public static void main(String[] notes) {
System.out.println(new Performance().talk(notes));
}
}

View File

@@ -0,0 +1,16 @@
package it.oracle.associate.java8.test;
import java.io.EOFException;
class Machine {
public boolean turnOn() throws EOFException { return true; }
}
public class Robot extends Machine {
@Override
public boolean turnOn() { return false; }
public static void main(String[] doesNotCompute) throws Exception {
Machine m = new Robot();
System.out.println(m.turnOn());
}
}

View File

@@ -0,0 +1,21 @@
package it.oracle.associate.java8.test;
abstract class Ball {
protected final int size;
public Ball(int size) {
this.size = size;
}
}
interface Equipment {}
public class SoccerBall extends Ball implements Equipment {
public SoccerBall() {
super(5);
}
public Ball get() { return this; }
public static void main(String[] args) {
Equipment equipment = (Equipment) (Ball) new SoccerBall().get();
System.out.println(((SoccerBall) equipment).size);
}
}

View File

@@ -0,0 +1,24 @@
package it.oracle.associate.java8.test;
interface Run {
default void walk() {
System.out.println("Walking and running!");
}
}
interface Jog {
default void walk() {
System.out.println("Walking and jogging!");
}
}
public class Sprint implements Run, Jog {
@Override
public void walk() {
System.out.println("Sprinting!");
}
public static void main(String[] args) {
new Sprint().walk();
}
}

View File

@@ -0,0 +1,17 @@
package it.oracle.associate.java8.test;
abstract class Parallelogram {
private int getEqualSides() { return 0; }
}
abstract class Rectangle extends Parallelogram {
public static int getEqualSides() { return 2; } // x1
}
public class Square extends Rectangle {
//public int getEqualSides() { return 4; } //x2
public static void main(String[] args) {
final Square myFigure = new Square(); // x3
System.out.print(myFigure.getEqualSides());
}
}

View File

@@ -0,0 +1,10 @@
package it.oracle.associate.java8.test;
interface MusicCreator { public Number play(); }
abstract class StringInstrument { public Long play() { return 3L; } }
public class Violin extends StringInstrument implements MusicCreator {
@Override
public Long play() {
return null;
}
}

View File

@@ -0,0 +1,14 @@
package it.oracle.associate.java8.test;
class Canine {}
class Dog extends Canine {}
class Wolf extends Canine {}
final class Husky extends Dog {}
public class Zoologist {
Canine animal;
public final void setAnimal(Dog animal) { this.animal = animal; }
public static void main(String[] furryFriends) {
new Zoologist().setAnimal(new Dog());
}
}