Passed C169, ask me anything!(O)

Sorry the spacing is a bit poor due to me copying and pasting from oneNote

Summary

Programming Environment • The Java compiler translates source code into class files that contain instructions for the Java virtual machine. • Classes are the fundamental building blocks of Java programs. • Every Java application contains a class with a main method. When the application starts, the instructions in the main method are executed. • Each class contains declarations of methods. Each method contains a sequence of instructions. • Pseudocode is an informal description of a sequence of steps for solving a problem. • An algorithm for solving a problem is a sequence of steps that is unambiguous, executable, and terminating.

Method • A method is a sequence of instructions with a name • Arguments are supplied when a method is called. • Methods can receive multiple arguments, but they return only one value. • Parameter variables hold the arguments supplied in the method call. • The return statement terminates a method call and yields the method result. • When the return statement is processed, the method exits immediately. • Use a return type of void to indicate that a method does not return a value.

 when you have a String object, you can invoke the length method: "Hello, World".length()
 We say that the length method is a method of the String class.
 PrintStream class has a println method, and the call: System.out.println("Hello, World!")

A method is a computation that can be used multiple times with different arguments, either in the same program or in different programs. 
Whenever a computation is needed more than once, turn it into a method.
• Eliminate replicated code or pseudocode by defining a method.

INHERITANCE AND INTERFACES • A subclass inherits data and behavior from a superclass. • You can always use a subclass object in place of a superclass object. • A subclass inherits all methods that it does not override. • A subclass can override a superclass method by providing a new implementation. • The subclass inherits all public methods from the superclass. • The extends reserved word indicates that a class inherits from a superclass.

First Program • The main method contains one or more instructions called statements • Each statement ends in a semicolon (;) • Arguments are enclosed in parentheses. • Multiple arguments are separated by commas. • Compile-time errors are often called syntax errors • Run-time errors are caused by logical flaws in the program >>> logic errors.

DATA TYPES Java supports quite a few data types: numbers, text strings, files, dates, and many others. • A variable is a storage location with a name. • When declaring a variable, you usually specify an initial value, you also specify the type of its values. • int cansPerPack = 6 >>> assignment declarations • cansPerPack = 8 >>> assignment statement • The operator is called modulus % • Use the printf method to specify how values should be formatted. • The construct %10.2f is called a format specifier: it describes how a value should be formatted. The API (Application Programming Interface) documentation lists the classes and methods of the Java library. • Classes can declare methods that are not invoked on objects. Such methods are called static methods. Math.sqrt(2) • Method that is invoked on an object is called an instance method. • Substring method to extract a part of a string • The next method of the Scanner class to read a string containing a single word. • The length method yields the number of characters in a string. • Instance variable belongs to object • Static variable belongs to class • Local variable and parameters belong method

DECISIONS • To test whether two strings are equal to each other, you must use the method called equals. Do not use the == operator to compare strings. • You must remember never to use == to compare strings. • When a decision statement is contained inside the branch of another decision statement, the statements are nested. • Nested decisions are required for problems that have two levels of decision making.

OBJECTS AND CLASSES } A class describes a set of objects with the same behavior. } Every class has a public interface: a collection of methods through which the objects of the class can be manipulated. } Encapsulation is the act of providing a public interface and hiding the implementation details } Encapsulation enables changes in the implementation without affecting users of a class. } An object's instance variables store the data required for executing its methods. } Each object of a class has its own set of instance variables. } An instance method can access the instance variables of the object on which it acts. } A private instance variable can only be accessed by the methods of its own class.

1. A mutator method changes the object on which it operates.
2. An accessor method does not change the object on which it operates.
3. For each accessor method, an object must either store or compute the result.

l A constructor initializes the instance variables of an object.
l The constructor is automatically called whenever an object is created with the new operator.
l The name of a constructor is the same as the class name.
l A class can have multiple constructors.
l By default, numbers are initialized as 0, Booleans as false, and object references as null.

LOOPS • A loop executes instructions repeatedly while a condition is true • The for loop is used when a value runs from a starting point to an ending point with a constant increment or decrement. Some people call this loop count-controlled. • The do loop is appropriate when the loop body must be executed at least once.

1. for (i = 0; i <= 5; i++) >>> 0 1 2 3 4 5 >>> loop is executed 6 times
2. for (i = 5; i >= 0; i--) >>> 5 4 3 2 1 0 >>>  Use i-- for decreasing values
3. for (i = 0; i < 9; i = i + 2) >>> 0 2 4 6 8 >>>  Use i = i + 2 for a step size of 2
4. for (i = 0; i != 9; i = i + 2) >>>  2 4 6 8 10 12 14 … (infinite loop)>>> You can use < or ≤ instead of != to avoid this problem
5. for (i = 1; i <= 20; i = i * 2) >>> 1 2 4 8 16 >>> 
6. for (i = 0; i < str.length(); i++) >>> 0 1 2 … until the last valid index of the string str

ARRAYS AND ARRAY LISTS • An array collects a sequence of values of the same type. • An array element can be used like any variable. • An array index must be at least zero and less than the size of the array. • Use the expression array.length to find the number of elements in an array. • You can use the enhanced for loop to visit all elements of an array. • Use a two-dimensional array to store tabular data.

1. An array list stores a sequence of values whose size can change.
2. The ArrayList class is a generic class: ArrayList<Type> collects elements of the specified type.
3. Use the size method to obtain the current size of an array list.
4. Use the get and set methods to access an array list element at a given index.
5. Use the add and remove methods to add and remove array list elements.
6. String last = names.get(names.size() - 1); //Gets the last element.
7. The last valid index is names.size() - 1.

INHERITANCE AND INTERFACES 1. A subclass inherits data and behavior from a superclass. 2. You can always use a subclass object in place of a superclass object. 3. A subclass inherits all methods that it does not override. 4. A subclass can override a superclass method by providing a new implementation. 5. The extends reserved word indicates that a class inherits from a superclass. 6. An overriding method can extend or replace the functionality of the superclass method. 7. Use the reserved word super to call a superclass method. 8. Unless specified otherwise, the subclass constructor calls the superclass constructor with no arguments.

◊ A subclass reference can be used when a superclass reference is expected.
◊ Polymorphism (“having multiple shapes”) allows us to manipulate objects that share a set of tasks, even though the tasks are executed in different ways.
◊ An abstract method is a method whose implementation is not specified.
◊ An abstract class is a class that cannot be instantiated.

· Override the toString method to yield a string that describes the object's state.
· The equals method checks whether two objects have the same contents.
· If you know that an object belongs to a given class, use a cast to convert the type.
· The instanceof operator tests whether an object belongs to a particular type.
/r/WGU Thread Parent