software technology - i constants variables data types

25
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Upload: meagan-horton

Post on 28-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

SOFTWARE TECHNOLOGY - I

CONSTANTS

VARIABLES

DATA TYPES

Page 2: 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

Page 3: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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

Page 4: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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

Page 5: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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.

Page 6: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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!

Page 7: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

DATA TYPES

PRIMITIVEDATA TYPESint, char, float

PRIMITIVEDATA TYPES

(objects)

Page 8: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Primitive Data Types

Page 9: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES 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;

Page 10: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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;

Page 11: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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...

Page 12: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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

Page 13: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Screen Class

private attribute (data)

package method

public methods

Page 14: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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?

Page 15: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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.

Page 16: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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!

Page 17: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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)); }}

Page 18: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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

Page 19: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Scope of Variables

Page 20: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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.

Page 21: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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?

Page 22: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Type Casting

type2variable = (type2) type1variable;

int m = 50;byte n = (byte) m;long o = (long) m;

narrowing or demotion

widening or promotion

Page 23: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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

Page 24: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

Floating Point Variable Declaration

float x = 50.232;

float x = 50.232f;

Wrong. Compile time error

Correct

Page 25: SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

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?