Esercizio
This commit is contained in:
@@ -8,6 +8,18 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class Eroe {
|
||||
public class Eroe implements Umano {
|
||||
protected int forza = 15;
|
||||
|
||||
public Eroe() {
|
||||
|
||||
}
|
||||
|
||||
public String getForza() {
|
||||
return "Forza rimanente: " + this.forza;
|
||||
}
|
||||
|
||||
public void combatti() {
|
||||
this.forza -= 3;
|
||||
}
|
||||
}
|
||||
46
src/interfacce/Giochiamo.java
Normal file
46
src/interfacce/Giochiamo.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
package interfacce;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class Giochiamo {
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Eroe eroe = new Eroe();
|
||||
Vampiro vampiro = new Vampiro();
|
||||
Licantropo licantropo = new Licantropo(true);
|
||||
|
||||
eroe.combatti();
|
||||
eroe.combatti();
|
||||
eroe.combatti();
|
||||
|
||||
vampiro.azzanna();
|
||||
|
||||
licantropo.azzanna();
|
||||
licantropo.azzanna();
|
||||
|
||||
System.out.println("Eroe: " + eroe.getForza());
|
||||
System.out.println("Vampiro: " + vampiro.getForza());
|
||||
System.out.println(licantropo.getForza());
|
||||
pausa();
|
||||
}
|
||||
|
||||
public static void pausa() {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,37 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class Licantropo {
|
||||
public class Licantropo implements Mostro, Umano{
|
||||
private boolean isUomo;
|
||||
|
||||
protected int forzaUmano;
|
||||
protected int forzaMostro;
|
||||
|
||||
public Licantropo(boolean luna) {
|
||||
isUomo = !luna;
|
||||
if (luna) {
|
||||
forzaMostro = 15;
|
||||
forzaUmano = 0;
|
||||
} else {
|
||||
forzaMostro = 0;
|
||||
forzaUmano = 15;
|
||||
}
|
||||
}
|
||||
|
||||
public String getForza() {
|
||||
return "Forza rimanente come umano: " + forzaUmano +
|
||||
"\nForza rimanente come mostro: " + forzaMostro;
|
||||
}
|
||||
|
||||
public void azzanna() {
|
||||
if (!isUomo) {
|
||||
forzaMostro -= 2;
|
||||
}
|
||||
}
|
||||
|
||||
public void combatti() {
|
||||
if (isUomo) {
|
||||
forzaUmano -= 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,6 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public interface Mostro {
|
||||
|
||||
public interface Mostro extends Personaggio {
|
||||
public void azzanna();
|
||||
}
|
||||
@@ -8,6 +8,6 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public interface {
|
||||
|
||||
public interface Personaggio {
|
||||
public String getForza();
|
||||
}
|
||||
@@ -8,6 +8,6 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public interface Umano {
|
||||
|
||||
public interface Umano extends Personaggio {
|
||||
public void combatti();
|
||||
}
|
||||
@@ -8,6 +8,17 @@ package interfacce;
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class Vampiro {
|
||||
public class Vampiro implements Mostro {
|
||||
protected int forza;
|
||||
|
||||
public Vampiro() {
|
||||
}
|
||||
|
||||
public String getForza() {
|
||||
return "Forza rimanente: " + this.forza;
|
||||
}
|
||||
|
||||
public void azzanna() {
|
||||
this.forza -= 2;
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
package interfacce;
|
||||
|
||||
import java.util.InputMismatchException;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Verde
|
||||
*/
|
||||
public class interfacce {
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
|
||||
static Scanner sc = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
int scelta = -1;
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
System.out.println("1. ");
|
||||
System.out.println("2. ");
|
||||
System.out.println("3. ");
|
||||
System.out.println("0. Esci");
|
||||
System.out.print("Opzione: ");
|
||||
|
||||
try {
|
||||
scelta = sc.nextInt();
|
||||
sc.nextLine();
|
||||
|
||||
switch (scelta) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
|
||||
pausa();
|
||||
break;
|
||||
case 2:
|
||||
|
||||
pausa();
|
||||
break;
|
||||
case 3:
|
||||
|
||||
pausa();
|
||||
break;
|
||||
default:
|
||||
System.out.println("Opzione non valida.");
|
||||
pausa();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (InputMismatchException _) {
|
||||
System.out.println("Errore: scelta non valida.");
|
||||
pausa();
|
||||
}
|
||||
} while (scelta != 0);
|
||||
}
|
||||
|
||||
public static void pausa() {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user