Lösungsvorschläge - Technische Universität München

Werbung
TECHNISCHE
UNIVERSITÄT
MÜNCHEN
FAKULTÄT FÜR INFORMATIK
Praktikum Grundlagen der Programmierung
Lösungsvorschläge zu Blatt 9
Robert Eigner, Florian Forster
SS 2010
28.06.2010
Praktikum Grundlagen der Programmierung
Aufgabe 43 Exceptions: try – catch – finally (Lösungsvorschlag)
a)
i) EOFException
Endlich!
ENDE
ii) IOException
Endlich!
iii) Endlich!
ENDE
b) Exception
c) Das würde gar nicht kompilieren, da dem Java-Compiler bewusst ist, dass der erste catchBlock schon alle auftretenden Exceptions abhandeln würde, weil er ja schon die allgemeinste Oberklasse Exception behandelt. D. h. die unteren Exceptions wären nicht mehr erreichbar:
Unresolved compilation problems:
Unreachable catch block for IOException. It is already handled by the catch
block for Exception
Unreachable catch block for EOFException. It is already handled by the catch
block for Exception
Unreachable catch block for EOFException. It is already handled by the catch
block for IOException
Aufgabe 44 (Ü)
import
import
import
import
import
import
import
Zeichenhäufigkeiten
java . io . File ;
java . io . FileInputStream ;
java . io . FileNotFoundException ;
java . io . IOException ;
java . util . TreeMap ;
java . util . Iterator ;
java . util . Map ;
public class CharStat {
Lösung 9/ Seite 2
public static void main ( String [] args ) {
String argument = null;
TreeMap < Character , Integer > tm = new TreeMap < Character , Integer >();
try {
argument = args [0];
} catch ( ArrayIndexOutOfBoundsException aioobe ) {
System . out . println (" Kein Argument angegeben !" );
System . exit ( -1);
}
try {
File file = new File ( argument );
FileInputStream fis = new FileInputStream ( file );
int n = 0;
while (( n = fis . read ()) != -1) {
char ch = (char) n;
int value = 0;
if ( tm . get ( ch ) == null) {
value = 1;
} else {
value = tm . get ( ch ) + 1;
}
tm . put (ch , value );
}
} catch ( FileNotFoundException fnfe ) {
System . out . println (" Die Datei " + args [0]
+ " konnte nicht gefunden werden " );
System . exit ( -2);
} catch ( IOException ioe ) {
System . out . println ("IO - Fehler " );
System . exit ( -3);
}
Iterator < Map . Entry < Character , Integer >> it = tm . entrySet (). iterator ();
while ( it . hasNext ()) {
System . out . println ( it . next ());
}
}
}
Das Zeichen ‘t“ kommt 309436 Mal vor.
Aufgabe 45 (Ü)
Client-Server-Kommunikation
import java . io .*;
import java . net .*;
public class Server {
public static void main ( String [] args ) {
Server server = new Server ();
server . listen ();
}
Lösung 9/ Seite 3
public void listen () {
ServerSocket listenSocket ;
Socket workSocket ;
BufferedWriter out ;
BufferedReader in ;
String msg ;
try {
listenSocket = new ServerSocket (9000);
workSocket = listenSocket . accept ();
out = new BufferedWriter (new OutputStreamWriter ( workSocket
. getOutputStream ()));
in = new BufferedReader (new InputStreamReader ( workSocket
. getInputStream ()));
msg = in . readLine ();
out . write (" Hallo " + msg + "\n" );
out . flush ();
in . close ();
out . close ();
workSocket . close ();
listenSocket . close ();
} catch ( IOException ex ) {
ex . printStackTrace ();
}
}
}
import java . io .*;
import java . net .*;
public class Client {
public static void main ( String [] args ) {
Client client = new Client ();
client . connect ( args [0]);
}
public void connect ( String sende ) {
Socket workSocket ;
BufferedWriter out ;
BufferedReader in ;
String receivedMsg ;
try {
workSocket = new Socket (" localhost " , 9000);
out = new BufferedWriter (new OutputStreamWriter ( workSocket
. getOutputStream ()));
in = new BufferedReader (new InputStreamReader ( workSocket
. getInputStream ()));
Lösung 9/ Seite 4
out . write ( sende + "\n" );
out . flush ();
receivedMsg = in . readLine ();
System . out . println ( receivedMsg );
in . close ();
out . close ();
workSocket . close ();
} catch ( IOException ex ) {
ex . printStackTrace ();
}
}
}
Herunterladen