Einführung
in die objektorientierte Programmierung
Einsendeaufgabe 1 (Sockets)
Server
import java.io.*;
import java.net.*;
public class ZaehlerServer {
static int counter=0;
public static void main(String[] argv) {
try {
while (true) {
ServerSocket serverSocket = new ServerSocket(1234);
Socket clientSocket = serverSocket.accept();
OutputStream socketoutstr = clientSocket.getOutputStream();
OutputStreamWriter osr = new OutputStreamWriter( socketoutstr );
BufferedWriter bw = new BufferedWriter( osr );
InputStream socketinstr = clientSocket.getInputStream();
InputStreamReader isr = new InputStreamReader( socketinstr );
BufferedReader br = new BufferedReader( isr );
String anfrage;
String antwort;
anfrage = br.readLine();
if (anfrage.equals("<")) {
counter--;
antwort= ""+counter;
} // end of if
else if (anfrage.equals(">")) {
counter++;
antwort= ""+counter;
} // end of if-else
else if (anfrage.equals("GET")) {
antwort= ""+counter;
} // end of if-else
antwort = ""+counter;
bw.write(antwort);
bw.newLine();
bw.flush();
bw.close();
br.close();
clientSocket.close();
serverSocket.close();
} // end of while
}
catch (UnknownHostException uhe) {
System.out.println(uhe);
}
catch (IOException ioe) {
System.out.println(ioe);
}
} // ende: main
} // Ende: public class MeinEchoServer
Client
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
/**
*
* Beschreibung
*
* @version 1.0 vom 01.07.2015
* @author
*/
public class Zaehler extends Frame {
// Anfang Attribute
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private TextField textField1 = new TextField();
// Ende Attribute
public Zaehler(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { dispose(); }
});
int frameWidth = 390;
int frameHeight = 189;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Panel cp = new Panel(null);
add(cp);
// Anfang Komponenten
button1.setBounds(64, 32, 33, 33);
button1.setLabel("<");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button1_ActionPerformed(evt);
}
});
cp.add(button1);
button2.setBounds(232, 32, 41, 33);
button2.setLabel(">");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button2_ActionPerformed(evt);
}
});
cp.add(button2);
button3.setBounds(64, 80, 209, 41);
button3.setLabel("quit");
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
button3_ActionPerformed(evt);
}
});
cp.add(button3);
textField1.setBounds(120, 32, 81, 33);
cp.add(textField1);
// Ende Komponenten
setVisible(true);
kommando("GET");
} // end of public Zaehler
// Anfang Methoden
public void button1_ActionPerformed(ActionEvent evt) {
kommando("<");
} // end of button1_ActionPerformed
public void button2_ActionPerformed(ActionEvent evt) {
kommando(">");
} // end of button2_ActionPerformed
public void kommando(String befehl){
try{
String host = "localhost";
Socket echoSocket = new Socket(host,1234);
OutputStream socketoutstr = echoSocket.getOutputStream();
OutputStreamWriter osr = new OutputStreamWriter( socketoutstr );
BufferedWriter bw = new BufferedWriter( osr );
InputStream socketinstr = echoSocket.getInputStream();
InputStreamReader isr = new InputStreamReader( socketinstr );
BufferedReader br = new BufferedReader( isr );
String anfrage = befehl;
String antwort;
bw.write(anfrage);
bw.newLine();
bw.flush();
antwort = br.readLine();
textField1.setText(antwort);
bw.close();
br.close();
echoSocket.close();
}
catch (UnknownHostException uhe) {
System.out.println(uhe);
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
public void button3_ActionPerformed(ActionEvent evt) {
System.exit(0);
} // end of button3_ActionPerformed
// Ende Methoden
public static void main(String[] args) {
new Zaehler("Zaehler");
} // end of main
} // end of class Zaehler
Hauptklausur 16 Aufg 2
public class Kommunikation{
public static void main(String[] args){
Handy handy = new Handy();
Tablet tablet=new Tablet();
SMS sms = new SMS();
EMail eMail = new EMail();
Textnachricht<?> nachricht = sms;
nachricht = eMail;
sms.versendeMit(handy);
eMail.versendeMit(tablet);
Handy smsSender = sms.womitVersendet();
Tablet eMailSender = eMail.womitVersendet();
System.out.println(smsSender);
System.out.println(eMailSender);
}
}
interface Kommunikationsgeraet{
}
class Handy implements Kommunikationsgeraet{
}
class Tablet implements Kommunikationsgeraet{
}
class Textnachricht<T extends Kommunikationsgeraet>{
T sender;
public void versendeMit(T geraet){
sender=geraet;
}
public T womitVersendet(){
return sender;
}
}
class SMS extends Textnachricht<Handy>{
}
class EMail extends Textnachricht<Tablet>{
}
Hauptklausur 16 Aufg 3
public class Kommunikation{
public static void main(String[] args){
Smartphone smartphone = new Smartphone();
smartphone.entsperre(1234);
try{
smartphone.versendeSms("Hallo Welt!");
}
catch (NichtVerbundenException e){
System.out.println("Smartphone ist gesperrt");
}
//
}
}
class NichtVerbundenException extends Exception{
}
class Smartphone{
boolean pinGueltig;
void entsperre(Integer pinCode){
pinGueltig= true;
}
public void versendeSms(String nachricht) throws NichtVerbundenException{
if (!pinGueltig) {
throw new NichtVerbundenException();
} // end of if
else {
System.out.println("Nachricht wird versendet.");
} // end of if-else
}
}
Hauptklausur 16 Aufg 5
import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;
public class SimpleServer {
public static void main(String[] args) throws IOException{
ServerSocket server = new ServerSocket(4242);
while (true) {
Socket socket = server.accept();
socket.getOutputStream().write("Hallo!".getBytes());
socket.close();
} // end of while
}
}
import java.net.*;
import java.io.*;
public class SimpleClient{
public static void main(String[] args) throws IOException{
Socket socket= new Socket("127.0.0.1",4242);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
System.out.println(bufferedReader.readLine());
socket.close();
}
}
Hauptklausur 13 Aufg 7
public class WiedergabegeraetDemo2 {
public static void main(String[] args) {
Wiedergabegeraet<Cd> player = new Wiedergabegeraet<Cd>();
Cd cd = new Cd();
player.mediumEinlegen(cd);
player.alleSpielen();
Cd medium = player.mediumEntnehmen();
}
}
class Wiedergabegeraet<T extends Hoerbar>{
T medium;
void mediumEinlegen(T medium){
this.medium=medium;
System.out.println(medium + " ist eingelegt");
}
T mediumEntnehmen(){
T ret=medium;
medium = null;
System.out.println(ret + " wird ausgeworfen");
return ret;
}
void alleSpielen(){
for (int i=1;i<=medium.anzahlLieder() ;i++ ) {
medium.liedSpielen(i);
} // end of for
}
}
interface Hoerbar{
void liedSpielen(int liedNr);
int anzahlLieder();
}
class Cd implements Hoerbar {
public void liedSpielen(int liedNr) {
System.out.println("Lied "+liedNr);
}
public int anzahlLieder() { /*...*/ return 42; }
}