cpsc 233 tutorial n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CPSC 233 Tutorial PowerPoint Presentation
Download Presentation
CPSC 233 Tutorial

Loading in 2 Seconds...

play fullscreen
1 / 17

CPSC 233 Tutorial - PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on

CPSC 233 Tutorial. Xin Jan 24, 2011. Assignment 1. Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on UNIX Submit submit - c < course number> -a <assignment number> <name of file or files to be submitted>

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CPSC 233 Tutorial' - joie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cpsc 233 tutorial

CPSC 233 Tutorial

Xin

Jan 24, 2011

assignment 1
Assignment 1
  • Due on Jan 28 at 4:00 PM
  • Part I  Assignment Box on 2nd floor
  • Part II  Submitted electronically on UNIX
    • Submit
      • submit -c <course number> -a <assignment number> <name of file or files to be submitted>
      • eg. submit -c 233 -a 3 README.txt
    • List submitted files
      • showstuff -c <course number> -a <assignment number>
      • eg. showstuff -c 233 -a 3
    • Submit early and update
keyboard input again
Keyboard Input Again
  • Scanner.nextLine() will pick up any leftovers
  • If Scanner.nextInt() was called previously, nextLine() usually pickup a carriage-return (CR)
    • use an extra nextLine() to get rid of the CR
  • Experiments with MyInput.java
    • Input 20 and see the results
    • Input 20 abc and see the results
pick up chars
Pick up chars
  • String str = in.nextLine();
  • char c = str.charAt(0);
  • What other methods String has?
  • Search in Google with
    • class String java API
    • using documents provided by oracle.com
switch statement
Switch statement

char x = ‘a’;

intval;

switch (x) {

case ‘a’:

val = 0;

// fall through

case ‘b’:

val = 1;

break;

case ‘c’: // fall through

case ‘C’: // do the same for ‘c’ and ‘C’

val = 3;

break;

default:

val = 4;

}

Fall through: Without an explicit break statement, the execution continues on the next case!!!

This can be used to do the same thing for a few values.

operator precedence
Operator precedence
  • An example
    • intv = 92
    • intx = 100 | 25 & 36 << 2 + 12 & 55 * ++ v;
    • = 100 | ((25 & (36 << (2 + 12))) & (55 * (++ v)))
  • Extremely confusing
  • Sometimes results are dependent on the specific complier (for c/c++)
  • Avoid by all means
  • Clarify with parentheses until it is easy to understand by common humans
x vs x
x ++ vs. ++x

public class IncOperators

{

public static void main (String [] args)

{

intx = 8;

System.out.println("x = " + x);

intreturnedValue = ++ x;

System.out.println("the returned value is: " + returnedValue);

System.out.println("x after the operation is: " + x);

}

}

x vs x1
x ++ vs. ++x

public class Order2

{

public static void main (String [] args)

{

int num1;

int num2;

num1 = 5;

num2 = ++num1 * num1++;

System.out.println("num1=" + num1);

System.out.println("num2=" + num2);

}

}

type cast
type cast
  • Double int
    • float x = 5; // OK
    • float x = 5.0;// Illegal
    • float x = 5.f; // OK
    • intx = 5.0; // Illegal
    • intx = (int) 5.f; // OK
    • double x = 5 / 2; // x = 2
    • double x = 5 / (double)2; // x = 2.5
  • char int
    • char c = 97; // OK
    • intx = ‘a’; // OK
    • char c = ‘a’ + 1; // OK
bitwise operations
bitwise operations
  • ~ & | ^ << >>

0101

& 0110

------

= 0100

0101

| 0110

------

= 0111

0101

^ 0110

------

= 0011

~ 0101

------

= 1010

1101 >> 1

------

= 1110

0101 >> 1

------

= 0010

1101 << 1

------

= 1010

showing integers in binary
Showing integers in binary

public class ShowBinary

{

public static void main (String [] args)

{

showInt(3);

}

public static void showInt(intx)

{

for (inti = 0; i < 32; i ++) {

if ((x & 0x80000000) == 0)

System.out.print("0");

else

System.out.print("1");

x = x << 1;

}

}

}

experiments with the program
Experiments with the program

public static void main (String [] args)

{

intx = 3;

System.out.println("x = " + x + " in binary: ");

showInt(x);

System.out.println();

inty = -2011;

System.out.println("y = " + y + " in binary: ");

showInt(y);

System.out.println();

System.out.println("x AND y in binary: ");

showInt(x & y);

System.out.println();

}