26
CSE 131 Computer Science 1 Module 1: (basics of Java)

CSE 131 Computer Science 1 Module 1: (basics of Java)

Embed Size (px)

Citation preview

CSE 131 Computer Science 1Module 1: (basics of Java)

2

Welcome to the World of Java

3

Eclipse

Integrated Development Environment (IDE)» “smart” editing of Java programs – continuous syntax checks» compiles program files automatically as necessary» integrated program testing and debugging tools» has plug-ins for version control system (Subversion)

Can be a powerful tool for managing large projects» but, can also be a bit overwhelming and mysterious at first» watch the first few Eclipse videos on the web site before lab

works the same way in Windows, Mac and Linux

4

Your First Program

public class HelloWorld {public static void main(String[] args) {

System.out.println("Hello, world!");}

}» defines a class called HelloWorld (a class defines an object)» class contains a single method called main

• in Java, the main method is the starting point of a program• the main method has an array of arguments (not used here)• Any program has one and only one main() method

» program “prints” a brief message and halts

5

Today: understand basic building blocks of JAVA

You will be able to» Output a slogan on the screen » Tell if a year is a Leap Year» Calculate the area of a circle

» Output anything you like» Do all kinds of calculation

6

Basic unit: variable A variable in Java is a piece of

information stored in a computer’s memory: like a jar holding food

Each variable has a type» Like different kinds of jars (different food,

size, usage, etc.) Main types:

int a; double b;

String s; boolean x;

7

Basic Data Types in Java

Type (keyword) Meaning Examples Primitive Operators

int Integer 3, 0, -5, 256 +, -, *, /

double Decimal number 1.5, -3.1415, 0.0 +, -, *, /

boolean true or false true, false &&, ||, !(return boolean: <,>,<=,>=,!=, ==)

String a sequence of characters

“hello world” + (concatenation)

88

Basic Definitions

Type. A category of values and a set of operations on such values.Variable. A name that refers to a value of a declared type.Assignment statement. Associates a value with a variable.

type

9

The int type

10

Integers

This surprises most students. Don’t feel bad:

experts forget this too sometimes.

11

Key points

Assignment statement Variable = Expression ; (For example: a = b+ 5/3; )

Expression’s type must match the variable’s type (will come back to this point)

Sequential execution » The order of statements matters

12

The double type

13

Example: compute the area of a circle

14

String: a sequence of characters

15

String operation

Meaning of characters depends on context.

Example: report +, -, *, / of two integers

16

Boolean

1717

Booleansboolean data type. Useful to control the logic of a program.

1818

ComparisonsComparisons. Take two operands of one type (int or double) and produce a result of type boolean.

1919

Leap YearQ. Is a given year a leap year?A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100.

public class LeapYear { public static void main(String[] args) { int year = 2012;

boolean isLeapYear;

// divisible by 4 but not 100 isLeapYear = (year % 4 == 0) && (year % 100 != 0); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0);

System.out.println(isLeapYear); }}

20

Data Type Conversion

Operations must be applied to same types, with the following exceptions

Java automatically converts some types of values to make them compatible

int i, j; double x, y;i = 5; j = i/2; y = 2; x = i/y; - As long as there is a double in an expression, the type of the

expression is promoted to double Combining strings and numeric values

int i = 5; String S = “two plus two =” + i;

21

Basic Expressions in JavaExpression Value Type

3+5 8 int

7/2.0 3.5 double

7/2 3 int

3+5-2/4+2 10 int

3+5-2.0/4+2 9.5 double

(3+5-2)/(4+2) 1 int

“Hello”+”there” “Hellothere” String

“I have “+5+5+” toes” “I have 55 toes” String

true && false false boolean

false || true true boolean

!true false boolean

(true&&false)||(!false) true boolean

22

Variable types A variable can only hold values of the proper type

int x; (default 0) int x = 1; … x = false; (error! Type-checking problem)

Any expression of type T can appear in an expression where a value of type T is required» E.g. int x = 3+5;

int x = 7/2.0; (type error) int x= (int) (7/2.0) (cast, x has value 3)

23

Method

What if we need to find areas of different circles?» Do we have write the code over and over again?

24

Procedural abstraction (method) Find areas of circles A method is like a blackbox (input output)

» Hide details of computation from outside A java method

Use it: double area = circleArea(10.0);

double circleArea(double radius) {return Math.PI * radius * radius;

}

25

Method Call and Return

Java methods used to decompose program into parts» allows us to solve a problem once and conveniently re-use the

solution many times

You can invoke the method by using it in an expression» Example: area = circleArea(2.0);» The number of input parameters should match the method

definition» The types of input parameters should match the method

definition

26

Method for Length of Hypotenuse

We know C=√A2+B2

Forms the basis of the Java method

double hypotenuse(double A, double B) {return Math.sqrt(A * A + B * B);

}» Math.sqrt(x) returns the square root of x

• part of a library of mathematical functions, includingsin(x), cos(x), tan(x), log(x), exp(x)

» Signature of a method: double hypotenuse(double, double)defines method name, return type, types of arguments

» Invocation of a method must match its signature Example: double h = hypotenuse(3, 4);

C

B

A