import java util Scanner public class Dance Lesson



![import java. util. Scanner; Υλοποίηση public class Dance. Lesson { public static void main(String[] import java. util. Scanner; Υλοποίηση public class Dance. Lesson { public static void main(String[]](http://slidetodoc.com/presentation_image/fb10808340de0c3aa4ed9efd469e844b/image-4.jpg)
import java. util. Scanner; Υλοποίηση public class Dance. Lesson { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in ); System. out. println("Enter number of male and female dancers: "); int men = keyboard. next. Int (); int women = keyboard. next. Int(); if (men == 0 && women == 0 ){ System. out. println("Lesson is canceled. No students. "); System. exit(0); }else if (men == 0){ System. out. println("Lesson is canceled. No men. "); System. exit(0); }else if (women == 0){ System. out. println("Lesson is canceled. No women. "); System. exit(0); } if (women >= men) System. out. println("Each man must dance with " + women/(double)men + " women. "); else System. out. println("Each woman must dance with " + men/(double)women + " men. "); System. out. println("Begin the lesson. "); } } χωρίς εξαιρέσεις







import java. util. Scanner; Υλοποίηση public class Dance. Lesson 2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in ); System. out. println("Enter number of male and female dancers: "); int men = keyboard. next. Int (); int women = keyboard. next. Int(); try{ if (men == 0 && women == 0) throw new Exception("Lesson is canceled. No students. "); else if (men == 0) throw new Exception("Lesson is canceled. No men. "); else if (women == 0) throw new Exception("Lesson is canceled. No women. "); if (women >= men) System. out. println("Each man must dance with " + women/(double)men + " women. "); else System. out. println("Each woman must dance with " + men/(double)women + " men. "); } catch(Exception e ){ String message = e. get. Message( ); System. out. println(message); System. exit(0); } System. out. println("Begin the lesson. "); } } με εξαιρέσεις



Παράδειγμα public class Division. By. Zero. Exception extends Exception { public Division. By. Zero. Exception( ) { super("Division by Zero!"); } public Division. By. Zero. Exception(String message) { super(message); } } Η κλάση κληρονομεί και την μέθοδο get. Message()

import java. util. Scanner; public class Division. Demo. First. Version { public static void main(String[] args) { try { Scanner keyboard = new Scanner(System. in); System. out. println("Enter numerator: "); int numerator = keyboard. next. Int(); System. out. println("Enter denominator: "); int denominator = keyboard. next. Int(); if (denominator == 0) throw new Division. By. Zero. Exception( ); double quotient = numerator/(double)denominator; System. out. println(numerator + "/" + denominator + " = " + quotient); } catch(Division. By. Zero. Exception e) { System. out. println(e. get. Message( )); system. Exit(0); } System. out. println("End of program. "); } }

import java. util. Scanner; public class Division. Demo. First. Version { public static void main(String[] args) { try { Scanner keyboard = new Scanner(System. in); System. out. println("Enter numerator: "); int numerator = keyboard. next. Int(); System. out. println("Enter denominator: "); int denominator = keyboard. next. Int(); if (denominator == 0) throw new Division. By. Zero. Exception( ); double quotient = numerator/(double)denominator; System. out. println(numerator + "/" + denominator + " = " + quotient); } catch(Division. By. Zero. Exception e) { System. out. println(e. get. Message( )); second. Chance( ); } System. out. println("End of program. "); } } Μπορούμε μέσα στο catch block να καλούμε μία άλλη μέθοδο

public static void second. Chance( ) { Scanner keyboard = new Scanner(System. in); System. out. println("Try again: "); System. out. println("Enter numerator: "); int numerator = keyboard. next. Int(); System. out. println("Enter denominator: "); System. out. println("Be sure the denominator is not zero. "); int denominator = keyboard. next. Int(); if (denominator == 0) { System. out. println("I cannot do division by zero. "); System. out. println("Aborting program. "); System. exit(0); } double quotient = ((double)numerator)/denominator; System. out. println(numerator + "/" + denominator + " = " + quotient); } }



public class Bad. Number. Exception extends Exception { private int bad. Number; public Bad. Number. Exception(int number) { super("Bad. Number. Exception"); bad. Number = number; } public Bad. Number. Exception( ) { super("Bad. Number. Exception"); } public Bad. Number. Exception(String message) { super(message); } public int get. Bad. Number( ) { return bad. Number; } }

import java. util. Scanner; public class Bad. Number. Exception. Demo { public static void main(String[] args) { try { Scanner keyboard = new Scanner(System. in); System. out. println("Enter year of birth: "); int input. Number = keyboard. next. Int(); if (input. Number > 2014) throw new Bad. Number. Exception(input. Number); System. out. println("Thank you for entering " + input. Number); } catch(Bad. Number. Exception e) { System. out. println(e. get. Bad. Number( ) + " is not valid. "); } System. out. println("End of program. "); } } Μας επιστρέφει τον αριθμό που προκάλεσε την εξαίρεση


