1 PROGRAMMING in JAVA (CSE4308)
CSE4308: PROGRAMMING IN JAVA Course modules • Java Programming Fundamentals • Introducing Data types and Operators • Program Control statements • Classes and Methods • Inheritance, Packages and Interfaces • Exception handling • Multithreading • Input/output • Introducing GUI Programming with JavaFX • Exploring JavaFX Controls 3/14/2024 CSE 2288 Department of CSE 2 Course Plan
Course objectives • To identify the difference between Procedure oriented programming and Object oriented programming. • To apply object oriented concepts like Classes, Objects, abstraction, data hiding, encapsulation, inheritance and polymorphism. • To create and use packages to demonstrate reusability. • To design GUI based applications using JavaFX. Course Syllabus
Jan 9, 2018 4 Text Books 1. Java Fundamentals-A Comprehensive Introduction – Herbert Schildt and Dale Skrien, Revised 1st edition, Tata McGraw Hill 2. The Complete Reference Java- Herbert Schildt, 9th Edition, Tata McGraw-Hill
Faculty member Details Name : Dr. MAMATHA BALACHANDRA Designation : Associate Professor Department : CSE Cabin No. : FC - 15 Location : AB-5, I floor Email-Id : mamtha.bc@manipal.edu Available timing : Thursday and Friday 3-5PM 3/14/2024 CSE 2288 Department of CSE 5
Jan 9, 2018 6 INTRODUCTION TO JAVA
August 6, 2009 7  Java is related to C++, which is a direct descendent of C  From C, Java derives its syntax  Many of Java’s object-oriented features were influenced by C++  Java was designed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991
• The primary motivation was the need for a platform-independent (that is, architecture- neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. • This effort ultimately led to the creation of Java. • About the time that the details of Java were being worked out, a second, and ultimately more important, factor was emerging that would play a crucial role in the future of Java. • This second force was the World Wide Web.
• It became obvious to members of the Java design team that the problems of portability frequently encountered when creating code for embedded controllers are also found when attempting to create code for the Internet. • The same problem that Java was initially designed to solve on a small scale could also be applied to the Internet on a large scale. This realization caused the focus of Java to switch from consumer electronics to Internet programming. So, while the desire for an architecture-neutral programming language provided the initial spark, the Internet ultimately led to Java’s large-scale success.
August 6, 2009 10  This language was initially called “Oak” but was renamed “Java” in 1995  Java can be used to create two types of programs: applications and applets  An application is a program that runs on your computer, under the operating system of that computer  An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser
August 6, 2009 11  An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip  An applet is a program that can react to user input and dynamically change—not just run the same animation or sound over and over
August 6, 2009 12 Security  When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection or malicious intent  Java achieves this protection by confining a Java program to the Java execution environment and not allowing access to other parts of the computer environment  Java is portable across many types of computers and operating systems that are in use throughout the world
August 6, 2009 13 Java’s Magic: The Bytecode  The output of a Java compiler is not executable code ; rather, it is bytecode  Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM)
August 6, 2009 14
August 6, 2009 15
August 6, 2009 16
August 6, 2009 17
August 6, 2009 18
August 6, 2009 19 JVM is an interpreter for bytecode
August 6, 2009 20  Translating a Java program into bytecode helps makes it much easier to run a program in a wide variety of environments  The reason is straightforward: only the JVM needs to be implemented for each platform  When a program is interpreted, it generally runs substantially slower than it would run if compiled to executable code  The use of bytecode enables the Java run-time system to execute programs much faster than you might expect
August 6, 2009 21  Sun supplies its Just In Time (JIT) compiler for bytecode, which is included in the Java 2 release  When the JIT compiler is part of the JVM, it compiles bytecode into executable code in real time, on a piece-by-piece, demand basis  The JIT compiler compiles code as it is needed, during execution
August 6, 2009 22 The Java Buzzwords  Simple  Secure  Portable  Object-oriented  Robust  Multithreaded  Architecture-neutral  Interpreted and High performance  Distributed  Dynamic These explain the design goals and accomplishments!
August 6, 2009 23 Simple  If you already understand the basic concepts of object-oriented programming, learning Java will be even easier  Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers have little trouble learning Java  Beyond its similarities with C/C++, Java has another attribute that makes it easy to learn: it makes an effort not to have surprising features
August 6, 2009 24 Object oriented  Java was not designed to be source-code compatible with any other language  The object model in Java is simple and easy to extend, while simple types, such as integers, are kept as high-performance non-objects
August 6, 2009 25 Robust  Ability to create robust programs was given a high priority in the design of Java  To better understand how Java is robust, consider two of the main reasons for program failure: memory management mistakes and mishandled exceptional conditions (that is, run-time errors)
August 6, 2009 26  Memory management can be a difficult, tedious task in traditional programming environments  For example, in C/C++, the programmer must manually allocate and free all dynamic memory  Programmers will either forget to free memory that has been previously allocated or, worse, try to free some memory that another part of their code is still using
August 6, 2009 27  Java virtually eliminates these problems by managing memory allocation and deallocation for you  No Pointers in JAVA  Java provides object-oriented exception handling
August 6, 2009 28 Multithreaded  Java supports multithreaded programming, which allows you to write programs that do many things simultaneously  The Java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enables you to construct smoothly running interactive systems
August 6, 2009 29 Architecture-Neutral  In many programming languages Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction  Java designers made several hard decisions in Java language and Java Virtual Machine in an attempt to alter this situation  Their goal was “write once; run anywhere, any time, forever.” ( W O R A )
August 6, 2009 30 Interpreted and High Performance  Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode  This code can be interpreted on any system that provides a Java Virtual Machine  the Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time (JIT) compiler
August 6, 2009 31 Distributed  Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols  Remote Method Invocation (RMI) feature enables the programmer to invoke methods across the network.
August 6, 2009 32 Dynamic  Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time.  Makes it possible to dynamically link code in a safe and expedient manner.  Small fragments of bytecode may be dynamically updated on a running system
August 6, 2009 33 An Overview of Java
August 6, 2009 34  Object-oriented programming is at the core of Java  all computer programs consist of two elements: code and data  A program can be conceptually organized around its code or around its data
August 6, 2009 35  That is, some programs are written around “what is happening” and others are written around “who is being affected.”  The first way is called the process-oriented model  The process-oriented model can be thought of as code acting on data
August 6, 2009 36  The second approach, Object-oriented programming organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data  Characterized as data controlling access to the code
August 6, 2009 37  Concept of Abstraction – focus is on essential features.  Humans manage complexity through abstraction which is through the use of hierarchical classifications.  Data can be transformed into component objects. A sequence of process steps can become a collection of messages between these objects - essence of OOP
August 6, 2009 38 The Three OOP Principles:  Encapsulation - is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse – class, member variables and methods  Inheritance - the process by which one object acquires the properties of another object
August 6, 2009 39
August 6, 2009 40
August 6, 2009 41  Polymorphism - is a feature that allows one interface to be used for a general class of actions – “ one interface , multiple methods”  Polymorphism, encapsulation and inheritance works together - every java program involves these.
The Key Attributes of Object Oriented Programming  Encapsulation - is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse  Polymorphism - is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.  Inheritance - the process by which one object acquires the properties of another object
August 6, 2009 43 A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } }
August 6, 2009 44 Compiling the Program  C:>javac Example.java  The javac compiler creates a file called Example.class that contains the bytecode version of the program • The output of javac is not code that can be directly executed
August 6, 2009 45 • To actually run the program, you must use the Java interpreter, called java. • C:>java Example • When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared
August 6, 2009 46 • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class • The keyword void simply tells the compiler that main( ) does not return a value • String[ ] args declares a parameter named args, which is an array of instances of the class String. args receives any number of command-line arguments.
August 6, 2009 47 A Second Short Program class Example2 { public static void main(String args[]) { int num; // this declares a variable called num num = 100; // this assigns num the value 100 System.out.println("This is num: " + num); num = num * 2; System.out.print("The value of num * 2 is "); System.out.println(num); } }
August 6, 2009 48 Two Control Statements The if Statement: if(condition) statement; if(num < 100) System.out.println("num is less than 100");
August 6, 2009 49 The for Loop: for(initialization; condition; iteration) statement; class ForTest { public static void main(String args[]) { int x; for(x = 0; x<10; x = x+1) System.out.println("This is x: " + x); } }
August 6, 2009 50 Using Blocks of Code: using { and }
August 6, 2009 51 Lexical Issues • Whitespace: – Java is a free-form language – In Java, whitespace is a space, tab, or newline • Identifiers: - Identifiers are used for class names, method names, and variable names - Java is case-sensitive
August 6, 2009 52
August 6, 2009 53 Literals: • A constant value in Java is created by using a literal representation of it
August 6, 2009 54 Comments • There are three types of comments defined by Java. • Single-line (// )and multiline (/* */ ) • The third type is called a documentation comment • This type of comment is used to produce an HTML file that documents your program • The documentation comment begins with a /** and ends with a */
August 6, 2009 55 Separators
August 6, 2009 56 The Java Keywords • There are 50 reserved keywords currently defined in the Java language • Can not be used as names for a variable, class, or method. • const and goto are reserved but not used • Also reserves true, false and null – values defined by Java.
August 6, 2009 57
August 6, 2009 58 Scanner Class in Java • Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. • To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. • System.in is InputStream which is typically connected to keyboard input of console program.
// Program to read the details of a student and display the same on the screen import java.util.Scanner; class ScannerTest{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter your rollno"); int rollno=sc.nextInt(); System.out.println("Enter your name"); String name=sc.next(); System.out.println("Enter your fee"); double fee=sc.nextDouble(); System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee); } }
// Program to compute the sume of digits of an inputted number import java.util.Scanner; class ScannerTest{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter any number"); int n=sc.nextInt(); System.out.println(“Sum of digits is"); while (n>0) { int r=n%10; sum+=r; n=n/10; } System.out.print(sum); } }

1.INTRODUCTION TO JAVA_2022 MB.ppt .

  • 1.
  • 2.
    CSE4308: PROGRAMMING INJAVA Course modules • Java Programming Fundamentals • Introducing Data types and Operators • Program Control statements • Classes and Methods • Inheritance, Packages and Interfaces • Exception handling • Multithreading • Input/output • Introducing GUI Programming with JavaFX • Exploring JavaFX Controls 3/14/2024 CSE 2288 Department of CSE 2 Course Plan
  • 3.
    Course objectives • Toidentify the difference between Procedure oriented programming and Object oriented programming. • To apply object oriented concepts like Classes, Objects, abstraction, data hiding, encapsulation, inheritance and polymorphism. • To create and use packages to demonstrate reusability. • To design GUI based applications using JavaFX. Course Syllabus
  • 4.
    Jan 9, 20184 Text Books 1. Java Fundamentals-A Comprehensive Introduction – Herbert Schildt and Dale Skrien, Revised 1st edition, Tata McGraw Hill 2. The Complete Reference Java- Herbert Schildt, 9th Edition, Tata McGraw-Hill
  • 5.
    Faculty member Details Name: Dr. MAMATHA BALACHANDRA Designation : Associate Professor Department : CSE Cabin No. : FC - 15 Location : AB-5, I floor Email-Id : mamtha.bc@manipal.edu Available timing : Thursday and Friday 3-5PM 3/14/2024 CSE 2288 Department of CSE 5
  • 6.
    Jan 9, 20186 INTRODUCTION TO JAVA
  • 7.
    August 6, 20097  Java is related to C++, which is a direct descendent of C  From C, Java derives its syntax  Many of Java’s object-oriented features were influenced by C++  Java was designed by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991
  • 8.
    • The primarymotivation was the need for a platform-independent (that is, architecture- neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls. • This effort ultimately led to the creation of Java. • About the time that the details of Java were being worked out, a second, and ultimately more important, factor was emerging that would play a crucial role in the future of Java. • This second force was the World Wide Web.
  • 9.
    • It becameobvious to members of the Java design team that the problems of portability frequently encountered when creating code for embedded controllers are also found when attempting to create code for the Internet. • The same problem that Java was initially designed to solve on a small scale could also be applied to the Internet on a large scale. This realization caused the focus of Java to switch from consumer electronics to Internet programming. So, while the desire for an architecture-neutral programming language provided the initial spark, the Internet ultimately led to Java’s large-scale success.
  • 10.
    August 6, 200910  This language was initially called “Oak” but was renamed “Java” in 1995  Java can be used to create two types of programs: applications and applets  An application is a program that runs on your computer, under the operating system of that computer  An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser
  • 11.
    August 6, 200911  An applet is actually a tiny Java program, dynamically downloaded across the network, just like an image, sound file, or video clip  An applet is a program that can react to user input and dynamically change—not just run the same animation or sound over and over
  • 12.
    August 6, 200912 Security  When you use a Java-compatible Web browser, you can safely download Java applets without fear of viral infection or malicious intent  Java achieves this protection by confining a Java program to the Java execution environment and not allowing access to other parts of the computer environment  Java is portable across many types of computers and operating systems that are in use throughout the world
  • 13.
    August 6, 200913 Java’s Magic: The Bytecode  The output of a Java compiler is not executable code ; rather, it is bytecode  Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM)
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    August 6, 200919 JVM is an interpreter for bytecode
  • 20.
    August 6, 200920  Translating a Java program into bytecode helps makes it much easier to run a program in a wide variety of environments  The reason is straightforward: only the JVM needs to be implemented for each platform  When a program is interpreted, it generally runs substantially slower than it would run if compiled to executable code  The use of bytecode enables the Java run-time system to execute programs much faster than you might expect
  • 21.
    August 6, 200921  Sun supplies its Just In Time (JIT) compiler for bytecode, which is included in the Java 2 release  When the JIT compiler is part of the JVM, it compiles bytecode into executable code in real time, on a piece-by-piece, demand basis  The JIT compiler compiles code as it is needed, during execution
  • 22.
    August 6, 200922 The Java Buzzwords  Simple  Secure  Portable  Object-oriented  Robust  Multithreaded  Architecture-neutral  Interpreted and High performance  Distributed  Dynamic These explain the design goals and accomplishments!
  • 23.
    August 6, 200923 Simple  If you already understand the basic concepts of object-oriented programming, learning Java will be even easier  Because Java inherits the C/C++ syntax and many of the object-oriented features of C++, most programmers have little trouble learning Java  Beyond its similarities with C/C++, Java has another attribute that makes it easy to learn: it makes an effort not to have surprising features
  • 24.
    August 6, 200924 Object oriented  Java was not designed to be source-code compatible with any other language  The object model in Java is simple and easy to extend, while simple types, such as integers, are kept as high-performance non-objects
  • 25.
    August 6, 200925 Robust  Ability to create robust programs was given a high priority in the design of Java  To better understand how Java is robust, consider two of the main reasons for program failure: memory management mistakes and mishandled exceptional conditions (that is, run-time errors)
  • 26.
    August 6, 200926  Memory management can be a difficult, tedious task in traditional programming environments  For example, in C/C++, the programmer must manually allocate and free all dynamic memory  Programmers will either forget to free memory that has been previously allocated or, worse, try to free some memory that another part of their code is still using
  • 27.
    August 6, 200927  Java virtually eliminates these problems by managing memory allocation and deallocation for you  No Pointers in JAVA  Java provides object-oriented exception handling
  • 28.
    August 6, 200928 Multithreaded  Java supports multithreaded programming, which allows you to write programs that do many things simultaneously  The Java run-time system comes with an elegant yet sophisticated solution for multiprocess synchronization that enables you to construct smoothly running interactive systems
  • 29.
    August 6, 200929 Architecture-Neutral  In many programming languages Operating system upgrades, processor upgrades, and changes in core system resources can all combine to make a program malfunction  Java designers made several hard decisions in Java language and Java Virtual Machine in an attempt to alter this situation  Their goal was “write once; run anywhere, any time, forever.” ( W O R A )
  • 30.
    August 6, 200930 Interpreted and High Performance  Java enables the creation of cross-platform programs by compiling into an intermediate representation called Java bytecode  This code can be interpreted on any system that provides a Java Virtual Machine  the Java bytecode was carefully designed so that it would be easy to translate directly into native machine code for very high performance by using a just-in-time (JIT) compiler
  • 31.
    August 6, 200931 Distributed  Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols  Remote Method Invocation (RMI) feature enables the programmer to invoke methods across the network.
  • 32.
    August 6, 200932 Dynamic  Java programs carry with them substantial amounts of run-time type information that is used to verify and resolve accesses to objects at run time.  Makes it possible to dynamically link code in a safe and expedient manner.  Small fragments of bytecode may be dynamically updated on a running system
  • 33.
    August 6, 200933 An Overview of Java
  • 34.
    August 6, 200934  Object-oriented programming is at the core of Java  all computer programs consist of two elements: code and data  A program can be conceptually organized around its code or around its data
  • 35.
    August 6, 200935  That is, some programs are written around “what is happening” and others are written around “who is being affected.”  The first way is called the process-oriented model  The process-oriented model can be thought of as code acting on data
  • 36.
    August 6, 200936  The second approach, Object-oriented programming organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data  Characterized as data controlling access to the code
  • 37.
    August 6, 200937  Concept of Abstraction – focus is on essential features.  Humans manage complexity through abstraction which is through the use of hierarchical classifications.  Data can be transformed into component objects. A sequence of process steps can become a collection of messages between these objects - essence of OOP
  • 38.
    August 6, 200938 The Three OOP Principles:  Encapsulation - is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse – class, member variables and methods  Inheritance - the process by which one object acquires the properties of another object
  • 39.
  • 40.
  • 41.
    August 6, 200941  Polymorphism - is a feature that allows one interface to be used for a general class of actions – “ one interface , multiple methods”  Polymorphism, encapsulation and inheritance works together - every java program involves these.
  • 42.
    The Key Attributesof Object Oriented Programming  Encapsulation - is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse  Polymorphism - is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the situation.  Inheritance - the process by which one object acquires the properties of another object
  • 43.
    August 6, 200943 A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Java program."); } }
  • 44.
    August 6, 200944 Compiling the Program  C:>javac Example.java  The javac compiler creates a file called Example.class that contains the bytecode version of the program • The output of javac is not code that can be directly executed
  • 45.
    August 6, 200945 • To actually run the program, you must use the Java interpreter, called java. • C:>java Example • When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared
  • 46.
    August 6, 200946 • The keyword static allows main( ) to be called without having to instantiate a particular instance of the class • The keyword void simply tells the compiler that main( ) does not return a value • String[ ] args declares a parameter named args, which is an array of instances of the class String. args receives any number of command-line arguments.
  • 47.
    August 6, 200947 A Second Short Program class Example2 { public static void main(String args[]) { int num; // this declares a variable called num num = 100; // this assigns num the value 100 System.out.println("This is num: " + num); num = num * 2; System.out.print("The value of num * 2 is "); System.out.println(num); } }
  • 48.
    August 6, 200948 Two Control Statements The if Statement: if(condition) statement; if(num < 100) System.out.println("num is less than 100");
  • 49.
    August 6, 200949 The for Loop: for(initialization; condition; iteration) statement; class ForTest { public static void main(String args[]) { int x; for(x = 0; x<10; x = x+1) System.out.println("This is x: " + x); } }
  • 50.
    August 6, 200950 Using Blocks of Code: using { and }
  • 51.
    August 6, 200951 Lexical Issues • Whitespace: – Java is a free-form language – In Java, whitespace is a space, tab, or newline • Identifiers: - Identifiers are used for class names, method names, and variable names - Java is case-sensitive
  • 52.
  • 53.
    August 6, 200953 Literals: • A constant value in Java is created by using a literal representation of it
  • 54.
    August 6, 200954 Comments • There are three types of comments defined by Java. • Single-line (// )and multiline (/* */ ) • The third type is called a documentation comment • This type of comment is used to produce an HTML file that documents your program • The documentation comment begins with a /** and ends with a */
  • 55.
    August 6, 200955 Separators
  • 56.
    August 6, 200956 The Java Keywords • There are 50 reserved keywords currently defined in the Java language • Can not be used as names for a variable, class, or method. • const and goto are reserved but not used • Also reserves true, false and null – values defined by Java.
  • 57.
  • 58.
    August 6, 200958 Scanner Class in Java • Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. • To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. • System.in is InputStream which is typically connected to keyboard input of console program.
  • 59.
    // Program toread the details of a student and display the same on the screen import java.util.Scanner; class ScannerTest{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter your rollno"); int rollno=sc.nextInt(); System.out.println("Enter your name"); String name=sc.next(); System.out.println("Enter your fee"); double fee=sc.nextDouble(); System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee); } }
  • 60.
    // Program tocompute the sume of digits of an inputted number import java.util.Scanner; class ScannerTest{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter any number"); int n=sc.nextInt(); System.out.println(“Sum of digits is"); while (n>0) { int r=n%10; sum+=r; n=n/10; } System.out.print(sum); } }