CSCI 1101B {101B} 09/19/13 The Golden Lecture Chapter 2 Mohammad - - PDF document

csci 1101b 101b 09 19 13 the golden lecture
SMART_READER_LITE
LIVE PREVIEW

CSCI 1101B {101B} 09/19/13 The Golden Lecture Chapter 2 Mohammad - - PDF document

9/21/2013 CSCI 1101B {101B} 09/19/13 The Golden Lecture Chapter 2 Mohammad T. Irfan Whats in a name? Variables Constants TYPE final int NUM = 10; NAME ; int i; Later on, you cant change the value of NUM int i, j,


slide-1
SLIDE 1

9/21/2013 1

CSCI 1101B {101B} 09/19/13 The Golden Lecture

Mohammad T. Irfan

Chapter 2

What’s in a name? Variables

  • TYPE

NAME;

  • int i;
  • int i, j, anotherVar;
  • 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;

Constants

  • final int NUM = 10;
  • Later on, you can’t change the value of NUM

NUM = 20; //Error

Objects

  • int num = 5;
  • FilledOval circle = new FilledOval (10, 50,100, 100,canvas);

Type (Class): FilledOval Name (Object): circle Value: constructor Type: int Name: num Value: 5

Chapter 3

Working with Numbers

slide-2
SLIDE 2

9/21/2013 2 Arithmetic Operations

  • double radius = 10.0;
  • double sideLength = 2.0;
  • double circleArea, squareArea, totalArea;
  • circleArea = 3.14159 * radius * radius;
  • squareArea = sideLength * sideLength;
  • totalArea = circleArea + squareArea;

Printing numbers and text

  • double numMiles;
  • numMiles = 10.50;
  • System.out.println (“I ran ” + numMiles + “ miles!”);

Chapter 4

Making Choices if Statement

  • if ( condition )

{ if-part; //one or more statements } else if (another condition) { else-if-part; //one or more statements } else { else-part; //one or more statements } //End of if-else: no semicolon is necessary here

  • //Next statements start here... (outside of if-else)

Optional

Example: Hannaford Finding the maximum shortage

  • Input
  • # of items n, stocks s1, s2, …, sn, demands d1, d2, …, dn
  • Steps

For item# i ranging from 1 to n Set its shortage ti = di – si EndFor maxShortage = t1 maxShortageItem = 1 For item# i ranging from 2 to n if ti > maxShortage then maxShortage = ti maxShortageItem = i endif EndFor

if (ti > maxShortage) { maxShortage = ti; maxShortageItem = i; }

Problem

  • Roll a die twice.
  • Both numbers are odd  You win
  • One is odd, the other even  It’s a tie
  • None is odd  Computer wins
  • Pseudocode
  • firstRoll = outcome of rolling the die
  • secondRoll = outcome of rolling the die again
  • if (firstRoll is odd AND secondRoll is also odd)
  • utput “I win ”;

else if (firstRoll is odd OR secondRoll is odd)

  • utput “It’s a tie!”

else

  • utput “Computer wins ”
slide-3
SLIDE 3

9/21/2013 3 Revisiting the Hannaford Example

  • Finding the 2nd maximum shortage

if (ti > maxShortage2 && ti != maxShortage) { maxShortage2 = ti; maxShortageItem2 = i; } if (ti > maxShortage2 and ti ≠ maxShortage) maxShortage2 = ti maxShortageItem2 = i Endif

Chapter 14

Arrays Array Definition

  • int [] numberList = new int[5];
  • numberList[0] = 10;
  • numberList[1] = 15;
  • numberList[2] = 5;
  • numberList[3] = 30;
  • numberList[4] = 50;

numberList

Position#: 0 1 2 3 4

numberList

10 15 5 30 50 1 2 3 4

Another way

  • int [] numberList = {10, 15, 5, 30, 50};

numberList

10 15 5 30 50 1 2 3 4

Operations with an Array

  • int b = numberList[0];
  • numberList[3] = numberList[1] + 2 * numberList[4];
  • numberList[2] = b * 4;

numberList

10 15 5 30 50 1 2 3 4 115 40

slide-4
SLIDE 4

9/21/2013 4 Revisiting Hannaford Example

  • Finding the 2nd maximum shortage

if (t[i] > maxShortage2 && t[i] != maxShortage) { maxShortage2 = t[i]; maxShortageItem2 = i; } if (ti > maxShortage2 and ti ≠ maxShortage) maxShortage2 = ti maxShortageItem2 = i Endif

Chapter 7, 13

Iterative Statements for Loop

  • for (initialization; condition; update)

{ statements to be repeated ... }

If condition is true If condition is false, exit the loop

for Loop Example

  • int i;
  • for (i = 0;

i < 5; i = i + 1) { System.out.println (numberList[i]); }

If condition is true 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)

slide-5
SLIDE 5

9/21/2013 5

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 2nd maximum shortage problem.