Docsity

Introduction to software design , Summaries of Software Engineering

Partial preview of the text

Download Introduction to software design and more Summaries Software Engineering in PDF only on Docsity!

J. Ousterhout,

Introduction to software design

TEST 2 REVIEW

this and super The difference between this and super: โ— this represents the current instance of a class โ— super represents the current instance of the parent class. How to use each one to call a method โ— Using this.act( ) would invoke the method in the current instance of the class โ— Using super.act( ) would invoke the method in the current instance of the parent class How to use each one to call one constructor from another โ— You can use this( ) to call no argument constructor of same class. โ— You can use super( ) to call no argument or default constructor of the parent class. โ— Reference variable that is declared โ€œfinalโ€ cannot ever be reassigned References What a reference variable is โ— Using a reference variable, we can access an object. โ— References are stored in reference variables โ—‹ Reference variables can hold Fields, parameters, or local variables โ— A reference variable is declared to be of a โ€œspecific typeโ€ and that type can never be changed. The object r1 = r2; โ— Assignment copies the reference. It does not duplicate โ— Object variables are simply pointers to objects โ— Primitive variables (ints booleans, doubles, etc.) ARE NOT reference variables because they store the variables themselves. โ— The โ€œ==โ€ tests identity, not equality, use equals( ) to check for equality What null means, and how to check for null โ— A very important principle is that, if a variable contains the null value, a method call should not be made on it. โ— We cannot call a method if the variable does not refer to an object How to handle null parameter values in your methods โ— To check for null parameter values in methods, we sometimes have to use an if-statement to test whether a variable contains null or not, before calling a method on that variable.

if (... actors.get( i ) ...//Desired condition(goal)) { Return actors.get( i ); } } // Find the last object List actors = โ€ฆ; for (int i = actors.size( ) - 1; i >= 0; i--) { if (... actors.get( i ) โ€ฆ) { return actors.get( i ); } } How to โ€œexit earlyโ€ โ— return terminates the method (and the loop) โ— break terminates the loop only โ— https://canvas.vt.edu/courses/29807/files/1535781?module_item_id= Containers (e.g., Lists) โ— How to add/remove elements ArrayList num = new ArrayList( ); num.add( 5 ); // Adds element to the end of this list num.add( 0, 4 ); // Adds element to the first position in list num.remove( 1 ); // Removes element in the second position in list num.set(0, 10); // Changes first element from 4 to 10 โ— How to repeat over all elements โ—‹ For each loop? โ— The difference between an interface (such as List) and a class that implements the interface (such as ArrayList) โ—‹ An interface is not a class - it definses a set of methods that one or more classes may provide โ—‹ Contains only method signatures and constants (no constructors, no method bodies) โ—‹ A class that implements an interface must provide all of the methods the interface defines. โ—‹ You cannot create an instance of an interface-you can only create instances of classes that implement the interface

โ— How to specify generic parameters correctly โ—‹ When declaring variables โ—‹ When creating objects More for Lists โ€ฆ How to access elements by position get( index of element ) How to insert or remove elements by position add( position desired , element ) remove( position desired ) How insertion/removal affects order โ— Inserting puts moves position of everything to the right 1 โ— Remove shifts everything after it to the left How to write a loop over list elements using an arithmetically-controlled loop for (int i = 0; i < size( ); i++) { Stuff here; } Methods that return values โ€ฆ How to write methods that return values, and how to write methods that do not public return type (like int, String, boolean, Car) getMethod( ) { return something; } public void changeThis() { Changes something, no return; } How the return statement works โ— Where a method contains a return statement, it is always the final statement of that method, because no further statements in the method will be executed once the return statement is executed. โ— Used to returning a value to match the return type in the methodโ€™s header. How to correctly declare the signature for a method that returns a value (of any type) โ— Together, the name of a method and the parameter types found in its header are called the methodโ€™s signature. Visibility modifier, return type, method name, parameters(if any)

โ–  next(); โ–  hasNext(); โ—‹ To read one line at a time โ–  nextLine(); โ–  hasNextLine(); โ—‹ To read int values โ–  nextInt(); โ–  hasNextInt() โ— How to use a PrintWriter โ—‹ To print lines โ—‹ To print multiple pieces of output on the same line Arrays โ— Difference between arrays and container classes like ArrayList โ—‹ Arrays have a fixed length, ArrayLists are dynamic in size. โ—‹ ArrayLists do not contain primitive data types (like int, float, double), it only contains Objects (autoboxing). Arrays can contain primitive and objects. โ—‹ To find length of an ArrayList, use size() โ—‹ To find length of an Array, use length โ—‹ For adding elements, in ArrayList we do the add()method while in arrays we use the assignment operator. โ—‹ Arrays can be multi dimensional, while ArrayList is always single dimensional โ— How to declare variables/fields that refer to an array โ— How to create array objects โ—‹ int[ ] numbers = {1, 2, 3, 4, 5}; โ—‹ int[ ] numbers = new int[ 20 ] (creates empty array of size 20) โ— How to access or change elements in an array โ— Arrays are objects (but without useful methods) โ— What values an arrayโ€™s cells are initialized to โ— How to use a for-each loop to traverse an array. โ— How to use a numeric for loop to traverse an array. 2D Arrays โ— How to create, access, and manipulate a 2D array โ—‹ How to create โ–  Int[][] cells = new int[rows][columns]; โ—‹ How to access

โ–  cells[1][2] would return whatโ€™s in the second row, third column. โ— A 2D array is really an โ€œarray of arraysโ€ โ— How to use nested loops (numeric for loops, or for-each) to traverse all cells in a 2D array For (int i = 0; i < num.length; i++){ for (int j = 0; j < num[i].length; j++){ Num[i][j] = some value; Maps โ— A map is a collection of key/value pairs of objects. As with the ArrayList, a map can store a flexible number of entries. โ— One difference between the ArrayList and a map is that with a Map each entry is not an object, but a pair of objects. โ— How to declare fields/variables that refer to maps โ— How to create HashMap objects โ— How to manipulate the information in a map โ—‹ Add a new key/value pair โ–  The put method inserts an entry into the map. The get method retrieves the value for a given key. โ–  put(key, value); โ–  get(key) returns the value; โ—‹ Replace a key/value pair โ—‹ Check to see if a key is defined โ–  containsKey(key) โ—‹ Look up the value associated with a key โ—‹ Get the mapโ€™s size โ–  map.size(); โ— How to use maps to represent a problemโ€™s data Interfaces โ— The difference between an interface and a class โ— How to declare an interface โ— What kinds of declarations are allowed (or prohibited) in an interface โ— What visibility level is required on all items in an interface