This commit is contained in:
2025-10-17 09:47:08 +02:00
parent c1f2c32bd7
commit 40c9df42d4
5 changed files with 399 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SuperVerificaMeravigliosa</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1758261738047</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View File

@@ -0,0 +1,157 @@
/*
NB. aggiungere dei parametri ai metodi dove indispensabile
Drawable (interfaccia)
void draw(); stampa le coordinate e le dimensioni del bottone.
Component (classe astratta)
classe base astratta che implementa Drawable e rappresenta un generico componente GUI.
Contiene le proprietà comuni a tutti i componenti: posizione (x, y) e dimensioni (width, height).
Button (classe concreta)
Estende Component e rappresenta un pulsante.
Aggiunge un'etichetta (label) e implementa i metodi:
click() → simula il click del bottone.
TextField (classe concreta)
Estende Component e rappresenta un campo di testo.
Aggiunge la gestione del contenuto testuale (text) e implementa i metodi:
setText() e getText() → gestione del contenuto.
Frame
Classe che rappresenta una finestra contenente componenti.
Gestisce:
Il titolo e le dimensioni della finestra.
Un array di componenti con numero massimo definito.
metodi addComponent()
draw() → disegna il frame e tutti i componenti e può lanciare una eccezione
Eccezioni da gestire:
OutOfFrameException → se un componente esce dai limiti della finestra
Main
Contiene il metodo main() che:
1. Crea un oggetto Frame.
2. Aggiunge un Button e un TextField.
3. Disegna la finestra e i componenti.
4. Simula un click sul bottone.
5. Cambia il testo nel TextField.
6. Ridisegna la finestra per mostrare il nuovo stato.
Drawing Frame: 'La mia finestra' size [800x600]
Components:
Drawing Button 'OK' at (50,100) size [100x40]
Drawing TextField with text 'Inserisci testo...' at (200,100) size [200x40]
Button 'OK' clicked!
Drawing Frame: 'La mia finestra' size [800x600]
Components:
Drawing Button 'OK' at (50,100) size [100x40]
Drawing TextField with text 'Inserisci testo...' at (200,100) size [200x40]
!!! Eccezione catturata !!! Il componente TextField esce dai limiti del frame (La mia finestra)
*/
class OutOfFrameException extends Exception {
OutOfFrameException(String message) {
super(message);
}
}
class MaxComponentsException extends Exception {
MaxComponentsException(String message) {
super(message);
}
}
interface Drawable {
void draw() throws OutOfFrameException;
}
abstract class Component implements Drawable {
protected int x,y,width,height;
}
class Button extends Component {
String label;
Button(int x, int y, int width, int height, String label) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.label = label;
}
public void draw() throws OutOfFrameException {
System.out.println("Drawing Button '" + label + "' size [" + width + "x" + height + "] at position (" + x + "," + y + ") status OK.");
}
public void click() {
System.out.println("Clicked button '" + label + "'");
}
}
class TextField extends Component {
TextField(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
protected String text;
public void draw() {
System.out.println("Drawing TextField with text '" + text + "' size [" + width + "x" + height + "] at position (" + x + "," + y + ") status OK.");
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
class Frame extends Component {
protected int maxComponents = 10;
protected int componentNumber = 0;
protected String title;
protected Component[] componentlist = new Component[maxComponents];
Frame(int width, int height, String title) {
this.width = width;
this.height = height;
this.title = title;
}
public void draw() throws OutOfFrameException{
System.out.println("Drawing Frame '" + title + "' size [" + width + "x" + height + "]");
System.out.println("Drawing components...");
for (int i = 0; i < componentNumber; i++) {
if (componentlist[i].width > this.width || componentlist[i].height > this.height) {
throw new OutOfFrameException("ERROR! The component is out of the frame limits!");
}
else {
componentlist[i].draw();
}
}
}
public void addComponent(Component c) throws MaxComponentsException{
if (componentNumber == maxComponents) {
throw new MaxComponentsException("ERROR! Maximum number of components reached!");
}
else {
componentlist[componentNumber] = c;
componentNumber++;
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
try {
Frame frame = new Frame(800,600,"La mia finestra");
TextField field = new TextField(60,10,801,11);
field.setText("Inserisci Testo...");
Button button = new Button(30, 40, 90, 30, "OK");
frame.addComponent(button);
frame.addComponent(field);
frame.draw();
button.click();
} catch (OutOfFrameException e) {
System.out.println(e.getMessage());
}
}
}

18
VerificaTest/README.md Normal file
View File

@@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

186
VerificaTest/src/Main.java Normal file
View File

@@ -0,0 +1,186 @@
import java.util.ArrayList;
import java.util.List;
record Arma(String nome, int danno) { }
interface Attaccante {
void attacca(Personaggio p);
int dannoMassimo();
void aggiungiArma(Arma arma);
}
interface Curatore {
void cura(Personaggio p);
int puntiCura();
}
abstract class Personaggio {
private final String nome;
private int puntiVita;
public Personaggio(String nome, int puntiVita) {
this.nome = nome;
this.puntiVita = puntiVita;
}
public String getNome() {
return nome;
}
public int getPuntiVita() {
return puntiVita;
}
public void riceviDanno(int danno) {
if (puntiVita - danno <= 0) {
puntiVita = 0;
throw new RuntimeException(nome + " è stato sconfitto!");
}
puntiVita -= danno;
}
public void riceviCura(int cura) {
puntiVita += cura;
}
@Override
public String toString() {
return getClass().getSimpleName() +
" {nome='" + nome + "', puntiVita=" + puntiVita + "}";
}
}
class Mago extends Personaggio implements Attaccante, Curatore {
private final int potereMagico;
private final List<Arma> armi = new ArrayList<>();
public Mago(String nome, int puntiVita, int potereMagico) {
super(nome, puntiVita);
this.potereMagico = potereMagico;
}
@Override
public void aggiungiArma(Arma arma) {
armi.add(arma);
}
@Override
public int dannoMassimo() {
return potereMagico * 6;
}
@Override
public void attacca(Personaggio p) {
if (armi.isEmpty()) return;
int danno = dannoMassimo() / 2 + armi.get(0).danno();
System.out.println(getNome() + " lancia un incantesimo su " + p.getNome() + " (danno: " + danno + ")");
p.riceviDanno(danno);
}
@Override
public int puntiCura() {
return potereMagico * 2;
}
@Override
public void cura(Personaggio p) {
int cura = puntiCura();
System.out.println(getNome() + " cura " + p.getNome() + " di " + cura + " punti vita.");
p.riceviCura(cura);
}
@Override
public String toString() {
return super.toString() + " [potereMagico=" + potereMagico + "]";
}
}
class Guerriero extends Personaggio implements Attaccante {
private final int forza;
private final List<Arma> armi = new ArrayList<>();
public Guerriero(String nome, int puntiVita, int forza) {
super(nome, puntiVita);
this.forza = forza;
}
@Override
public void aggiungiArma(Arma arma) {
armi.add(arma);
}
@Override
public int dannoMassimo() {
return forza * 10;
}
@Override
public void attacca(Personaggio p) {
if (armi.isEmpty()) return;
int danno = dannoMassimo() / 2 + armi.get(0).danno();
System.out.println(getNome() + " colpisce " + p.getNome() + " (danno: " + danno + ")");
p.riceviDanno(danno);
}
@Override
public String toString() {
return super.toString() + " [forza=" + forza + "]";
}
}
class Guaritore extends Personaggio implements Curatore {
private final int cura;
public Guaritore(String nome, int puntiVita, int cura) {
super(nome, puntiVita);
this.cura = cura;
}
@Override
public int puntiCura() {
return cura * 4;
}
@Override
public void cura(Personaggio p) {
int punti = puntiCura();
System.out.println(getNome() + " guarisce " + p.getNome() + " di " + punti + " punti vita.");
p.riceviCura(punti);
}
@Override
public String toString() {
return super.toString() + " [cura=" + cura + "]";
}
}
public class Main {
public static void main(String[] args) {
var guerriero = new Guerriero("Thor", 100, 8);
var mago = new Mago("Merlino", 90, 12);
Curatore guaritore = new Guaritore("Elrond", 80, 25);
Arma bastone = new Arma("Bastone Incantato", 20);
Arma spada = new Arma("Spada del Destino", 50);
mago.aggiungiArma(bastone);
mago.aggiungiArma(spada);
guerriero.aggiungiArma(spada);
guerriero.aggiungiArma(bastone);
System.out.println(mago);
System.out.println(guerriero);
System.out.println(guaritore);
System.out.println();
try {
guerriero.attacca(mago);
mago.attacca(guerriero);
System.out.println(mago);
System.out.println(guerriero);
guaritore.cura(guerriero);
mago.cura(mago);
System.out.println(mago);
System.out.println(guerriero);
System.out.println();
guerriero.attacca(mago); // deve sollevare eccezione
} catch (RuntimeException e) {
System.out.println("⚠️ " + e.getMessage());
}
}
}