Results 1 to 14 of 14
Like Tree2Likes
  • 2 Post By Junky

Thread: Help with Inheritance and Static Methods

  1. #1
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Help with Inheritance and Static Methods

    I have three classes. Two of these classes are a child and parent. I would like to inherit static methods from the parent to the child class.
    I would like to use a String called diamonds in the method in the Diamonds class, and a String called clubs in the Clubs class. Also, could someone please tell me if my access modifiers are correct? Thanks in advance.

    Is this possible? If not, what would be a better way to achieve this?

    Java Code:
    class DisplayPoints
    {
      String clubs = "248";
      String diamonds = "352";
      
      int pointTotal = (Clubs.getPoints()) + (Diamonds.getPoints());
      System.out.println(pointTotal);
    }
    
    class Clubs
    {
      private String clubs = "";
      protected static int total = 0;
      
      public Clubs(String c)
      {
        c = clubs;
        points(clubs);
      }
      
      
      public static void points(String clubs)
      {
        for (int i = 0; i < clubs.length();i++)
        {
          if (clubs.charAt(i) == '2')
          {
            total+=1;
          }
        }
      }
      
      static int getPoints()
      {
        return total;
      }
      
    }
    
    class Diamonds extends Clubs
    {
      //everything is the same, but String diamonds instead of String clubs
      private static String diamonds="";
      protected static int total = 0;
      
      public Diamonds()
      {
        super();
        points(diamonds);
      }
      
      public static void points()
      {
        super.points(diamonds);
      }
      static int getPoints()
      {
        super();
      }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    In my opinion your design is flawed. Java is an Object Orientated language. Making everything static goes against that principle. I can have a Beagle class extend Dog because a beagle IS A dog. However a diamond IS NOT a club so why do you have Diamond extending Club. A better design would be to have an abstract Suit class and then have Diamond, Club, Heart and Spade all extend Suit.
    Fubarable and Singing Boyo like this.

  3. #3
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    I think I'll do that instead.
    Thanks for the advice.

  4. #4
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    Sorry for the double-post.
    I have been working on a GUI program that will take an input of a hand of cards in bridge and count the points the hand is worth, then sort the cards.
    I've been doing research, but I still don't understand how this is supposed to work.

    Please read the comments for more details. I also included comments of the errors I am getting.

    Here is the console output:

    6 errors and 1 warning found:
    --------------
    *** Errors ***
    --------------
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 89]
    Error: input cannot be resolved
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 102]
    Error: cardsC cannot be resolved to a variable
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 105]
    Error: outClub cannot be resolved
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 105]
    Error: clubHand cannot be resolved to a variable
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 161]
    Error: Invalid character constant
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 207]
    Error: Invalid character constant
    -------------
    ** Warning **
    -------------
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 18]
    Warning: The serializable class BridgeGUI does not declare a static final serialVersionUID field of type long


    Java Code:
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    
    public class BridgePoints
    {
      public static void main (String [] args)
      {
        BridgeGUI bridgeApp = new BridgeGUI();
        bridgeApp.setSize(500,500);
        bridgeApp.setResizable(false);
        bridgeApp.setVisible(true);
      }
    }
    
    class BridgeGUI extends JFrame implements ActionListener
    {
      //main gui, with input text field and output field
      JLabel in = new JLabel("Input hand:");
      JTextField input = new JTextField(26);
      JPanel inputPanel = new JPanel();
      
      JLabel outC = new JLabel("Clubs");
      JLabel outT = new JLabel("Total");
      
      JTextField outClub = new JTextField();
      JTextField outTotal = new JTextField();
      JPanel outputPanel= new JPanel();
      
      JButton runC = new JButton("Clubs");//button to run the Club class
      ButtonGroup runB = new ButtonGroup();
      
      public BridgeGUI()
      {
        setTitle("Bridge Point Counter");
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        input.addActionListener(this);
        runC.addActionListener(new ClubPoints());
        outClub.setEditable(false);
        
        inputPanel.add(in);
        inputPanel.add(input);
        runB.add(runC);
        inputPanel.add(runC);
    
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));
        
        outputPanel.add(outC);
        outputPanel.add(outClub);
        outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.X_AXIS));
        
        add( inputPanel, BorderLayout.NORTH );
        add (outputPanel,BorderLayout.CENTER);
    
      }
      
      public void actionPerformed(ActionEvent evt)
      {
       //
      }
      
    }//end main GUI class
      
      abstract class Suits
      {
        String clubs;
        String diamonds;
        String hearts;
        String spades;
        int pointTotal;
        String [] card;
        int [] cards;
        
        abstract void actionPerformed(ActionEvent evt);
        abstract void countPoints();
        abstract void prepareCards();
        abstract int sortCards(int [] a);
        abstract void aString();
      }
      
      class ClubPoints extends Suits implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          //this class will work with club cards in the input
          StringBuffer club =  new StringBuffer();
          String userIn = input.getText();//input cannot be resolved
          
    //this for loop will look for characters followed by a C in the input
    //if a character is followed by a 'C', is is a club
          for (int i = 0; i < userIn.length();i+=2)
          {
            if (userIn.charAt(i + 1) == 'C')
            {
              club.append(userIn.charAt(i) + " ");
            }
          }
          
          String clubs = club.toString();
          countPoints();
          prepareCards();
          sortCards(cardsC);//cardsC cannot be resolved to a variable
          aString();
          
          outClub.setText(clubHand);//outClub cannot be resolved
          //clubHand cannot be resolved to a variable
        }//end actionPerformed method
        
        public void countPoints()
        {
          //will count the points the cards are worth
          int pointTotal = 0;
          if (clubs.length() == 1)
          {
            pointTotal+=3;
          }
          else if (clubs.length() == 2)
          {
            pointTotal +=2;
          }
          else if (clubs.length() == 3)
          {
            pointTotal += 1;
          }
          else
          {
            pointTotal+=0;
          }
          
          if (clubs.length() > 1)
          {
            for (int a = 0; a< clubs.length(); a++)
            {
              if (clubs.charAt(a) == 'A')
              {
                pointTotal+=4;
              }
              else if (clubs.charAt(a) == 'K')
              {
                pointTotal+=3;
              }
              else if (clubs.charAt(a) == 'Q')
              {
                pointTotal+=2;
              }
              else if (clubs.charAt(a) == 'J')
              {
                pointTotal+=1;
              }
              else
              {
                pointTotal+=0;
              }
            }
          }
        }//end countPoints method
        
        public void prepareCards()
        {
          //in order to sort the cards, any cards represented by a letter must be replaced by a value first
          clubs.replace('T', '10');//invalid character constant
          clubs.replace('J', '11');
          clubs.replace('Q', '12');
          clubs.replace('K', '13');
          clubs.replace('A', '14');
          
          //populating an int array to sort
          String [] cardC = clubs.split(" ");
          int [] cardsC = new int [cardC.length];
          
          for (int y = 0; y < cardC.length; y++)
          {
            cardsC[y] = Integer.parseInt(cardC[y]);
          }
        }
        
        public int sortCards(int []cardsC)
        {
          //insertion sort
          int index = 0;
          int itemToInsert;
          
          for(int i = 1; i < cardsC.length; i++)
          {
            itemToInsert = cardsC[i];
            index = i;
            
            while((index >= 1) && (itemToInsert < cardsC[index -1]))
            {
              cardsC[index] = cardsC [index - 1];
              index--;
            }
            
            cardsC[index] =  itemToInsert;   
          }  
          return cardsC[index];
          
        }
        
        public void aString()
        {
          //the sorted cards will now be converted into a string and prepared for output
          String temp = Arrays.toString(cardsC);
          String conv = temp.replace(", ", " ");
          String clubH = conv.substring(1, conv.length()-1);
          //replacing the values with the card name
          clubH.replace('11', 'J');//invalid character constant
          clubH.replace('12', 'Q');
          clubH.replace('13', 'K');
          clubH.replace('14', 'A');
          String clubHand = clubH;
        }
      }
    Last edited by modhz; 11-22-2011 at 03:14 AM. Reason: Added full error messages

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    Quote Originally Posted by modhz View Post
    I also included comments of the errors I am getting.
    If you are getting errors then copy and paste the full error messages.

  6. #6
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    I edited the post, but I'll put it here in a separate post to try to organize:


    6 errors and 1 warning found:
    --------------
    *** Errors ***
    --------------
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 89]
    Error: input cannot be resolved
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 102]
    Error: cardsC cannot be resolved to a variable
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 105]
    Error: outClub cannot be resolved
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 105]
    Error: clubHand cannot be resolved to a variable
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 161]
    Error: Invalid character constant
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 207]
    Error: Invalid character constant
    -------------
    ** Warning **
    -------------
    File: C:\Documents and Settings\Administrator\Desktop\Java\BridgePoints.j ava [line: 18]
    Warning: The serializable class BridgeGUI does not declare a static final serialVersionUID field of type long

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    Several of your errors are due to attempting to use variables in the wrong scope. For example the input variable is declared in the BridgeGui class yet you are trying to use it in the ClubPoints class.

  8. #8
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    I have been looking at this thread for a guideline:
    help with GUI and multiple classes

    What I found is that it appears that the classes need to be nested. I'm not familiar with this, so I did not know, and it took me a while to find that there was bracket that enclosed everything together.

    The replace method only works with char, and chars can only fit one character, not two. I'm not sure what I was thinking earlier, perhaps I got lost in my code while working on something else.
    Thanks for the help. :)

  9. #9
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    I've been working on improving my GUI.
    Now when I run it, I get a lot of error messages, and I am not sure what they mean.
    From what I know, I think it means that something(the spot in the string being tested?) is empty. Is that correct?
    I am not getting this error for any of the other classes though.

    This is the sample input I used. The first character represents the card value. The character that follows it represents its suit. In this case, the first card is the 4 of clubs.
    I will edit this post again once I try a different test input.
    This was the sample input I used: 4C3C6CTSTHAD2S8DACQHKS2D4H
    I tried a different sample input, and I get the same set of errors, but for a different character.

    These are my error messages:


    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "T"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at SortBridge$SortSpades.actionPerformed(SortBridge.j ava:313)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    This is my code:

    Java Code:
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class SortBridge extends JFrame
    {
      JFrame frame;
      JPanel contentPane;
      JLabel in = new JLabel("Input hand:");
      JTextField input = new JTextField(26);
      
      JLabel outC = new JLabel("Clubs");
      JLabel outD = new JLabel("Diamonds");
      JLabel outH = new JLabel("Hearts");
      JLabel outS = new JLabel("Spades");
      JLabel outT = new JLabel("Total");
      JLabel blank = new JLabel();//only added to help space the components
      
      JTextField outClub = new JTextField();
      JTextField outDiamond = new JTextField();
      JTextField outHeart = new JTextField();
      JTextField outSpade = new JTextField();
      JTextField outTotal = new JTextField();
      
      
      JButton run = new JButton("Run Program");
      
      JButton cont = new JButton("Continue");
      JButton exit = new JButton("Exit");
      
      public SortBridge()
      {
        frame=new JFrame("Bridge Point Counter");
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2, 5, 5));
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        outClub.setEditable(false);
        outDiamond.setEditable(false);
        outHeart.setEditable(false);
        outSpade.setEditable(false);
        outTotal.setEditable(false);
        
        cont.setActionCommand("continue");
        exit.setActionCommand("quit");
        cont.addActionListener(new PromptButton());
        exit.addActionListener(new PromptButton());
        run.addActionListener(new TotalPoints());
        run.addActionListener(new SortClubs());
        run.addActionListener(new SortDiamonds());
        run.addActionListener(new SortHearts());
        run.addActionListener(new SortSpades());
        
        contentPane.add(in);
        contentPane.add(input);
        contentPane.add(run);
        contentPane.add(blank);
        
        contentPane.add(outC);
        contentPane.add(outClub);
        contentPane.add(outD);
        contentPane.add(outDiamond);
        contentPane.add(outH);
        contentPane.add(outHeart);
        contentPane.add(outS);
        contentPane.add(outSpade);
        contentPane.add(outT);
        contentPane.add(outTotal);
        
        contentPane.add(cont);
        contentPane.add(exit);
        
        
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
      }
      
      
      abstract class Suits
      {
        String clubs;
        String diamonds;
        String hearts;
        String spades;
        
        abstract void actionPerformed(ActionEvent evt); 
      }
      
      class SortClubs extends Suits implements ActionListener
      {
        //will sort all Clubs
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer club =  new StringBuffer();
          String userIn1 = input.getText();
          
          for (int i = 0; i < userIn1.length();i+=2)
          {
            if (userIn1.charAt(i + 1) == 'C')
            {
              club.append(userIn1.charAt(i) + " ");
            }
          }
          
          String clubs = club.toString();
          
          //any cards represented by a character must have an int value for the sort
          clubs.replaceAll("T", "10");
          clubs.replaceAll("J", "11");
          clubs.replaceAll("Q", "12");
          clubs.replaceAll("K", "13");
          clubs.replaceAll("A", "14");
          
          String [] cardC = clubs.split(" ");
          int [] cardsC = new int [cardC.length];
          
          for (int y = 0; y < cardC.length; y++)
          {
            cardsC[y] = Integer.parseInt(cardC[y]);
          }
          
          boolean sorted = true;
          int end = cardsC.length;
          
          while (sorted)
          {
            end--;
            sorted = false;
            for (int i = 0; i < end; i++)
            {
              if (cardsC [i + 1] > cardsC [i])
              {
                int temp = cardsC [i];
                cardsC [i] = cardsC[i + 1];
                cardsC [i + 1] = temp;
                sorted = true;
              }
            }
          }
          
          String tempClub = Arrays.toString(cardsC);
          String conv = tempClub.replace(", ", " ");
          String clubH = conv.substring(1, conv.length()-1);
          //replace the numeric values back with their letters for output
          clubH.replaceAll("10", "T");
          clubH.replaceAll("11", "J");
          clubH.replaceAll("12", "Q");
          clubH.replaceAll("13", "K");
          clubH.replaceAll("14", "A");
          String clubHand = clubH;
          outClub.setText(clubHand);        
        }
      }
      
      class SortDiamonds extends Suits implements ActionListener
      {      
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer diamond =  new StringBuffer();
          String userIn2 = input.getText();
          
          for (int j = 0; j < userIn2.length();j+=2)
          {
            if (userIn2.charAt(j + 1) == 'D')
            {
              diamond.append(userIn2.charAt(j) + " ");
            }
          }
          
          String diamonds = diamond.toString();
          
          diamonds.replaceAll("T", "10");
          diamonds.replaceAll("J", "11");
          diamonds.replaceAll("Q", "12");
          diamonds.replaceAll("K", "13");
          diamonds.replaceAll("A", "14");
          
          String [] cardD = diamonds.split(" ");
          int [] cardsD = new int [cardD.length];
          
          for (int k = 0; k < cardD.length; k++)
          {
            cardsD[k] = Integer.parseInt(cardD[k]);
          }
          
          boolean sorted2 = true;
          int end2 = cardsD.length;
          
          while (sorted2)
          {
            end2--;
            sorted2 = false;
            for (int l = 0; l < end2; l++)
            {
              if (cardsD [l + 1] > cardsD [l])
              {
                int temp2 = cardsD [l];
                cardsD [l] = cardsD[l + 1];
                cardsD [l + 1] = temp2;
                sorted2 = true;
              }
            }
          }
          
          String tempDiamond = Arrays.toString(cardsD);
          String conv2 = tempDiamond.replace(", ", " ");
          String diamondH = conv2.substring(1, conv2.length()-1);
          diamondH.replaceAll("10", "T");
          diamondH.replaceAll("11", "J");
          diamondH.replaceAll("12", "Q");
          diamondH.replaceAll("13", "K");
          diamondH.replaceAll("14", "A");
          String diamondHand = diamondH;
          outDiamond.setText(diamondHand);        
        }
      }
      
      class SortHearts extends Suits implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer heart =  new StringBuffer();
          String userIn3 = input.getText();
          
          for (int m = 0; m < userIn3.length();m+=2)
          {
            if (userIn3.charAt(m + 1) == 'H')
            {
              heart.append(userIn3.charAt(m) + " ");
            }
          }
          
          String hearts = heart.toString();
          
          hearts.replaceAll("T", "10");
          hearts.replaceAll("J", "11");
          hearts.replaceAll("Q", "12");
          hearts.replaceAll("K", "13");
          hearts.replaceAll("A", "14");
          
          String [] cardH = hearts.split(" ");
          int [] cardsH = new int [cardH.length];
          
          for (int n = 0; n < cardH.length; n++)
          {
            cardsH[n] = Integer.parseInt(cardH[n]);
          }
          
          boolean sorted3 = true;
          int end3 = cardsH.length;
          
          while (sorted3)
          {
            end3--;
            sorted3 = false;
            for (int o = 0; o < end3; o++)
            {
              if (cardsH [o + 1] > cardsH [o])
              {
                int temp3 = cardsH [o];
                cardsH [o] = cardsH[o + 1];
                cardsH [o + 1] = temp3;
                sorted3 = true;
              }
            }
          }
          
          String tempHeart = Arrays.toString(cardsH);
          String conv3 = tempHeart.replace(", ", " ");
          String heartH = conv3.substring(1, conv3.length()-1);
          heartH.replaceAll("10", "T");
          heartH.replaceAll("11", "J");
          heartH.replaceAll("12", "Q");
          heartH.replaceAll("13", "K");
          heartH.replaceAll("14", "A");
          String heartHand = heartH;
          outHeart.setText(heartHand);        
        }
      }
      
      class SortSpades extends Suits implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer spade =  new StringBuffer();
          String userIn3 = input.getText();
          
          for (int p = 0; p < userIn3.length();p+=2)
          {
            if (userIn3.charAt(p + 1) == 'H')
            {
              spade.append(userIn3.charAt(p) + " ");
            }
          }
          
          String spades = spade.toString();
          
          spades.replaceAll("T", "10");
          spades.replaceAll("J", "11");
          spades.replaceAll("Q", "12");
          spades.replaceAll("K", "13");
          spades.replaceAll("A", "14");
          
          String [] cardS = spades.split(" ");
          int [] cardsS = new int [cardS.length];
          
          for (int q = 0; q < cardS.length; q++)
          {
            cardsS[q] = Integer.parseInt(cardS[q]);//line error message refers to
          }
          
          boolean sorted4 = true;
          int end4 = cardsS.length;
          
          while (sorted4)
          {
            end4--;
            sorted4 = false;
            for (int r = 0; r < end4; r++)
            {
              if (cardsS [r + 1] > cardsS [r])
              {
                int temp4 = cardsS [r];
                cardsS [r] = cardsS[r + 1];
                cardsS [r + 1] = temp4;
                sorted4 = true;
              }
            }
          }
          
          String tempSpade = Arrays.toString(cardsS);
          String conv4 = tempSpade.replace(", ", " ");
          String spadeH = conv4.substring(1, conv4.length()-1);
          spadeH.replaceAll("10", "T");
          spadeH.replaceAll("11", "J");
          spadeH.replaceAll("12", "Q");
          spadeH.replaceAll("13", "K");
          spadeH.replaceAll("14", "A");
          String spadeHand = spadeH;
          outSpade.setText(spadeHand);    
        }
      }
      
      public class PromptButton implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          if (evt.getActionCommand().equals("continue"))
          {
            //will reset all fields
            input.setText("");
            outClub.setText("");
            outDiamond.setText("");
            outHeart.setText("");
            outSpade.setText("");
            outTotal.setText("");
          }
          else if (evt.getActionCommand().equals("quit"))
          {
            //exit out of the program
            System.exit( 0 );
          }
        }
      }
      
      
      public class TotalPoints implements ActionListener
      {
        //will add up the total points the hand is worth
        public void actionPerformed(ActionEvent evt)
        {
          String hand = input.getText();
          String clubHand ="";
          String diamondHand="";
          String heartHand="";
          String spadeHand="";
          int clubPoints=0;
          int diamondPoints=0;
          int heartPoints=0;
          int spadePoints=0;
          
          for (int z = 0; z<hand.length(); z+=2)
          {
            if (hand.charAt(z+1)=='C')
            {
              clubHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='D')
            {
              diamondHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='H')
            {
              heartHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='S')
            {
              spadeHand.concat(Character.toString(hand.charAt(z)));
            }
          }
          
          if (clubHand.length() == 1)
          {
            clubPoints +=3;
          }
          else if (clubHand.length() == 2)
          {
            clubPoints +=2;
          }
          else if (clubHand.length() == 3)
          {
            clubPoints += 1;
          }
          else
          {
            clubPoints +=0;
          }
          
          if(clubHand.length() >1)
          {
            for (int y = 1; y< clubHand.length(); y++)
            {
              if (clubHand.charAt(y) == 'A')
              {
                clubPoints+=4;
              }
              else if (clubHand.charAt(y) == 'K')
              {
                clubPoints+=3;
              }
              else if (clubHand.charAt(y) == 'Q')
              {
                clubPoints+=2;
              }
              else if (clubHand.charAt(y) == 'J')
              {
                clubPoints+=1;
              }
              else
              {
                clubPoints+=0;
              }
            }
          }    
          
          if (diamondHand.length() == 1)
          {
            diamondPoints +=3;
          }
          else if (diamondHand.length() == 2)
          {
            diamondPoints +=2;
          }
          else if (diamondHand.length() == 3)
          {
            diamondPoints += 1;
          }
          else
          {
            diamondPoints +=0;
          }
          
          
          if (heartHand.length() == 1)
          {
            heartPoints +=3;
          }
          else if (heartHand.length() == 2)
          {
            heartPoints +=2;
          }
          else if (heartHand.length() == 3)
          {
            heartPoints += 1;
          }
          else
          {
            heartPoints +=0;
          }
          
          
          if (spadeHand.length() == 1)
          {
            spadePoints +=3;
          }
          else if (spadeHand.length() == 2)
          {
            spadePoints +=2;
          }
          else if (spadeHand.length() == 3)
          {
            spadePoints += 1;
          }
          else
          {
            spadePoints +=0;
          }
          
          
          int grandTotal = clubPoints + diamondPoints + heartPoints + spadePoints;
          String total = Integer.toString(grandTotal);
          outTotal.setText(total);
        }
      }
      
      
      private static void runGUI() 
      {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SortBridge bridgeGUI = new SortBridge();
      }
      
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            runGUI();
          }
        });
      }
    }
    Last edited by modhz; 11-23-2011 at 01:30 AM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    4C3C6CTSTHAD2S8DACQHKS2D4H

    Take a close look at your input. Every odd char is the value and every even char is the suit. What value does the 7th char ('T') have? And 9th and 11th etc

  11. #11
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    Java Code:
     for (int p = 0; p < userIn3.length();p+=2)
          {
            if (userIn3.charAt(p + 1) == 'H')
            {
              spade.append(userIn3.charAt(p) + " ");
            }
          }
          
          String spades = spade.toString();
          
          spades.replaceAll("T", "10");
          spades.replaceAll("J", "11");
          spades.replaceAll("Q", "12");
          spades.replaceAll("K", "13");
          spades.replaceAll("A", "14");
          
          String [] cardS = spades.split(" ");
          int [] cardsS = new int [cardS.length];
    So even though I spaced out the String before converting it to an array, I'm getting this problem?
    Or am I replacing the cards represented by letters incorrectly?
    T should have a value of 10.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    Strings are immutable.

  13. #13
    modhz is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Help with Inheritance and Static Methods

    So what I've done is this:
    Java Code:
          String tempSpade = Arrays.toString(cardsS);
          String conv4 = tempSpade.replace(", ", " ");
          String spadeH = conv4.substring(1, conv4.length()-1);
          String spade1 = spadeH.replaceAll("10", "T");
          String spade2 = spade1.replaceAll("11", "J");
          String spade3 = spade2.replaceAll("12", "Q");
          String spade4 = spade3.replaceAll("13", "K");
          String spade5 = spade4.replaceAll("14", "A");
          outSpade.setText(spade5);
    And now my program outputs something for the spade field.
    I copy and pasted this in all my classes, but now I'm getting an error for the hearts class. And I'm guessing this will continue to the other classes as well. Can you please explain to me why this happens? Thanks again for your help.

    Here are updated code and error messages:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "T"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at SortBridge$SortHearts.actionPerformed(SortBridge.j ava:248)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Java Code:
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    
    public class SortBridge extends JFrame
    {
      JFrame frame;
      JPanel contentPane;
      JLabel in = new JLabel("Input hand:");
      JTextField input = new JTextField(26);
      
      JLabel outC = new JLabel("Clubs");
      JLabel outD = new JLabel("Diamonds");
      JLabel outH = new JLabel("Hearts");
      JLabel outS = new JLabel("Spades");
      JLabel outT = new JLabel("Total");
      JLabel blank = new JLabel();
      
      JTextField outClub = new JTextField();
      JTextField outDiamond = new JTextField();
      JTextField outHeart = new JTextField();
      JTextField outSpade = new JTextField();
      JTextField outTotal = new JTextField();
      
      
      JButton run = new JButton("Run Program");
      
      JButton cont = new JButton("Continue");
      JButton exit = new JButton("Exit");
      
      public SortBridge()
      {
        frame=new JFrame("Bridge Point Counter");
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 2, 5, 5));
        contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        
        outClub.setEditable(false);
        outDiamond.setEditable(false);
        outHeart.setEditable(false);
        outSpade.setEditable(false);
        outTotal.setEditable(false);
        
        cont.setActionCommand("continue");
        exit.setActionCommand("quit");
        cont.addActionListener(new PromptButton());
        exit.addActionListener(new PromptButton());
        run.addActionListener(new TotalPoints());
        run.addActionListener(new SortClubs());
        run.addActionListener(new SortDiamonds());
        run.addActionListener(new SortHearts());
        run.addActionListener(new SortSpades());
        
        contentPane.add(in);
        contentPane.add(input);
        contentPane.add(run);
        contentPane.add(blank);
        
        contentPane.add(outC);
        contentPane.add(outClub);
        contentPane.add(outD);
        contentPane.add(outDiamond);
        contentPane.add(outH);
        contentPane.add(outHeart);
        contentPane.add(outS);
        contentPane.add(outSpade);
        contentPane.add(outT);
        contentPane.add(outTotal);
        
        contentPane.add(cont);
        contentPane.add(exit);
        
        
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
      }
      
      
      abstract class Suits
      {
        String clubs;
        String diamonds;
        String hearts;
        String spades;
        
        abstract void actionPerformed(ActionEvent evt); 
      }
      
      class SortClubs extends Suits implements ActionListener
      {
        //will sort all Clubs
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer club =  new StringBuffer();
          String userIn1 = input.getText();
          
          for (int i = 0; i < userIn1.length();i+=2)
          {
            if (userIn1.charAt(i + 1) == 'C')
            {
              club.append(userIn1.charAt(i) + " ");
            }
          }
          
          String clubs = club.toString();
          
          //any cards represented by a character must have an int value for the sort
          String clubs1 = clubs.replaceAll("T", "10");
          String clubs2 = clubs1.replaceAll("J", "11");
          String clubs3 = clubs2.replaceAll("Q", "12");
          String clubs4 = clubs3.replaceAll("K", "13");
          String clubs5 = clubs4.replaceAll("A", "14");
          
          String [] cardC = clubs5.split(" ");
          int [] cardsC = new int [cardC.length];
          
          for (int y = 0; y < cardC.length; y++)
          {
            cardsC[y] = Integer.parseInt(cardC[y]);
          }
          
          boolean sorted = true;
          int end = cardsC.length;
          
          while (sorted)
          {
            end--;
            sorted = false;
            for (int i = 0; i < end; i++)
            {
              if (cardsC [i + 1] > cardsC [i])
              {
                int temp = cardsC [i];
                cardsC [i] = cardsC[i + 1];
                cardsC [i + 1] = temp;
                sorted = true;
              }
            }
          }
          
          String tempClub = Arrays.toString(cardsC);
          String conv = tempClub.replace(", ", " ");
          String clubH = conv.substring(1, conv.length()-1);
          //replace the numeric values back with their letters for output
          String club1 = clubH.replaceAll("10", "T");
          String club2 = club1.replaceAll("11", "J");
          String club3 = club2.replaceAll("12", "Q");
          String club4 = club3.replaceAll("13", "K");
          String club5 = club4.replaceAll("14", "A");
          outHeart.setText(club5);               
        }
      }
      
      class SortDiamonds extends Suits implements ActionListener
      {      
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer diamond =  new StringBuffer();
          String userIn2 = input.getText();
          
          for (int j = 0; j < userIn2.length();j+=2)
          {
            if (userIn2.charAt(j + 1) == 'D')
            {
              diamond.append(userIn2.charAt(j) + " ");
            }
          }
          
          String diamonds = diamond.toString();
          
          String diamonds1 = diamonds.replaceAll("T", "10");
          String diamonds2 = diamonds1.replaceAll("J", "11");
          String diamonds3 = diamonds2.replaceAll("Q", "12");
          String diamonds4 = diamonds3.replaceAll("K", "13");
          String diamonds5 = diamonds4.replaceAll("A", "14");
          
          String [] cardD = diamonds5.split(" ");
          int [] cardsD = new int [cardD.length];
          
          for (int k = 0; k < cardD.length; k++)
          {
            cardsD[k] = Integer.parseInt(cardD[k]);
          }
          
          boolean sorted2 = true;
          int end2 = cardsD.length;
          
          while (sorted2)
          {
            end2--;
            sorted2 = false;
            for (int l = 0; l < end2; l++)
            {
              if (cardsD [l + 1] > cardsD [l])
              {
                int temp2 = cardsD [l];
                cardsD [l] = cardsD[l + 1];
                cardsD [l + 1] = temp2;
                sorted2 = true;
              }
            }
          }
          
          String tempDiamond = Arrays.toString(cardsD);
          String conv2 = tempDiamond.replace(", ", " ");
          String diamondH = conv2.substring(1, conv2.length()-1);
          String diamond1 = diamondH.replaceAll("10", "T");
          String diamond2 = diamond1.replaceAll("11", "J");
          String diamond3 = diamond2.replaceAll("12", "Q");
          String diamond4 = diamond3.replaceAll("13", "K");
          String diamond5 = diamond4.replaceAll("14", "A");
          outDiamond.setText(diamond5);        
        }
      }
      
      class SortHearts extends Suits implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer heart =  new StringBuffer();
          String userIn3 = input.getText();
          
          for (int m = 0; m < userIn3.length();m+=2)
          {
            if (userIn3.charAt(m + 1) == 'H')
            {
              heart.append(userIn3.charAt(m) + " ");
            }
          }
          
          String hearts = heart.toString();
          
          String hearts1 = hearts.replaceAll("10", "T");
          String hearts2 = hearts1.replaceAll("11", "J");
          String hearts3 = hearts2.replaceAll("12", "Q");
          String hearts4 = hearts3.replaceAll("13", "K");
          String hearts5 = hearts4.replaceAll("14", "A");
          
          String [] cardH = hearts5.split(" ");
          int [] cardsH = new int [cardH.length];
          
          for (int n = 0; n < cardH.length; n++)
          {
            cardsH[n] = Integer.parseInt(cardH[n]);//line error refers to
          }
          
          boolean sorted3 = true;
          int end3 = cardsH.length;
          
          while (sorted3)
          {
            end3--;
            sorted3 = false;
            for (int o = 0; o < end3; o++)
            {
              if (cardsH [o + 1] > cardsH [o])
              {
                int temp3 = cardsH [o];
                cardsH [o] = cardsH[o + 1];
                cardsH [o + 1] = temp3;
                sorted3 = true;
              }
            }
          }
          
          String tempHeart = Arrays.toString(cardsH);
          String conv3 = tempHeart.replace(", ", " ");
          String heartH = conv3.substring(1, conv3.length()-1);
          String heart1 = heartH.replaceAll("10", "T");
          String heart2 = heart1.replaceAll("11", "J");
          String heart3 = heart2.replaceAll("12", "Q");
          String heart4 = heart3.replaceAll("13", "K");
          String heart5 = heart4.replaceAll("14", "A");
          outHeart.setText(heart5);        
        }
      }
      
      class SortSpades extends Suits implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          StringBuffer spade =  new StringBuffer();
          String userIn3 = input.getText();
          
          for (int p = 0; p < userIn3.length();p+=2)
          {
            if (userIn3.charAt(p + 1) == 'S')
            {
              spade.append(userIn3.charAt(p) + " ");
            }
          }
          
          String spades = spade.toString();
          
          String spades1 = spades.replaceAll("T", "10");
          String spades2 = spades1.replaceAll("J", "11");
          String spades3 = spades2.replaceAll("Q", "12");
          String spades4 = spades3.replaceAll("K", "13");
          String spades5 = spades4.replaceAll("A", "14");
          
          String [] cardS = spades5.split(" ");
          int [] cardsS = new int [cardS.length];
          
          for (int q = 0; q < cardS.length; q++)
          {
            cardsS[q] = Integer.parseInt(cardS[q]);
          }
          
          boolean sorted4 = true;
          int end4 = cardsS.length;
          
          while (sorted4)
          {
            end4--;
            sorted4 = false;
            for (int r = 0; r < end4; r++)
            {
              if (cardsS [r + 1] > cardsS [r])
              {
                int temp4 = cardsS [r];
                cardsS [r] = cardsS[r + 1];
                cardsS [r + 1] = temp4;
                sorted4 = true;
              }
            }
          }
          
          String tempSpade = Arrays.toString(cardsS);
          String conv4 = tempSpade.replace(", ", " ");
          String spadeH = conv4.substring(1, conv4.length()-1);
          String spade1 = spadeH.replaceAll("10", "T");
          String spade2 = spade1.replaceAll("11", "J");
          String spade3 = spade2.replaceAll("12", "Q");
          String spade4 = spade3.replaceAll("13", "K");
          String spade5 = spade4.replaceAll("14", "A");
          outSpade.setText(spade5);    
        }
      }
      
      public class PromptButton implements ActionListener
      {
        public void actionPerformed(ActionEvent evt)
        {
          if (evt.getActionCommand().equals("continue"))
          {
            //will reset all fields
            input.setText("");
            outClub.setText("");
            outDiamond.setText("");
            outHeart.setText("");
            outSpade.setText("");
            outTotal.setText("");
          }
          else if (evt.getActionCommand().equals("quit"))
          {
            //exit out of the program
            System.exit( 0 );
          }
        }
      }
      
      
      public class TotalPoints implements ActionListener
      {
        //will add up the total points the hand is worth
        public void actionPerformed(ActionEvent evt)
        {
          String hand = input.getText();
          String clubHand ="";
          String diamondHand="";
          String heartHand="";
          String spadeHand="";
          int clubPoints=0;
          int diamondPoints=0;
          int heartPoints=0;
          int spadePoints=0;
          
          for (int z = 0; z<hand.length(); z+=2)
          {
            if (hand.charAt(z+1)=='C')
            {
              clubHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='D')
            {
              diamondHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='H')
            {
              heartHand.concat(Character.toString(hand.charAt(z)));
            }
            if (hand.charAt(z+1)=='S')
            {
              spadeHand.concat(Character.toString(hand.charAt(z)));
            }
          }
          
          if (clubHand.length() == 1)
          {
            clubPoints +=3;
          }
          else if (clubHand.length() == 2)
          {
            clubPoints +=2;
          }
          else if (clubHand.length() == 3)
          {
            clubPoints += 1;
          }
          else
          {
            clubPoints +=0;
          }
          
          if(clubHand.length() >1)
          {
            for (int y = 1; y< clubHand.length(); y++)
            {
              if (clubHand.charAt(y) == 'A')
              {
                clubPoints+=4;
              }
              else if (clubHand.charAt(y) == 'K')
              {
                clubPoints+=3;
              }
              else if (clubHand.charAt(y) == 'Q')
              {
                clubPoints+=2;
              }
              else if (clubHand.charAt(y) == 'J')
              {
                clubPoints+=1;
              }
              else
              {
                clubPoints+=0;
              }
            }
          }    
          
          if (diamondHand.length() == 1)
          {
            diamondPoints +=3;
          }
          else if (diamondHand.length() == 2)
          {
            diamondPoints +=2;
          }
          else if (diamondHand.length() == 3)
          {
            diamondPoints += 1;
          }
          else
          {
            diamondPoints +=0;
          }
          
          
          if (heartHand.length() == 1)
          {
            heartPoints +=3;
          }
          else if (heartHand.length() == 2)
          {
            heartPoints +=2;
          }
          else if (heartHand.length() == 3)
          {
            heartPoints += 1;
          }
          else
          {
            heartPoints +=0;
          }
          
          
          if (spadeHand.length() == 1)
          {
            spadePoints +=3;
          }
          else if (spadeHand.length() == 2)
          {
            spadePoints +=2;
          }
          else if (spadeHand.length() == 3)
          {
            spadePoints += 1;
          }
          else
          {
            spadePoints +=0;
          }
          
          
          int grandTotal = clubPoints + diamondPoints + heartPoints + spadePoints;
          String total = Integer.toString(grandTotal);
          outTotal.setText(total);
        }
      }
      
      
      private static void runGUI() 
      {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SortBridge bridgeGUI = new SortBridge();
      }
      
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            runGUI();
          }
        });
      }
    }

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Help with Inheritance and Static Methods

    Clearly you still have "T" in your String when you try to parse it into an int. Try debugging your code by putting in print statements in various places to display the value of your variable(s) so you can see what they hold and if they match what you think they hold.

    Java Code:
    String text = "TCKCQCJC";
    text = text.replaceAll("T", "10").replaceAll("K", "13").replaceAll("Q", "12").replaceAll("J", "11");
    Depending upon your point of view you can simplify you code as above. Also I see you have duplicated a lot of code in your subclasses. When you find yourself doing this you should think about how you can consolidate this code into the superclass.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  3. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  4. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM
  5. Inheritance, methods, and toString...
    By Kreuz14 in forum New To Java
    Replies: 1
    Last Post: 04-01-2008, 10:12 PM

Posting Permissions

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