q1 review. other news us news top cs universities global-universities/computer- science?int=994b08
TRANSCRIPT
![Page 1: Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/1.jpg)
Q1 Review
![Page 2: Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/2.jpg)
Other News• US News top CS Universitieshttp://www.usnews.com/education/best-global-universities/computer-science?int=994b08
![Page 3: Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/3.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/4.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/5.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/6.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/7.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/8.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/9.jpg)
Output largest
System.out.println(largest);
![Page 10: Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/10.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/11.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/12.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/13.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/14.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/15.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/16.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/17.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/18.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/19.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/20.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/21.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/22.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/23.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/24.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/25.jpg)
Short Circuit
![Page 26: Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/26.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/27.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/28.jpg)
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](https://reader037.vdocuments.mx/reader037/viewer/2022110319/56649c755503460f949295de/html5/thumbnails/29.jpg)
Nested LoopLoop within a loop
for (int i = 0; i < 5; i++)for (int j = 0; j < 6; j++)
System.out.println(“beep”);