Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Postbote
- {
- private String name;
- private String message;
- private Postbote nextPostbote;
- // Konstruktor
- public Postbote(String name)
- {
- this.name = name;
- }
- public Postbote(String name, Postbote nextPostbote )
- {
- this.name = name;
- this.nextPostbote = nextPostbote;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- char[] nameArray = name.toCharArray();
- if(nameArray.length>=2){
- this.name = name;
- }
- else System.out.println("Fehler");
- }
- public String getMessage()
- {
- return message;
- }
- public void setMessage(String message)
- {
- this.message = message;
- }
- public Postbote getNextPostbote()
- {
- return nextPostbote;
- }
- public void setNextPostbote(Postbote nextPostbote)
- {
- this.nextPostbote = nextPostbote;
- }
- // Methode soll die eigene Message auf der Konsole ausgeben;
- public void tellMessage()
- {
- System.out.println(message);
- }
- // Methode ändert die eigene Nachricht und gibt sie an den nächsten Postboten weiter und an den Aufrufer
- public String whisper()
- {
- if(nextPostbote == null) return (message);
- char[] messageArray = message.toCharArray();
- Random rand = new Random();
- int zufallszahl = rand.nextInt(messageArray.length);
- char zeichen = getRandomChar();
- messageArray[zufallszahl] = zeichen;
- String m = new String(messageArray);
- nextPostbote.setMessage(m);
- String r = nextPostbote.whisper();
- return r;
- }
- // Methode generiert die Zufallszahl
- public static char getRandomChar() {
- Random rand = new Random();
- int zufallszahl = rand.nextInt(122);
- System.out.println(zufallszahl + " <- erste Zahl");
- if(zufallszahl <= 31 && zufallszahl >= 26){
- zufallszahl = zufallszahl + 59;
- System.out.println(zufallszahl + " = +59");
- }
- else if(zufallszahl <= 57) {
- zufallszahl = zufallszahl + 65;
- System.out.println(zufallszahl + " = +65");
- }
- else if(zufallszahl < 65 && zufallszahl > 57){
- zufallszahl = zufallszahl + 57;
- System.out.println(zufallszahl + " = +57");
- }
- else if(zufallszahl < 97 && zufallszahl > 90) {
- zufallszahl = zufallszahl + 7;
- System.out.println(zufallszahl + " = +7");
- }
- char zeichen = (char)zufallszahl;
- return zeichen;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement