Czy ten program jest poprawny?

0
import java.io.*;
import java.util.*;
public class Adaptor1 {
	public void write(ArrayList<Sample> sampleList) throws FileNotFoundException
	{
		PrintWriter pw = new PrintWriter("kolo.txt");
				
		for (Sample s: sampleList)
		{
			pw.println(s.getCPU() + " " + s.getMem());
		}
		pw.close();
	}
	
	public void getAllSamples(ArrayList<Sample> sampList)
	{
		FileReader fr = null;
		try
		{
			fr = new FileReader("kolo.txt");
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Błąd przy otwieraniu pliku");
		}
		String data = null;
		
		BufferedReader br = new BufferedReader(fr);
		String[] tab = new String[2];
		try
		{
			while((data = br.readLine()) != null)
			{
				tab = data.split(" ");
				int cpu = 0, mem = 0;
				cpu = Integer.parseInt(tab[0]);
				mem = Integer.parseInt(tab[1]);
				Sample temp = new Sample(cpu, mem);
				sampList.add(temp);
			}
		}
		catch(IOException e)
		{
			System.out.println("Błąd odczytu z pliku");
		}
		try
		{
			fr.close();
		}
		catch(IOException e)
		{
			System.out.println("Błąd przy zamykaniu pliku");
		}
	}
}
import java.util.*;
public class Program {
	protected ArrayList<Sample> sampleList; 
	protected void Run()
	{
		testSample();
	}
	
	private void testSample()
	{
		Adaptor1 ad = new Adaptor1();
		sampleList = new ArrayList<Sample>();
		for (int i = 0; i < 100; i++)
		{
			Sample samp = new Sample();
			sampleList.add(samp);
			//samp.showSample();
		}
		try
		{
			ad.write(sampleList);
		}
		catch(Exception e)
		{
			System.out.println("Błąd");
		}
	}
}
import java.util.Random;
public class Sample {
	protected int cpu;
	protected int memory;
	protected int maxMemory;
	public Sample()
	{
		cpu = 99;
		memory = 6;
		maxMemory = 8;
		
		init();
	}
	
	public Sample(int cpuu, int mem)
	{
		cpu = cpuu;
		memory = mem;
	}
	
	public int getCPU()
	{
		return cpu;
	}
	
	public int getMem()
	{
		return memory;
	}
	
	private void init()
	{
		Random r = new Random();
		cpu = r.nextInt(101);
		memory = r.nextInt(9);
	}
	
	public void showSample()
	{
		System.out.println("cpu:" + cpu + "\nmem:" + memory + "\n");
	}
}
public class Starter {

	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
		Program prog = new Program();
		prog.Run();
	}

}
0

Skompiluj i uruchom.

1 użytkowników online, w tym zalogowanych: 0, gości: 1