First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Chapter 8 Arrays and Strings
 Pearson Education, Inc. All rights reserved Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Review of Simple/Primitive Data Types A simple/primitive data type can store only one single value. This means an int can only store one integer. A double.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Introduction Chapter 12 introduced the array data structure. Historically, the array was the first data structure used by programming languages. With.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Java Arrays  Java has a static array capable of multi-dimensions.  Java has a dynamic array, which is also capable of multi-dimensions.  The ArrayList.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Review of Simple/Primitive Data Types A simple/primitive data type can store only one single value. This means an int can only store one integer. A.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Introduction Chapter 12 introduced the array data structure. Historically, the array was the first data structure used by programming languages. With.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Introduction Correct program design makes the complexity of today's program requirements possible. The average student thinks that any program over 1000.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Java Arrays  Java has a static array capable of easy multi-dimensions.  Java has a dynamic array, which is also capable of multi-dimensions, but it.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Exposure Java 2011 APCS Edition
AP Java Elevens Lab.
MC Question Strategies
Exposure Java 2014 APCS Edition
PowerPoint Presentation Authors of Exposure Java
Arrays! Introduction to Data Structures.
with the ArrayList Class
Chapter 10 Slides Java Static 1D & 2D Arrays.
Section 11.1 Introduction to Data Structures.
An Introduction to Java – Part I, language basics
Section 11.1 Introduction to Data Structures.
Exposure Java 2015 AP®CS Edition
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Introduction to Data Structure
Take out a piece of paper and PEN.
Take out a piece of paper and PEN.
Take out a piece of paper and PEN.
Take out a piece of paper and PEN.
Presentation transcript:

First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.

Data Structure Starting Point Any data type that can store more than one value is a data structure.

First Array Definition An array is a data structure with one, or more, elements of the same type. A 1-dimensional array is frequently called a vector. A 2-dimensional array is frequently called a matrix. The array is the first historical data structure which was introduced in the language FORTRAN.

Record Definition A record is a data structure with one, or more, elements, called fields, of the same or different data types. The language FORTRAN did NOT have records which is why it was NOT good for business. COBOL (Common Business Oriented Language) introduced the record data structure.

A Note About Classes A class is a record that can also store methods.

File Definition A file is an internal data structure - with an unspecified number of elements of the same type - assigned to an external file name. The file data structure allows transfer of data between internal and external storage.

Stack Definition A stack is a data structure with elements of the same type. Data elements of the stack data structure can only be accessed (stored or retrieved) at one end of the stack in a LIFO (Last In, First Out) manner.

Improved Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types. The storing and retrieval of the data elements is performed by accessing methods that characterize the data structure.

First Array Definition Again An array is a data structure with one, or more, elements of the same type.

Improved Array Definition An array is a data structure with a fixed number of elements of the same type. Every element of the array can be accessed directly.

Array Example [16] Ingrid [17] Darlene [18] Gene [19] Sean [20] Stephanie [11] Holly [12] Blake [13] Michelle [14] Remy [15] Haley [06] Diana [07] Jessica [08] David [09] Anthony [10] Alec [01] Isolde [02] John [03] Greg [04] Maria [05] Heidi

// Java1101.java // This program declares 10 different int variables. // Each variable is assigned a value and each variable value is displayed. // This approach is very inefficient for a large number of variables. public class Java1101 { public static void main(String args[]) { int number0 = 100; int number1 = 101; int number2 = 102; int number3 = 103; int number4 = 104; int number5 = 105; int number6 = 106; int number7 = 107; int number8 = 108; int number9 = 109; System.out.print(number0 + " "); System.out.print(number1 + " "); System.out.print(number2 + " "); System.out.print(number3 + " "); System.out.print(number4 + " "); System.out.print(number5 + " "); System.out.print(number6 + " "); System.out.print(number7 + " "); System.out.print(number8 + " "); System.out.print(number9 + " "); System.out.println(); }

