cs111: programming language ii ·  · 2014-01-20text i/o standard output. flexible os abstraction...

25
CS111: PROGRAMMING LANGUAGE II Lecture 1(b): Java Basics (I) Computer Science Department 1

Upload: vudiep

Post on 27-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

CS111: PROGRAMMING

LANGUAGE II

Lecture 1(b): Java Basics (I) Computer Science

Department

1

Lecture Contents

Computer Science Department

Java basics

Input/output

Variables

Expressions

2

Your first Java program.. 3

// indicates a

comment.

class keyword

Java is case

sensitive

braces { , }

delimit a

class body

main Method

Computer Science Department

“Everything must be in a class”

There are no global functions or global data.

Basic language elements 4

Text I/O

Standard output.

Flexible OS abstraction for output.

In Java, applications use the standard output object

(System.out) to display text on terminal.

Computer Science Department

5

Command line output 6

Multiple line

O/P

Formatting

output

public class TestIO {

public static void main(String[] args)

{

System.out.println(“Welcome to java”);

System.out.println(“Welcome to \n java”);

System.out.print(“Welcome to”);

System.out.println(“java”);

System.out.printf(“%s\n%s\n“, “Welcome

to”,”java”);

}

}

}

Computer Science Department

Common escape sequences

Computer Science Department

7

Printf Conversion-Characters

Computer Science Department

8

Strings & Text

String msg1 = new String( “Hello” );

String msg2 = “Hello” ;

String msg3 = “Year “ + 2005; //valid??

9

Computer Science Department

Standard Input

input is received from Terminal window.

Input entered while program is executing.

Computer Science Department

10

Reading values

Computer Science Department

use : import java.util.Scanner;

Define an object of the Scanner class:

Scanner input = new Scanner(System.in);

input values:

num1 = input.nextInt();

Display after calculation:

System.out.printf(“the square is : %d

\n”, num1*num1);

11

example 12

Computer Science Department

example

Computer Science Department

13

variables

Declaration, memory allocation, initialization

int total = 0;

int count, temp, result;

Multiple variables can be created in one declaration

data type variable name

Computer Science Department

14

Constants

Computer Science Department

A “constant variable” is an identifier that is similar to a

variable except that it holds one value for its entire existence

Why constants:

give names to otherwise unclear literal values

facilitate changes to the code

prevent inadvertent errors

In Java:

final double PI = 3.14159265;

15

Arithmetic Expressions

An expression is a combination of operators and operands

Arithmetic expressions (we will see logical expressions later)

are essentially special methods applied to numerical data

objects: compute numeric results and make use of the arithmetic

operators:

Addition +

Subtraction -

Multiplication *

Division /

Remainder %

Computer Science Department

16

Assignment-related Operators

Increment and decrement operators: ++, --

Assignment operators: +=, -=, *=, /=

count = count + 1;

count += 1;

count ++;

count = count - 10;

count -= 10;

these three expressions have the same effect

these two expressions have the same effect

Computer Science Department

17

Operator Precedence

What is the order of evaluation in the following expressions?

a + b + c + d + e

4 3 2

a + b * c - d / e

3 2 4 1

a / (b + c) - d % e

2 3 4 1

a / (b * (c + (d - e)))

4 1 2 3

1

Computer Science Department

18

How Do Data Conversions Happen?

Implicitly:

occurs automatically

uses widening conversion,

Examples: 4.0 / 8 (which / is it: double/double, float/float, int/int)

4 / 8.0 (which / is it: double/double, float/float, int/int)

4 + 5 / 9 + 1.0 + 5 / 9 / 10.0 (what is the value?)

Computer Science Department

19

How Do Data Conversions Happen?

Computer Science Department

20

Explicitly: Casting

widening / narrowing conversions

Examples: double MyResult;

MyResult = 12.0 / 5.0; //OK

int myInt = (int) MyResult; // truncation

MyResult = (double)myInt/3.0;

Be careful!!

Example: in 1996, Ariane 5 rocket exploded after

takeoff because of bad type conversion.

Computer Science Department

21

Class Math

All Math class methods are static

Each is called by preceding the name of the method with the class name Math and the dot (.) separator

Method arguments may be constants, variables or expressions

Computer Science Department

22

Computer Science Department 23

Example

Solve quadratic equation ax2 + bx + c = 0.

public class Quadratic {

public static void main(String[] args) {

double a,b,c,d;

// input coefficient values..

// calculate roots

d = Math.sqrt(b*b - 4.0*a*c);

double root1 = (-b + d) / (2.0*a);

double root2 = (-b - d) / (2.0*a);

// print them out

System.out.println(root1);

System.out.println(root2);

}

}

Computer Science Department

24

Text Book:

Chap 2

That’s all for today…..

Computer Science Department

25