Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.

Similar presentations


Presentation on theme: "1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for."— Presentation transcript:

1 1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for Everyday Life

2 2 pritisajja.info Unit 4: Course Content Basic Java Programming Concepts : (8 hrs) Introduction to java and key features Primitive Data Types, Variable Names, Scope, Operators, Expressions, Control Flow Statements, Arrays, Anatomy of Java Applications and Applets. Schildt H. : The Complete Reference Java 2, 5 th Edition, McGraw-Hill / Osborne, 2002

3 3 pritisajja.info What is Java? Java is object-oriented with built in Application Programming Interface (API) It has borrowed its syntax from C/C++ Java does not have pointers directly. Applications and applets are available. Java is both compiled and interpreted. –Source code is compiled to bytecode. –The Java Virtual Machine (JVM) loads and links parts of the code dynamically at run time (late or dynamic binding).

4 4 pritisajja.info Platform independence of Java Java instruction code … Byte Code … compiler Java virtual Machine it has an instruction set it manipulates various memory areas at run time. Java virtual Machine it has an instruction set it manipulates various memory areas at run time. Byte code Byte codes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of byte codes for each method in the class. The byte codes streams are stored in the method area of the JVM. The byte codes for a method are executed when that method is invoked during the course of running the program. They can be executed by interpretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM. Byte code Byte codes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of byte codes for each method in the class. The byte codes streams are stored in the method area of the JVM. The byte codes for a method are executed when that method is invoked during the course of running the program. They can be executed by interpretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM. Host system …

5 5 pritisajja.info Features of Java: To follow Simple Remote applets are not trusted and not allowed to use local resources Secure Supports advantages of OOA Object-oriented Independent form hardware and software platforms Platform independent and Architecture Neural It is complied also and interpreted also. Interpreted Java is strong, replacing pointer by reference and provides automatic memory management Robust Supports concurrent procedures Multi threaded Supports dynamic binding and links parts of code at the time of execution. Distributed and Dynamic Java provides native language support High performance