// Java1102.java // This program declares an array of 10 elements. // Each array element value is individually assigned and displayed. // There does not appear any real benefit from the from program example. public class Java1102 { public static void main(String args[]) { int list[]; // declares the array object identifier list = new int[10]; // allocates memory for 10 array elements list[0] = 100; list[1] = 101; list[2] = 102; list[3] = 103; list[4] = 104; list[5] = 105; list[6] = 106; list[7] = 107; list[8] = 108; list[9] = 109; System.out.print(list[0] + " "); System.out.print(list[1] + " "); System.out.print(list[2] + " "); System.out.print(list[3] + " "); System.out.print(list[4] + " "); System.out.print(list[5] + " "); System.out.print(list[6] + " "); System.out.print(list[7] + " "); System.out.print(list[8] + " "); System.out.print(list[9] + " "); System.out.println(); } Index Value

Array Index Note Java arrays indicate individual elements with an index inside two brackets, following the array identifier, like list[3] The array index is always an integer and starts at 0. In an array of N elements, the largest index is N-1.

// Java1103.java // The previous program with separate statements for each array member // assignment and display is now replaced with two loops. The loop counter, // index, is used to specify each array element in an efficient manner. public class Java1103 { public static void main(String args[]) { int list[]; list = new int[10]; for (int index = 0; index <=9; index++) list[index] = index + 100; for (int index = 0; index <=9; index++) System.out.print(list[index] + " "); System.out.println(); } Index Value

Defining Static Arrays int list[]; // declares the array list identifier list = new int[10]; // allocates memory for 10 integers char names[]; // declares the names array identifier names = new char[25]; // allocates memory for 25 characters double grades[]; // declares the grades array identifier grades = new double[50]; // allocates memory for 50 doubles

Defining Static Arrays Preferred Method int list[ ] = new int[10]; char names[ ] = new char[25]; double grades[ ] = new double[50];

This is similar to what you learned in Chapters 3 & 6. ChapterThis will work:This is preferred: 3 int x; x = 5; int x = 5; 6 Bank tom; tom = new Bank(); Bank tom = new Bank(); 11 int list[ ]; list = new int [10]; int list[ ] = new int[10];

// Java1104.java // This program is the same list array and the same list values // as the previous program. This time note that the array declaration // is accomplished with a single statement. public class Java1104 { public static void main(String args[]) { int list[] = new int[10]; for (int index = 0; index <=9; index++) list[index] = index + 100; for (int index = 0; index <=9; index++) System.out.print(list[index] + " "); } Index Value

// Java1105.java // This program demonstrates how to initialize array elements. // The operator is not necessary in this case. public class Java1105 { public static void main(String args[]) { int list[] = {100,101,102,103,104,105,106,107}; for (int k = 0; k <= 7; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } This is called an initializer list. Note that the size of the array does not need to be specified.

// Java1105.java // This program demonstrates how to initialize array elements. // The operator is not necessary in this case. public class Java1105 { public static void main(String args[]) { int list[] = {100,101,102,103,104,105,106,107}; for (int k = 0; k <= 7; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } list[0] = 100 list[1] = 101 list[2] = 102 list[3] = 103 list[4] = 104 list[5] = 105 list[6] = 106 list[7] = 107

// Java1106.java // This program demonstrates a character array and a string array. // Both arrays use an initializer list. public class Java1106 { public static void main(String args[]) { char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; for (int k = 0; k < 26; k++) System.out.print(list1[k]); System.out.println("\n"); String list2[] = {"John","Greg","Maria","Heidi","Diana","David"}; for (int k = 0; k < 6; k++) System.out.println(list2[k]); System.out.println(); } ABCDEFGHIJKLMNOPQRSTUVWXYZ list JohnGregMariaHeidiDianaDavid ABCDEFGHIJKLMNOPQRSTUVWXYZ John Greg Maria Heidi Diana David

// Java1106.java // This program demonstrates a character array and a string array. // Both arrays use an initializer list. public class Java1106 { public static void main(String args[]) { char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; for (int k = 0; k < 26; k++) System.out.print(list1[k]); System.out.println("\n"); String list2[] = {"John","Greg","Maria","Heidi","Diana","David"}; for (int k = 0; k < 6; k++) System.out.println(list2[k]); System.out.println(); } Try This! Add one or more names to this list. Will they show up in the output? Why?

// Java1106.java // This program demonstrates a character array and a string array. // Both arrays use an initializer list. public class Java1106 { public static void main(String args[]) { char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; for (int k = 0; k < 26; k++) System.out.print(list1[k]); System.out.println("\n"); String list2[] = {"John","Greg","Maria","Heidi","Diana","David"}; for (int k = 0; k < 6; k++) System.out.println(list2[k]); System.out.println(); } Now Try This! Remove several names from this list. Does the program still work? Why?

