Archivi categoria: Senza categoria

JParabola

import java.awt.Color;
import java.awt.Graphics;

public class JParabola extends JOxy {

	private double a, b, c;

	public double f(double x) {
		return a * x * x + b * x + c;
	}

	public JParabola() {
		a = 1.0;
		b = 0.0;
		c = 0.0;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		g.setColor(Color.BLUE);
		for (int i = 0; i < getWidth() - 1; i++) {
			double x1 = xr(i);
			double y1 = f(x1);
			double x2 = xr(i+1);
			double y2 = f(x2);
			g.drawLine(i, yg(y1), i + 1, yg(y2));
		}
	}

	public double getA() {
		return a;
	}

	public void setA(double a) {
		this.a = a;
	}

	public double getB() {
		return b;
	}

	public void setB(double b) {
		this.b = b;
	}

	public double getC() {
		return c;
	}

	public void setC(double c) {
		this.c = c;
	}

}

JOxy

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;

public class JOxy extends JComponent {

	private int sx, sy, tx, ty;
	private String asseX, asseY;
	private double xMouse, yMouse;

	private class AscoltaMouse extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent e) {
			int x=e.getX();
			int y=e.getY();
			xMouse=xr(x);
			yMouse=yr(y);
			repaint();
		}
	}

	public JOxy() {
		setPreferredSize(new Dimension(400, 400));
		setSx(50);
		setSy(50);
		setTx(100);
		setTy(100);
		setAsseX("X");
		setAsseY("Y");
		addMouseMotionListener(new AscoltaMouse());
	}

	public int xg(double x) {
		return (int) Math.round(tx + x * sx);
	}

	public int yg(double y) {
		return (int) Math.round(getHeight() - 1 - ty - y * sy);
	}

	public double xr(int x) {
		return (double) (x - tx) / sx;
	}

	public double yr(int y) {
		return (double) (getHeight() - 1 - ty - y) / sy;
	}

	public void paintComponent(Graphics g) {
		int w = getWidth() - 1;
		int h = getHeight() - 1;

		g.setColor(Color.WHITE);
		g.fillRect(0, 0, w, h);
		g.setColor(Color.BLACK);

		// asse X
		g.drawLine(0, yg(0), w, yg(0));
		for (int i = (int) xr(0); i <= (int) xr(w); i++) {
			g.drawLine(xg(i), yg(0) - 5, xg(i), yg(0) + 5);
			g.drawString(i + "", xg(i) + 3, yg(0) - 3);
		}
		g.drawString(asseX, w - 5 - g.getFontMetrics().stringWidth(asseX), yg(0) - 20);

		// asse Y
		g.drawLine(xg(0), 0, xg(0), h);
		for (int i = (int) yr(h); i <= yr(0); i++) {
			g.drawLine(xg(0) - 5, yg(i), xg(0) + 5, yg(i));
			g.drawString(i + "", xg(0) + 3, yg(i) - 3);
		}
		g.drawString(asseY, xg(0) + 20, 15);

		g.drawString("("+xMouse+","+yMouse+")", xg(xMouse), yg(yMouse));
	}

	public int getSx() {
		return sx;
	}

	public void setSx(int sx) {
		if (sx < 20)
			sx = 20;
		this.sx = sx;
	}

	public int getSy() {
		return sy;
	}

	public void setSy(int sy) {
		if (sy < 20)
			sy = 20;
		this.sy = sy;
	}

	public int getTx() {
		return tx;
	}

	public void setTx(int tx) {
		if (tx < 0)
			tx = 0;
		this.tx = tx;
	}

	public int getTy() {
		return ty;
	}

	public void setTy(int ty) {
		if (ty < 0)
			ty = 0;
		this.ty = ty;
	}

	public String getAsseX() {
		return asseX;
	}

	public void setAsseX(String asseX) {
		this.asseX = asseX;
	}

	public String getAsseY() {
		return asseY;
	}

	public void setAsseY(String asseY) {
		this.asseY = asseY;
	}

}

