Download Computer Programming with Java

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

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

Document related concepts
no text concepts found
Transcript
Computer Programming with
Java
Chapter 2
Primitive Types,
Assignment, and
Expressions
Contents








Variables
Primitive Types
Assignment Statements
Constants
Arithmetic Operators
The String class
Documentation
Keyboard and Screen I/O
Computer Programming with
Java
2
Variables

Definition




Variables in a program are used to store data
such as numbers and letters.
Values are the number or letter or other data
item in a variable
A variable can have its value changed.
Identifiers are the variable names and must
satisfy the spelling rules in Chapter 1.
Computer Programming with
Java
3
public class EggBasket
{
public static void main(String[] args)
{
int numberOfBaskets, eggsPerBasket, totalEggs;
System.out.println("Enter the number of eggs in each basket:");
eggsPerBasket = SavitchIn.readLineInt();
System.out.println("Enter the number of baskets:");
numberOfBaskets = SavitchIn.readLineInt();
totalEggs = numberOfBaskets * eggsPerBasket;
System.out.println(eggsPerBasket + " eggs per basket.");
System.out.println(numberOfBaskets + " baskets.");
System.out.println("Total number of eggs is " + totalEggs);
eggsPerBasket = eggsPerBasket - 2;
totalEggs = numberOfBaskets * eggsPerBasket;
…
Computer Programming
with A
Display 2.1:
Java
Simple Java Program
4
Primitive Types

Primitive Data Types

integers: byte, short, int, long
• e.g.) 0, 1, -1, 2, -2

floating point: float, double
• e.g.) 9.9, 3.14159, -5.63

others: char, boolean
• e.g.) char
• char symbol;
• symbol= ‘A’;
• e.g.) boolean
• true or false
Computer Programming with
Java
5
Type Name
Kind of Value
Memory Used Size Range (see textbook)
byte
integer
1 byte
-28 ~ 28-1
short
integer
2 bytes
-216 ~ 216-1
int
integer
4 bytes
-232 ~ 232-1
Long
integer
8 bytes
-264 ~ 264-1
Float
floating-point
number
4 bytes
 3.40282347  10+38 to
 1.40239846  10-45
Double
floating-point
number
8 bytes
single character
(unicode)
2 bytes
Char
boolean
true of false
1 bit
All unicode characters
Not applicable
Display 2.2: Primitive Types
Computer Programming with
Java
6
Assignment Statements

Assignment operator

Variable = Expression or Values;
• e.g.)
• amount = 3.99;
• score = numberOfCards + handicap;


Declaration and Initialization
Specialized assignment operators

+=, -=, /=, *=, %=
• e.g.)
• amount += 5; equals to amount = amount + 5;
Computer Programming with
Java
7
Assignment Statements (1)

Assignment Compatibilities

Principle of Compatibility
• you cannot put a value of one type in a variable of
another type.
• you can assign a value of any type on the following
list to a variable of any type that appears further
down the list
• byte -> short -> int -> long -> float -> double
• if you want to assign a value of type double to a
variable of type int, you must change the type of the
value using a type cast
Computer Programming with
Java
8
Assignment Statements (2)

Type Casting



the changing of the type of a value from its
normal type to some other type.
(Type_Name) Expression
e.g.)
•
•
•
•
double guess = 7.8;
int answer = (int) guess; (correct)
double distance = 9.0;
int points = distance; (incorrect)
Computer Programming with
Java
9
Constants

Number constants


It is literal values like 2 or 3.7 and its value do
not change in a Java program.
integer
• e.g.)
• 2, 3, 0, -3, 752, +12 (correct)
• 1,000 (incorrect)

floating-point
• e.g.)
• 2.1, 3.14159, 865000000.0
• 8.65108: 865000000.0 or 8.65e8
Computer Programming with
Java
10
Constants (1)

Named Constants



