Download Lab 3 Data Types and Operations

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
Islamic University of Gaza (IUG)
Faculty of Engineering
Computer Engineering Department
Java Programing (1) Lab.
ECOM 2114
Eng. Asma Obeid
Lab 3
Data Types and Operations
Continue
Shorthand Operators:
Java allows you to combine assignment and addition operators using a shorthand operator. For example,
i = i + 8; can be written as: i += 8;
The += is called the addition assignment operator. Other shorthand operators are shown in following
tables.
Shorthand Operators
Operator Name
+=
Addition assignment
-=
Subtraction assignment
*=
Multiplication assignment
/=
Division assignment
%=
Remainder assignment
Operator Name
++var
preincrement
Example
i += 8
f -= 8.0
i*= 8
i /= 8
i %= 8
Equivalent
i=i+8
f = f - 8.0
i=i*8
i=i/8
i=i%8
Increment and Decrement Operators
Description
The expression (++var) increments var by 1 and evaluates to the new value
in var after the increment.
var++
--var
var--
postincrement
The expression (var++) evaluates to the original value in var and
increments var by 1.
predecrement The expression (––var) decrements var by 1 and evaluates to the new value
in var after the decrement.
postdecrement The expression (var––) evaluates to the original value in var and
decrements var by 1.
If the operator is before (prefixed to) the variable, the variable is incremented or decremented by 1, then
the new value of the variable is returned. If the operator is after (suffixed to) the variable, the original
old value of the variable is returned.
In this case, i is incremented by 1, then the old value of i is returned and used in the multiplication. So
newNum becomes 100. If i++ is replaced by ++i as follows,
i is incremented by 1, and the new value of i is returned and used in the multiplication. Thus newNum
becomes 110.
Casting:
You can always assign a value to a numeric variable whose type supports a larger range of values; thus,
for instance, you can assign a long value to a float variable. You cannot, however, assign a value to a
variable of a type with smaller range unless you use type casting. Casting is an operation that converts a
value of one data type into a value of another data type.
Widening a type: Casting a variable of a type with a small range to a variable of a type with a larger
range.
Narrowing a type: Casting a variable of a type with a large range to a variable of a type with a smaller
range .
Widening a type can be performed automatically without explicit casting(e.g. casting int to double) .
Narrowing a type must be performed explicitly(e.g double to int).
float f = (float)10.1; // narrowing the double 10.1 to the float f
int i = (int)f; // narrowing the float f to the int i
[Pick the date] Page 2
2.9. Character Data Type and Operations
char, is used to represent a single character. A character literal is enclosed in single quotation marks.
Consider the following code:
char letter = 'A';
char numChar = '4';
The first statement assigns character A to the char variable letter. The second statement assigns the digit
character 4 to the char variable numChar.
 Note:
"A" is a string
'A' is a character
 Note
The increment and decrement operators can also be used on char variables to get the next or preceding
Unicode character. For example, the following statements display character b.
char ch = 'a';
System.out.println(++ch);
Escape Sequences for Special Characters
An escape sequence begins with the backslash character (\) followed by a character that has a special
meaning to the compiler.
Java Escape Sequences
Character Escape Sequence Name
Unicode Code
\b
Backspace
\u0008
\t
Tab
\u0009
\n
Linefeed
\u000A
\f
Formfeed
\u000C
\r
Carriage Return \u000D
\\
Backslash
\u005C
\'
Single Quote
\u0027
\"
Double Quote
\u0022
Suppose you want to print the quoted message shown below:
He said "Java is fun"
[Pick the date] Page 3
The statement to print it should be
System.out.println("He said \"Java is fun\"");
Casting Between char and Numeric Types
A char can be cast into any numeric type, and vice versa. When an integer is cast into a char, only its
lower sixteen bits of data are used; the other part is ignored. For example, see the following code:
char c = (char)0XAB0041; // the lower 16 bits hex code 0041 is
// assigned to c
System.out.println(c);
// c is character A
When a floating-point value is cast into a
point value is cast into a char.
char, the integral part of the floating-
char c = (char)65.25; // decimal 65 is assigned to t
System.out.println(c); // c is character A
When a char is cast into a numeric type, the character's Unicode is cast into the specified numeric type.
int i = (int)'A';
// the Unicode of character A is assigned to i
System.out.println(i); // i is 65
byte b = 'a'; // implicit casting cause the Unicode of 'a' = 97
//in the range
int i = 'a';
But the following casting is incorrect, because the Unicode \uFFF4 cannot fit into a byte:
byte b = '\uFFF4';
To force assignment, use explicit casting, as follows:
byte b = (byte)'\uFFF4';
Any positive integer between 0 and FFFF in hexadecimal can be cast into a character implicitly. Any
number not in this range must be cast into a char explicitly.
[Pick the date] Page 4
The String Type
The char type only represents one character. To represent a string of characters, use the data type called
String. For example, the following code declares the message to be a string that has an initial value of
"Welcome to Java".
String message = "Welcome to Java"; // String is not primitive, it's a referential type
The plus sign (+) is the concatenation operator if one of the operands is a string.
// Three strings are concatenated
String message = "Welcome" + "to" + "Java";
// String Chapter is concatenated with number 2
String s = "Chapter" + 2; // s becomes Chapter2
// String Supplement is concatenated with character B
String s1 = "Supplement" + 'B'; // s becomes SupplementB
Example 1:
[Pick the date] Page 5
[Pick the date] Page 6
Example 2:
Getting Input from Input Dialogs
You can use the showInputDialog method in the JOptionPane class to get input at runtime. When this
method is executed, a dialog is displayed to enable you to enter an input value, as shown in the
following figure:
After entering a string, click OK to accept the input and dismiss the dialog box. The input is returned
from the method as a string. You can invoke the method with four arguments, as follows:
[Pick the date] Page 7
The first argument can always be null. The second argument is a string that prompts the user. The third
argument
is
the
title
of
the
input
box.
The
fourth
argument
can
be
JOptionPane.QUESTION_MESSAGE, which causes the icon (
) to be displayed in the input box.
How to use Input Dialogs:
One is to use a statement as shown in the example:
String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE);
where x is a string for the prompting message, and y is a string for the title of the input dialog box.
The other is to use a statement like this one:
JOptionPane.showInputDialog(x);
where x is a string for the prompting message.
Converting Strings to Numbers
The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it
returns "123". You have to convert a string into a number to obtain the input as a number.
To convert a string into an int value, use the parseInt method in the Integer class, as follows:
int intValue = Integer.parseInt(intString);
where intString is a numeric string such as "123".
To convert a string into a double value, use the parseDouble method in the Double class, as follows:
double doubleValue = Double.parseDouble(doubleString);
where doubleString is a numeric string such as "123.45".
[Pick the date] Page 8
The Integer and Double classes are both included in the java.lang package, and thus are automatically
imported.
HW:
Repeat the same work and Refering to text book.
2.7 & 2.9
[Pick the date] Page 9