26
CPSC 233 - Introduction to Computer Science for Computer Science Majors II Monir Zaman (Md Moniruzzaman)

CPSC 233 - Introduction to Computer Science for Computer Science …pages.cpsc.ucalgary.ca/~mmoniruz/233/tut1.pdf ·  · 2012-09-17CPSC 233 - Introduction to Computer Science for

Embed Size (px)

Citation preview

CPSC 233 - Introduction to Computer Science for Computer

Science Majors II

Monir Zaman

(Md Moniruzzaman)

Java inventor

• Java programs

– Application

– Applet

Applets

• Lightweight Java program

• Runs from a web browser; alternately, we can run applet from an applet viewer

• Can run from a location on the Internet; it does not have to be stored in the local machine

• Always use Windowing interface; doesn’t support console I/O

http://english.op.org/~peter/ChessApp/#applet

• In this course we will focus on applications not applets

– Application may or may not have a windowing interface

– Application supports console input and output

• Java Basics

Exercise: Write these instructions in a Java program

int a=23; int b=3; int s=a+b*2; System.out.println(s); s=(a+b)*2; System.out.println(s);

Precedence Rules

1-10 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Variable declarations

int number2, number1;

float leng= 3.4f;

double diam=54.3;

number2 = number1 = 3;

long num3=789l;

Casting

int b=23;

float c=1.2f;

b=c;

c=b;

Casting

int b=23;

float c=1.2f;

b=(int) c; //information loss

c=b;

Casting

int b=23;

float c=1.2f;

b=(int) c; //information loss

c=b; Python type casting: temp=input(“enter a number”) N=int(temp)

byte -> short -> int -> long -> float ->double

printf Format Specifier

• The code double price = 19.8;

System.out.print("$");

System.out.printf("%6.2f", price);

System.out.println(" each");

will output the line $ 19.80 each

2-16 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Right and Left Justification in printf

• The code double value = 12.123;

System.out.printf("Start%8.2fEnd", value);

System.out.println();

System.out.printf("Start%-8.2fEnd", value);

System.out.println();

will output the following

2-17 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Right and Left Justification in printf

• The code double value = 12.123;

System.out.printf("Start%8.2fEnd", value);

System.out.println();

System.out.printf("Start%-8.2fEnd", value);

System.out.println();

will output the following

Start 12.12End

Start12.12 End

2-18 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Multiple arguments with printf

• What is the output of the following code:

double price = 19.8;

String name = "magic apple";

System.out.printf("$%6.2f for each %s. %n" , price, name);

System.out.println("Wow");

2-19 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Shorthand Assignment Statements

Example: Equivalent To:

count += 2; count = count + 2;

sum -= discount; sum = sum – discount;

bonus *= 2; bonus = bonus * 2;

time /=

rushFactor;

time =

time / rushFactor;

change %= 100; change = change % 100;

amount *=

count1 + count2;

amount = amount *

(count1 + count2);

1-20 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.

Unary operator: ++, --

int a=3;

++a; //a=a+1

System.out.println(++a);

System.out.println(a);

Unary operator: ++, --

int a=3;

++a; //means a=a+1

System.out.println(a++);

System.out.println(a);

int i=0;

while(i++ < 4){

System.out.println(i);

}

Exercise

• Thanks

Precedence and Associativity Rules

– Unary operators of equal precedence are grouped right-to-left +-+rate is evaluated as +(-(+rate))

– Binary operators of equal precedence are grouped left-to-right base + rate + hours is evaluated as

(base + rate) + hours

– Exception: A string of assignment operators is grouped right-to-left n1 = n2 = n3; is evaluated as n1 = (n2 = n3);

1-26 Copyright © 2012 Pearson Addison-Wesley. All rights

reserved.