Collectable Types and Collections
Notebook from Trimester 3 (3/28/2023). This is a deeper review on the data structure, collections, which requires a deeper understanding of objects and the generic type.
Hack Instructions
- Watch the college board video Classes and Objects* Blog and Define the details of a Class: Access modifier, constructor, modifiers/setters, getters, etc.
- Build example code in jupyter notebook with Linked List, Queues, and Stacks.
- Show familiarity with managing data (aka nodes in LL) in these structures.
- Show familiarity with Generic data and ForEach loop support, similar to ArrayLists T. Here is sample Java Generic T and the Java Iterable interface by Geeks4Geeks.
College Board Video Notes
Public Interface of a Class
public class Person
{
public Person(String name, int age, String Location)
{...}
public String getName()
{...}
public in getAge()
{...}
public String getLocation()
{...}
public void changeLocation(String newLocation)
{...}
public void haveABirthday()
{...}
}
- we have a constructor with parameters
- the parameters show that the person has a name, age, and location
- three methods which are getName, getAge, and getLocation
- accessor methods, return a value (string or int)
- mutator or modifier methods that change the state of the object
- change the instance variables somehow (one has a parameter and another doesn't)
Construction Statements
- Person teacher = new Person("Mr. Schultz", 39, "Ohio");
- Person student = new Person("Ketevan", 16, "Virginia");
- person teacher and student is the class name (needed to declare class variable)
- student and teacher is the name of the object
- keyword new constructs the object
- constructor is the same name as the class (in this case, Person)
- actual parameters, in the parameters
- two distinct objects using the same class (similar to a blueprint)
Using Objects
public static void main(String[] args)
{
Person teacher = new Person("Mr. Schultz", 39, "Ohio");
Person student = new Person("Ketevan", 16, "Virginia");
String.location = teacher.getLocation();
System.out.println(location);
student.haveABirthday();
System.out.println(student.getName());
System.out.println(student.getAge());
teacher.changeLocation("Florida");
System.out.println(teacher.getName());
System.out.println(teacher.getAge());
}
- teacher.getLocation is the syntax object.method
- returns a variable (because it is an accessor method) and storing and catching the returned string into a variable
- then prints Ohio
- student.haveABirthday is a mutator method (no return type)
- not part of an assignment statement
- not trying to catch anything that gets returned
- call object.method and modify the state of the object
- changes age
- calling object.method in the context of a println statement
- whatever is returned is going to go into system.out.println statement
- prints out Ketevan and 17 because he had a birthday
- whatever is returned is going to go into system.out.println statement
- teacher object.method calls mutator method again (changeLocation)
- sending a parameter this time though (Florida)
- teacher.getName and teacher.getLocation are the accessor methods and return values right into print line statement
- prints Mr. Schultz and Florida
Using static methods
- the Math class provides static methods that we can utilize through class.method convention
- classes have methods and don't have to create objects to use the methods in the class
- examples
- System.out.println(Math.abs(-45));
- System.out.println(Math.pow(2,4));
- System.out.println(Math.sqrt(16.0));
- Math.abs, Math.pow, and Math.sqrt are all class.method
- prints out 45, 16, and 4.0
Using String Class Objects
- string is a class we create objects with
- we don't have to say = new String, can just use ""
- String word = "theater"
- index values starts from 0
- we don't have to say = new String, can just use ""
- to use methods of a string class on a string object, use object.method again
- System.out.println(word.length());
- System.out.println(word.substring(1,5));
- System.out.println(word.indexOf("ate"));
- string object is word
- word.length is accessor method that returns the number of characters in the string (7)
- word.substring(1,5) returns the substring beginning at index number 1 all the way to index number 5 (heat)
- word.indexOf("ate") gives a smaller string tells the first location, or the starting location, of that string in string object (3)
Hack Challenge 1
Add and Delete elements from Queue. Working with the code that is given, you will need to adjust Add and write Delete, to output from the Queue as follows.
Enqueued data: seven
Words count: 1, data: seven
Enqueued data: slimy
Words count: 2, data: seven slimy
Enqueued data: snakes
Words count: 3, data: seven slimy snakes
Enqueued data: sallying
Words count: 4, data: seven slimy snakes sallying
Enqueued data: slowly
Words count: 5, data: seven slimy snakes sallying slowly
Enqueued data: slithered
Words count: 6, data: seven slimy snakes sallying slowly slithered
Enqueued data: southward
Words count: 7, data: seven slimy snakes sallying slowly slithered southward
Dequeued data: seven
Words count: 6, data: slimy snakes sallying slowly slithered southward
Dequeued data: slimy
Words count: 5, data: snakes sallying slowly slithered southward
Dequeued data: snakes
Words count: 4, data: sallying slowly slithered southward
Dequeued data: sallying
Words count: 3, data: slowly slithered southward
Dequeued data: slowly
Words count: 2, data: slithered southward
Dequeued data: slithered
Words count: 1, data: southward
Dequeued data: southward
Words count: 0, data: null