Chapter 1: Introduction to Computers the Internet and Java
Nuggets
- Three different versions of Java
- SE: Standard Edition
- EE: Enterprise Edition (additional components for web and distributed programming)
- ME: Micro Edition (subset of SE for resource-constrained devices)
- Java Programming Paradigms
- Procedural
- Object-Oriented
- Generic
- (Java 8) Functional Programming (easier to parallelize)
Chapter 2: Introduction to Java Applications: Input/Output and Operators
Nuggets
- Three types of comments
- // end of line
- /* */ multiple line
- /** */ javadoc
- Naming
- Class Name and Filename must match
- Class Names should begin with an Upper Case and Capitalize the first letter of each word, camel case
- Strings cannot span multiple lines
Snippets
- public static void main(String[] args) {} //program load point
- public static void main(String … args) {} //alternate program load point using elipses
- javac Welcome1.java //Compiles the java class Welcome1
- java Welcome1 //run the Welcome1 program
- import java.util.Scanner; // keyboard
- Scanner kb = new Scanner(System.in); //kb object to get nextLine, nextInt, etc.
- String name = JOptionPane.showInputDialog(“What is your name?”); //Input dialog box, stores entered value into variable “name”
- JOptionPane.showMessageDialog(null, message); //Message Dialog box
- System.out.printf(“This is a string %s and this is an integer %d and this is 2 decimal float %.2f and this is new line %n”,strAString, intANumber, floatAFloat); //Formatted Output
- Some operators
- == //equals — use with a number or to mean the same object reference, strings s.equals(aString)
- != //not equals
- <= //less than or equal to
- >= //greater than or equal to
- import javax.swing.JOptionPane; //swing class JOptionPane
Chapter 3: Control Statements Part 1: Assignment, ++, and — operators
Nuggets
- TERM: Transfer of Control: the next statement is not always the one that executes
- 3 Control Structures
- Sequence
- Selection
- Single-selection (if)
- Double-selection (if…else)
- Multiple-selection (switch)
- Repetition/Iteration/Looping
- while
- do while
- for
- enhanced for
- Conditional Operator (?:) — Java’s only ternary operator (conditional ? true : false)
- sentinel-controlled repitition — have some condition or user input to signal the end of the loop
- unary cast operator: (double) average = (double) (n1 + n2)/n3
- compound assignment operator:
- c += 7; // c = c + 7
- d -= 4; // d = d – 4
- e *= 5; // e = e * 5
- f /= 3; // f = f / 3
- g %= 9; // g = g % 9
- preincrementing / predecrementing: ++a ; –a;
- postincrementing / postdecrementing: a++; a–;
- strongly typed languague (primitive data types)
- extends (a class is an enhanced version of a class)
- inherits (when a class extends another class it gets the other class’s methods and attributes)
- superclass / subclass: parent/child; base/derived
Snippets
- if {} else if {} else {}
- System.out.println(studentGrade >= 60 ? “Passed” : “Failed”);
- while () {}
Chapter 4: Control Statements: Part 2: Logical Operators
Nuggets
- Essentials of Counter-controlled Repetition
- Control variable (loop counter)
- initial value of the control variable
- increment (how the control variable is modified each time)
- loop-continuation condition (determines if we continue to loop)
- break; // terminates the loop
- continue; // skip back to the controlling statement
- conditional and (&&), conditional or (||) xor (^)
- && : both must be true in order to be true
- ||: both must be false in order to be false
- ^: true if only one is true
- !: negation
- 3 forms of control
- sequence
- selection
- repetition
Snippets
- for (int counter = 1; counter <= 10; counter++){}
- for (;;) {}
- while () {}
- do {} while ();
- switch () { case x: statement; break; default: statement; break;}
Chapter 5: Methods
Nuggets:
- Static Method or Class Method applies to the class and can be called directly: Math.pow
- Static Variables: Math.PI, Math.E
- String concatenation “Hello ” + “World”
- argument promotion, java will attempt to promote what you send to the correct variable type
- If a block contains a variable name used elsewhere the other name is hidden until the block terminates — this is called shadowing. You can access the shadowed variable by using the ClassName.variableName;
- Method overloading: same method name, different variable types
Snippets
- Key Java API packages
- java.awt.event
- java.awt.geom // 2D shapes
- java.io
- java.lang
- java.net // networking
- java.security
- java.sql // SQL package
- java.util
- java.util.concurrent // parallel execution
- javax.swing // GUI components
- javax.swing.event // GUI events
- javax.xml.ws // web services
- javafx.packages // preferred GUI
- java.time // Java 8 – Date/Time API
- java.util.function & java.util.stream // Java 8
- SecureRandom randomNumbers = new SecureRandom();
- private enum Status { CONTINUE, WON, LOST };
Chapter 6:
Chapter 7: Introduction to Classes and Objects
TERM: Extensible Language: You can add classes/types
TERM: Instance Variable: attributes of a class
TERM: Application Class: Has a main method that calls the class
SNIPPET: public class Account
- public — access modifier
- class — keyword
- Account — class name, must be in a file named Account.class
TERM: Instance Methods: Nonstatic methods
CONVENTION: Class names use Camel Case where names begin with an upper case letter
CONVENTION: Method names use Camel Case where names begin with a lower case letter
TERM: Shadowed Instance variable: Variable declared at instance and local variable
TERM: Driver Class: calls another class
CONCEPT: Static can be called directly
public class A { double void f1; double static f2; } A a = new A(); a.f1(); A.f2();
CONCEPT: Private allows information hiding / data hiding
CONCEPT: Local variables are not initialized by default
CONCEPT: Constructors can be private and cannot return a value
UML:
- + public
- – private
- <<constructor>> guillemets
SNIPPETS
- total += grade; // accumulation
- return (double) total / grades.length; // note casting
- for (int grade : grades) //loop over grades array