a name be defined for constant
Type Variable = Constant;
e.g.)
• double PI = 3.14159;
Computer Programming with
Java
11
public class CircleCalculation
{
public static void main(String[] args)
{
double radius; //in inches
double area; //in square inches
System.out.println("Enter the radius of a circle in inches:");
radius = SavitchIn.readLineDouble();
area = 3.14159 * radius * radius;
System.out.println("A circle of radius " + radius + " inches");
System.out.println("has an area of " + area + " square inches.");
}
}
Display 2.9: Comments and Indenting
Computer Programming with
Java
12
Arithmetic Operators

Basic Operators

+, -, *, /
• it can used to combine variables and/or numbers

Arithmetic Expression
rate2  delta
2( salary  bonus)
1
time  3mass
a7
t  9v
rate * rate  delta
2 * (salary  bonus)
(rate * rate)  delta
2 * (salary  bonus)
1/(time  3 * mass)
1/(time  3 * mass)
(a - 7)/(t  9 * v)
(a - 7)/(t  (9 * v))
Computer Programming with
Java
13
Arithmetic Operators (1)

Increment and Decrement Operators



it can be used to increase or decrease the
value of a variable by one.
++, -e.g.)
• count = count + 1; count++;
• count = count - 1; count--;
Computer Programming with
Java
14
Arithmetic Operators (2)

Precedence Rules (pp. 140-143)

when the computer is deciding which of two
operators to perform first and the order is not
dictated by parentheses, then it does the
operator of higher precedence before the
operator of lower precedence. Some operators
have equal precedence, in which the order of
operations is determined by the left-to-right
order of the operators.
Computer Programming with
Java
15
Higher Precedence
First: the unary operators: +, -, ++, --, !
Second: the binary arithmetic operators: *, /, %
Third: the binary arithmetic operators: +, Fourth: the boolean operators: <,>,<=,>=
Fifth: the boolean operators: ==, !=
Sixth: the boolean operators: &
Seventh: the boolean operators: |
Eighth: the boolean operators: &&
Ninth: the boolean operators: ||
Lowest Precedence
Display 3.13: Precedence Rules
Computer Programming with
Java
16
The String Class

String class


it can be used to store and process strings of
characters.
String methods
• see text pp.55-59

zero-based index
• String tmp = “Java is fun.”;
• char tmp_len = tmp.length();
0
1
2
3
J
a
v
a
4
5
6
i
s
7
8
9
10 11
f
u
n
Computer Programming with
Java
.
17
public class StringDemo
{
public static void main(String[] args)
{
String sentence = "Text processing is hard!";
int position;
position = sentence.indexOf("hard");
System.out.println(sentence);
System.out.println("012345678901234567890123");
System.out.println("The word \"hard\" starts at index " + position);
sentence = sentence.substring(0, position) + "easy!";
System.out.println("The changed string is:");
System.out.println(sentence);
}
}
Display 2.7: Using the String class
Computer Programming with
Java
18
The String Class (1)

Escape characters



it escape from the usual meaning of a
character.
\”: double quote, \’: single quote, \\:
backslash, \n: new line, \r: carriage return, \t:
tab
e.g.) The word “hard” starts at index 19
• System.out.println(“The word “hard” starts at index
“+19); -> “The word” //incorrect
• System.out.println(“The word \”hard\” starts at
index “+19); //correct
Computer Programming with
Java
19
Documentation

Documentation style

Truth on S/W development
• A Project: Development (20~30%), Maintenance
(70~80%)
• if the program is not easy to read and understand, it
will not be easy to maintain it.

Rule of thumb
• Use meaningful names for variables
• Self-documenting
• thanks to a very clean style and very well-chosen
variable names (and other names)
Computer Programming with
Java
20
Documentation (1)

Comments


//, /*…*/: Things written into your program
that help a person understand the program,
but that are ignored by the compiler.
e.g.)
•
•
•
•

String sentence; //Spanish version
/*This program should only be
used….
*/
Indenting
Computer Programming with
Java
21
Keyboard and Screen I/O

I/O


input and output of program data
Screen Output


System.out.{println, print}()
e.g.)
• System.out.println(“Enter the number of eggs ..:”);
• System.out.println(“Lucky number = “+13+”Secret
number = “+ number);
Computer Programming with
Java
22