*p5.2

From GGCWiki
Jump to: navigation, search

William's 5.2 assignment.


  • William this code looks excellent I have something that might make the code less bulk:

if(name.toLowerCase().startsWith("j")) 38 faceValue=11; 39 else if(name.toLowerCase().startsWith("q")) 40 faceValue=12; 41 else if(name.toLowerCase().startsWith("k")) 42 faceValue=13;


this is taken straight from http://ggc.javawide.org/index.php/intermediate/Rank and it tests if the case of the first letter starts with a j for example but it also includes J, Jack , Jill , j just as long as it starts with j no matter the case. - Chris

package assignments;

import java.util.Scanner;



public class Card {

	
	
	private String suit, value;
	
	

    Card(String value, String suit)
    {
    	this.value = value;
    	this.suit = suit;
    }
	
    
    public String getValue()
    {
    	if(value.equals("2") || value.equals("3") || value.equals("4") || value.equals("5") || value.equals("6") || value.equals("7") || value.equals("8") || value.equals("9") || value.equals("10"))
		{value = value;}
    	else if(value.equals("J") || value.equals("j"))
		{value = "Jack";}
    	else if(value.equals("Q") || value.equals("q"))
		{value = "Queen";}
    	else if(value.equals("A") || value.equals("a") || value.equals("1"))
		{value = "Ace";}
    	else if(value.equals("K") || value.equals("k"))
		{value = "King";}
    	else {value = "n/a";}
    	return value;
    }
    
    public String getSuit()
    {
    	if(suit.equals("S") || suit.equals("s"))
		{suit = "Spades";}
    	else if(suit.equals("C") || suit.equals("c"))
		{suit = "Clubs"; }
    	else if(suit.equals("H") || suit.equals("h"))
		{suit = "Hearts";}
    	else if(suit.equals("D") || suit.equals("d"))
		{suit = "Diamonds";}
    	else
    	{suit = "n/a";}
    	return suit;
    }
    
    
	public String getDescription(String value, String suit)
	{
		return "Your card is: " + getValue() + " of " + getSuit();
	}

	
	public static void main(String[]args) 
	{
		Scanner in = new Scanner(System.in);
		String input;
		String a;
		String b;
		
		System.out.println("Input card value and suit in the form VS : ");
		input=in.next();
		a = input.substring(0,1);
		b = input.substring(input.length()-1, input.length());
		
		Card card;
		card = new Card(a, b);
		
		System.out.println(card.getDescription(a,b));
		
		
	}
	
}
Personal tools