6 6 pritisajja.info First java program: Hello World Application Step 1: Write java code /** The HelloWorld class implements an application that simply displays “Hello World!” to the standard output (console) */ public class HelloWorld { public static void main (String args[]) { System.out.println(“Hello world!”); } // end of main ………………………………………….. }// end of class ………………………………………………... Output: Hello World!

7 7 pritisajja.info Naming Conventions Java distinguishes between UPPER and lower case variables. The convention is to capitalize the first letter of a class name. If the class name consists of several words, they are run together with successive words capitalized within the name (instead of using underscores to separate the names). The name of the constructor is the same as the name of the class. All keywords (words that are part of the language and cannot be redefined) are written in lower case.

8 8 pritisajja.info Prototype of the main method public static void main (String args[]) public is the access specifier. static is the storage class. void is the return type. String args[ ] is an array of arguments. Check public static void main( ) ? Will it cause any error? If yes, what?

9 9 pritisajja.info About main method… Several main methods can be defined in a java class. The interpreter will look for a main method with the prescribed signature as the entry point. A method named main, which has some other signature is of no particular significance. It is like any other method in the class. Therefore, if the main method is not declared correctly, the application will not execute. There may not be any compilation problem. This class will compile correctly, but will not execute. The interpreter will say In class NoMain: void main (String argv[]) is not defined

10 10 pritisajja.info public class TwoMains { /** This class has two main methods with * different signatures */ public static void main (String args[]) { //required prototype for main method System.out.println(“Hello world!”); int i; i = main(2); System.out.println (“i = ” + i ); } /**This is the additional main method*/ public static int main(int i) { return i*i; } } // end of class Try this…. Output will be…. Hello World! i = 4 Output will be…. Hello World! i = 4 PSS

11 11 pritisajja.info Is it true? The argument to the mandatory main function public static void main (String args[]) which is String args [] can also be written as String [] args

12 12 pritisajja.info Comments There are three types of comments defined by Java. 1. Single-line comment :Java single line comment starts from // and ends till the end of that line. 2.Multiline comment: Java multiline comment is between /* and */. 3.Documentation comment : Documentation comment is used to produce an HTML file that documents your program. The documentation comment begins with a /** and ends with a */.

13 13 pritisajja.info Identifiers Identifiers are used for class names, method names, and variable names. An identifier may be any sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. Identifiers must not begin with a number. Java Identifiers are case-sensitive. Some valid identifiers are ATEST, count, i1, $Atest, and this_is_a_test Some invalid identifiers are 2count, h-l, and a/b Identifiers are used for class names, method names, and variable names. An identifier may be any sequence of uppercase and lowercase letters, numbers, or the underscore and dollar-sign characters. Identifiers must not begin with a number. Java Identifiers are case-sensitive. Some valid identifiers are ATEST, count, i1, $Atest, and this_is_a_test Some invalid identifiers are 2count, h-l, and a/b

14 14 pritisajja.info Operators Java operators can be grouped into the following four groups: Arithmetic, Bitwise, Relational, and Logical. Java operators can be grouped into the following four groups: Arithmetic, Bitwise, Relational, and Logical.

15 15 pritisajja.info Arithmetic Operators Operator Result + Addition - Subtraction (unary minus) * Multiplication / Division % Modulus ++ Increment += Addition assignment -= Subtraction assignment *= Multiplication assignment /= Division assignment %= Modulus assignment -- Decrement Operator Result + Addition - Subtraction (unary minus) * Multiplication / Division % Modulus ++ Increment += Addition assignment -= Subtraction assignment *= Multiplication assignment /= Division assignment %= Modulus assignment -- Decrement The operands of the arithmetic operators must be of a numeric type. You cannot use arithmetic operators on boolean types, but you can use them on char types.

16 16 pritisajja.info Bitwise Operators Operator Result ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift right >>> Shift right zero fill << Shift left &= Bitwise AND assignment |= Bitwise OR assignment ^= Bitwise exclusive OR assignment >>= Shift right assignment >>>= Shift right zero fill assignment <<= Shift left assignment Operator Result ~ Bitwise unary NOT & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR >> Shift right >>> Shift right zero fill << Shift left &= Bitwise AND assignment |= Bitwise OR assignment ^= Bitwise exclusive OR assignment >>= Shift right assignment >>>= Shift right zero fill assignment <<= Shift left assignment Java bitwise operators can be applied to the integer types: long, int, short, char, byte. Bitwise Operators act upon the individual bits of their operands. Java bitwise operators can be applied to the integer types: long, int, short, char, byte. Bitwise Operators act upon the individual bits of their operands.

17 17 pritisajja.info Relational Operators Operator Result == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Operator Result == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to The relational operators determine the relationship between two operands.

18 18 pritisajja.info Boolean Logical Operators Operator Result & Logical AND | Logical OR ^ Logical XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! Logical unary NOT &= AND assignment |= OR assignment ^= XOR assignment == Equal to != Not equal to ? : Ternary if-then-else Operator Result & Logical AND | Logical OR ^ Logical XOR (exclusive OR) || Short-circuit OR && Short-circuit AND ! Logical unary NOT &= AND assignment |= OR assignment ^= XOR assignment == Equal to != Not equal to ? : Ternary if-then-else The relational operators determine the relationship between two operands.

19 19 pritisajja.info Data Types Three kinds of data types are supported by Java. –primitive data types –reference data types –the special null data type {that is we may write if (obj!= null)}

20 20 pritisajja.info Primitive Data Types in Java TypeKindMemoryRange byte integer1 byte-128 to 127 short integer2 bytes to int integer4 bytes to long integer8 bytes to float floating point4 bytes ± x to ± x double floating point8 bytes ± x to ± x char single character 2 bytesall Unicode characters boolean true or false1 bit There is no unsigned integer in java.

21 21 pritisajja.info /** This program demonstrates how Java * adds two integers. */ public class BigInt { public static void main(String args[]) { int a = ; //(9 zeros) int b = ; System.out.println ( “This is how Java adds integers”); System.out.println ( a + “+” + b + “ = ” + (a+b) ); } // end of main }// end of class Try this…. Output: This is how Java adds integers = Output: This is how Java adds integers =

22 22 pritisajja.info public class Significant { public static void main (String args[]) { final float PI = f; float radius = 1.0f; float area; area = PI * radius * radius; System.out.println (“The area of the circle = ” + area); }// end of main }// end of class Try this…. Output: area of the circle = Output: area of the circle =

23 23 pritisajja.info Declaration of variable A variable is defined by an identifier, a type, and an optional initializer. The variables also have a scope(visibility / lifetime). In Java, all variables must be declared before they can be used. The basic form of a variable declaration is : type identifier [ = value][, identifier [= value]...] ; Java allows variables to be initialized dynamically. For example: double c = 2 * 2;

24 24 pritisajja.info Scope and life of a variable: Variables declared inside a scope are not accessible to code outside. Scopes can be nested. The outer scope encloses the inner scope. Variables declared in the outer scope are visible to the inner scope. Variables declared in the inner scope are not visible to the outside scope.

25 25 pritisajja.info public class Main { public static void main(String args[]) { int x; // known within main x = 10; if (x == 10) { int y = 20; System.out.println("x and y: " + x + " " + y); x = y + 2; } System.out.println("x is " + x); }// end of main }// end of class Try this…. Output: x and y: x is 22 Output: x and y: x is 22 PSS

26 26 pritisajja.info public class Main2 { public static void main(String args[]) { if (true) {int y = 20; System.out.println("y: " + y); } // end of if y = 100; }// end of main }// end of class Try this…. Output: D:\>javac Main.java Main.java:9: cannot find symbol symbol : variable y location: class Main y = 100; // Error! y not known here ^ 1 error Output: D:\>javac Main.java Main.java:9: cannot find symbol symbol : variable y location: class Main y = 100; // Error! y not known here ^ 1 error PSS

27 27 pritisajja.info public class Main3 { public static void main(String args[]) { int i = 1; {int i = 2; } Try this…. Output: Results in compilation error. ‘i‘ is already defined…… Output: Results in compilation error. ‘i‘ is already defined…… PSS

28 28 pritisajja.info Flow Control: if: if(condition) statement; Note: Write a java program that compares two variables and print appropriate message. The condition can be expression that result in a value. Expression may return boolean value. if (b) is equivalent to if (b== true). if(condition) statement; Note: Write a java program that compares two variables and print appropriate message. The condition can be expression that result in a value. Expression may return boolean value. if (b) is equivalent to if (b== true).

29 29 pritisajja.info Flow Control: if else: if (condition) statement1; else statement2; Each statement may be a single statement or a compound statement enclosed in curly braces (a block). The condition is any expression that returns a boolean value. Nested if statements are possible if (condition) statement1; else statement2; Each statement may be a single statement or a compound statement enclosed in curly braces (a block). The condition is any expression that returns a boolean value. Nested if statements are possible

30 30 pritisajja.info Flow Control: if else ladder: if(condition) statement; Example else if(condition) statement; … else statement; if(condition) statement; Example else if(condition) statement; … else statement; public class Main4 { public static void main(String args[]) { int month = 4; String value; if (month == 1) value = "A"; else if (month == 2) value = "B"; else if (month == 3) value = "C"; else if (month == 4) value = "D"; else value = "Error"; System.out.println("value = " + value); } public class Main4 { public static void main(String args[]) { int month = 4; String value; if (month == 1) value = "A"; else if (month == 2) value = "B"; else if (month == 3) value = "C"; else if (month == 4) value = "D"; else value = "Error"; System.out.println("value = " + value); } PSS

31 31 pritisajja.info Switch statement: switch (expression) { case value1: statement sequence break; case value2:statement sequence break;... case valueN: statement sequence break; default: default statement sequence }. Switch statement can be nested

32 32 pritisajja.info Command Line arguments public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[0] ); boolean Leap; Leap= (year % 4 == 0); if ((Leap) && (year!=100)) System.out.println(Leap); } public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[0] ); boolean Leap; Leap= (year % 4 == 0); if ((Leap) && (year!=100)) System.out.println(Leap); } Execution java LeapYear 2000 true Execution java LeapYear 2000 true PSS

33 33 pritisajja.info Command Line arguments public class PowersOfTwo {public static void main(String[] args) {int N = Integer.parseInt(args[0]); int i = 0; int powerOfTwo = 1; while (i <= N) { System.out.println(i + " " + powerOfTwo); powerOfTwo = 2 * powerOfTwo; i = i + 1; } } public class PowersOfTwo {public static void main(String[] args) {int N = Integer.parseInt(args[0]); int i = 0; int powerOfTwo = 1; while (i <= N) { System.out.println(i + " " + powerOfTwo); powerOfTwo = 2 * powerOfTwo; i = i + 1; } } Execution java PowersOfTwo 4 ???? Execution java PowersOfTwo 4 ???? PSS

34 34 pritisajja.info Command Line arguments public class Sqrt { public static void main(String[] args) { double c = Double.parseDouble(args[0]); double epsilon = 1e-15; double t = c; // relative error tolerance while (Math.abs(t - c/t) > epsilon*t) { t = (c/t + t) / 2.0; } // print out the estimate of the square root of System.out.println(t); } public class Sqrt { public static void main(String[] args) { double c = Double.parseDouble(args[0]); double epsilon = 1e-15; double t = c; // relative error tolerance while (Math.abs(t - c/t) > epsilon*t) { t = (c/t + t) / 2.0; } // print out the estimate of the square root of System.out.println(t); } Execution java Sqrt 4.5 ???? Execution java Sqrt 4.5 ???? PSS

35 35 pritisajja.info Recursion class factorial{ int fact(int n){ if (n==1) return 1; else return (n*fact(n-1));} } class factdemo{ public static void main (String args[]){ int a = 4; int fa=0; factorial f = new factorial (); fa=f.fact(a); System.out.println(fa); } class factorial{ int fact(int n){ if (n==1) return 1; else return (n*fact(n-1));} } class factdemo{ public static void main (String args[]){ int a = 4; int fa=0; factorial f = new factorial (); fa=f.fact(a); System.out.println(fa); } PSS

36 36 pritisajja.info Fibonacci class fibonacci { int fibo(int n){ if (n==1) return 1; else return ( fibo(n-1) + fibo(n-2) ); } } class fibodemo{ public static void main (String args[]){ int a = 3; int fa=0; fibonacci f = new fibonacci (); fa=f.fibo(a); System.out.println(fa); } class fibonacci { int fibo(int n){ if (n==1) return 1; else return ( fibo(n-1) + fibo(n-2) ); } } class fibodemo{ public static void main (String args[]){ int a = 3; int fa=0; fibonacci f = new fibonacci (); fa=f.fibo(a); System.out.println(fa); } PSS

37 37 pritisajja.info Arrays General form of one dim array declaration is type array-name[size]; Examples are: int a[10]; –Defines 10 integers such as a[0], a[1], … a[9] char let[26]; –Defines 26 alphabets let[1]=‘B’; float x[20]; Employee e[100]; //Employee is a class definition Tree t[15]; // Tree is a class

38 38 pritisajja.info Array Definition with Initialization int maxmarks[6]= {71,56,67,65,43,66} char let[5]= {‘a’, ‘e’, ‘I’, ’o’, ’u’}; Initialization of an array can be done using new statement as follows: –int a[j]; // defines a as an array contains j integrs –a=new int [10] // assigns 10 integers to the array a This can also be written as –int [] a = new int [10]; int maxmarks[6]= {71,56,67,65,43,66} char let[5]= {‘a’, ‘e’, ‘I’, ’o’, ’u’}; Initialization of an array can be done using new statement as follows: –int a[j]; // defines a as an array contains j integrs –a=new int [10] // assigns 10 integers to the array a This can also be written as –int [] a = new int [10];

39 39 pritisajja.info Example of array class array{ public static void main (String args[ ]){ int score [] = { 66,76,45,88,55,60}; for (int i=0; i<6; i++) System.out.println(score[i]); System.out.println(“==============”); } class array{ public static void main (String args[ ]){ int score [] = { 66,76,45,88,55,60}; for (int i=0; i<6; i++) System.out.println(score[i]); System.out.println(“==============”); } PSS

40 40 pritisajja.info Example of array public class Main4 { public static void main(String[] args) { int[] intArray = new int[] { 1, 2, 3, 4, 5 }; // calculate sum int sum = 0; for (int i = 0; i < intArray.length; i++) { sum = sum + intArray[i]; } // calculate average double average = sum / intArray.length; System.out.println("average: " + average); } public class Main4 { public static void main(String[] args) { int[] intArray = new int[] { 1, 2, 3, 4, 5 }; // calculate sum int sum = 0; for (int i = 0; i < intArray.length; i++) { sum = sum + intArray[i]; } // calculate average double average = sum / intArray.length; System.out.println("average: " + average); } PSS

41 41 pritisajja.info Example of array public class Main6 { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; int a3[] = {4, 3, 2, 1}; System.out.println("length of a1 is " + a1.length); System.out.println("length of a2 is " + a2.length); System.out.println("length of a3 is " + a3.length); } public class Main6 { public static void main(String args[]) { int a1[] = new int[10]; int a2[] = {1, 2, 3, 4, 5}; int a3[] = {4, 3, 2, 1}; System.out.println("length of a1 is " + a1.length); System.out.println("length of a2 is " + a2.length); System.out.println("length of a3 is " + a3.length); } PSS

42 42 pritisajja.info Example of array with functions class ArrayPass { void printing(int s[]){ int i=0; for (i=0; i<6; i++) System.out.println(s[i]); System.out.println("============="); } class arraydemo{ public static void main (String args[ ]){ ArrayPass student = new ArrayPass(); int score[] = {66,76,45,88,55,60}; student.printing(score); } class ArrayPass { void printing(int s[]){ int i=0; for (i=0; i<6; i++) System.out.println(s[i]); System.out.println("============="); } class arraydemo{ public static void main (String args[ ]){ ArrayPass student = new ArrayPass(); int score[] = {66,76,45,88,55,60}; student.printing(score); } PSS

43 43 pritisajja.info import java.util.*; public class array{ public static void main(String[] args){ int num[] = {50,20,45,82,25,63}; int l = 6; // you may use l= num.length; int i,j,t; System.out.print("Given number : "); for (i = 0;i < l;i++ ) { System.out.print(" " + num[i]); } System.out.println("\n"); System.out.print("Accending order number : "); Arrays.sort(num); for(i = 0;i < l;i++){ System.out.print(" " + num[i]); } } } import java.util.*; public class array{ public static void main(String[] args){ int num[] = {50,20,45,82,25,63}; int l = 6; // you may use l= num.length; int i,j,t; System.out.print("Given number : "); for (i = 0;i < l;i++ ) { System.out.print(" " + num[i]); } System.out.println("\n"); System.out.print("Accending order number : "); Arrays.sort(num); for(i = 0;i < l;i++){ System.out.print(" " + num[i]); } } }

44 44 pritisajja.info Two Dimensional Arrays Declaration of a two dimensional array called twoD with size 4*5 int twoD[][] = new int[4][5]; (0,0)(0,3)(0,4) (1,0)(1,1)……(1,4) (2,0)…(2,2)…(2,4) (3,0)……(3,3)(3,4)

45 45 pritisajja.info Matrix public class Main { public static void main(String args[]) { int twoD[][] = new int[4][5]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { twoD[i][j] = i*j; } } // for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { System.out.print(twoD[i][j] + " "); } System.out.println(); } } public class Main { public static void main(String args[]) { int twoD[][] = new int[4][5]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { twoD[i][j] = i*j; } } // for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) { System.out.print(twoD[i][j] + " "); } System.out.println(); } }

46 46 pritisajja.info Initialization of Two Dimensional Array public class Main{ public static void main(String args[]) { double m[][] = { { 0, 1, 2, 3 }, { 0, 1, 2, 3 }, { 0, 1, 2, 3 } }; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } } public class Main{ public static void main(String args[]) { double m[][] = { { 0, 1, 2, 3 }, { 0, 1, 2, 3 }, { 0, 1, 2, 3 } }; for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } }

47 47 pritisajja.info Three Dimensional Array public class Main { public static void main(String args[]) { int threeD[][][] = new int[3][4][5]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) for (int k = 0; k < 5; k++) threeD[i][j][k] = i * j * k; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 5; k++) System.out.print(threeD[i][j][k] + " "); System.out.println(); } } } public class Main { public static void main(String args[]) { int threeD[][][] = new int[3][4][5]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) for (int k = 0; k < 5; k++) threeD[i][j][k] = i * j * k; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 5; k++) System.out.print(threeD[i][j][k] + " "); System.out.println(); } } }

48 48 pritisajja.info Jagged array When you allocate memory for a multidimensional array, you can allocate the remaining dimensions separately. For example, the following code allocates the second dimension manually. public class Main { public static void main(String[] argv) { int twoD[][] = new int[4][]; twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; twoD[3] = new int[5]; } }

49 49 pritisajja.info public class Main { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4];

50 50 pritisajja.info for (int i = 0; i < 4; i++) { for (int j = 0; j < i + 1; j++) { twoD[i][j] = i + j; } } // for (int i = 0; i < 4; i++) { for (int j = 0; j < i + 1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } for (int i = 0; i < 4; i++) { for (int j = 0; j < i + 1; j++) { twoD[i][j] = i + j; } } // for (int i = 0; i < 4; i++) { for (int j = 0; j < i + 1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } }

51 51 pritisajja.info Bank demo Student1 Student 2 Employee

52 52 pritisajja.info Bank constructor class Bank { int accno; String accname; float accbal; Bank() {accno=999; accname= "XXX"; accbal= 0;} Bank(int x, String y, float z) {accno=x; accname= y; accbal= z;} Bank(int x, String y)// default t constructor {accno=x; accname= y; accbal= 1000;} void printbal() { System.out.println (accno); System.out.println ( accname ); System.out.println (accbal); } }// end of class class Bank { int accno; String accname; float accbal; Bank() {accno=999; accname= "XXX"; accbal= 0;} Bank(int x, String y, float z) {accno=x; accname= y; accbal= z;} Bank(int x, String y)// default t constructor {accno=x; accname= y; accbal= 1000;} void printbal() { System.out.println (accno); System.out.println ( accname ); System.out.println (accbal); } }// end of class

53 53 pritisajja.info Bank constructor class BankDemo { public static void main (String args[ ]){ Bank b1= new Bank(); Bank b2 = new Bank(123, "PSS"); Bank b3 = new Bank (124, "XYZ", 5000); b1.printbal(); b2.printbal(); b3.printbal(); } class BankDemo { public static void main (String args[ ]){ Bank b1= new Bank(); Bank b2 = new Bank(123, "PSS"); Bank b3 = new Bank (124, "XYZ", 5000); b1.printbal(); b2.printbal(); b3.printbal(); }

54 54 pritisajja.info Bank with methods and array class Bank { int accno; String accname; float accbal; Bank() {accno=999; accname= "XXX"; accbal= 0;} Bank(int x, String y, float z) {accno=x; accname= y; accbal= z;} Bank(int x, String y) {accno=x; accname= y; accbal= 1000;} void printbal() { System.out.println (accno); System.out.println ( accname ); System.out.println (accbal); System.out.println(" "); } void deposit(float Amt) { System.out.println("Depositing....."+ Amt); accbal=accbal + Amt; } void withdraw(float Amt) { System.out.println("Withdrwing....."+ Amt); accbal=accbal - Amt; } }// end of class class Bank { int accno; String accname; float accbal; Bank() {accno=999; accname= "XXX"; accbal= 0;} Bank(int x, String y, float z) {accno=x; accname= y; accbal= z;} Bank(int x, String y) {accno=x; accname= y; accbal= 1000;} void printbal() { System.out.println (accno); System.out.println ( accname ); System.out.println (accbal); System.out.println(" "); } void deposit(float Amt) { System.out.println("Depositing....."+ Amt); accbal=accbal + Amt; } void withdraw(float Amt) { System.out.println("Withdrwing....."+ Amt); accbal=accbal - Amt; } }// end of class

55 55 pritisajja.info Bank Calling Class class BankDemo3 { public static void main (String args[ ]){ Bank [] b = new Bank[3]; b[0]= new Bank(); b[0].printbal(); b[1]= new Bank(111, "PPP", 5000); b[1].printbal(); b[2]= new Bank(222,"SSS", 10000); b[2].printbal(); b[2].deposit (10000); b[2].printbal(); b[2].withdraw(15000); b[2].printbal(); } class BankDemo3 { public static void main (String args[ ]){ Bank [] b = new Bank[3]; b[0]= new Bank(); b[0].printbal(); b[1]= new Bank(111, "PPP", 5000); b[1].printbal(); b[2]= new Bank(222,"SSS", 10000); b[2].printbal(); b[2].deposit (10000); b[2].printbal(); b[2].withdraw(15000); b[2].printbal(); }

56 56 pritisajja.info Home Assignment Consider students class as follows: –Sno  integer –Sname  String –Marks  6 integers Write java class having the above Student structure. Define method for total, average and result printing in this class. Define a main class, having an array of 3 students. Use the developed utilities for these 3 students. Consider students class as follows: –Sno  integer –Sname  String –Marks  6 integers Write java class having the above Student structure. Define method for total, average and result printing in this class. Define a main class, having an array of 3 students. Use the developed utilities for these 3 students.

57 57 pritisajja.info Strings Strings in java are not primitive data types but members of String class. + operator can be used to join two strings.

58 58 pritisajja.info avaworld/jw /jw- 09-bytecodes.htmlhttp://www.javaworld.com/j avaworld/jw /jw- 09-bytecodes.html pctechs.biz Java2s.com edu/java/code/http://introcs.cs.princeton. edu/java/code/

59 59 pritisajja.info Strings Strings in java are not primitive data types but members of String class. + operator can be used to join two strings.

60 60 pritisajja.info avaworld/jw /jw- 09-bytecodes.htmlhttp://www.javaworld.com/j avaworld/jw /jw- 09-bytecodes.html pctechs.biz Java2s.com


Download ppt "1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for."

Similar presentations


Ads by Google