q1 review. other news us news top cs universities global-universities/computer- science?int=994b08
Post on 14-Dec-2015
214 Views
Preview:
TRANSCRIPT
Q1 Review
Other News• US News top CS Universitieshttp://www.usnews.com/education/best-global-universities/computer-science?int=994b08
General Problem SolvingRemember:• CS is really about solving
problems• Programming is just a means of
building these solutions
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
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
Set largest to –inf
int largest = Integer.MIN_VALUE;
For each number in the list, called n
for (int n : list)
Compare n to largestSet 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 ProblemIs a number in a list I give you?
Input: list, numberOutput: boolean true for yes, false for no
Basicspublic static void main(String[] args) {
…}
Always where programs start.
BasicsEverything 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 MatterAbsolutely 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
InputScanner 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 StringsTo 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=m2Ux2PnJe6E
Syntax – Memorize!if (<condition>) {
// Do this code when condition true} else {
// Do this all other cases}
ConditionsLogical evaluations that are true/false• x == y• !(x-1 > 0 && y < 10)• (state.equals(“dead”) ||
state.equals(“sleeping”)
DeMorgan’s LawLike distribution in math• && ||• < >=• > <=• == !=!(x > 0 && y == 10) (x <= 0 || y != 10)
Short Circuit
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.
For LoopsFor loops are designed for sequential looping or count based looping (most common)
for (int i = 0; i < size; i+=2) {…}
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
}
Nested LoopLoop within a loop
for (int i = 0; i < 5; i++)for (int j = 0; j < 6; j++)
System.out.println(“beep”);
top related