// Java1107.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 17 // to observe what happens when the length field is altered. public class Java1107 { public static void main(String args[]) { String names[ ] = {"Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n ; k++) System.out.println("names[" + k + "] = " + names[k]); //names.length = 10; } 0123 JoeTomSueMeg There are 4 array elements. names[0] = Joe names[1] = Tom names[2] = Sue names[3] = Meg

// Java1107.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 16 // to observe what happens when the length field is altered. public class Java1107 { public static void main(String args[]) { String names[ ] = { "Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n ; k++) System.out.println("names[" + k + "] = " + names[k]); //names.length = 10; } Try This! Add one or more names to this list. Will they show up in the output? Why? Why is this different from program Java1106?

// Java1107.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 16 // to observe what happens when the length field is altered. public class Java1107 { public static void main(String args[]) { String names[ ] = { "Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n ; k++) System.out.println("names[" + k + "] = " + names[k]); //names.length = 10; } Now Try This! Remove several names from this list. Does the program still work? Why? Why is this different from program Java1106?

// Java1107.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 16 // to observe what happens when the length field is altered. public class Java1107 { public static void main(String args[]) { String names[ ] = { "Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n ; k++) System.out.println("names[" + k + "] = " + names[k]); //names.length = 10; } Try This Also! Remove the comment symbol from the last program statement in this program. Will the program still compile?

// Java1107.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 16 // to observe what happens when the length field is altered. public class Java1107 { public static void main(String args[]) { String names[ ] = { "Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n ; k++) System.out.println("names[" + k + "] = " + names[k]); names.length = 10; } The length field is a final or constant attribute just like the PI in Math.PI NO!

Static Arrays vs. Dynamic Arrays The size or length of a static array cannot be changed. This is why it is called a static array. The size of a dynamic array can be changed. You will learn about dynamic arrays in the next chapter.

// Java1108.java // This program fills an array with a random set of numbers. public class Java1108 { public static void main(String[] args) { int list[] = new int[20]; for (int k = 0; k < 20; k++) { double temp = Math.random() * 900; int rand = (int) temp + 100; list[k] = rand; } for (int k = 0; k < 20; k++) System.out.println("list[" + k + "] = " + list[k]); } list[0] = 244 list[1] = 594 list[2] = 902 list[3] = 385 list[4] = 949 list[5] = 792 list[6] = 636 list[7] = 894 list[8] = 126 list[9] = 103 list[10] = 938 list[11] = 592 list[12] = 584 list[13] = 112 list[14] = 766 list[15] = 791 list[16] = 447 list[17] = 298 list[18] = 596 list[19] = 243

// Java1109.java // This program will display 15 random sentences. // With 7 different ranks, 7 different people, 7 different actions and 7 different locations, // there are more than 2400 different sentences possible. Random random = new Random(); String rank[ ] = {"Private","Corporal","Sargent","Lieutenant","Captain","Major","General"}; String person[ ] = {"Smith", "Gonzales", "Brown", "Jackson", "Powers", "Jones", "Nguyen"}; String action[ ] = {"drive the tank", "drive the jeep", "take the troops", "bring all supplies", "escort the visitor", "prepare to relocate", "bring the Admiral"}; String location[ ] = {"over the next hill", "to the top of the mountain", "outside the barracks", "30 miles into the dessert", "to the middle of the forest", "to my present location", "to anywhere but here"}; for (int j = 1; j <= 15; j++) { int randomRank = random.nextInt(rank.length); int randomPerson = random.nextInt(person.length); int randomAction = random.nextInt(action.length); int randomLocation = random.nextInt(location.length); String sentence = rank[randomRank] + " " + person[randomPerson] + " " + action[randomAction] + " " + location[randomLocation] + "."; System.out.println("\n" + sentence); }

Sargent Brown, bring all supplies outside the barracks. Major Brown, drive the tank to anywhere but here. General Smith, prepare to relocate to the middle of the forest. Corporal Jones, bring the Admiral over the next hill. General Jackson, take the troops outside the barracks. Captain Brown, escort the visitor to my present location. Corporal Powers, drive the tank to my present location. Private Smith, drive the jeep to my present location. Captain Jackson, escort the visitor to anywhere but here. Corporal Smith, bring the Admiral over the next hill. Corporal Smith, drive the jeep to anywhere but here. Corporal Gonzales, prepare to relocate outside the barracks. Major Gonzales, escort the visitor to the top of the mountain. Major Smith, escort the visitor to the top of the mountain. Lieutenant Gonzales, escort the visitor over the next hill.

// Java1110.java // This program introduces the Java Version 5.0 enhanced loop // with an array. public class Java1110 { public static void main(String args[]) { int list[] = {11,22,33,44,55,66,77,88,99}; for (int k = 0; k loop syntax System.out.print(list[k] + " "); System.out.println("\n\n"); for (int item: list)// New loop syntax System.out.print(item + " "); System.out.println("\n\n"); }

// Java1111.java // This program uses the Java Version 5.0 loop with a array. public class Java1111 { public static void main(String args[]) { String names[] = {"Tom","Sue","Joe","Jan","Bob","Lee","Ann","Meg"}; for (int k = 0; k loop syntax System.out.print(names[k] + " "); System.out.println("\n\n"); for (String name: names)// New loop syntax System.out.print(name + " "); System.out.println("\n\n"); } Tom Sue Joe Jan Bob Lee Ann Meg

// Java1112.java // This program demonstrates a very generalized loop usage // with the class. public class Java1112 { public static void main(String args[]) { System.out.println("Java1112\n"); String names[] = {"Tom","Sue","Joe","Jan","Bob","Lee","Ann","Meg"}; for (int k = 0; k < 8; k++) System.out.print(names[k] + " "); System.out.println("\n\n"); for ( Object obj: names) System.out.print(obj + " "); System.out.println("\n\n"); }

Enhancing the for Loop The enhanced for loop is called the for.. each loop. The new loop structure does not replace the older for loop, because it is not possible to access specific array elements. int numbers[ ] = {1,2,3,4,5,6,7,8,9}; for (int number: numbers) System.out.print(number + " ");

// Java1112.java // This program shows that actual parameters are not // altered by a call to the method. public class Java1112 { public static void main(String[] args) { int p = 10; int q = 20; System.out.println(p + " " + q); swap(p,q); System.out.println(p + " " + q); } public static void swap (int x, int y) { System.out.println(x + " " + y); int temp = x; x = y; y = temp; System.out.println(x + " " + y); }

// Java1113.java // In this program it appears that a call to the // can alter values in the calling method. public class Java1113 { public static void main(String[] args) { int[ ] list = {111,222,333,444,555,666,777,888,9999}; System.out.println(list[3] + " " + list[4]); swap(list,3,4); System.out.println(list[3] + " " + list[4]); } public static void swap(int[ ] x, int p, int q) { int temp = x[p]; x[p] = x[q]; x[q] = temp; }

// Card01.java // This class is the version, // which was introduced in Chapter IX. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Card01 { private String suit; private String rank; private int pointValue; public Card01(String s, String r, int pV) { suit = s; rank = r; pointValue = pV; } public String getSuit() { return suit;} public String getRank() { return rank; } public int getPointValue() { return pointValue; } public void setSuit(String s) { suit = s; } public void setRank(String r) { rank = r; } public void setPointValue(int pV) { pointValue = pV; } }

// Card02.java // The class re-defines the method, // which is used to display the attributes. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Card02 { private String suit; private String rank; private int pointValue; public Card01(String s, String r, int pV) { suit = s; rank = r; pointValue = pV; } public String getSuit() { return suit;} public String getRank() { return rank; } public int getPointValue() { return pointValue; } public void setSuit(String s) { suit = s; } public void setRank(String r) { rank = r; } public void setPointValue(int pV) { pointValue = pV; } public String toString() { return "[" + suit + ", " + rank + ", " + pointValue + "]"; }

// CardTester02.java // This program tests the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class CardTester02 { public static void main(String[] args) { Card02 card = new Card02("Spades","Ace",11); System.out.println("Suit: " + card.getSuit()); System.out.println("Rank: " + card.getRank()); System.out.println("Value: " + card.getPointValue()); System.out.println(); card.setSuit("Hearts"); card.setRank("Seven"); card.setPointValue(7); System.out.println(card); } Suit: Spades Rank: Ace Value: 11 [Hearts, Seven, 7]

