CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Python lists

Arrays in Java

Creating arrays

Accessing arrays

Declaring, creating and initializing arrays

Arrays of objects

Class exercises

Each of the next programs has errors.

Exercise A

class ExerciseA {

    public static void main(String[] args) {
        String[] module_codes = {"cs2513", "cs2514", "cs2515", "cs2516"};
        for (int i = 0; i < module_codes.length(); i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Solution to Exercise A

class SolutionA {

    public static void main(String[] args) {
        String[] module_codes = {"cs2513", "cs2514", "cs2515", "cs2516"};
        for (int i = 0; i < module_codes.length; i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Exercise B

class ExerciseB {

    public static void main(String[] args) {
        String[] module_codes = new int[4];
        module_codes[1] = "cs2513";
        module_codes[2] = "cs2514";
        module_codes[3] = "cs2515";
        module_codes[4] = "cs2516";
        for (int i = 0; i < module_codes.length; i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Solution to Exercise B

class ExerciseB {

    public static void main(String[] args) {
        String[] module_codes = new String[4];
        module_codes[0] = "cs2513";
        module_codes[1] = "cs2514";
        module_codes[2] = "cs2515";
        module_codes[3] = "cs2516";
        for (int i = 0; i < module_codes.length; i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Exercise C

class ExerciseC {

    public static void main(String[] args) {
        String[] module_codes = new String[4];
        module_codes[0] = "cs2513";
        module_codes[1] = "cs2514";
        module_codes[2] = "cs2515";
        module_codes[2] = "cs2516";
        for (int i = 0; i < module_codes.length; i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Solution to Exercise C

class ExerciseC {

    public static void main(String[] args) {
        String[] module_codes = new String[4];
        module_codes[0] = "cs2513";
        module_codes[1] = "cs2514";
        module_codes[2] = "cs2515";
        module_codes[3] = "cs2516";
        for (int i = 0; i < module_codes.length; i = i + 1) {
            System.out.println(module_codes[i].toUpperCase());
        }
    }
    
}

Class activity

  1. On the next slide is a very basic class definition for playing card objects.
    Q: Can you think of a fancy word (from previous lectures) to describe objects created from this class definition?

    The subsequent slide contans a main method that creates 52 playing cards, puts them in an array, and prints them out again

  2. But what we do in the lecture is better: we will write a class definition for decks of cards, whose interface can include:
    • public Deck()
    • public void shuffle()
    • public Card deal()
    The deal method will remove and return the last card in the array. We will write/discuss two ways to implement it:
    1. using a loop to copy the array of cards into a smaller array
    2. using the Arrays.copyOf class method
    (A later lecture will discuss a third, more efficient implementation)

    Q: By the way, is shuffle a getter or a setter? What about deal?

Card.java

class Card {

    int rank;
    String suit;
    
    public Card(int r, String s) {
        rank = r;
        suit = s;
    }
    
    public boolean higher(Card c) {
        return rank > c.rank;
    }
    
    public boolean lower(Card c) {
        return rank < c.rank;
    }
    
    public String toString() {
        return rank + " of " + suit;
    }
    
}

CardTester.java

class CardTester {

    public static void main(String[] args) {
        String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        Card[] cards = new Card[52];
        int indexOfNextCard = 0;
        for (int i = 0; i < suits.length; i = i + 1) {
            for (int j = 1; j < 14; j = j + 1) {
                cards[indexOfNextCard] = new Card(j, suits[i]);
                indexOfNextCard = indexOfNextCard + 1;
                // cards[i * 13 + j - 1] = new Card(j, suits[i]);
            }
        }
        for (int k = 0; k < cards.length; k = k + 1) {
            System.out.println(cards[k]);
        }
    }
}

Deck.java (Version A)

import java.util.Random;

class Deck {

    Card[] cards;
    
    public Deck() {
        String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
        cards = new Card[52];
        int indexOfNextCard = 0;
        for (int i = 0; i < suits.length; i = i + 1) {
            for (int j = 1; j < 14; j = j + 1) {
                cards[indexOfNextCard] = new Card(j, suits[i]);
                indexOfNextCard = indexOfNextCard + 1;
            }
        }
    }
    
    public void shuffle() {
        Random randy = new Random();
        for (int i = 0; i < cards.length; i = i + 1) {
            int j = randy.nextInt(cards.length);
            Card temp = cards[i];
            cards[i] = cards[j];
            cards[j] = temp;
        }
    }
    
    public Card deal() {
        Card c = cards[cards.length - 1];
        Card[] all_but_last = new Card[cards.length - 1];
        for (int i = 0; i < all_but_last.length; i= i + 1) {
            all_but_last[i] = cards[i];
        }
        cards = all_but_last;
        return c;
    }
}

Deck.java (Version B)

You must import java.utils.Arrays

    public Card deal() {
        Card c = cards[cards.length - 1];
        Card[] all_but_last = Arrays.copyOf(cards, cards.length - 1);
        cards = all_but_last;
        return c;
    }

Class activity, cont'd

  1. We will write a main method to play the game Play Your Cards Right! Bruce Forsythe
  2. But what we do next is better: we will write a class definition for the game, whose interface can include:
    • public Game()
    • public void play()
  3. The final version of the main method simply creates and launches the game!

This class activity shows you object-oriented thinking in action!

DeckTester.java

import java.util.Scanner;
class DeckTester {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int score = 0;
        Deck d = new Deck();
        d.shuffle();
        Card currentCard = d.deal();
        for (int i = 0; i < 4; i = i + 1) {
            System.out.print("Will the next card be higher (H) or lower (L) than a " + currentCard + "? ");
            String guess = sc.nextLine();
            Card nextCard = d.deal();
            System.out.print("It's a " + nextCard + ". ");
            if ((guess.equals("L") && nextCard.lower(currentCard)) || (guess.equals("H") && nextCard.higher(currentCard))) {
                System.out.println("Yes!");
                score = score + 1;
            } else {
                System.out.println("Bad luck!");
            }
            currentCard = nextCard;
        }
        System.out.println("You scored " + score + " out of 4.");
    }
}

Game.java

import java.util.Scanner;
class Game {

    public void play() {
        Scanner sc = new Scanner(System.in);
        int score = 0;
        Deck d = new Deck();
        d.shuffle();
        Card currentCard = d.deal();
        for (int i = 0; i < 4; i = i + 1) {
            System.out.print("Will the next card be higher (H) or lower (L) than a " + currentCard + "? ");
            String guess = sc.nextLine();
            Card nextCard = d.deal();
            System.out.print("It's a " + nextCard + ". ");
            if ((guess.equals("L") && nextCard.lower(currentCard)) || (guess.equals("H") && nextCard.higher(currentCard))) {
                System.out.println("Yes!");
                score = score + 1;
            } else {
                System.out.println("Bad luck!");
            }
            currentCard = nextCard;
        }
        System.out.println("You scored " + score + " out of 4.");
    }
}

GameTester

class GameTester {

    public static void main(String[] args) {
        Game g = new Game();
        g.play();
    }
    
}