public static void main(String ar

Werbung
/* WillkommensGruss.java */
/*
* @author Benjamin Kormann
*/
public class WillkommensGruss
{
public static void main(String args[])
{
if (args.length != 1)
{
System.out.println("java WillkommensGruss parameter");
System.exit(0);
}
System.out.print("Herzlich Willkommen ");
System.out.println(args[0]);
}
}
/* Aufgabe2.java */
import java.util.*;
/*
* @author Benjamin Kormann
*/
public class Aufgabe2
{
public static void main(String args[])
{
int x[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
System.out.println();
// Bubblesort
for(int i = 0; i < x.length-1; i++)
{
for(int j = i; j < x.length; j++)
{
if (x[i] > x[j])
{
int tmp = x[i];
x[i] = x[j];
x[j] = tmp;
}
}
}
for(int i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
System.out.println();
String y[] = {"Juergen", "Hubert", "Josef", "Liesel", "Vera",
"Christian"};
// Bubblesort
for(int i = 0; i < y.length-1; i++)
{
for(int j = i; j < y.length; j++)
{
if (y[i].compareTo(y[j]) > 0)
{
String tmp = y[i];
y[i] = y[j];
y[j] = tmp;
}
}
}
for(int i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
System.out.println();
}
}
/* Aufgabe3.java */
/*
* @author ???
*/
public class Aufgabe3
{
public static void main(String args[])
{
try {
int i = Integer.valueOf(args[0]).intValue();
int j = Integer.valueOf(args[1]).intValue();
System.out.println("i = " + i);
System.out.println("j = " + j);
int n;
n = i + j;
System.out.println("n
n = i - j;
System.out.println("n
n = i * j;
System.out.println("n
n = i / j;
System.out.println("n
n = i % j;
System.out.println("n
= i + j = " + n);
= i - j = " + n);
= i * j = " + j);
= i / j = " + n);
= i % j = " + n);
i++;
System.out.println("Nach i++: i =
j--;
System.out.println("Nach j--: j =
n = i++;
System.out.println("Nach n = i++:
n = ++i;
System.out.println("Nach n = ++i:
System.out.println("i = " + i);
System.out.println("j = " + j);
" + i);
" + j);
n = " + n + ", i = " + i);
n = " + n + ", i = " + i);
n = ++i + j--;
System.out.println("Nach n = ++i + j--: n = " + n + ", i = "
+ i + " j = " + j);
System.out.println("i = " + i);
System.out.println("j = " + j);
n = i + j * ++j;
System.out.println("Nach n = i + j * ++j: n = " + n + ", i =
" + i + " j = " + j);
i = 1;
j = 0;
System.out.println("i = " + i);
System.out.println("j = " + j);
n = i / j;
System.out.println("n = i / j = " + n);
}
catch(Exception e)
{
System.out.println("Fehler bei der Eingabe: java a b");
System.out.println(e.getMessage());
}
}
}
/* Aufgabe4.java */
/*
* @author Benjamin Kormann
*/
public class Aufgabe4
{
private static void prim(int max)
{
for(int i = 2; i <= Math.sqrt(max); i++)
{
if ((max % i) == 0)
{
System.out.println(i + " kleinster gem. Teiler
von " + max);
return;
}
}
System.out.println(max + " ist Primzahl");
}
public static void main(String args[])
{
if (args.length != 1)
{
System.out.println("java Aufgabe4 number");
System.exit(0);
}
int num = Integer.valueOf(args[0]).intValue();
prim(num);
}
}
/* Aufgabe5.java */
/*
* @author Benjamin Kormann
*/
public class Aufgabe5
{
public static void main(String args[])
{
int iteration = 100;
double sqrt[] = new double[iteration];
sqrt[0] = 2;
for(int i = 0; i < iteration-1; i++)
{
sqrt[i+1] = (sqrt[i] + 2/sqrt[i]) / 2;
}
System.out.println("Wurzel 2 durch Math.sqrt(2):");
System.out.println(" " + Math.sqrt(2));
System.out.println("Wurzel 2 approximiert mit " + iteration + "
Iterationsschritten:");
System.out.println(" " + sqrt[iteration-1]);
}
}
Herunterladen