-4

I have 2 class files. One has the get/set statements and I need to write the code to use these get/set statements on another class file in the same project. How can I do that?

Also, I want 2 random numbers as an output but I get only 1.

Card class:

public class Card{

//variables
public  int spades = 0;
public  int hearts = 1;
public  int diamonds = 2;
public  int clubs = 3;

public  int ace = 1;
public  int jack = 11;
public  int queen = 12;
public  int king = 13;

 private String rank;
 private String suit;


 //set methods
public void setSpades(String s){
    spades = 0;
}
public void setHearts(int h){
    hearts = 1;
}
public void setDiamonds(int d){
    diamonds = 2;
}
public void setClubs(int c){
    clubs = 3;
}

//get methods
public int getSpades(){
    return spades;
}
public int getHearts(){
    return hearts;
}
public int getDiamonds(){
    return diamonds;
}
public int getClubs(){
    return clubs;
}



public String getSuit() {
    return suit;
}
public void setSuit(String suit) {
    this.suit = suit;
}
public String getRank() {
    return rank;
}
public void setRank(String rank) {
    this.rank = rank;
}
}//end class

The second file:

public class PickTwoCards {

public static void main(String[] args){

     Card Object = new Card();
     Card Object2 = new Card();




     int ranSuit = (int)(Math.random()*10);
     System.out.println(ranSuit);       
}

public void Card (int theValue, int theSuit, int suit, int value){
    value = theValue;
    suit= theSuit;

}




}//End class PickTwocCards
ultra99
  • 413
  • 1
  • 7
  • 11
  • 4
    Please provide more details, and preferably a code example. Where is the problem of just calling the getters and setters? And what do you mean by 'class file'? A class definition? – Chris Feb 26 '12 at 12:18
  • see [this](http://www.google.co.in/#hl=en&site=&q=using+getter+and+setter+methods+in+java&oq=using+getter+&aq=0&aqi=g1g-v9&aql=&gs_sm=3&gs_upl=426l5602l0l6729l20l20l2l1l1l1l222l1513l10.6.1l17l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=62b6c57602795a94&biw=1024&bih=629) & [this](http://stackoverflow.com/search?q=getter+setter+[java]&submit=search) – Fahim Parkar Feb 26 '12 at 12:21
  • please learn java naming conventions and stick to them – kleopatra Feb 26 '12 at 13:37

1 Answers1

2

Let's say that you have the following setter and getter methods in your first class:

public void setName(String name) {
   this.name = name;
}

public String getName() {
   return name;
}

from the other class, you would call setName with yourObject.getName() once you've instantiated.

Keep in mind, however, that beyond basic functions, getters and setters are considered bad:

Are getters and setters poor design? Contradictory advice seen

Allen Holub wrote "You should never use get/set functions", is he correct?

Community
  • 1
  • 1
pland
  • 858
  • 1
  • 9
  • 17