|
From: <b_...@us...> - 2003-07-23 20:25:33
|
Update of /cvsroot/ordweb/uop/pos407/team/week5/examples
In directory sc8-pr-cvs1:/tmp/cvs-serv20030/pos407/team/week5/examples
Added Files:
Arrays1.java Arrays2.java Arrays3.java Arrays4.java
Arrays5.java
Log Message:
init
--- NEW FILE: Arrays1.java ---
public class Arrays1 {
public static void main (String [] args) {
App app = new App();
app.start();
}
}
class App {
void start () {
Deck d = new Deck();
System.out.println(d.toString());
}
}
class Deck {
int count = 52;
Card [ ] cards = new Card[52];
Deck() {
for (int suit = 1; suit <= 4; suit ++) {
for (int face = 1; face <= 13; face++) {
cards[((suit-1)*13)+(face-1)] = new Card(face, suit);
}
}
}
public String toString() {
String result = "";
for (int i = 0; i < count; i++) {
result += cards[i].toString() + '\n';
}
return result;
}
}
class Card {
int face;
int suit;
Card (int f, int s) {
this.face = f; //or just face = f;
this.suit = s; //or just suit = s;
return;
}
public String toString() {
String result = "";
if (face == 1) result = "Ace";
if (face == 11) result = "Jack";
if (face == 12) result = "Queen";
if (face == 13) result = "King";
if (face > 1 && face < 11) result = Integer.toString(face);
result = result + " of ";
if (suit == 1) result += "Clubs";
if (suit == 2) result += "Diamonds";
if (suit == 3) result += "Hearts";
if (suit == 4) result += "Spades";
return result;
}
}
--- NEW FILE: Arrays2.java ---
import java.io.*;
public class Arrays2 {
public static void main (String [] args) throws IOException {
App app = new App();
app.start();
}
}
class App {
void start () throws IOException {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
Deck d = new Deck();
Card c = null;
boolean again = true;
while (again) {
System.out.print ("Main Menu: (D)eal (L)ist (Q)uit: ");
String input = br.readLine().toLowerCase();
if (input == null || input.length() == 0)
continue;
char letter = input.charAt(0);
switch (letter) {
case 'd':
c = d.deal();
System.out.println("Card is: " + c);
break;
case 'l':
System.out.println (d);
break;
case 'q':
again = false;
break;
default:
System.out.println ("Reply not recongized. Please try again.");
}
}
}
}
class Deck {
int count = 52;
Card [ ] cards = new Card[52];
Deck() {
for (int suit = 1; suit <= 4; suit ++) {
for (int face = 1; face <= 13; face++) {
cards[((suit-1)*13)+(face-1)] = new Card(face, suit);
}
}
}
Card deal () {
if (count <= 0)
return null;
count = count - 1;
return cards[count];
}
public String toString() {
String result = "";
for (int i = 0; i < count; i++) {
result += cards[i].toString() + '\n';
}
return result;
}
}
class Card {
int face;
int suit;
Card (int f, int s) {
this.face = f; //or just face = f;
this.suit = s; //or just suit = s;
return;
}
public String toString() {
String result = "";
if (face == 1) result = "Ace";
if (face == 11) result = "Jack";
if (face == 12) result = "Queen";
if (face == 13) result = "King";
if (face > 1 && face < 11) result = Integer.toString(face);
result = result + " of ";
if (suit == 1) result += "Clubs";
if (suit == 2) result += "Diamonds";
if (suit == 3) result += "Hearts";
if (suit == 4) result += "Spades";
return result;
}
}
--- NEW FILE: Arrays3.java ---
//This demonstrates shuffling.
import java.io.*;
public class Arrays3 {
public static void main (String [] args) throws IOException {
App app = new App();
app.start();
}
}
class App {
void start () throws IOException {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
Deck d = new Deck();
Card c = null;
boolean again = true;
while (again) {
System.out.print ("Main Menu: (D)eal (L)ist (S)huffle (Q)uit: ");
String input = br.readLine().toLowerCase();
if (input == null || input.length() == 0)
continue;
char letter = input.charAt(0);
switch (letter) {
case 'd':
c = d.deal();
System.out.println("Card is: " + c);
break;
case 'l':
System.out.println (d);
break;
case 's':
d.shuffle();
System.out.println("Deck shuffled");
break;
case 'q':
again = false;
break;
default:
System.out.println ("Reply not recongized. Please try again.");
}
}
}
}
class Deck {
int count = 52;
Card [ ] cards = new Card[52];
Deck() {
for (int suit = 1; suit <= 4; suit ++) {
for (int face = 1; face <= 13; face++) {
cards[((suit-1)*13)+(face-1)] = new Card(face, suit);
}
}
}
Card deal () {
if (count <= 0)
return null;
count = count - 1;
return cards[count];
}
public void shuffle () {
if (count <= 0)
return;
for (int i = 0; i <100; i++) {
int a = (int) (Math.random() * count);
int b = (int) (Math.random() * count);
Card temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}
}
public String toString() {
String result = "Number of cards is " + count + ".\n";
for (int i = 0; i < count; i++) {
result += cards[i].toString() + '\n';
}
return result;
}
}
class Card {
int face;
int suit;
Card (int f, int s) {
this.face = f; //or just face = f;
this.suit = s; //or just suit = s;
return;
}
public String toString() {
String result = "";
if (face == 1) result = "Ace";
if (face == 11) result = "Jack";
if (face == 12) result = "Queen";
if (face == 13) result = "King";
if (face > 1 && face < 11) result = Integer.toString(face);
result = result + " of ";
if (suit == 1) result += "Clubs";
if (suit == 2) result += "Diamonds";
if (suit == 3) result += "Hearts";
if (suit == 4) result += "Spades";
return result;
}
}
--- NEW FILE: Arrays4.java ---
//This demonstrates finding the max and min of an array.
import java.io.*;
public class Arrays4 {
public static void main (String [] args) throws IOException {
App app = new App();
app.start();
}
}
class App {
void start () throws IOException {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
Deck d = new Deck();
Card c = null;
boolean again = true;
while (again) {
System.out.print ("Main Menu: (D)eal (L)ist (S)huffle (Q)uit: ");
String input = br.readLine().toLowerCase();
if (input == null || input.length() == 0)
continue;
char letter = input.charAt(0);
switch (letter) {
case 'd':
c = d.deal();
System.out.println("Card is: " + c);
break;
case 'l':
System.out.println (d);
break;
case 's':
d.shuffle();
System.out.println("Deck shuffled");
break;
case 'q':
again = false;
break;
default:
System.out.println ("Reply not recongized. Please try again.");
}
}
}
}
class Deck {
int count = 52;
Card [ ] cards = new Card[52];
Deck() {
for (int suit = 1; suit <= 4; suit ++) {
for (int face = 1; face <= 13; face++) {
cards[((suit-1)*13)+(face-1)] = new Card(face, suit);
}
}
}
Card deal () {
if (count <= 0)
return null;
count = count - 1;
return cards[count];
}
Card highest () {
if (count <= 0)
return null;
Card h = cards[0];
for (int i = 1; i < count; i++) {
if (cards[i].higherThan(h))
h = cards[i];
}
return h;
}
Card lowest () {
if (count <= 0)
return null;
Card h = cards[0];
for (int i = 1; i < count; i++) {
if (!cards[i].higherThan(h))
h = cards[i];
}
return h;
}
public void shuffle () {
if (count <= 0)
return;
for (int i = 0; i <100; i++) {
int a = (int) (Math.random() * count);
int b = (int) (Math.random() * count);
Card temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}
}
public String toString() {
String result = "Number of cards is " + count + ".\n";
result += "Highest card is " + highest() + ".\n";
result += "Lowest card is " + lowest() + ".\n";
for (int i = 0; i < count; i++) {
result += cards[i].toString() + '\n';
}
return result;
}
}
class Card {
int face;
int suit;
Card (int f, int s) {
this.face = f; //or just face = f;
this.suit = s; //or just suit = s;
return;
}
boolean higherThan (Card a) {
if (this.face > a.face)
return true;
else if (this.face < a.face)
return false;
else if (this.suit > a.suit)
return true;
else
return false;
}
public String toString() {
String result = "";
if (face == 1) result = "Ace";
if (face == 11) result = "Jack";
if (face == 12) result = "Queen";
if (face == 13) result = "King";
if (face > 1 && face < 11) result = Integer.toString(face);
result = result + " of ";
if (suit == 1) result += "Clubs";
if (suit == 2) result += "Diamonds";
if (suit == 3) result += "Hearts";
if (suit == 4) result += "Spades";
return result;
}
}
--- NEW FILE: Arrays5.java ---
//This demonstrates sorting an array.
import java.io.*;
public class Arrays5 {
public static void main (String [] args) throws IOException {
App app = new App();
app.start();
}
}
class App {
void start () throws IOException {
BufferedReader br = new BufferedReader
(new InputStreamReader(System.in));
Deck d = new Deck();
Card c = null;
boolean again = true;
while (again) {
System.out.print ("Main Menu: (D)eal (L)ist (S)huffle (O)rder (Q)uit: ");
String input = br.readLine().toLowerCase();
if (input == null || input.length() == 0)
continue;
char letter = input.charAt(0);
switch (letter) {
case 'd':
c = d.deal();
System.out.println("Card is: " + c);
break;
case 'l':
System.out.println (d);
break;
case 's':
d.shuffle();
System.out.println("Deck shuffled");
break;
case 'o':
d.sort();
System.out.println("Deck Ordered (sorted)");
break;
case 'q':
again = false;
break;
default:
System.out.println ("Reply not recongized. Please try again.");
}
}
}
}
class Deck {
int count = 52;
Card [ ] cards = new Card[52];
Deck() {
for (int suit = 1; suit <= 4; suit ++) {
for (int face = 1; face <= 13; face++) {
cards[((suit-1)*13)+(face-1)] = new Card(face, suit);
}
}
}
Card deal () {
if (count <= 0)
return null;
count = count - 1;
return cards[count];
}
void sort () {
boolean swap = true;
while (swap) {
swap = false;
for (int i = 0; i <= count - 2; i++) {
if (!cards[i].higherThan(cards[i+1])) {
Card temp = cards[i];
cards[i] = cards[i+1];
cards[i+1] = temp;
swap = true;
}
}
}
}
Card highest () {
if (count <= 0)
return null;
Card h = cards[0];
for (int i = 1; i < count; i++) {
if (cards[i].higherThan(h))
h = cards[i];
}
return h;
}
Card lowest () {
if (count <= 0)
return null;
Card h = cards[0];
for (int i = 1; i < count; i++) {
if (!cards[i].higherThan(h))
h = cards[i];
}
return h;
}
public void shuffle () {
if (count <= 0)
return;
for (int i = 0; i <100; i++) {
int a = (int) (Math.random() * count);
int b = (int) (Math.random() * count);
Card temp = cards[a];
cards[a] = cards[b];
cards[b] = temp;
}
}
public String toString() {
String result = "Number of cards is " + count + ".\n";
result += "Highest card is " + highest() + ".\n";
result += "Lowest card is " + lowest() + ".\n";
for (int i = 0; i < count; i++) {
result += cards[i].toString() + '\n';
}
return result;
}
}
class Card {
int face;
int suit;
Card (int f, int s) {
this.face = f; //or just face = f;
this.suit = s; //or just suit = s;
return;
}
boolean higherThan (Card a) {
if (this.face > a.face)
return true;
else if (this.face < a.face)
return false;
else if (this.suit > a.suit)
return true;
else
return false;
}
public String toString() {
String result = "";
if (face == 1) result = "Ace";
if (face == 11) result = "Jack";
if (face == 12) result = "Queen";
if (face == 13) result = "King";
if (face > 1 && face < 11) result = Integer.toString(face);
result = result + " of ";
if (suit == 1) result += "Clubs";
if (suit == 2) result += "Diamonds";
if (suit == 3) result += "Hearts";
if (suit == 4) result += "Spades";
return result;
}
}
|