Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package inout;
- import java.util.Scanner;
- import java.util.Calendar;
- import java.util.Locale;
- import java.util.Date;
- import java.util.NoSuchElementException;
- import java.util.InputMismatchException;
- import java.util.regex.Pattern;
- import java.text.ParseException;
- import java.text.DateFormat;
- /** Diese Klasse stellt Methoden zur Verfügung, <br/>
- * um Texte und einfache Typen von der Konsole einzulesen.<br/>
- * Die Ausnahmebehandlung ist Aufgabe des Aufrufers.<hr/>
- * @author Helmut Balzert
- * @version 2.1 / 1.11.2012
- */
- public class Console
- {
- private static Scanner sc;
- //Unterdrückung des default-Konstruktor,
- //um eine Objekterzeugung zu verhindern
- private Console()
- {
- //Dieser Konstruktor wird nie aufgerufen
- }
- /**Liest eine Zeile von der Konsole
- * @return Eingelesene Zeile vom Typ String.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static String readString()
- throws NoSuchElementException, IllegalStateException
- {
- Scanner sc = new Scanner(System.in);
- return sc.nextLine();
- }
- /**Liest eine Zeile von der Konsole
- * @return Eingelesene Zeile vom Typ char[].
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static char[] readCharArray()
- throws NoSuchElementException, IllegalStateException
- {
- sc = new Scanner(System.in);
- String text = sc.nextLine();
- return text.toCharArray();
- }
- /**Liest einen booleschen Wert von der Konsole
- * @return Boolescher Wert true oder false.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- */
- public static boolean readBoolean() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- sc = new Scanner(System.in);
- return sc.nextBoolean();
- }
- /**Liest eine ganze Zahl vom Typ int von der Konsole
- * @return Ganze Zahl vom Typ int.
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static int readInt() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- return new Scanner(System.in).nextInt();
- }
- /**Liest eine ganze Zahl vom Typ long von der Konsole
- * @return Ganze Zahl vom Typ long
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static long readLong() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- return new Scanner(System.in).nextLong();
- }
- /**Liest eine Gleitpunktzahl vom Typ float von der Konsole
- * Englische Notation: Trennung der
- * Nachkommastellen durch Punkt
- * @return Gleitpunktzahl vom Typ float
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static float readFloatPoint() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- Locale.setDefault(Locale.ENGLISH);
- return new Scanner(System.in).nextFloat();
- }
- /**Liest eine Gleitpunktzahl vom Typ float von der Konsole
- * Deutsche Notation: Trennung der
- * Nachkommastellen durch Komma
- * @return Gleitpunktzahl vom Typ float
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static float readFloatComma() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- Locale.setDefault(Locale.GERMAN);
- return new Scanner(System.in).nextFloat();
- }
- /**Liest eine Gleitpunktzahl vom Typ double von der Konsole
- * Englische Notation: Trennung der
- * Nachkommastellen durch Punkt
- * @return Gleitpunktzahl vom Typ double
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static double readDoublePoint() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- Locale.setDefault(Locale.ENGLISH);
- return new Scanner(System.in).nextDouble();
- }
- /**Liest eine Gleitpunktzahl vom Typ double von der Konsole
- * Deutsche Notation: Trennung der
- * Nachkommastellen durch Komma
- * @return Gleitpunktzahl vom Typ double
- * @exception InputMismatchException:
- * Die Eingabe entspricht nicht dem Typ.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- * @exception IllegalStateException:
- * Die verwendete Methode ist nicht geöffnet.
- */
- public static double readDoubleComma() throws
- InputMismatchException, NoSuchElementException,
- IllegalStateException
- {
- Locale.setDefault(Locale.GERMAN);
- return new Scanner(System.in).nextDouble();
- }
- /**Liest ein Zeichen vom Typ char von der Konsole
- * @return Erstes eingegebene Zeichen vom Typ char.
- * @exception NoSuchElementException:
- * Es wurde keine Eingabezeile gefunden.
- */
- public static char readChar() throws
- NoSuchElementException,IllegalStateException
- {
- String s = new Scanner(System.in).next();
- return s.charAt(0);
- }
- }
- Legen Sie für diese Klasse eine Datei Console.java in dem Ordner inout an und übersetzen Sie diese. In Ihr Programm BMI3 importieren Sie nun dieses Paket:
- Java Symbol
- BMI3
- /************************************************
- Programm zur Berechnung des BMI (Body Mass Index)
- Eingabewerte werden über die Konsole eingelesen
- *************************************************/
- //Importieren des Pakets inout mit der Klasse Console
- import inout.Console;
- public class BMI3
- {
- //Berechnet den BMI
- public static void main (String args[])
- {
- double bmi; //lokale Variablen
- double koerperGewicht = 0.0;
- double koerperGroesse = 0.0;
- System.out.println
- ("Geben Sie bitte Ihr Gewicht in kg ein:");
- //Aufruf der Methode readDoubleComma()
- koerperGewicht = Console.readDoubleComma();
- System.out.println
- ("Geben Sie bitte Ihre Groesse in m ein:");
- //Aufruf der Methode readDoubleComma()
- koerperGroesse = Console.readDoubleComma();
- bmi = koerperGewicht / (koerperGroesse * koerperGroesse);
- System.out.println
- ("Ihr Gewicht von " + koerperGewicht + " kg");
- System.out.println
- ("und Ihre Groesse von " + koerperGroesse + " m");
- System.out.println("ergeben einen BMI von " + bmi);
- System.out.println
- ("Die hoechste Lebenserwartung haben Menschen");
- System.out.println("mit einem BMI zwischen 20 und 24");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement