Download presentation
Presentation is loading. Please wait.
Published byGordon Price Modified over 9 years ago
1
Mid-Year Review
2
Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code into general tasks Implement details for the tasks Test and track bugs (debugging)
3
Compiling and Running Compiling your java files: javac *.java Running your program: java
4
Conditions Logical statements that evaluate to true or false Key to controlling program behavior Used in loops and branches (if-else)
5
Exercise What values of a and b make the following conditional true: (!a && b) == !(a || b) 1.None 2.(true, true) 3.(true, *) 4.(true, false), (false, true) 5.All values
6
Loops Two key kinds of loops: for loop and while loop There are do while loops and foreach loops too. Easy to understand after you get for and while
7
For Loop for ( ; ; ) { // Task to repeat }
8
While Loop while( ) { // Task to repeat }
9
Exercise What is the output of this code? for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); }
10
Basic/Primitive Types int char double long boolean Variables hold actual value.
11
Exercise Say x has the max int value (2^32 – 1). What is the value of x after this statement. Why? x += x;
12
Complex Types String – most important complex type you need to know about Object Use.equals( ) to compare if 2 objects are equal (this applies to Strings) Complex types capitalized Variables hold reference not object/value
13
Defining Your Own To define your own complex type, you use a class Class vs. object? All java code must be inside a class Everything inside the class is bound to instances, unless made static
14
Example
15
Parts of a class
16
Methods Procedures – should perform 1 thing and 1 thing well Takes input (parameters) and can give output (return value) Associated with the class it is defined inside
17
Overloading Having many functions with the same name, but different signatures public class DataArtist {... public void draw(String s) {... } public void draw(int i) {... } public void draw(double f) {... } public void draw(int i, double f) {... }
18
Overriding Redefining a function that your parent defined (after inheriting) class B extends A { int k; void show() { super.show(); // this calls A's show() System.out.println("k: " + k); }
19
Recursion A method that refers to itself. Breaks down the problem into smaller and smaller version until the problem is trivial Base case – the trivial part Inductive step – how to break hard problems into smaller problems
20
Arrays A contiguous chunk of memory where items of a given type reside, side by side Very useful for collections. You don’t have to name each value in the collection, just the array itself Fixed size Random access Ordered
21
ArrayList Like regular array, is a List but has additional features Takes care of reorganizing when inserting/removing from anywhere other than the end Takes care of sizing
22
Exercise What is wrong with this code? int[] a; for (int i = 0; i < 10; i++) a[i] = i * i;
23
Exersize
24
Exercise
25
Object Oriented Strengths Inheritance: way to establish subtypes of an existing object (code reuse) Polymorphism: the ability for vary different objects to be handled with 1 interface (simplification) Abstraction: isolation of interface from actual implementation (simplification) Encapsulation: bundling data and methods together while restricting access to data/methods (simplification)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.