Lösungen zum Programmierteil des Prologs:
import java.util.*;
class BahnhofAutomat
{
private static Scanner m_Scanner = new Scanner(System.in);
public static void main (String[] arguments)
{
char Letter;
double Price = 0.0;
do
{
Letter = AskForDestination();
}while(Letter < 'A' || Letter > 'Z');
switch(Letter)
{
case 'A':
Price = 100.0;
break;
case 'B':
Price = 200.0;
break;
case 'C':
Price = 300.0;
break;
case 'D':
Price = 400.0;
break;
case 'E':
Price = 500.0;
break;
case 'F':
Price = 600.0;
break;
case 'G':
Price = 700.0;
break;
case 'H':
Price = 800.0;
break;
case 'I':
Price = 900.0;
break;
default:
System.out.println("Epic Fail!");
}
PrintHeader();
System.out.println("Der Preis fuer Ihre gewuenschtes Reiseziel betraegt:");
System.out.println(Double.toString(Price) + " Euro fuer 1 Erwachsenene und " + Double.toString(Price/2.0) + " fuer 1 Kind");
System.out.println("");
System.out.println("Wieviel Erwachsene fahren mit ?");
int CountOfAdults = m_Scanner.nextInt();
System.out.println("Wieviel Kinder fahren mit ?");
int CountOfChildren = m_Scanner.nextInt();
Price = Price * CountOfAdults + Price / 2.0 * CountOfChildren;
PrintHeader();
System.out.println("Der Gesamtpreis fuer Ihre gewuenschtes Reiseziel betraegt:");
System.out.println(Double.toString(Price) + " Euro");
double GivenAmount = 0.0;
do
{
System.out.println("Bitte geben Sie den geforderten Betrag in das Diskettenlaufwerk ein:");
GivenAmount = m_Scanner.nextDouble();
}while(Price - GivenAmount > 0);
PrintHeader();
System.out.println("Vielen Dank!");
if(Price - GivenAmount != 0)
System.out.println("Ihr Restgeld betraegt: " + Double.toString(GivenAmount - Price) + " Euro");
}
private static void PrintHeader()
{
ClearScreen();
System.out.println("###########################################################");
System.out.println("# -------------------------------------------- #");
System.out.println("# Willkommen bei der TUBB - TU Wien Bundesbahn #");
System.out.println("# -------------------------------------------- #");
System.out.println("###########################################################");
System.out.println("");
}
private static void ClearScreen()
{
// Seems there is no other solution of clearing the screen... System.Console.Clear() ... I miss .Net!
// If you think about interop and the different kinds of console (Unix, Windows)
// its clear that there is no general way of doing this or ?
for(int i=0; i<100; i++)
{
System.out.println("");
}
}
private static char AskForDestination()
{
PrintHeader();
System.out.println("Bitte geben Sie Ihr Ziel bekannt:");
System.out.println("A - Bregenz");
System.out.println("B - Innsbruck");
System.out.println("C - Salzburg");
System.out.println("D - Linz");
System.out.println("E - Klagenfurt");
System.out.println("F - Graz");
System.out.println("G - St. Poelten");
System.out.println("H - Eisenstadt");
System.out.println("I - Wien");
System.out.println("");
String s = m_Scanner.next();
return s.toUpperCase().charAt(0);
}
}
Fibonacci:
import java.util.*;
class Fibonacci
{
public static void main (String[] arguments)
{
Scanner scan = new Scanner(System.in);
System.out.print("Geben Sie bitte einen Wert fuer n an: ");
int Count = scan.nextInt();
System.out.println(CalcFib(Count));
}
public static int CalcFib(int number)
{
if(number == 0 || number == 1)
return 1;
return CalcFib(number-1) + CalcFib(number-2);
}
}
PI-Berechnung:
import java.util.*;
class Pi
{
public static void main (String[] arguments)
{
Scanner scan = new Scanner(System.in);
System.out.print("Geben Sie bitte einen Wert fuer n an: ");
int Count = scan.nextInt();
System.out.println(CalcPi(Count));
}
public static double CalcPi(int number)
{
double result = 1.0;
for(int i = 0; i < number; i++)
{
//Call the function to calculate the Squareroot, divide it and multiply it with the former result.
result = result * CalcWurzel(i) / 2.0;
}
return 2.0 / result; //Reciprocal and multiply with 2! Thats our PI
}
// Function to calculate the Squareroot on top of the fraction
public static double CalcWurzel(int count)
{
double w = Math.sqrt(2);
for( int i = 0 ; i < count ; i++)
{
w = Math.sqrt(2 + w);
}
return w;
}
}
Hiç yorum yok:
Yorum Gönder