Biblioteca

Creare un’applicazione visuale che gestisca una biblioteca scolastica. Devono essere presenti 4 caselle di testo (con a sinistra delle etichette) per contenere Autore, Titolo, Editore, Collocazione. Devono essere anche presenti 3 pulsanti: Apre database, Salva dati, Chiude database. Il primo deve aprire il file “Biblioteca.csv” in scrittura (in modalità append), il secondo deve salvare su file i dati presenti in quel momento sulla finestra separati dal “;”, il terzo deve chiudere il file.

Soluzione: Biblioteca (file ZIP con il progetto Eclipse)

Esercizi in preparazione alla verifica di informatica

Realizzare un programma che utilizzando un ascoltatore di eventi connesso con il sensore di luce faccia partire in avanti l’NXT quando la luminosità supera un valore prefissato in una costante SOGLIA_LUCE all’inizio del programma e lo faccia fermare quando la luminosità scende al di sotto della soglia.

Realizzare un programma che utilizzando due sensori sonori collegati a due porte lo facciano deviare dal suo percorso rettilineo, girando a destra o a sinistra, in maniera tale da evitare le zone con maggior rumore. Per esempio se il sensore di destra fornisce un’intensità sonora maggiore del sinistro, deve girare a sinistra.

Realizzare un programma che chiede il nome della classe e il numero di allievi. Quindi chiede di inserire cognome, nome, sesso, luogo di nascita e data di nascita e memorizzi i dati in un file classe.csv. Quindi un secondo programma chiede all’utente il nome della città di nascita (ad es. Cuneo), legge dal file l’elenco e stampa cognome, nome e data di nascita dei nati a Cuneo.

Rubrica

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class Rubrica {

	public static void main(String[] args) throws IOException {
		FileWriter fw = new FileWriter("rubrica.csv");
		BufferedWriter bw = new BufferedWriter(fw);
		BufferedReader input = new BufferedReader(new InputStreamReader(
				System.in));

		while (true) {
			System.out.println("Inserisci cognome (fine per terminare): ");
			String cognome = input.readLine();
			if (cognome.equals("fine")) {
				break;
			}
			System.out.println("Inserisci nome: ");
			String nome = input.readLine();
			System.out.println("Inserisci numero di telefono: ");
			String telefono = input.readLine();

			String dato = cognome + ";" + nome + ";" + telefono;
			bw.write(dato);
			bw.newLine();
		}
		bw.close();
		System.out.println("Dati salvati");
	}

}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class StampaRubrica {

	public static void main(String[] args) throws IOException {

		FileReader fr = new FileReader("rubrica.csv");
		BufferedReader br = new BufferedReader(fr);

		System.out.println("CognometNometTelefono");
		while (true) {
			String dato = br.readLine();
			if (dato != null) {
				String[] d = dato.split(";");
				System.out.println(d[0] + "t" + d[1] + "t" + d[2]);
			} else
				break;
		}
		br.close();
	}
}

FileDiDati

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Scrittura {

	public static void main(String[] args) {

		try {
			FileWriter fw = new FileWriter("dati.txt");
			BufferedWriter bw = new BufferedWriter(fw);

			for (int i = 1; i <= 10; i++) {
				bw.write("3 x " + i + " = " + 3 * i);
				bw.newLine();
			}

			bw.close();
		} catch (IOException e) {
			System.out.println("Errore nel salvataggio dei dati");
		}
	}

}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Lettura {

	public static void main(String[] args) {

		try {
			FileReader fr = new FileReader("dati.txt");
			BufferedReader br = new BufferedReader(fr);

			for (int i = 1; i <= 10; i++) {
				System.out.println(br.readLine());
			}

			br.close();
		} catch (IOException e) {
			System.out.println("Errore nel salvataggio dei dati");
		}
	}

}