class Main
{
public static void main(String args[])
{
int a;
LosujeSortuje klasa = new LosujeSortuje();
WezLiczbe zaczytaj = new WezLiczbe();
System.out.println("Podaj liczbe elementow tablicy: ");
a = (int)zaczytaj.getDouble("n = ");
klasa.losuje(a);
System.out.println("Elementy tablicy przed posortowaniem:");
klasa.wyswietl(a);
System.out.println("Elementy tablicy po sortowaniu:");
klasa.sortuje(a);
klasa.wyswietl(a);
}
}
Klasa LosujeSortuje:
import java.util.Random;
public class LosujeSortuje {
int t[];
public void losuje(int n)
{
this.t = new int[n];
for (int i = 0; i < n; i++)
{
Random random = new Random();
this.t[i] = random.nextInt();
}
}
public void wyswietl(int n)
{
for (int i = 0; i < n; i++)
{
System.out.println(+this.t[i]);
}
}
public void sortuje(int n)
{
for (int i = n-1; i > 0; i--)
for (int j = 0; j < i; j++)
{
if (this.t[j] > this.t[j + 1])
{
int temp;
temp = this.t[j];
this.t[j] = this.t[j + 1];
this.t[j + 1] = temp;
}
}
}
}
Klasa WezLiczbe
class WezLiczbe {
public double getDouble(String s)
{
char c;
StringBuffer buf = new StringBuffer();
String sx;
double x;
System.out.print(s);
// pobierz x z konsoli jako ciąg znaków
try
{
while ((c = (char) System.in.read() ) != '\n')
buf.append(c);
// przekonwertuj x z typu String na typ double
sx = buf.toString();
x = Double.parseDouble( sx );
} catch (java.io.IOException e) { x = 0; };
buf.setLength(0); // oczyść bufor
return x;
}
}
Brak komentarzy:
Prześlij komentarz