Docsity

IPAddress con BubbleSort, Esercizi di Programmazione Java

Toggle sidebar
bg1
import java.io.*;
import java.util.*;
class Computer {
String ipAddress;
public Computer(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getIpAddress() {
return ipAddress;
}
}
class Rete {
List<Computer> computerList = new ArrayList<>();
// Metodo per leggere il file
public void leggiFile(String filePath) {
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
computerList.add(new Computer(line.trim()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Metodo per stampare a console la lista dei computer
public void stampaIP() {
for (Computer computer : computerList) {
System.out.println(computer.getIpAddress());
}
}
// Metodo per ordinare la lista di computer utilizzando il Bubble Sort
public void ordinaIP() {
int n = computerList.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
String ip1 = computerList.get(j).getIpAddress();
String ip2 = computerList.get(j + 1).getIpAddress();
if (ip1.compareTo(ip2) > 0) {
// Swap
Collections.swap(computerList, j, j + 1);
}
}
}
}
// Metodo per scrivere la lista ordinata su un file
public void scriviFile(String filePath) {
try (PrintWriter writer = new PrintWriter(filePath)) {
pf2

Anteprima parziale del testo

Scarica IPAddress con BubbleSort e più Esercizi in PDF di Programmazione Java solo su Docsity!

import java.io.; import java.util.; class Computer { String ipAddress; public Computer(String ipAddress) { this.ipAddress = ipAddress; } public String getIpAddress() { return ipAddress; } } class Rete { List computerList = new ArrayList<>(); // Metodo per leggere il file public void leggiFile(String filePath) { try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { String line; while ((line = br.readLine()) != null) { computerList.add(new Computer(line.trim())); } } catch (IOException e) { e.printStackTrace(); } } // Metodo per stampare a console la lista dei computer public void stampaIP() { for (Computer computer : computerList) { System.out.println(computer.getIpAddress()); } } // Metodo per ordinare la lista di computer utilizzando il Bubble Sort public void ordinaIP() { int n = computerList.size(); for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { String ip1 = computerList.get(j).getIpAddress(); String ip2 = computerList.get(j + 1).getIpAddress(); if (ip1.compareTo(ip2) > 0) { // Swap Collections.swap(computerList, j, j + 1); } } } } // Metodo per scrivere la lista ordinata su un file public void scriviFile(String filePath) { try (PrintWriter writer = new PrintWriter(filePath)) {

for (Computer computer : computerList) { writer.println(computer.getIpAddress()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { // Punto 9: Creare un'istanza della classe Rete Rete rete = new Rete(); // Punto 5: Leggere il file e popolare la lista di computer rete.leggiFile("input.txt"); // Punto 6: Stampare la lista prima dell'ordinamento System.out.println("Lista prima dell'ordinamento:"); rete.stampaIP(); // Punto 7: Ordinare la lista rete.ordinaIP(); // Punto 6: Stampare la lista dopo l'ordinamento System.out.println("\nLista dopo l'ordinamento:"); rete.stampaIP(); // Punto 8: Scrivere la lista ordinata su un file rete.scriviFile("output.txt"); } }