Java Programming
--------------------------
02/06/2021
--------------
1. Introduction
2. Setting Java working environment
3. First Java Program
4. Variables and Methods in Java
5. Getting Data from keyboard
6. Arrays and ArrayList
7. Control Statements
------------------------------
1. Introduction to Java Programming
----------------------------------------------
-> Java is a high level programming language
-> Java is pure object oriented programming language
-> Java was created by Sun Microsystem in 1995
-> Java is good in developing small and large scale applications
-> Java uses Application Programming Interface(API) to simplify the work of
programmers
-> Java support multithreading
-> Java organizes related classes in a package
-> Java has three big components:
=>Java2SE: Java 2 Standard Edition used to develop small application
=>Java2EE: Java 2 Enterprise Edition used to deevelop large scale
applications
=>Java2ME: Java 2 Micro-Edition used to develop Applications for mobile and
hand held devices
-> Java is excellent in developing Graphical User Interface(GUI) using Java Swing
-> Java is excellent in manipulating database using Java Database
Connectivity(JDBC) API
-> Java is excellent in developing web applications using Java Servlet and Java
Server Page(JSP)
-> Java simplifies Software development process by using Frameworks, java has many
Frameworks but the most popular is Spring
-> Java Frameworks use Model View Controller (MVC) components which separates
presentation from business logic of software.
-> Java is excellent in developing secured applications and data protection
-> Java Server Faces(JSF) is a framework which is used to develop faster and
dynamic web applications=> JSF is part of this course
---------------------------------------------
2. Setting Java working environment
-----------------------------------------------
-> To run Java programs we first install:
=> Java Development Kit(JDK for this course JDK 1.8 is used)
=> Integrated Development Environment(IDE, for this course JCreator and Netbeans
are used but Eclipse also is good)
-> We need to configure environment variables:
=> CLASSPATH=C:\Program Files\Java\jdk1.8.0_162\bin
=> PATH=.;C:\Program Files\Java\jdk1.8.0_162\bin
-> JDK has compiler and Interpreter (JVM)
-> JVM means is Java Virtual Machine, a Java component which interpretes compiled
java classes (Bytecode)
-----------------------------------------------
3. First Java Program
------------------------------------
class FirstProgram
{
public static void main(String args[])
,{
System.out.println("Hello CS2 Musanze Students");
}
}
------------------------------------
4. Variables and Methods in Java
----------------------------
There 2 types of variables: Local Variable and Global Variable
-> Local Variable: is declared inside a method
-> Global Variable: is declared before any method, it is available in the whole
program
=> Variables are declared using data types, their names start with letter or
underscore and do not contain free space
---------------------
Example:
----------
class MyVariables
{
static int c;//global is c
public static void main(String args[])
{
int a=10;//a and b are local variable
int b=20;
c=a+b;
System.out.println("Sum="+c);
}
}
-----------------------------
Methods in Java:
----------------------
There are 2 types of Methods: Method with Arguments and Method without Arguments.
-> Method without Arguments:
---------------------------
Format:
---------
DataType MethodName()
{
Statements;
}
-----------------
Method with Arguments
--------------------------
Format:
-------
DataType MethodName(DataType Arg1, DataType Arg2,..., DataType ArgN)
{
Statements;
}
---------------------
Example:
------------
class MyMethods
{
static String name;
static float c;
public static float product(float a,float b)
{
c=a*b;
return c;
, }
public static void Display()
{
System.out.println("Product="+c);
name="Silas";
}
public static void main(String args[])
{
product(12.5,6.4);
System.out.println("Hello "+name);
Display();
}
}
-----------------------------
5. Getting Data from keyboard
-------------------------------------
There 4 ways of getting input from user:
-> Using Scanner class
-> Using DataInputStream class
-> Using BufferedReader class
-> Using JOptionPane class
------------------------
5.1 Getting Data using Scanner class
------------------------------------------
-> Scanner class is found in java.util package
-> Scanner class has useful methods such as:
=> next() used to get text data
=> nextInt() used to get integer numbers
=> nextFloat() used get float numbers
=> nextDouble() used to get double numbers
---------------------------------
Example:
--------------
import java.util.*;
class MyInput
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int a,c;
System.out.println("Type a Number:");
a=s.nextInt();//
c=a*a;
System.out.println("Square="+c)
}
}
------------------------------------
02/04/2019
--------------
5.2 Getting Data using DataInputStream
-----------------------------------
-> DataInputStream class is found in the package java.io
-> DataInputStream has a method called readLine() which accepts only string data,
if we need numbers we have convert by using parseInt(), parseFloat() or
parseDouble()
-> The use of java.io generates exception that can be handled by using throws
IOException or try and catch
------------------------------------------------------
Example:
--------------------------
02/06/2021
--------------
1. Introduction
2. Setting Java working environment
3. First Java Program
4. Variables and Methods in Java
5. Getting Data from keyboard
6. Arrays and ArrayList
7. Control Statements
------------------------------
1. Introduction to Java Programming
----------------------------------------------
-> Java is a high level programming language
-> Java is pure object oriented programming language
-> Java was created by Sun Microsystem in 1995
-> Java is good in developing small and large scale applications
-> Java uses Application Programming Interface(API) to simplify the work of
programmers
-> Java support multithreading
-> Java organizes related classes in a package
-> Java has three big components:
=>Java2SE: Java 2 Standard Edition used to develop small application
=>Java2EE: Java 2 Enterprise Edition used to deevelop large scale
applications
=>Java2ME: Java 2 Micro-Edition used to develop Applications for mobile and
hand held devices
-> Java is excellent in developing Graphical User Interface(GUI) using Java Swing
-> Java is excellent in manipulating database using Java Database
Connectivity(JDBC) API
-> Java is excellent in developing web applications using Java Servlet and Java
Server Page(JSP)
-> Java simplifies Software development process by using Frameworks, java has many
Frameworks but the most popular is Spring
-> Java Frameworks use Model View Controller (MVC) components which separates
presentation from business logic of software.
-> Java is excellent in developing secured applications and data protection
-> Java Server Faces(JSF) is a framework which is used to develop faster and
dynamic web applications=> JSF is part of this course
---------------------------------------------
2. Setting Java working environment
-----------------------------------------------
-> To run Java programs we first install:
=> Java Development Kit(JDK for this course JDK 1.8 is used)
=> Integrated Development Environment(IDE, for this course JCreator and Netbeans
are used but Eclipse also is good)
-> We need to configure environment variables:
=> CLASSPATH=C:\Program Files\Java\jdk1.8.0_162\bin
=> PATH=.;C:\Program Files\Java\jdk1.8.0_162\bin
-> JDK has compiler and Interpreter (JVM)
-> JVM means is Java Virtual Machine, a Java component which interpretes compiled
java classes (Bytecode)
-----------------------------------------------
3. First Java Program
------------------------------------
class FirstProgram
{
public static void main(String args[])
,{
System.out.println("Hello CS2 Musanze Students");
}
}
------------------------------------
4. Variables and Methods in Java
----------------------------
There 2 types of variables: Local Variable and Global Variable
-> Local Variable: is declared inside a method
-> Global Variable: is declared before any method, it is available in the whole
program
=> Variables are declared using data types, their names start with letter or
underscore and do not contain free space
---------------------
Example:
----------
class MyVariables
{
static int c;//global is c
public static void main(String args[])
{
int a=10;//a and b are local variable
int b=20;
c=a+b;
System.out.println("Sum="+c);
}
}
-----------------------------
Methods in Java:
----------------------
There are 2 types of Methods: Method with Arguments and Method without Arguments.
-> Method without Arguments:
---------------------------
Format:
---------
DataType MethodName()
{
Statements;
}
-----------------
Method with Arguments
--------------------------
Format:
-------
DataType MethodName(DataType Arg1, DataType Arg2,..., DataType ArgN)
{
Statements;
}
---------------------
Example:
------------
class MyMethods
{
static String name;
static float c;
public static float product(float a,float b)
{
c=a*b;
return c;
, }
public static void Display()
{
System.out.println("Product="+c);
name="Silas";
}
public static void main(String args[])
{
product(12.5,6.4);
System.out.println("Hello "+name);
Display();
}
}
-----------------------------
5. Getting Data from keyboard
-------------------------------------
There 4 ways of getting input from user:
-> Using Scanner class
-> Using DataInputStream class
-> Using BufferedReader class
-> Using JOptionPane class
------------------------
5.1 Getting Data using Scanner class
------------------------------------------
-> Scanner class is found in java.util package
-> Scanner class has useful methods such as:
=> next() used to get text data
=> nextInt() used to get integer numbers
=> nextFloat() used get float numbers
=> nextDouble() used to get double numbers
---------------------------------
Example:
--------------
import java.util.*;
class MyInput
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int a,c;
System.out.println("Type a Number:");
a=s.nextInt();//
c=a*a;
System.out.println("Square="+c)
}
}
------------------------------------
02/04/2019
--------------
5.2 Getting Data using DataInputStream
-----------------------------------
-> DataInputStream class is found in the package java.io
-> DataInputStream has a method called readLine() which accepts only string data,
if we need numbers we have convert by using parseInt(), parseFloat() or
parseDouble()
-> The use of java.io generates exception that can be handled by using throws
IOException or try and catch
------------------------------------------------------
Example: