CS 2420 Class notes week 2
CS 2420 Class notes week 2 CS 2420
Popular in Intro Algorithms and Data Structures
verified elite notetaker
Popular in Computer science
verified elite notetaker
This 4 page Class Notes was uploaded by Nick on Monday September 5, 2016. The Class Notes belongs to CS 2420 at University of Utah taught by Miriah Meyer in Fall 2016. Since its upload, it has received 55 views. For similar materials see Intro Algorithms and Data Structures in Computer science at University of Utah.
Reviews for CS 2420 Class notes week 2
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 09/05/16
CS 2420 This should all be a review Primitive types are different than reference types. When pass a variable to a function it gets copied into that function so if try to use the passed variable after passing it, then if it is a primitive then it will not be changed, but if it is a reference type then it will be changed. Be familiar with Arrays and ArrayLists, they are your friends in java. Object oriented programming Data is treated as encapsulated in objects. Objects are instantiations of classes. Not talking about the specific Object class (big O) Inheritance prevents the need for duplication of code when creating similar things. The keyword for inheritance is extends Override changes the implementation of an inherited method only in the inherited class. So can use the same name for both and get specific use of code. We can partially override to use the base class and add to it directly. Polymorphism A derived class is compatible with a base class Java will use the most specific class possible. Every class inherits from the Object class in the Java language. Abstract classes are a kind of class Uses the keyword abstract This means that this class cannot be used to make an object. If Shape is an abstract method then cannot do this: Shape s = new Shape (); Interface is a kind of class that is thought of as the ultimate abstract class. This means that this class cannot be used to make an object and no methods in it have bodies. Uses the keyword implements Provide a contract that guarantees objects of a certain type can do certain things. All abstract methods must be implemented in subclasses. Subclasses can only implement multiple interfaces but can only extend one base class. We will use the Comparable interface a lot Inheriting Comparable means that can use the compareTo method. Read Ch. 3 & 4 Assignment 1 is due Wed 83116 Generic types T is a variable name. But T is the convention expected by programmers. Can have more than one generic Assignment 2 uses generics Cannot use an instantiated generic type as one of its parents. Where us generics, it must be exactly the same type. Cannot pass in a Triangle to a function parameter generic asking for a Shape. Can use the question mark '?' as a wildcard. < ? extends Shape> will allow Triangle which extends from Shape <?> allows anything After using < ? extends Shape> it only knows Shapes. It does not know if anything extends shape. After using generic as a parameter, the generic data structure used cannot be changed. Now illegal to modify shapes in a method that uses this. Not a good idea to work with inherited type after importing a parent type into the function parameter. This is how you use generics. If find yourself needing to ‘break’ java to get around it’s failsafe’s, then probably doing it wrong and should talk to a TA. Generics allow checking at compile time not runtime. Compile time errors are better than runtime errors Generics only work with reference types, so use wrapper classes for primitives: Integer, Double … Static methods can have their ow generic types <T> Can refer to T as a type inside that method only. Function objects Object that contains a single method Every sorting function wants to sort any type. To do this, need to compare and determine which is larger. Comparable interface defines a natural ordering. So can write a sorting function that takes a Comparator. The compare method needs to return an integer Comparator is different than CompareTo Collection interface A generic data structure. Doesn’t say how the items are held. Specifies the methods that such a class should have. Will use this for Assign 3 Will use an array not ArrayList. Items must be sorted as they are entered and should not allow duplicates. When fill up the data structure underneath then need to grow it. Good to double the size instead of adding one element. And, then copy over to new array. And set the reference to the new array. To prevent holes in the data in the underlying array you need to shift everything over. Collection is like an array but can have more complex behaviors. Iterators Not all data structures allow looping over an array with a for loop because there is no array in it. The iterator knows how to traverse through all elements of the data structure. hasNext: determines f iteration is complete next: gets the next item … To use remove need to have called next () at least once. Enhanced for loop for(Thing t : things) Do something with t It doesn’t care how you visit each item Read Ch. 5 & 6 No Class on Monday Next class will cover a lot of material.