import java util Hash Set import java util

  • Slides: 50
Download presentation

import java. util. Hash. Set; import java. util. Scanner; public class Hash. Set. Example

import java. util. Hash. Set; import java. util. Scanner; public class Hash. Set. Example { public static void main(String[] args){ Hash. Set<String> my. Set = new Hash. Set<String>(); Scanner input = new Scanner(System. in); while(input. has. Next()){ String name = input. next(); if (!my. Set. contains(name)){ my. Set. add(name); } } for(String name: my. Set){ System. out. println(name); } Τοποθετούμε στο Hash. Set μόνο τα Strings τα οποία δεν έχουμε ήδη δει (δεν είναι ήδη στο σύνολο) Ένας τρόπος για να διατρέξουμε και να τυπώσουμε τα στοιχεία του Hash. Set Object[] array = my. Set. to. Array(); for (int i = 0; i < array. length; i ++){ String name = (String)array[i]; System. out. println(name); } } } Δήλωση μιας μεταβλητής Hash. Set από Strings. Ένας άλλος τρόπος για να διατρέξουμε το Hash. Set χρησιμοποιώντας την εντολή to. Array(). Ο πίνακας είναι πίνακας από Objects, και πρέπει να κάνουμε downcasting

import java. util. Hash. Map; import java. util. Scanner; Δήλωση μιας μεταβλητής Hash. Map

import java. util. Hash. Map; import java. util. Scanner; Δήλωση μιας μεταβλητής Hash. Map που συσχετίζει Strings (κλειδια) και Integers (τιμές) Για κάθε όνομα (String) το id (Integer) public class Hash. Map. Example 1 { public static void main(String[] args){ Hash. Map<String, Integer> my. Map = new Hash. Map<String, Integer>(); Scanner input = new Scanner(System. in); int counter = 0; while(input. has. Next()){ String name = input. next(); if (!my. Map. contains. Key(name)){ my. Map. put(name, counter); counter ++; } } Διατρέχοντας το Hash. Map Αν το όνομα δεν είναι ήδη στο Hash. Map τότε ανάθεσε στο όνομα αυτό τον επόμενο αύξοντα αριθμό και πρόσθεσε ένα νέο ζευγάρι (όνομα αριθμός) στο Hash. Map. Διέτρεξε το σύνολο με τα κλειδιά (ονόματα) στο Hash. Map for(String name: my. Map. key. Set()){ System. out. println(name + ": "+ my. Map. get(name)); } } } Για κάθε κλειδί (όνομα) πάρε το id που αντιστοιχεί στο όνομα αυτό και τύπωσε το.

import java. util. Hash. Map; import java. util. Scanner; public class Hash. Map. Example

import java. util. Hash. Map; import java. util. Scanner; public class Hash. Map. Example 2 { public static void main(String[] args){ Hash. Map<String, Person> my. Map = new Hash. Map<String, Person>(); Scanner input = new Scanner(System. in); int counter = 0; while(input. has. Next()){ String name = input. next(); if (!my. Map. contains. Key(name)){ Person p = new Person(name, counter); my. Map. put(name, p); counter ++; Δημιουργούμε ένα Hash. Map το οποίο } σε κάθε διαφορετικό όνομα αντιστοιχεί } ένα αντικείμενο Person. for(String name: my. Map. key. Set()){ System. out. println(my. Map. get(name)); } } } Καλείται η to. String της κλάσης Person

import java. util. Hash. Set; import java. util. Iterator; import java. util. Scanner; public

import java. util. Hash. Set; import java. util. Iterator; import java. util. Scanner; public class Iterator. Example { public static void main(String[] args){ Hash. Set<String> my. Set = new Hash. Set<String>(); Scanner input = new Scanner(System. in); while(input. has. Next()){ if (!my. Set. contains(name)){ my. Set. add(input. next()); } } Διατρέχει το σύνολο και αν βρει το Iterator it = my. Set. iterator(); String “a” το αφαιρεί από το σύνολο. while (it. has. Next()){ if (it. next(). equals("a")){ it. remove(); Πρέπει να κάνουμε break γιατί αν break; αφαιρέσουμε μία τιμή ενώ διατρέχουμε το } σύνολο μπορεί να προκληθεί λάθος. } it = my. Set. iterator(); Ξανα-διατρέχουμε τον πίνακα. while (it. has. Next()){ Ο iterator πρέπει να ξανα-οριστεί για System. out. println(it. next()); να ξεκινήσει από την αρχή του } } } συνόλου.

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[] 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. "); } } χωρίς εξαιρέσεις

Μηχανισμός try-throw-catch • Ο κώδικας που μπορεί να δημιουργήσει εξαίρεση μπαίνει σε ένα try-block

Μηχανισμός try-throw-catch • Ο κώδικας που μπορεί να δημιουργήσει εξαίρεση μπαίνει σε ένα try-block • Αν η εξέλιξη του κώδικα είναι προβληματική εκτελείται η εντολή throw η οποία «πετάει» την εξαίρεση • Αν υπάρξει εξαίρεση η ροή του κώδικα μεταφέρεται στο catch-block το οποίο χειρίζεται τις εξαιρέσεις try { if (men == 0 && women == 0) throw new Exception("No students. Νο Lesson. "); else if (men == 0) throw new Exception("No men. Νο Lesson. "); else if (women == 0) throw new Exception("No women. Νο Lesson. "); 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); }

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[] 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.

Παράδειγμα 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

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

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.

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.

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

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 > 2013) 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( ) {

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 =

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

import java. util. Scanner; public class Division. Demo. Second. Version { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); try { System. out. println("Enter numerator: "); int numerator = keyboard. next. Int(); System. out. println("Enter denominator: "); int denominator = keyboard. next. Int(); Εφόσον έχουμε μία μέθοδο που πετάει εξαίρεση, θα πρέπει να τη βάλουμε μέσα σε try-catch block 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

import java. util. Scanner; public class Division. Demo. Second. Version { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); try { System. out. println("Enter numerator: "); int numerator = keyboard. next. Int(); System. out. println("Enter denominator: "); int denominator = keyboard. next. Int(); Εφόσον έχουμε δεν πετάει εξαίρεση, θα πρέπει να τη βάλουμε μέσα σε trycatch block 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); } System. out. println("End of program. "); } Η safe. Percentage δεν χρειάζεται try-catch block γιατί πετάει κι αυτή την εξαίρεση της safe. Divide (declare) 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.

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(); done = true; } catch(Input. Mismatch. Exception e) { keyboard. next. Line(); System. out. println("Not a correctly written whole number. "); System. out. println("Try again. "); } } System. out. println("You entered " + number); } }