# Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.

## Presentation on theme: "Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08."— Presentation transcript:

Q1 Review

Other News US News top CS Universities http://www.usnews.com/education/best- global-universities/computer- science?int=994b08

General Problem Solving Remember: CS is really about solving problems Programming is just a means of building these solutions

General Problem Solving Approaching a problem is really like writing a story The point of the story  solving the problem The outline or structure  algorithm Start by outlining the approach, in general

Example: Largest number Given: List of numbers Output: The largest number in that list 1.Set largest to -inf 2.For each number in the list, called n 1.Compare n to largest 2.Set largest to n, if n is larger 3.Output largest

Set largest to –inf int largest = Integer.MIN_VALUE;

For each number in the list, called n for (int n : list)

Compare n to largest Set largest to n, if n is larger if (n > largest) { largest = n; }

Output largest System.out.println(largest);

int largest = Integer.MIN_VALUE; for (int n : list) { if (n > largest) { largest = n; } System.out.println(largest);

Challenge Problem Is a number in a list I give you? Input: list, number Output: boolean true for yes, false for no

Basics public static void main(String[] args) { … } Always where programs start.

Basics Everything should go inside a class (which we’ll learn more about later) public class MyProgram {…}

Naming Classes and complex types should have a capitalized name –MyProgramName Variables should have a lower camel case name –myProgramsVariable

Data Types Matter Absolutely most important ones are: int (32-bit size) double String

Casting You can cast to convert, with consequences –int i = (int) 5.5; Other kinds of conversions require parsing, thus code to convert –int i = Integer.valueOf(“153414”);

Binary Everything the computer sees is binary Know how to calculate binary numbers Hex is shorthand for binary 1 hex digit = 4 bits Byte = 8 bits

Input Scanner makes your life easier. Scanner s = new Scanner(System.in); int i = s.nextInt(); String string = s.next();

Output Strings are basically the universal form of data System.out.print(…) tries to convert everything to a string System.out.println(complexObject);

Comparing Strings To compare 2 complex types like Strings, use.equals. == is for primatives “Hello” == “Hello” // false “Hello”.equals(“Hello”) // true

If If statements control your code Lets you determine what code does and doesn’t execute https://www.youtube.com/watch?v=m2Ux2 PnJe6E

Syntax – Memorize! if ( ) { // Do this code when condition true } else { // Do this all other cases }

Conditions Logical evaluations that are true/false x == y !(x-1 > 0 && y < 10) (state.equals(“dead”) || state.equals(“sleeping”)

DeMorgan’s Law Like distribution in math &&  || = >  <= ==  != !(x > 0 && y == 10)  (x <= 0 || y != 10)

Short Circuit

Loops Allows you to repeat something over and over, with small parameter changes E.g., when finding max number, you can’t really look at an entire list of numbers at once, you quickly look at each number one by one.

For Loops For loops are designed for sequential looping or count based looping (most common) for (int i = 0; i < size; i+=2) {…}

While Loops General loop to just keep repeating until a specific thing thing changes. Good for infinite loops, when you want them boolean wantsToQuit = false; while (!wantsToQuit) { if (input.equals(“quit”); wantsToQuit = true; // Or just break }

Nested Loop Loop within a loop for (int i = 0; i < 5; i++) for (int j = 0; j < 6; j++) System.out.println(“beep”);

Download ppt "Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08."

Similar presentations