Download Issues in ATM Network Control - Washington University in

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 131 Computer Science 1
Module 1: (basics of Java)
Welcome to the World of Java
‹#›
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
‹#›
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
‹#›
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
‹#›
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;
‹#›
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)
‹#›
Basic Definitions
type
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.
‹#›
The int type
‹#›
Integers
This surprises most
students. Don’t feel
bad: experts forget
this too sometimes.
‹#›
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
‹#›
The double type
‹#›
Example: compute the area of a circle
‹#›
String: a sequence of characters
‹#›
String operation
 Meaning
of characters
depends on context.
 Example: report +, -,
*, / of two integers
‹#›
Boolean
‹#›
Booleans
data type. Useful to control the logic of
a program.
boolean
‹#›
Comparisons
Comparisons.
Take two operands of one type (int
or double) and produce a result of type boolean.
‹#›
Leap Year
Q.
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);
}
}
‹#›
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;
‹#›
Basic Expressions in Java
Expression
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
‹#›
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)
‹#›
Method
 What
if we need to find areas of different circles?
» Do we have write the code over and over again?
‹#›
Procedural abstraction (method)
 Find
areas of circles
 A method is like a blackbox (input  output)
» Hide details of computation from outside
A
java method
double circleArea(double radius) {
return Math.PI * radius * radius;
}
 Use
it:
double area = circleArea(10.0);
‹#›
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
‹#›
Method for Length of Hypotenuse
 We
know C=√A2+B2
 Forms the basis of the Java method
C
A
B
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, including
sin(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);
‹#›