Skip to content

Activity 6: Memory Diagrams

Instructions

Example Code

Model 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
public class Arrays {

    public static void main(String[] args) {
        int[] data = null;
        int[] counts = {10, 3, 7, -5};
        double[] scores = new double[3];
        String[] words = new String[2];

        int[] nums = {159, 227};
        printArray(nums);
    }

    public static void printArray(int[] a) {
        System.out.print("{" + a[0]);
        for (int i = 1; i < a.length; i++) {
            System.out.print(", " + a[i]);
        }
        System.out.println("}");
    }

}

Model 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Card {

    private int rank;  // 1=Ace, ..., 11=Jack, 12=Queen, 13=King
    private int suit;  // 0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades

    public Card(int rank, int suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public static void main(String[] args) {
        Card card = new Card(8, 1);
    }

}

Model 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class BankAccount {
    private static final String PREFIX = "1234";
    private static int nextNumber = 1;

    private String number;
    private String owner;
    private double balance;

    public BankAccount(String owner) {
        this.number = PREFIX + String.format("%04d", nextNumber);
        this.owner = owner;
        nextNumber++;
    }

    public static void main(String[] args) {
        BankAccount ba1 = new BankAccount("Stacie");
        BankAccount ba2 = new BankAccount("Trevor");
    }
}