



|
|
P Dunn's Super CS1 Site
|
Class Notes
Wednesday October 25 , 2000
|
|
The blue.card.Card class
Constructors
new Card (< int rank >, < string suit >) -- > < Card > for number cards
new Card (< string rank >, < string suit >) -- > < Card > for royalty cards
Methods
< card > .print () - describe the card w /o newline
< card > .println () - describe the card w /newline
< card > .rank () -- > < int > - produce the rank of the card
< card > .suit () -- > < int > - produce the suit of the card
< card > .printRank () - describe the card rank
< card > .printSuit () - describe the card suit
Constants
Card.JACK -- > < int >
Card.QUEEN -- > < int >
Card.KING -- > < int >
Card.ACE -- > < int >
Card.CLUB -- > < int >
Card.DIAMOND -- > < int >
Card.HEART -- > < int >
Card.SPADE -- > < int >
|
Example on how to use the Card class
Card c = new Card (4, '' spade''); (suit should be lowercase)
Card d = new Card ( ``Jack'', '' club''); - for royalty
|
Suppose x and y are Card objects.
1) Display the rank of x:
This will give the integer representation of the rank.
2) Do a better job of 1)
3) Display the suit of x
4) Display both cards, one per line
x.println ();
y.println ();
|
5) Display the word ``red'' if x is a red card
boolean xHeart = (x.suit ()) == Card.HEART);
boolean xDiamond = (x.suit () == Card.DIAMOND);
if (xHeart | | xDiamond)
{
IO.println ( ``red'');
}
|
At this point, initial code for programming challenge 4, part 1 is introduced. It is reproduced here since it was given in class:
Java Application --
// General Information
// ---------------------------------------------------
// File: CardThingApp.java
// Type: java application file
// Date: Wed Nov 1, 2000
// Name: Patrick Dunn
// Line: V1 - reads 50 card pair representations
// from the input file and displays them
// Application Description
// ---------------------------------------------------
/*
This program reads in 50 card pair representations,
creates a card object for each of them and displays
what they are by rank and suit on the standard
output.
*/
// Required Packages
// ---------------------------------------------------
import blue.io.*;
import blue.cards.*;
// Application Class
// ---------------------------------------------------
class CardThingApp
{
static public void main (String args[])
{
Card c; // card 1
Card d; // card 2
int counter = 1; // init counter
while(counter <= 50)
{
// read in the card rank and suit
String cRank = IO.readString();
String cSuit = IO.readString();
// create a new card object
c = new Card(cRank,cSuit);
// read in the card rank and suit
String dRank = IO.readString();
String dSuit = IO.readString();
// create a new card object
d = new Card(dRank,dSuit);
// display the objects
c.print();
d.print();
IO.println();
// increment the counter
counter++;
}
}
}
// Demo
// ---------------------------------------------------
/*
[patdunn@k6 v1]$ java CardThingApp < ../CardThing.data
(2,S)(K,S)
(K,D)(K,H)
(3,C)(4,D)
(Q,H)(Q,S)
(7,D)(J,D)
(2,H)(2,D)
(7,H)(2,S)
(9,S)(10,C)
(J,C)(J,S)
(Q,S)(6,H)
(6,H)(7,D)
(6,C)(6,S)
(A,D)(2,C)
(K,C)(J,H)
(J,S)(10,S)
(J,S)(10,C)
(9,D)(9,H)
(5,S)(J,C)
(5,H)(6,H)
(9,D)(4,C)
(8,C)(8,S)
(9,D)(10,S)
(Q,H)(2,H)
(5,C)(6,C)
(9,D)(A,H)
(6,S)(K,H)
(K,D)(10,H)
(3,C)(4,S)
(5,H)(Q,D)
(J,C)(J,D)
(2,H)(2,D)
(7,D)(3,S)
(9,S)(9,C)
(J,C)(J,S)
(7,S)(6,H)
(6,C)(7,D)
(7,S)(6,S)
(A,D)(A,C)
(K,D)(J,H)
(J,C)(10,S)
(J,S)(J,C)
(9,D)(5,D)
(5,C)(J,C)
(5,H)(6,H)
(9,D)(4,H)
(8,C)(8,D)
(9,D)(6,S)
(Q,H)(2,C)
(5,S)(6,C)
(Q,H)(A,H)
[patdunn@k6 v1]$
*/
|
Problem:
Read several (at least one) non zero integers & display the largest of these (maximum).
Example
in:10 7 95 4 66 0
out:95
in: - 10 - 7 - 95 - 4 - 66 0
out: - 4
Procedure - take 1 st item, make it the top & compare to next #. Keep whats largest and proceed until all numbers are compared and displayed.
Program > -- >
int maxSoFar = IO.read_int ();
int challenger = IO.read_int ();
while (challenger! = 0)
{
if (challenger > maxSoFar)
{
maxSoFar = challenger;
}
challenger = IO.read_int ();
}
int max = maxSoFar;
IO.println ( ``max value = ``+ max);
|
|
|