Referring to other classes.
Hello! I have a problem concerning referring to other classes.
I have two classes, one called Blackjack which is the main class and one called Spelerkaarten (which is Dutch for playercards). In this last class I give my card a certain value with an if-statement. Now I would like to use these values in my other class, blackjack. And this is the part where I'm encounter a problem. It seems that what I'm doing doesn't work.
Code:
package blackjack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Blackjack {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
Spelerkaarten kaartenspeler = new Spelerkaarten();
System.out.println("Welkom bij blackjack! Wat is je naam?");
String naam = br.readLine();
System.out.println("Van harte welkom " + naam + " je zal vandaag opnemen tegen onze supercomputer in het spelletje blackjack!");
System.out.println("Hier volgen je eerste drie kaarten!");
System.out.println(kaartenspeler.spelerkaart1);
System.out.println(kaartenspeler.spelerkaart2);
}
}
Code:
package blackjack;
public class Spelerkaarten {
public static void main(String[] args) {
int spelerwaarde1 = (int) Math.floor((Math.random()*1)+13);
int spelerwaarde2 = (int) Math.floor((Math.random()*1)+13);
String spelerkaart1 = "";
String spelerkaart2 = "";
if (spelerwaarde1 < 10) {
spelerkaart1 = "Uw eerste kaart is een " + spelerwaarde1 + "!";
}
if (spelerwaarde1 == 11) {
spelerkaart1 = "Uw eerste kaart is een boer!";
}
if (spelerwaarde1 == 12) {
spelerkaart1 = "Uw eerste kaart is een dame!";
}
if (spelerwaarde1 == 13) {
spelerkaart1 = "Uw eerste kaart is een heer!";
}
if (spelerwaarde2 < 10) {
spelerkaart1 = "Uw tweede kaart is een " + spelerwaarde2 + "!";
}
if (spelerwaarde2 == 11) {
spelerkaart1 = "Uw tweede kaart is een boer!";
}
if (spelerwaarde2 == 12) {
spelerkaart1 = "Uw tweede kaart is een dame!";
}
if (spelerwaarde2 == 13) {
spelerkaart1 = "Uw tweede kaart is een heer!";
}
}
}
Thanks in advance!
Re: Referring to other classes.
That code there, in main(), does nothing with the 'kaartenspeler' (it's a variable, so should start with a lowercase letter) reference.
All it does is create that object, then a couple of ints and then a couple of Strings.
None of them are related at all.
Re: Referring to other classes.
Quote:
Originally Posted by
Tolls
That code there, in main(), does nothing with the 'kaartenspeler' (it's a variable, so should start with a lowercase letter) reference.
All it does is create that object, then a couple of ints and then a couple of Strings.
None of them are related at all.
I'm sorry but I don't really know what you mean. However I noticed I forgot a part of my code, I edited it.
Re: Referring to other classes.
OK, so which of those two main()'s are you actually using as the entry point to your code?
Re: Referring to other classes.
Quote:
Originally Posted by
Tolls
OK, so which of those two main()'s are you actually using as the entry point to your code?
That class Blackjack serves as the main class, this is the class where I want to generate the output. The first three cards I want to let the player draw are calculated in the class Spelerkaarten, and printed in Blackjack. That's why I'd want to take those strings from Spelerkaarten to Blackjack. I hope that awnsers your question.
Re: Referring to other classes.
Except Spelekarten has not attributes, and no methods other than that main() one, which is not how these things are normally written.
Do you know how classes and objects work?
I'm just trying to see where you are in your learning.
Re: Referring to other classes.
Quote:
Originally Posted by
Tolls
Except Spelekarten has not attributes, and no methods other than that main() one, which is not how these things are normally written.
Do you know how classes and objects work?
I'm just trying to see where you are in your learning.
I do not know, no.
Re: Referring to other classes.
Then I'm afraid you're going to need the tutorials.
The basics at the very least.
Re: Referring to other classes.
He's right, you need to start spending as much time as you can on OOP tutorials/books. It's not that you don't use the same programming syntax etc. that you already know, but thinking in objects is a whole different way of programming. You build your programs by creating classes, out of those classes you make objects, which are instances of those classes.
Say you have a "Person" class which has some field values(variables/objects of other classes)like height, weight, eyeColor etc. and methods to get them(called "getters", gets the field values, fx getHeight() ) methods to set them(called setters/mutators, fx setHeight() ) an object of that class would be a person with a certain height etc. this doesn't change the person class at all though, it just makes a "reference" to the person class (the class never changes) only the objects of the class(the object of the class is a "reference" to the class) you make changes to their field values, to reflect various people-objects you made from your person class.
like this:
Code:
Person joe = new Person();
joe.setHeight( 100 );
joe.setWeight( 120 );
joe.setEyeColor( "green" );
now the object Joe is an instance of the Person class, but with his own unique field values. ( a field value is just a variable that's listed in the top of the class above everything else, it can be a primitive type or another class object)
Think of it as creating "organisms/machines" that can do many jobs and contain field values (properties) which can be manipulated.
Anyways to fully understand you need to take the time to read on it alot, read about objects and when you feel you understand, read about inheritance.
Just a beginner myself but this is my advice.
A typical class is set up like this.
Code:
public class Person {
// Field values (properties of each object).
<field value 1>;
<field value 2>;
etc....
// Constructor(s)
public Person() {
}
// more contructors.... if u need them.
// Methods.
public void set<fieldvalue1>( <data type> newValue ) {
this.<field value1> = newValue;
}
// lots of methods, getters and setters etc.
}