We will look more closely at the object-oriented programming and the implications of instantiated objects.
Learning Targets
I can describe what it means to be an object-oriented programmer.
I can describe the difference between values and references.
I can build unit tests to check every method in my class definition.
Class Vocab
Object-Oriented Programming
Why don't we list all of our code in one long file? How do we start to organize big projects? We start with objects. Already we've been using a class with a main method as its starting point. All of our methods have been static. What if we wanted to make a game with a lot of monsters? We can define our own Monster class and create as many instances of that object as we need in the game. We can't use static methods anymore as a result.
Let's try out a simple game in class using while-true loop and a Monster.
Encapsulation
Keep your data private, accessible only to methods that allow for more careful controls.
Primitives and Refs
importjava.util.ArrayList;publicclassPractice {publicstaticvoidmain(String[] args) {// create a primitiveint myPrimitive =5;// pass it by value to a method that adds one to the item.addOne(myPrimitive);// print the unchanged value of the primitiveSystem.out.println("Primitive: "+ myPrimitive);// instantiate a list and store the reference to it in myRefArrayList<Integer> myRef =newArrayList<>();// add the value 5 to a new Integer myRef.add(5);// pass it by reference to a method that adds one to the first element in the listaddOne(myRef);// print what's now in the first spot of the object that was passed by referenceSystem.out.println("Reference: "+myRef.get(0)); } // closes mainpublicstaticvoidaddOne(ArrayList x) {// get the current value of the first item in the list, convert to intint current = (int)x.get(0);// set the first item's value to one more than its currentx.set(0,++current); }publicstaticvoidaddOne(int x) {++x; }} // closes the class
Intro to ArrayLists
ArrayLists are introduced in this section because they're objects themselves. They illustrate some of the differences we see when working with objects. Instead of arrays when we can access an element just by printing someCollection[x], we now need to use someOtherCollection.get(x). Let's get into these differences.
Different Kind of Loop
Because ArrayLists are only accessible through methods, you can use the same type of access[index] you can with an array. Here's what a simple traversal looks like with a good ol' array:
int[] myArray = {5,10,15,20};for(int x =0; x <myArray.length; x++){System.out.println(myArray[x]);}
Notice how I used length as a property not as an accessor method() and I accessed an element from the collection using [brackets]. Now let's take a look at an ArrayList:
List myList =newArrayList<Integer>(); myList.add(5); // I could loop and add all thesemyList.add(10); myList.add(15); myList.add(20); for(int x =0; x <myList.size(); x++){System.out.println(myList.get(x));}
There's a couple important things happening above you should look closely at:
Notice how I declared a List and then turned it into an ArrayList? It wouldn't have worked if I tried to do new List because a List is abstract. It's like mammal. You can tell the computer that you'd like to make a new mammal named x and it should run the new Dog() constructor. That's a polymorphic declaration. Use a general ancestor as a type and then be more specific during the constructor.
If you look inside the ArrayList class programmed inside our Java library, you'll notice it has an array at its core. It's a wrapper for an array. So there is a .length property that's relevant to its internal, private programming. Every time you .add(something) to an ArrayList it destroys its old array and copies all the content to a new, longer array. So .length is serious business. Since that's occupied, we use the .size() accessor method to retrieve the length of an ArrayList.
SecureList: An Example
What's the point of creating our own objects? How does an ArrayList differ from an array in practical implementation? Let's go through an example project to help iIllustrate these concepts.
What is a SecureList?
SecureLists are made up. They're a silly object that provides an extra layer of "security" by storing your list of 12 names in two different objects, an array and an ArrayList. Does that actually provide additional security? No, not really. But let's pretend it does so you have an opportunity to work with an array and an ArrayList simultaneously.
Let's set up your project with the following files:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */importjava.util.Scanner;publicclassApp {// from a static method, we'll create instances of our made-up objectpublicstaticvoidmain(String[] args) throwsException {System.out.println("Let's build a list of names to test.");Scanner s =newScanner(System.in);// our example containerString[] testList =newString[12];// build an example list to work withSystem.out.print("Would you like to enter names? (y/n): ");if(s.nextLine().equals("y")){// populate the array with names from userfor(int x =0; x <12; x++){System.out.print("\nInsert name at index "+ x +": "); testList[x] =s.nextLine();System.out.println(); } } else {// populate the array with just numbersfor(int x =0; x <12; x++){ testList[x] ="Student #"+ x; } } System.out.println("\n----- SecureList Unit Tests ------\n");// test the constructorSecureList test1 =newSecureList(testList);// test title mutator and accessorSystem.out.println("\nTITLE TEST: \n");test1.title("Demo"); // mutator System.out.println(test1.title()); // accessor// implicitly call the .toString() methodSystem.out.println("\nPRINT TEST: \n");System.out.println(test1);// draw a random name from the listSystem.out.println("\nGET RANDOM: \n");System.out.println(test1.getRandom());// check that each element in both collections matchSystem.out.println("\nIS SECURE: \n");System.out.println(test1.isSecure()); // should print "true"// get the first name in the listSystem.out.println("\nGET STUDENT 0: \n");System.out.println(test1.getName(0));// get the first name in the list using the seat number not the indexSystem.out.println("\nGET SEAT 1: \n");System.out.println(test1.getStudentBySeat(1));// randomize the order of the namesSystem.out.println("\nSHUFFLE: \n");test1.shuffle();System.out.println(test1); }}
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */importjava.util.ArrayList;/** * Doubles the protection of your data by storing your 12 names in both an array * and an ArrayList */publicclassSecureList {// ENCAPSULATED INSTANCE VARIABLES// a var for the title String// uninitialized String Array// uninstantiated ArrayList typecast to String// -----------// CONSTRUCTORS// ----------- /** * This is the basic constructor that initializes all the instance variables */publicSecureList(){// create a blank title title ="";// create a blank array of Strings nameArray =newString[12];// instantiate an ArrayList and then loop and add 12 blank Strings nameList =newArrayList<String>(); for(int i =0; i <12; i++) nameList.add(""); } /** * Extracts names from an ArrayList to store in both class containers * @param nameList : starting names */publicSecureList(ArrayList<String> nameList){// call the first constructor to setup the blank stuffthis();// copy the data from the given list } /** * Extracts names from an array to store in both class containers * @param nameArray : starting names */publicSecureList(String[] nameArray){// call the first constructor to setup the blank stuffthis();// copy the data from the given array }// -----------// ACCESSORS// ----------- /** * Accessor method for the title instance variable * @return this.title */publicStringtitle(){ } /** * After checking that the names between the two strings * @param n : the zero-indexed location of the name * @return */publicStringgetName(int n){// use the isSecure method to check nif(!isSecure(n)){System.out.println("NOT SECURE");returnnull; }// return the name in the index location of n } /** * Wrapper for the getName method that is NOT zero-indexed * @param s: Seat location * @return */publicStringgetStudentBySeat(int s){ }// -----------// MUTATORS// ----------- /** * Modify this instance's title property * @param title : new label for this secure list object */publicvoidtitle(String title){ } /** * Change the name of both collections at a given index location * @param n : index location to target * @param name : new name value to set */publicvoidchangeName(int n,String name){ }// -----------// UTILITIES// ----------- /** * Checks if the given index location is a blank "" string * @param n : index location to check * @return : true if the index location had a length of zero */publicbooleanisEmpty(int n){ } /** * Returns the comparision between the two Strings in the container at position n * @param n : the zero-indexed location in the collection to check * @return true if the two Strings .equal */publicbooleanisSecure(int n){ } /** * Traverses the length of the collection and checks in location is secure. * If any is found insecure, it returns false * @return list integrity */publicbooleanisSecure(){// loop// if insecure at that postion, return false// if you make it through the loop safely, return true } /** * Draws a random index location and returns the name at that location * @return : the String value at a random index location */publicStringgetRandom(){// generate a random number from 0 up until but not including the length// return the call to the getName method } /** * Randomizes the location of all the Strings using the Selection Shuffle * algorithm */publicvoidshuffle(){ } /** * Traverses name collection, printing the seat location followed * by the name */ @OverridepublicStringtoString(){ }}