// Card03.java // The class adds the method. // This class now is identical to the class of the "Elevens" lab. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Card03 { private String suit; private String rank; private int pointValue; public Card03(String s, String r, int pV) { suit = s; rank = r; pointValue = pV; } public String suit() { return suit; } public String rank() { return rank; } public int pointValue() { return pointValue; } public boolean matches(Card03 otherCard) { return otherCard.suit().equals(this.suit()) && otherCard.rank().equals(this.rank()) && otherCard.pointValue() == this.pointValue(); } public String toString() { return rank + " of " + suit + " (point value = " + pointValue + ")"; } }

// CardTester03.java // This program tests the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class CardTester03 { public static void main(String[] args) { Card03 card1 = new Card03("Spades","Ace",11); Card03 card2 = new Card03("Seven","Clubs",7); Card03 card3 = new Card03("Spades","Ace",11); System.out.println(card1); System.out.println(card2); System.out.println(card3); System.out.println(); System.out.println(card1.matches(card2)); System.out.println(card1.matches(card3)); System.out.println(card2.matches(card3)); } Ace of Spades (point value = 11) Clubs of Seven (point value = 7) Ace of Spades (point value = 11) false true false

// Deck01.java // The first stage of the is very basic. // It shows how the class is used to create a // array as a data structure attribute of the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Deck01 { private Card[] cards; private int size; public Deck01() { cards = new Card[52]; size = 0; } public int size() { return size; } public boolean isEmpty() { return size == 0; } }

// DeckTester01.java // This program tests the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class DeckTester01 { public static void main(String[] args) { Deck01 deck = new Deck01(); System.out.println(deck); System.out.println(deck.size()); System.out.println(deck.isEmpty()); } 0 true

// Deck02.java // Methods and are added to the class. // Objects of the class can now be stored in the array. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Deck02 { // only new methods shown public void add(String suit, String rank, int pointValue) { Card temp = new Card(suit,rank,pointValue); cards[size] = temp; size++; } public void display() { for (int k = 0; k < size; k++) System.out.println(cards[k]); System.out.println(); }

// DeckTester02.java // This program tests the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class DeckTester02 { public static void main(String[] args) { Deck02 deck = new Deck02(); deck.add("Clubs","Three",3); deck.add("Diamonds","Four",4); deck.add("Hearts","Five",5); deck.add("Spades","Six",6); deck.display(); System.out.println(deck.size()); System.out.println(deck.isEmpty()); } Three of Clubs (point value = 3) Four of Diamonds (point value = 4) Five of Hearts (point value = 5) Six of Spades (point value = 6) 4 false

// Deck02.java // Methods and are added to the class. // Objects of the class can now be stored in the array. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Deck02 { // only new methods shown public void add(String suit, String rank, int pointValue) { Card temp = new Card(suit,rank,pointValue); cards[size] = temp; size++; } public void display() { for (Card card: cards) System.out.println(card); System.out.println(); } Try This! Change the display method so that it uses the for..each loop. Recompile and execute Decktester02.java.

:::::::::::::::::::: The static array requires a memory block for 52 cards to handle a full deck. Currently only 4 cards are entered.

Important Note: You just saw an example where a fundamental question was raised. Does the new for..each loop provide the same output as the traditional index for loop? Both loops do display the array elements, but the indexed loop can be easily controlled to only include those elements that reference a deeper value. It is precisely in the asking of the what..if questions, followed by some experimenting that students learn the most about programming.

// Deck03.java // The method is now replaced by the method. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class Deck03 { // only new methods shown public String toString() { String temp = ""; for (int k = 0; k < size; k++) temp = temp + cards[k].toString() + "\n"; return temp; }

// DeckTester03.java // This program tests the class. // ////////////////////////////////////////////////////////// // This program is designed to explain the "Elevens" lab created // by Michael Clancy, Robert Glen Martin and Judith Hromcik. // Divided into stages and altered August 2014 by Leon Schram. public class DeckTester03 { public static void main(String[] args) { Deck03 deck = new Deck03(); deck.add("Clubs","Three",3); deck.add("Diamonds","Four",4); deck.add("Hearts","Five",5); deck.add("Spades","Six",6); System.out.println(deck); System.out.println(deck.size()); System.out.println(deck.isEmpty()); } Three of Clubs (point value = 3) Four of Diamonds (point value = 4) Five of Hearts (point value = 5) Six of Spades (point value = 6) 4 false