lecture 9: bits and pieces, part 2 tami meredith

38
CSCI1226 Introduction to Computing Science and Programming Lecture 9: Bits and Pieces, Part 2 Tami Meredith

Upload: phoebe-hawkins

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Slide 1

CSCI1226Introduction to Computing Science and Programming Lecture 9: Bits and Pieces, Part 2 Tami MeredithRoadmapA brief self-evaluation exerciseToday's lecture has a bit of everythingContinuation of last classthings we've skippedthings that aren't critical but usefulintroductions to things you don't really need to know, but that you should be aware that they existEven more exercises Self EvaluationHere is some Java code:Scanner scn = new Scanner(System.in);int number = scn.nextInt();Write a piece of Java code that prints, to the screen, "The ultimate answer" if number is equal to 42Part 2if (number == 42) { System.out.println("The ultimate answer");}

Now, extend this code so that it also prints "Y2K" if number equals 2000 and "Just a boring number" if it equals anything else.Part 3if (number == 42) { System.out.println("The ultimate answer");} else if (number == 2000) { System.out.println("Y2K");} else { System.out.println("Just a boring number");}

Write a piece of java code that prints your name to the screen 13 timesPart 4for (int i = 1; i y) ? x : y;return((n == 0) ? 0 : sum/n);Stuff you won't useShifts:byte x = 9; // x = 0000 1001; x = x > 1; // x = 0001 0010;Bitwise Operations: & is bitwise and, not the same as &&| is bitwise or, not the same as ||^ is bitwise complement, not the same as !Hex constants are prefixed with 0xint x = 0x10; // x = hexidecimal 10 = 16;

Short-Circuit EvaluationRecall: x && y is true when both x and y are trueIf x is false, y doesn't matterTo save effort, y is never examined if x is false Recall: x || y is true if x is true or y is trueIf x is true, y doesn't matterTo save effort, y is never examined if x is trueWe call this behaviour short-circuit evaluationASCIIEvery character has a valueWe store the value, not the characterCharacters are just numbers!We can use the ASCII table to find the value of any character

ExerciseWrite a method called isUpper that takes a character as its parameter and returns true if the character is upper case and false otherwise.Solutionspublic static boolean isUpper (char c) { if (('A'