Presentation is loading. Please wait.

Presentation is loading. Please wait.

SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.

Similar presentations


Presentation on theme: "SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES."— Presentation transcript:

1 SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES

2 Java Constants ● Numeric Constants – Integer Constants – Real Constants Fixed values that do not change during the execution of a program are clled 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

3 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 75015,750$15750

4 Real Constants ● Also called floating point constants. Legal values: 0.0344-34.2338341.0 215..96-.71 0.65E50.65E+50.65E-5 Parts of a Real Constant: A whole number A decimal point A fractional part An exponent

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

6 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 integer String name;// name is a variable which hold a string Person me;// me is a variable of type Person Variable names should be self documentory to ease the life!

7 DATA TYPES PRIMITIVE DATA TYPES int, char, float PRIMITIVE DATA TYPES (objects)

8 Primitive Data Types

9 Declaration of Variables typevariablename;// variablename is of type type typex,y,z;// x,y and z are of type type Examples: int age; float price, height; byte step;

10 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;

11 Reading from Key Board import 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...

12 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

13 Screen Class private attribute (data) package method public methods

14 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?

15 Without a separate class class 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.

16 Conversion of Strings A 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!

17 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 work try { price = Float.parseFloat(line); } catch (NumberFormatException nfe) {} // Do nothing System.out.println("Price of five shirts is " + (price * 5)); }

18 Exercises Write a program that reads a line from the key board and display it on the screen and repeat the same until the user types the string "quit". Add new methods to the Screen class that performs the following functions: readInt() Returns the integer value of what you type (as a string) from the 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 the name 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 the following line: Price of 10 pen(s) is Rs 105.00

19 Scope of Variables

20 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 assigned value does not vary as the program executes. Upper case letters are used for the symbolic names and cannot be defined inside methods.

21 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?

22 Type Casting type2variable = (type2) type1variable; int m = 50; byte n = (byte) m; long o = (long) m; narrowing or demotion widening or promotion

23 Automatic Type Conversion byte b = 75; // A byte is stored in 1 byte of memory int a = b; // An integer uses 4 bytes of memory Automatically promoted

24 Floating Point Variable Declaration float x = 50.232; float x = 50.232f; Wrong. Compile time error Correct

25 Standard Default Values If you do not specify values of member variables of a class, they will have default values! Person p = new Person(); p.setName("Chandana"); // We only set the name p.showInfo(); What is the age of Chandana?


Download ppt "SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES."

Similar presentations


Ads by Google