Results 1 to 10 of 10
  1. #1
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default Conditions through seperate classes

    Alright, so i've this code where I want to print out 5 cards from a deck and I want to create a condition which says that the cards cannot be the same. I've created a seperate randomizer for each "number" of the card which there is 13 of (ace to king) and for colors, which there is 4 of each.
    The cards will randomize seperately, I made it: System.out.println(Card+Color);

    My problem is that I want to create a condition which says something along these lines:if (Card2==Card&&Color2==Color2){System.out.println(C olor2+Card2} when the 2nd card will be printed out, but how do I do this when they're in seperate classes? I'll show you my code,it's in swedish however so i'm sorry for that... fyi: kort=card, färg=color...


    public class Kort {

    private int kast, kast2;
    private static int valör=13;
    private static int färg =4;
    private String KORT, FÄRG;

    public void blandakort(){

    kast=(int)(Math.random()*valör)+1;

    if (kast==1){
    KORT=Integer.toString(kast);
    KORT="ESS";
    }

    else if (kast==2){KORT="2";}
    else if (kast==3){KORT="3";}
    else if (kast==3){KORT="3";}
    else if (kast==4){KORT="4";}
    else if (kast==5){KORT="5";}
    else if (kast==6){KORT="6";}
    else if (kast==7){KORT="7";}
    else if (kast==8){KORT="8";}
    else if (kast==9){KORT="9";}
    else if (kast==10){KORT="10";}


    else if (kast==11){
    KORT=Integer.toString(kast);
    KORT="knekt";
    }

    else if (kast==12){
    KORT=Integer.toString(kast);
    KORT="DAM";
    }

    else if (kast==13){
    KORT=Integer.toString(kast);
    KORT="KUNG";
    }
    }

    public void blandaFärg(){
    kast2=(int)(Math.random()*färg)+1;

    if (kast2==1){
    FÄRG=Integer.toString(kast2);
    FÄRG="Hjärter";
    }
    else if (kast2==2){
    FÄRG=Integer.toString(kast2);
    FÄRG="Spader";
    }
    else if (kast2==3){
    FÄRG=Integer.toString(kast2);
    FÄRG="Klöver";
    }
    else if (kast2==4){
    FÄRG=Integer.toString(kast2);
    FÄRG="Ruter";
    }
    }

    public void skrivUt(){


    System.out.println(FÄRG+" "+KORT);

    }

    }

    NEXT CARD


    public class Kort2 {

    private int kast, kast2;
    private static int valör=13;
    private static int färg =4;
    private String KORTNr2, FÄRGNr2;

    public void blandakort(){

    kast=(int)(Math.random()*valör)+1;

    if (kast==1){
    KORTNr2=Integer.toString(kast);
    KORTNr2="ESS";
    }

    else if (kast==2){KORTNr2="2";}
    else if (kast==3){KORTNr2="3";}
    else if (kast==3){KORTNr2="3";}
    else if (kast==4){KORTNr2="4";}
    else if (kast==5){KORTNr2="5";}
    else if (kast==6){KORTNr2="6";}
    else if (kast==7){KORTNr2="7";}
    else if (kast==8){KORTNr2="8";}
    else if (kast==9){KORTNr2="9";}
    else if (kast==10){KORTNr2="10";}


    else if (kast==11){
    KORTNr2=Integer.toString(kast);
    KORTNr2="knekt";
    }

    else if (kast==12){
    KORTNr2=Integer.toString(kast);
    KORTNr2="DAM";
    }

    else if (kast==13){
    KORTNr2=Integer.toString(kast);
    KORTNr2="KUNG";
    }
    }

    public void blandaFärg(){
    kast2=(int)(Math.random()*färg)+1;

    if (kast2==1){
    FÄRGNr2=Integer.toString(kast2);
    FÄRGNr2="Hjärter";
    }
    else if (kast2==2){
    FÄRGNr2=Integer.toString(kast2);
    FÄRGNr2="Spader";
    }
    else if (kast2==3){
    FÄRGNr2=Integer.toString(kast2);
    FÄRGNr2="Klöver";
    }
    else if (kast2==4){
    FÄRGNr2=Integer.toString(kast2);
    FÄRGNr2="Ruter";
    }
    }

    public void skrivUt(){

    this is where i cant get the condition to work, i dont want FÄRGNr2 and KORTNr2 to be the same as FÄRG and KORT in the first randomizer.. but if i create this.. if (FÄRGNr2==FÄRG&&KORTNr2==KORT){System.out.println( FÄRGNr2+" "+KORTNr2);} it doesn't seem to find neither KORT or FÄRG because it can't find the value..

    System.out.println(FÄRGNr2+" "+KORTNr2);

    }

    }


    public class KortProgram {

    public static void main (String[] args){

    Kort SpelkortNr1 = new Kort();
    SpelkortNr1.blandakort();
    SpelkortNr1.blandaFärg();
    SpelkortNr1.skrivUt();

    Kort2 SpelkortNr2 = new Kort2();
    SpelkortNr2.blandakort();
    SpelkortNr2.blandaFärg();
    SpelkortNr2.skrivUt();
    }

    }


    I hope I made any sense to you :D

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    As far as I understand your question: stick your cards in a Collection, say an ArrayList, call the Collections.shuffle( ... ) method and display the first five cards in the list.

    kind regards,

    Jos

  3. #3
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    As far as I understand your question: stick your cards in a Collection, say an ArrayList, call the Collections.shuffle( ... ) method and display the first five cards in the list.

    kind regards,

    Jos
    yeah, problem is that i'm not allowed to use array lists on this assigment (school) the things we've gone through is loops, doubleloops, variables, and/if statements and the very basics...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LennyKosmos View Post
    yeah, problem is that i'm not allowed to use array lists on this assigment (school) the things we've gone through is loops, doubleloops, variables, and/if statements and the very basics...
    Well, stick all your cards in an array then and shuffle them yourself; shuffling is a bit tricky but not too difficult; here's a way:

    Java Code:
    Random rnd= new Random();
    for (int i=arr.length; i > 1; i--)
       swap(arr, i-1, rnd.nextInt(i));
    The swap( ... ) method swaps two elements in an array.

    kind regards,

    Jos

  5. #5
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    alright, well check this out:

    Java Code:
    import java.util.Random;
    
    public class Kortlek {
    
    	public static void main (String[] args){
    		
    		Random rnd= new Random();
    		
    		String Kort[];
    		Kort= new String[5];
    		String[] Kort = {"Ace", "King", "Queen", "Knight", "5"};
    		
    		for(int i=Kort.length; i>1; i--);
    		swap(Kort, i-1, rnd.nextInt(i));
    		
    		System.out.println(Kort);
    	}
    }
    it says Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Duplicate local variable Kort
    i cannot be resolved to a variable
    i cannot be resolved to a variable

    at Kortlek.main(Kortlek.java:12)


    i dont even know what this means.. and one more question, do i have to assign a value to every array.. like, do i need to do ace of hearts, ace of spades etc, all 52 cards? :/

  6. #6
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    my god.. i dont have time to do this, ive to do it without making arrays :/

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LennyKosmos View Post
    my god.. i dont have time to do this, ive to do it without making arrays :/
    Why do the creators of those exercises want students to write a form of crippled Java? Any letter combination not allowed for any reason that is beyond me?

    kind regards,

    Jos

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    I think the problem lies in them using Java to teach basic software algorithms/concepts.
    They should use something like Pascal.

  9. #9
    LennyKosmos is offline Member
    Join Date
    Sep 2010
    Posts
    28
    Rep Power
    0

    Default

    well i guess it's because the teacher(s) need to know if the class keeps up with the course... we'll have 3 assigments throughout the course and this one was specifically about variables/selection/loops...but it beats me what's easier and/or what's the best, i'm a newbie! i do what i'm told especially if there's a deadline :)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    I think the problem lies in them using Java to teach basic software algorithms/concepts.
    They should use something like Pascal.
    Yep, and I think that the "requirement" (mind the quotes) that no arrays should be used, is over the edge. I can come up with a base 52 encoding of a huge number representing a deck of cards but that is also highly artificial and over the edge as well. I can't properly answer those questions.

    kind regards,

    Jos

Similar Threads

  1. while and proper conditions for if
    By Saletra in forum New To Java
    Replies: 11
    Last Post: 08-25-2010, 10:37 AM
  2. trouble validating 2 conditions
    By cottoneye1256 in forum New To Java
    Replies: 2
    Last Post: 08-23-2010, 06:53 PM
  3. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 05:53 AM
  4. [SOLVED] if else conditions not working. T_T
    By uncanny in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •