29
Q1 Review

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

Embed Size (px)

Citation preview

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

Q1 Review

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

General Problem SolvingRemember:• CS is really about solving

problems• Programming is just a means of

building these solutions

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

General Problem SolvingApproaching a problem is really like writing a story• The point of the story solving the

problem• The outline or structure algorithmStart by outlining the approach, in general

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

Example: Largest numberGiven: List of numbersOutput: The largest number in that list1. Set largest to -inf2. For each number in the list, called n

1. Compare n to largest2. Set largest to n, if n is larger

3. Output largest

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

Set largest to –inf

int largest = Integer.MIN_VALUE;

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

For each number in the list, called n

for (int n : list)

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

Compare n to largestSet largest to n, if n is larger

if (n > largest) {largest = n;

}

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

Output largest

System.out.println(largest);

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

int largest = Integer.MIN_VALUE;for (int n : list) {

if (n > largest) {largest = n;}

}System.out.println(largest);

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

Challenge ProblemIs a number in a list I give you?

Input: list, numberOutput: boolean true for yes, false for no

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

Basicspublic static void main(String[] args) {

…}

Always where programs start.

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

BasicsEverything should go inside a class (which we’ll learn more about later)

public class MyProgram {…}

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

Naming• Classes and complex types

should have a capitalized name–MyProgramName

• Variables should have a lower camel case name–myProgramsVariable

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

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

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

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”);

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

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

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

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

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

Output• Strings are basically the

universal form of data• System.out.print(…) tries to convert

everything to a string• System.out.println(complexObject);

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

Comparing StringsTo compare 2 complex types like Strings, use .equals. == is for primatives

• “Hello” == “Hello” // false• “Hello”.equals(“Hello”) // true

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

If• If statements control your code• Lets you determine what code

does and doesn’t execute

https://www.youtube.com/watch?v=m2Ux2PnJe6E

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

Syntax – Memorize!if (<condition>) {

// Do this code when condition true} else {

// Do this all other cases}

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

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

state.equals(“sleeping”)

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

DeMorgan’s LawLike distribution in math• && ||• < >=• > <=• == !=!(x > 0 && y == 10) (x <= 0 || y != 10)

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

Short Circuit

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

LoopsAllows 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.

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

For LoopsFor loops are designed for sequential looping or count based looping (most common)

for (int i = 0; i < size; i+=2) {…}

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

While LoopsGeneral 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

}

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

Nested LoopLoop within a loop

for (int i = 0; i < 5; i++)for (int j = 0; j < 6; j++)

System.out.println(“beep”);