Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Q1 Review

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

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

4 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

5 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

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

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

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

9 Output largest System.out.println(largest);

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

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

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

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

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

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

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

17 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

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

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

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

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

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

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

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

25 Short Circuit

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

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

28 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 }

29 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


Ads by Google