Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Postbote
  4.     {
  5.         private String name;
  6.         private String message;
  7.         private Postbote nextPostbote;
  8.  
  9.        
  10.        
  11.         // Konstruktor
  12.         public Postbote(String name)
  13.         {
  14.             this.name = name;
  15.         }
  16.         public Postbote(String name, Postbote nextPostbote )
  17.         {
  18.             this.name = name;
  19.             this.nextPostbote = nextPostbote;
  20.         }
  21.  
  22.         public String getName()
  23.         {
  24.             return name;
  25.         }
  26.        
  27.         public void setName(String name)
  28.         {
  29.             char[] nameArray = name.toCharArray();
  30.             if(nameArray.length>=2){
  31.                 this.name = name;
  32.             }
  33.             else System.out.println("Fehler");
  34.         }
  35.        
  36.         public String getMessage()
  37.         {
  38.             return message;
  39.         }
  40.        
  41.         public void setMessage(String message)
  42.         {
  43.             this.message = message;
  44.         }  
  45.        
  46.         public Postbote getNextPostbote()
  47.         {
  48.             return nextPostbote;
  49.         }  
  50.        
  51.         public void setNextPostbote(Postbote nextPostbote)
  52.         {
  53.             this.nextPostbote = nextPostbote;
  54.         }
  55.        
  56.         // Methode  soll die eigene Message auf der Konsole ausgeben;
  57.         public void tellMessage()
  58.         {
  59.             System.out.println(message);
  60.         }  
  61.        
  62.         // Methode ändert die eigene Nachricht und gibt sie an den nächsten Postboten weiter und an den Aufrufer  
  63.         public String whisper()
  64.         {
  65.             if(nextPostbote == null) return (message);
  66.             char[] messageArray = message.toCharArray();       
  67.             Random rand = new Random();
  68.             int zufallszahl = rand.nextInt(messageArray.length);
  69.             char zeichen = getRandomChar();
  70.             messageArray[zufallszahl] = zeichen;
  71.             String m = new String(messageArray);
  72.             nextPostbote.setMessage(m);
  73.             String r = nextPostbote.whisper();
  74.            
  75.             return r;
  76.         }
  77.        
  78.         // Methode generiert die Zufallszahl       
  79.         public static char getRandomChar() {
  80.             Random rand = new Random();
  81.             int zufallszahl = rand.nextInt(122);
  82.             System.out.println(zufallszahl + " <- erste Zahl");
  83.             if(zufallszahl <= 31 && zufallszahl >= 26){
  84.                 zufallszahl = zufallszahl + 59;
  85.                 System.out.println(zufallszahl + " = +59");
  86.             }
  87.             else if(zufallszahl <= 57) {
  88.                
  89.                 zufallszahl = zufallszahl + 65;
  90.                 System.out.println(zufallszahl + " = +65");
  91.             }
  92.             else if(zufallszahl < 65 && zufallszahl > 57){
  93.                 zufallszahl = zufallszahl + 57;
  94.                 System.out.println(zufallszahl + " = +57");
  95.             }
  96.             else if(zufallszahl < 97 && zufallszahl > 90) {
  97.                 zufallszahl = zufallszahl + 7;
  98.                 System.out.println(zufallszahl + " = +7");
  99.             }
  100.             char zeichen = (char)zufallszahl;          
  101.             return zeichen;
  102.         }
  103.        
  104.    
  105.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement