Methoden Attribut Abfrage eines anderen Objektes

Nik.jar

Mitglied
Hey Leute,
in einem kleinen Projekt für die Schule bräuchte ich mal eure Hilfe:
Ein Objekt weißt ein anderes dazu an, eine Zufallszahl zu Würfeln, dass funktioniert.
Jedoch soll man bei dem ersten Objekt auf diese Zufallszahl zugreifen und sie abfragen können! Sie soll also auch als Attribut bei dem ersten Objekt bereit stehen!
Gemacht wird das ganze in BlueJ.
Vielen Dank für eure Hilfe!
 
/**
* Write a description of class Becher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Becher
{
// instance variables - replace the example below with your own
private int x;

/**
* Constructor for objects of class Becher
*/
public Becher()
{
// initialise instance variables
x = 0;
}

/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @Return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}

public void wirfDieWuerfel(){
gelberWuerfel GelberWuerfel = new gelberWuerfel();
GelberWuerfel.neueZufallszahl();
}

public int getzahl() {
GelberWuerfel.getzahl;
}

}
 
Java:
import java.util.Random;
/**
* Write a description of class gelberWuerfel here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class gelberWuerfel
{
    // instance variables - replace the example below with your own
    private int x;
    Random zufall = new Random();
    public int zahl;
   
    /**
     * Constructor for objects of class gelberWuerfel
     */
    public gelberWuerfel()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }
   
    public void neueZufallszahl(){
        zahl = zufall.nextInt(6) + 1;
    }
   
    public void giveZahl(){
   
    }
}
 
Wie meinst du das?
Habe jetzt nochmal ein bisschen verändert, aber es kommt immer noch nur 0 zurück!

Java:
import java.util.Random;
/**
* Write a description of class Becher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Becher
{
    // instance variables - replace the example below with your own
    private int x;
    private int zahl;

    /**
     * Constructor for objects of class Becher
     */
    public Becher()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }

    public void wirfDieWuerfel(){
        gelberWuerfel GelberWuerfel = new gelberWuerfel();
        GelberWuerfel.neueZufallszahl();
    }
   
    public int getzahl() {
        return zahl;
    }
}
 
Java:
    public void wirfDieWuerfel(){
        gelberWuerfel GelberWuerfel = new gelberWuerfel();
        GelberWuerfel.neueZufallszahl();
    }

Du musst nach sagen das deinem Attribut "zahl" der Klasse Becher der Wert von dem Attribut "zahl" vom Objekt der Klasse GelberWuerfel zugewiesen wird.
zahl = gelberWuerfel.zahl

Anmerkung zu deinem Code: Klassennamen werden in UpperCamelCase gschrieben, alles andere in lowerCamelCase 😉
 
Java:
import java.util.Random;
/**
* Write a description of class Becher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Becher
{
    // instance variables - replace the example below with your own
    private int x;

   
    private String zahl;
    /**
     * Constructor for objects of class Becher
     */
    public Becher()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }

    public void wirfDieWuerfel(){
        gelberWuerfel GelberWuerfel = new gelberWuerfel();
        GelberWuerfel.neueZufallszahl();
    }
   
    public String getzahl(){
        return zahl;
    }
}
Java:
import java.util.Random;
/**
* Write a description of class gelberWuerfel here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class gelberWuerfel
{
    // instance variables - replace the example below with your own
    private int x;
    Random zufall = new Random();
    public int zahl;
    String geben = String.valueOf(zahl);
    /**
     * Constructor for objects of class gelberWuerfel
     */
    public gelberWuerfel()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }
   
    public void neueZufallszahl(){
        zahl = zufall.nextInt(6) + 1;
    }
   
    public String getzahl() {
        return geben;
    }
}
als Antwort kriege ich aber immer noch nur null raus :/
 
Code:
public Class Becher {
  private String zahl;

  public Becher() {

  }

  public void wirfDieWuerfel() {
    GelberWuerfel gelberWuerfel = new GelberWuerfel();
    gelberWuerfel.neueZufallszahl();
    zahl = gelberWuerfel.getZah()l;
  }

  public String getZahl() {
    return zahl;
  }
}



import java.util.Random;
public Class GelberWuerfel {

  Random zufall = new Random;
  public int zahl;
  String geben;

  public GelberWuerfel() {

  }

  public void neueZufallszahl() {
    zahl = zufall.nextInt(6) + 1;
    geben = String.valueOf(zahl);
  }

  public String getZahl() {
    return geben;
  }
}

Ist jetzt nicht getestet, aber ich denke dass sollte so funktionieren
 
Ich hab das jetzt auf dem Smartphone geschrieben, von dem her waren wahrscheinlich schon 2-3 Schreibfehler drin 🙂

Verstehtst du den Code, denn auch? Das ist meiner Meinung nach das wichtigste.
 
Ich hätte noch eine Frage: Wie kann ich Strings zusammefügen? Also ich möchte sie nicht addieren, sondern ich möchte weitere Würfel hinzufügen! Wie kann ich dann mehrere Strings im Ouput durch Kommas trennen?
 
Java:
public String concat (String zahl, String zahlRot){
        return zahl + zahlRot;
    }
habs so probiert, da kommt aber nichts raus, nichtmal null oder so :/
 
Ich hätte noch eine Frage: Wie kann ich Strings zusammefügen?
Entweder durch die Methode "concat" welche dir die Klasse String schon zur Verfügung stellt. Oder mit dem "+" Operator.

Also ich möchte sie nicht addieren, sondern ich möchte weitere Würfel hinzufügen!
Strings können auch nicht addiert werden 😉 .... was meinst du damit weitere Würfel hinzufügen?

Wie kann ich dann mehrere Strings im Ouput durch Kommas trennen?
System.out.println(value1 + "," + value2 + "," value3 + .....);

habs so probiert, da kommt aber nichts raus, nichtmal null oder so :/
Wie rufst du diesen Code auf?
 
Ok Moment, die einzelnen Ergebnisse bekomme ich jetzt endlich 😀
Hab mich falsch ausgedrückt, ich wollte zum einen die einzelnen Ergebnisse und die dann Final addieren.
Das würde also nur gehen, wenn ich die Strings zu ints mache und dann addiere, richtig?
 
Deine Klasse Wuerfel generiert dir ja eine Zufallszahl, diese ist schon "int".
Sprich ich sehe keinen Grund irendwo aus einem String wieder ein int zu parsen 😉

Am besten du zeigst uns den Code wo du jetzt was ausgibst bzw. addieren willst.
 
Java:
 private String zahl;
    private String zahlRot;
    private String zahlBlau;
    private String output = zahl + zahlRot + zahlBlau;
das hab ich jetzt im becher oben geschrieben
 
Ja weil du den String output direkt bei der Deklarierung initialisierst. Zu diesem Zeitpunkt haben die Strings zahl, zahlRot bzw. zahlBlau aber noch keinen Wert sondern sind "null". Daher ist output auch nur "nullnullnull".

Java:
public class TestClass {
   private String zahl;
   private String zahlRot;
   private String zahlBlau;
   private String output = zahl + zahlRot + zahlBlau;
   
   public static void main(String[] args) {
       TestClass tc = new TestClass();
       tc.printData();
       tc.zahl = "ErsterWert";
       tc.zahlRot = "neuer Wert";
       tc.printData();
       tc.output = zahl + zahlRot + zahlBlau;
       tc.printData();       
   }
   
   private void printData() {
       System.out.println(zahl);
       System.out.println(zahlRot);
       System.out.println(zahlBlau);
       System.out.println(output);
   }
 
Java:
import java.util.Random;
/**
* Write a description of class Becher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Becher
{
    // instance variables - replace the example below with your own
    private int x;
    private String zahl;
    private String zahlRot;
    private String zahlBlau;
    private String output;
    /**
     * Constructor for objects of class Becher
     */
    public Becher()
    {
        // initialise instance variables
        x = 0;
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y   a sample parameter for a method
     * @return     the sum of x and y
     */
    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }

    public void wirfDieWuerfel(){
        gelberWuerfel GelberWuerfel = new gelberWuerfel();
        GelberWuerfel.neueZufallszahl();
        zahl = GelberWuerfel.getzahl(); //neu, angucken
       
        roterWuerfel RoterWuerfel = new roterWuerfel();
        RoterWuerfel.neueZufallszahl();
        zahlRot = RoterWuerfel.getzahl();

        blauerWuerfel BlauerWuerfel = new blauerWuerfel();
        BlauerWuerfel.neueZufallszahl();
        zahlBlau = BlauerWuerfel.getzahl();
       
        String output = "zahl" + "zahlRot" + "zahlBlau";
    }   
}
Könnt ihr mir vielleicht nochmal weiterhelfen? D: Ich bekomme immer noch nur null zurück! Eigentlich verwende ich doch die Strings, wenn die nicht mehr null sind! Das sagt auch der debugger! wenn ich bei String output = ... bin, haben alle anderen Strings schon konkrete Zahlen!
Bin zugegeben ein wenig aufgeschmissen :O
 
Du solltest dir wirklich erstmal ein Anfänger Buch zulegen und das von anfang an durcharbeiten. Deine augenblickliche Lernmethode macht so einfach keinen Sinn.
 
Ich bekomme immer noch nur null zurück!
Ich sehe in deinem geposteten Code nirgends wo du überhaupt irgendeinen String zurückgibst. Offenbar fehlen in deinem Post wesentliche Teile.
In wirfDieWürfel deklarierst du eine lokale Variable output, die genauso heisst wie die Instanzvariable, und die auch vom gleichen Typ ist. Wenn du glaubst, damit den Wert der Instanzvariablen zu setzen, irrst du dich. Die Instanzvariable ist auch nach Aufruf der Methode wirfDieWürfel noch null.
Sowas verursacht mir Kopfschmerzen:
Java:
gelberWuerfel GelberWuerfel = new gelberWuerfel();
Bitte, bitte, bitte so schreiben (Klassenname Groß, Variablenname klein):
Java:
GelberWuerfel gelberWuerfel = new GelberWuerfel();
Und statt drei Klassen GelberWuerfel, RoterWuerfel und BlauerWuerfel müsste eine Klasse Wuerfel ausreichen, die über eine Variable "farbe" (von welchem Typ auch immer) hat.
 

Zurück
Oben