public class Negative. Number. Exception extends Exception { public Negative. Number. Exception( ) { super("Negative Number Exception!"); } public Negative. Number. Exception(String message) { super(message); } }

try { System. out. println("How many pencils do you have? "); int pencils = keyboard. next. Int(); if (pencils < 0) throw new Negative. Number. Exception("pencils"); System. out. println("How many erasers do you have? "); int erasers = keyboard. next. Int(); double pencils. Per. Eraser; if (erasers < 0) throw new Negative. Number. Exception("erasers"); else if (erasers != 0) pencils. Per. Eraser = pencils/(double)erasers; else throw new Division. By. Zero. Exception( ); System. out. println("Each eraser must last through " + pencils. Per. Eraser + " pencils. "); } catch(Negative. Number. Exception e) { System. out. println("Cannot have a negative number of " + e. get. Message( )); } catch(Division. By. Zero. Exception e) { System. out. println("Do not make any mistakes. "); }





import java. util. Scanner; public class Division. Demo. Second. Version { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); Εφόσον έχουμε μία μέθοδο που πετάει εξαίρεση, πρέπει να τη βάλουμε μέσα σε try-catch block try { System. out. println("Enter numerator, denominator : "); int numerator = keyboard. next. Int (); int denominator = keyboard. next. Int(); double quotient = safe. Divide(numerator, denominator); System. out. println(numerator + "/" + denominator + " = " + quotient); } catch(Division. By. Zero. Exception e) { System. out. println(e. get. Message( )); System. exit(0); } System. out. println("End of program. "); } Η εξαίρεση δημιουργείται στην safe. Divide αλλά την πιάνουμε και την χειριζόμαστε στην main public static double safe. Divide(int top, int bottom) throws Division. By. Zero. Exception { if (bottom == 0) throw new Division. By. Zero. Exception( ); return top/(double)bottom; } }


import java. util. Scanner; public class Division. Demo. Second. Version { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); Εφόσον η main δεν πετάει εξαίρεση, θα πρέπει να βάλουμε την κλήση της safe. Percentageμέσα σε try-catch block try { System. out. println("Enter numerator, denominator : "); int numerator = keyboard. next. Int (); int denominator = keyboard. next. Int(); int percentage = safe. Percentage(numerator, denominator); System. out. println("percentage = " + percentage +"%"); } catch(Division. By. Zero. Exception e) { System. out. println(e. get. Message( )); System. exit(0); } } Η safe. Percentage δεν χρειάζεται try-catch block γιατί πετάει κι αυτή την εξαίρεση της safe. Divide (declare). Αλλιώς θα είχαμε compile error. public static int safe. Percentage(int top, int bottom) throws Division. By. Zero. Exception { double ratio = safe. Divide(top, bottom); return (int)(ratio*100); } public static double safe. Divide(int top, int bottom) throws Division. By. Zero. Exception { if (bottom == 0) throw new Division. By. Zero. Exception( ); return top/(double)bottom; } }


import java. util. Scanner; import java. util. Input. Mismatch. Exception; public class Input. Mismatch. Exception. Demo { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); int number = 0; //to keep compiler happy boolean done = false; Αν και δεν είναι απαραίτητο μπορούμε να πιάσουμε ένα Runtime. Exception. Στο παράδειγμα αυτό χρησιμοποιούμε το Input. Mismatch. Exception για να δημιουργήσουμε ένα βρόχο μέχρι να δοθεί το σωστό input while (! done) { try { System. out. println("Enter a whole number: "); number = keyboard. next. Int(); done = true; H εξαίρεση δημιουργείται } catch(Input. Mismatch. Exception e) την μέθοδο next. Int() { keyboard. next. Line(); System. out. println("Not a correctly written whole number. "); System. out. println("Try again. "); } } από System. out. println("You entered " + number); } } To Input. Mismatch. Exception είναι υπάρχουσα Runtime. Exception της Java

import java. util. Scanner; import java. util. Input. Mismatch. Exception; public class Input. Mismatch. Exception. Demo { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); int number = 0; //to keep compiler happy boolean done = false; while (! done) { try { System. out. println("Enter a whole number: "); number = keyboard. next. Int(); Άλλος τρόπος να κάνουμε break; } τον ίδιο κώδικα catch(Input. Mismatch. Exception e) χρησιμοποιώντας την break. { keyboard. next. Line(); System. out. println("Not a correctly written whole number. "); System. out. println("Try again. "); } } System. out. println("You entered " + number); } }



- Slides: 37