Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
-
Upload
meagan-horton -
Category
Documents
-
view
220 -
download
0
Transcript of SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
SOFTWARE TECHNOLOGY - I
CONSTANTS
VARIABLES
DATA TYPES
Java Constants
● Numeric Constants– Integer Constants– Real Constants
Fixed values that do not change during the execution of a program areclled constants.
Example:"Hi There" is a string constant in the followin program line:
System.out.println("Hi There");
● Character Constants– Single Character
Constants– String Constants
Integer Constants
● Decimal Intergers (Base 10)123, -23, 0, 938754
● Octal Integers (Base 8)037, 0, -0555
● Hexadecimal Integers (Base 16)0x4, 0xFA, 0x0
Illegal values:
15 750 15,750 $15750
Real Constants
● Also called floating point constants.
Legal values:
0.0344 -34.2338 341.0215. .96 -.710.65E5 0.65E+5 0.65E-5
Parts of a Real Constant:
A whole numberA decimal pointA fractional partAn exponent
Character Constants
● Single Character Constants'5' 'A' '@' ' '
● String Constants"Hello World!" "123" "+"
● Backslash Character Constants (Escape sequences)These are special characters mostly used in output methods.
VARIABLES
A variable is an identifier of a storage location in memory.
You can define variables, set values to them and use them in your program.
While defining variables, you should specify the type of them.
Example:int age; // age is a variable which is of type integerString name; // name is a variable which hold a stringPerson me; // me is a variable of type Person
Variable names should be self documentory toease the life!
DATA TYPES
PRIMITIVEDATA TYPESint, char, float
PRIMITIVEDATA TYPES
(objects)
Primitive Data Types
Declaration of Variables
type variablename; // variablename is of type typetype x,y,z; // x,y and z are of type type
Examples:
int age;float price, height;byte step;
Values of variables
variableName = value;
Examples:
age = 18;price = 13.50;checked = true;itemCode = 'h';x = y = z = 34;
int i = 5;boolean sealed = false;name = "Kamal Sisira Kumara";double x = 23.4, y = 43.8;
Reading from Key Boardimport java.io.*;
class Screen {
private BufferedReader in;
Screen() {InputStreamReader ins = new InputStreamReader(System.in);in = new BufferedReader(ins);
}
public String readLine() {String line = null;try {
// Continued in next page...
Cont. Reading from Key Board public String readLine() {
String line = null;try {
line = in.readLine();} catch (IOException ioe) {
System.err.println("IO problem occured"); line = "IO problem occured";
}return line;
}
public void sendLine(String line) {System.out.println(line);
}} // Screen class end
Screen Class
private attribute (data)
package method
public methods
Using the Screen Class
class ReadTest { public static void main(String[] args) {
Screen screen = new Screen(); // Create a screen object
String line = null;// Will store what is read from s
System.out.print("Enter your name: ");line = screen.readLine();System.out.println("Your name is " + line + ".");
}}
Q: Is it useful to create several Screen objects in your program?
Without a separate classclass ReadTest {
public static void main(String[] args) {BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
try {System.out.print("Enter your name: ");System.out.println("Your name is " + br.readLine());
} catch (IOException ioe) {System.err.println("IO Error occurred");
}
}}
Ex: Modify the program to exit from the program if an error occurs.
Conversion of StringsA String to an integer:
Integer.parseInt(String)int x = Integer.parseInt(s.readLine());
A String to a float:
Float.parseFloat(String)float y = Float.parseFloat("5.9");
A String to a double:
Double.parseDouble(String)double price = Double.parseDouble(".334");
Exceptions might arise!
With Exception Handling...class ReadTest { public static void main(String[] args) {
Screen s = new Screen(); // Create a screen object
System.out.print("Enter the price of a shirt: ");line = s.readLine();System.out.println("Price of a single shirt is " + line + ".");
float price = 0.0f; // Just declaration does not worktry {
price = Float.parseFloat(line);} catch (NumberFormatException nfe) {} // Do nothing
System.out.println("Price of five shirts is " + (price * 5)); }}
Exercises
Write a program that reads a line from the key board and display iton the screen and repeat the same until the user types the string "quit".
Add new methods to the Screen class that performs the followingfunctions:
readInt()Returns the integer value of what you type (as a string) fromthe key board.
readFloat()Returns the float value of what you type from the key board.
Write a program that uses the above modified Screen class to read thename of an Item in a shop (say "pen"), price of the item (say Rs 10.50)and number of items (say 10) and produce an output similar to thefollowing line:
Price of 10 pen(s) is Rs 105.00
Scope of Variables
Symbolic Constants
Constants that do not change in a program can be assigned symbols.
final float PI = 3.14159f;
PI is the symbol given for the constant 3.14159.
Once initialized, PI cannot be changed.
Symbolic constants are used very much similar to variables but the assignedvalue does not vary as the program executes.
Upper case letters are used for the symbolic names and cannot be definedinside methods.
Advantages of Symbolic Constants
● Modification of the program is easy
final int WORKERS = 103;........avgSalary = totalSalary/WORKERS;
● Understanding the program is easy
present = 103 - absents; // What is 103?
Type Casting
type2variable = (type2) type1variable;
int m = 50;byte n = (byte) m;long o = (long) m;
narrowing or demotion
widening or promotion
Automatic Type Conversion
byte b = 75; // A byte is stored in 1 byte of memoryint a = b; // An integer uses 4 bytes of memory
Automatically promoted
Floating Point Variable Declaration
float x = 50.232;
float x = 50.232f;
Wrong. Compile time error
Correct
Standard Default Values
If you do not specify values of member variables of a class, they willhave default values!
Person p = new Person();p.setName("Chandana"); // We only set the namep.showInfo();
What is the age of Chandana?