9/21/2013 CSCI 1101B {101B} 09/19/13 The Golden Lecture Chapter 2 Mohammad T. Irfan What’s in a name? Variables Constants • TYPE • final int NUM = 10; NAME ; • int i; • Later on, you can’t change the value of NUM • int i, j, anotherVar; NUM = 20; //Error • double i, j = 10.37, anotherVar; • Same as the following two lines: • double i, j, anotherVar; • j = 10.37; • boolean isFound = false; • char c = ‘x’; • Why are they called variables ? int num = 10; num = 20; Objects • int num = 5; Type: Name: Value: int num 5 • FilledOval circle = new FilledOval (10, 50,100, 100,canvas); Chapter 3 Type (Class): Name (Object): Value: FilledOval circle constructor Working with Numbers 1
9/21/2013 Arithmetic Operations Printing numbers and text • double radius = 10.0; • double numMiles; • double sideLength = 2.0; • numMiles = 10.50; • double circleArea, squareArea, totalArea; • System.out.println (“I ran ” + numMiles + “ miles!”); • circleArea = 3.14159 * radius * radius; • squareArea = sideLength * sideLength; • totalArea = circleArea + squareArea; if Statement • if ( condition ) { if-part; //one or more statements } else if ( another condition ) { else-if-part; //one or more statements } Optional else { Chapter 4 else-part; //one or more statements } //End of if-else: no semicolon is necessary here Making Choices • //Next statements start here... (outside of if-else) Example: Hannaford Problem Finding the maximum shortage • Input • Roll a die twice. • # of items n, stocks s 1 , s 2 , …, s n , demands d 1 , d 2 , …, d n • Both numbers are odd You win • Steps • One is odd, the other even It’s a tie • None is odd Computer wins For item# i ranging from 1 to n • Pseudocode Set its shortage t i = d i – s i EndFor • firstRoll = outcome of rolling the die maxShortage = t 1 • secondRoll = outcome of rolling the die again maxShortageItem = 1 For item# i ranging from 2 to n • if (firstRoll is odd AND secondRoll is also odd) if (t i > maxShortage) if t i > maxShortage then output “I win ”; { maxShortage = t i maxShortage = t i ; else if (firstRoll is odd OR secondRoll is odd) maxShortageItem = i maxShortageItem = i; output “It’s a tie!” endif } else output “Computer wins ” EndFor 2
9/21/2013 Revisiting the Hannaford Example • Finding the 2 nd maximum shortage if (t i > maxShortage2 and t i ≠ maxShortage) maxShortage2 = t i maxShortageItem2 = i Endif if (t i > maxShortage2 && t i != maxShortage) { maxShortage2 = t i ; maxShortageItem2 = i; } Array Definition • int [] numberList = new int[5]; Position#: 0 1 2 3 4 numberList • numberList[0] = 10; • numberList[1] = 15; Chapter 14 10 15 5 30 50 • numberList[2] = 5; 0 1 2 3 4 • numberList[3] = 30; numberList Arrays • numberList[4] = 50; Another way Operations with an Array • int [] numberList = {10, 15, 5, 30, 50}; • int b = numberList[0]; • numberList[3] = numberList[1] + 2 * numberList[4]; • numberList[2] = b * 4; 10 15 5 30 50 0 1 2 3 4 numberList 10 15 40 5 115 30 50 0 1 2 3 4 numberList 3
9/21/2013 Revisiting Hannaford Example • Finding the 2 nd maximum shortage if (t i > maxShortage2 and t i ≠ maxShortage) maxShortage2 = t i maxShortageItem2 = i Endif Chapter 7, 13 if (t[i] > maxShortage2 && t[i] != maxShortage) { Iterative Statements maxShortage2 = t[i]; maxShortageItem2 = i; } for Loop for Loop Example • int i; • for (initialization; condition; update) • for (i = 0; i < 5; i = i + 1) { { If condition is true If condition is true statements to be repeated ... System.out.println (numberList[i]); } } If condition is false, exit the loop If condition is false, exit the loop Java Reference • docs.oracle.com/javase/7/docs/api/ Lab Assignments Due next week in lab (No penalty for errors) 4
9/21/2013 Bring (partial) solutions to the next lab 1. Calculate the roots of a given quadratic equation. 2. For the rolling die problem, exchange the if and the else if part. What goes wrong? 3. Solve the problem of finding the maximum shortage using Java. • Input: • int n = 10; • int [] stock = {20, 5, 30, 90, 60, 15, 25, 75, 150, 10}; • int [] demand = {14, 6, 2, 33, 25, 46, 277, 129, 182, 199}; • //Test with different numbers 4. Solve the 2 nd maximum shortage problem. 5
Recommend
More recommend