Implementieren Sie die Aufgabe 14 und Aufgabe 15 als Funktionen.
Lösung
Aufgabe 14
import java.util.Scanner; // Scanner für die Eingabe von Werten
public class Aufgabe20a
{
public static double Pi(int iModus, int iParameter)
{ // berechnet Kreiszahl Pi im gewählten Modus
// Variablen Deklaration
double dErgebnis, dAbweichung, dEPS;
int iSchritt;
// Variablen Initialisierung
dErgebnis = 1.;
dEPS = 1. / Math.pow(10, iParameter);
iSchritt = 0;
if (iModus == 1)
for (iSchritt = 1; iSchritt <= iParameter; ++ iSchritt)
dErgebnis += Math.pow(-1, iSchritt)*(1. / (2 * iSchritt+1));
else if (iModus == 2)
do
{ // berechnet Pi bis zur angegebenen Dezimalstelle
++ iSchritt;
double dPrev = 4. * dErgebnis;
dErgebnis += Math.pow(-1, iSchritt)*(1. / (2 * iSchritt+1));
dAbweichung = dPrev-dErgebnis * 4;
if (dAbweichung < 0)
dAbweichung *= -1.;
} // end do
while (dAbweichung > dEPS);
return 4 * dErgebnis; // Formel berechnet Ergebnis = Pi / 4
} // end Pi
public static void main(String[] args)
{
// Variablen Deklaration
Scanner scnEingabe = new Scanner(System.in);
int iDezimalstellen, iSchritte, iModus;
// Variablen Initialisierung
System.out.println("Möglichkeiten zur Berechnung von Pi:");
System.out.println("(1) Anzahl der Entwicklungsschritte vorgeben");
System.out.println("(2) genaue Dezimalstellen vorgeben");
System.out.print("\n Bitte wählen: ");
iModus = scnEingabe.nextInt();
if (iModus == 1)
{
System.out.print("\n Entwicklungsschritte: ");
iSchritte = scnEingabe.nextInt();
System.out.println("Ergebnis: pi = "+Pi(iModus, iSchritte));
} // end if
else if (iModus == 2)
{
System.out.print("\n genaue Dezimalstellen: ");
iDezimalstellen = scnEingabe.nextInt();
System.out.println("Ergebnis: pi = "
+ Pi(iModus, iDezimalstellen));
} // end else if
else
System.out.println("Ungültigen Modus gewählt!");
} // end main
} // end class
Aufgabe 15
import java.util.Scanner; // Scanner für die Eingabe von Werten
public class Aufgabe20b
{
public static double Cosinus(int iModus, double dZahl, int iParameter)
{ // Berechnet den Cosinus von dZahl im gewählten Modus
// Variablen Deklaration
double dErgebnis, dZaehler, dAbweichung, dEPS;
int iSchritt;
long iNenner;
// Variablen Initialisierung
dErgebnis = 1.;
dZaehler = 1.;
iNenner = 2;
dEPS = 1. / Math.pow(10, iParameter);
iSchritt = 0;
// Anzahl der Entwicklungsschritte vorgegeben
if (iModus == 1)
for (iSchritt = 1; iSchritt <= iParameter; ++ iSchritt)
{
dZaehler *= (dZahl * dZahl);
if (iSchritt > 1)
iNenner *= (iSchritt * 2- 1) * (iSchritt * 2);
dErgebnis += Math.pow(-1, iSchritt) * (dZaehler / iNenner);
} // end for
// genaue Dezimalstellen vorgegeben
else if (iModus == 2)
do
{ // Schleife berechnet den Cosinus auf n Stellen genau
++ iSchritt;
dZaehler *= (dZahl * dZahl);
if (iSchritt > 1)
iNenner *= (iSchritt * 2- 1) * (iSchritt * 2);
double dPrev = dErgebnis;
dErgebnis += Math.pow(-1, iSchritt) * (dZaehler / iNenner);
dAbweichung = dPrev-dErgebnis;
if (dAbweichung < 0)
dAbweichung *= -1.;
} // end do
while (dAbweichung > dEPS);
return dErgebnis;
} // end cosinus
public static void main(String[] args)
{
// Variablen Deklaration
Scanner scnEingabe = new Scanner(System.in);
double dZahl;
int iDezimalstellen, iSchritte, iModus;
// Variablen Initialisierung
System.out.print("Berechnung des Cosinus von: ");
dZahl = scnEingabe.nextDouble();
System.out.println("\nMöglichkeiten zur Berechnung von Pi:");
System.out.println("(1) Anzahl der Entwicklungsschritte vorgeben");
System.out.println("(2) genaue Dezimalstellen vorgeben");
System.out.print("\n Bitte wählen: ");
iModus = scnEingabe.nextInt();
if (iModus == 1)
{
System.out.print("\n Entwicklungsschritte: ");
iSchritte = scnEingabe.nextInt();
System.out.println("Ergebnis: Cosinus("+dZahl+") = "
+ Cosinus(iModus, dZahl, iSchritte));
} // end if
else if (iModus == 2)
{
System.out.print("\n genaue Dezimalstellen: ");
iDezimalstellen = scnEingabe.nextInt();
System.out.println("Ergebnis: Cosinus("+dZahl+") = "
+ Cosinus(iModus, dZahl, iDezimalstellen));
} // end else if
else
System.out.println("Ungültigen Modus gewählt!.");
} // end main
} // end class


