6. THE WAY JAVA WORKS
• The goal is to write one application (in this example an interactive party invitation) and
have it work on whatever device your friends have.
12. CODE
STRUCTURE
IN JAVA
public class Dog {
}
Class
public class Dog {
void bark(){
}
}
Method
public class Dog {
void bark () {
statement1;
statement2;
}
}
statements
14. WRITING A CLASS WITH A MAIN
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("TheWorld!");
}
}
15. PRIMITIVE DATA TYPES
Category Types Size (bits) Minimum Value Maximum Value Precision Example
Integer
byte 8 -128 127 From +127 to -128 byte b = 65;
char 16 0 216-1 All Unicode characters
char c = 'A';
char c = 65;
short 16 -215 215-1 From +32,767 to -32,768 short s = 65;
int 32 -231 231-1 From +2,147,483,647 to -2,147,483,648 int i = 65;
long 64 -263 263-1
From +9,223,372,036,854,775,807 to -
9,223,372,036,854,775,808 long l = 65L;
Floating-point
float 32 2-149 (2-2-23)·2127 From 3.402,823,5 E+38 to 1.4 E-45 float f = 65f;
double 64 2-1074 (2-2-52)·21023 From 1.797,693,134,862,315,7 E+308 to
4.9 E-324 double d = 65.55;
Other
boolean 1 -- -- false, true boolean b = true;
void -- -- -- -- --
16. STATEMENTS
int x = 3;
String name = "Dirk";
x = x * 17;
System.out.print("x is " + x);
double d = Math.random();
// this is a comment
17. CONDITION
if (x == 10) {
System.out.println("x must be 10");
} else {
System.out.println("x isn't 10");
}
if ((x < 3) && (name.equals("Dirk"))) {
System.out.println("Gently");
}
if ((x < 3) || (name.equals("Dirk"))) {
System.out.println("Gently");
}
18. LOOPING
while (x > 12) {
x = x - 1;
System.out.println("x is " + x);
}
for (x = 0; x < 10; x = x + 1) {
System.out.println("x is " + x);
}
19. METHODS
public class ExampleMinNumber{
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("MinimumValue=" + c);
}
/** returns the minimum of two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else min = n1;
return min;
}
}
23. EXERCISE 1
Write two methods:
a) First computes area of circle ( take
radius as parameter )
b) Second computes sum of area of circles
( array of double of radius )
28. THINKING
ABOUT
OBJECTS
When you design a class, think about the
objects that will be created from that
class type.Think about
• things the object knows
• things the object does
30. WHAT’S THE DIFFERENCE BETWEEN A
CLASS AND AN OBJECT?
• A class is not an object (but it’s used to construct them)
• A class is a blueprint for an object.
34. CONSTRUCTORS
Each time a new object is created, at least one
constructor will be invoked.
The main rule of constructors is that they should have
the same name as the class.A class can have more
than one constructor.
38. STATIC VARIABLES
• Static variables are shared.
• All instances of the same class share a
single copy of the static variables.
• Instance variables : 1 per instance
• Static variables : 1 per class
41. COMPONENTS OF THE JAVA
PROGRAMMING LANGUAGE
• The language itself is a collection of keywords and symbols that we put
together to express how we want our code to run.
Data types
Int, float, Boolean, char, String.
Variables
Container used to hold data.
Methods
section of code that we can call from elsewhere in our code, will perform some action or return some
kind of result that we can use.
42. COMPONENTS OF THE JAVA
PROGRAMMING LANGUAGE
Comments
lines of code that don’t have any effect on how the program runs.They are purely informational, meant to
help us understand how the code works or is organized.
Classes and Objects
A class is meant to define an object and how it works. Classes define things about objects as
properties, or member variables of the class.And abilities of the object are defined as methods.
Access Modifiers
For classes, member variables, and methods, we usually want to specify who can access them.
Keywords public, private, and protected are access modifiers that control who can access the
class, variable, or method.
44. A N OT H E R I M P O RTA N T
J AVA C O N C E P T S
YO U ’ L L RU N I N TO A
L OT:
INHERITANCE
inheritance means that Java classes or objects can be
organized into hierarchies with lower, more specific,
classes in the hierarchy inheriting behavior and traits from
higher, more generic, classes.
Super Class
Sub Class
45. EXTENDS KEYWORD
• Extends is the keyword used to inherit the properties of a class. Below given is the syntax of
extends keyword.
class Super {
.....
}
class Sub extends Super{
....
}
50. A N OT H E R I M P O RTA N T
J AVA C O N C E P T S YO U ’ L L
RU N I N TO A L OT:
INTERFACES
An interface is a reference type in Java, it is
similar to class, it is a collection of abstract
methods.
A class implements an interface, thereby
inheriting the abstract methods of the
interface.
51. DECLARING INTERFACE
/* File name : NameOfInterface.java */
import java.lang.*;
//Any number of import statements
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations
}
54. A N OT H E R I M P O RTA N T
J AVA C O N C E P T S YO U ’ L L
RU N I N TO A L OT:
POLYMORPHISM
Polymorphism is the ability of an object to take on many
forms.The most common use of polymorphism in OOP
occurs when a parent class reference is used to refer to a
child class object.
Quack!
Animal Animal
Woof!
Animal
Meow!
Speak()
55. POLYMORPHISM EXAMPLE
public interfaceVegetarian{}
public class Animal{}
public class Deer extends Animal implementsVegetarian{}
• Following are true for the above example:
– A Deer IS-A Animal
– A Deer IS-AVegetarian
– A Deer IS-A Deer
– A Deer IS-A Object
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
56. A N OT H E R I M P O RTA N T
J AVA C O N C E P T S YO U ’ L L
RU N I N TO A L OT:
OVERRIDING
The benefit of overriding is: ability to define a behavior
that's specific to the subclass type which means a subclass
can implement a parent class method based on its
requirement.
Quack!
Animal Animal
Woof!
Animal
Meow!
Speak()
62. JAVA GRAPHICS APIS
• Java Graphics APIs - AWT and Swing - provide a huge set of reusable GUI components, such as
button, text field, label, choice, panel and frame for building GUI applications.You can simply
reuse these classes rather than re-invent the wheels. I shall start with the AWT classes before
moving into Swing to give you a complete picture. I have to stress that AWT component
classes are now obsoleted by Swing's counterparts.
66. MORE ABOUT GUI
• GUI Programming
https://www.ntu.edu.sg/home/ehchua/programming/java/J4a_GUI.html
• NetbeansTutorial
https://netbeans.org/kb/docs/java/gui-functionality.html
• Main documentation:
https://docs.oracle.com/javase/tutorial/uiswing/
70. JAVA AWT EVENT HANDLING
• Java provides a rich set of libraries to create Graphical User Interface in platform independent
way. In this session we'll look in AWT (AbstractWindowToolkit), especially Event Handling.
• Event is a change in the state of an object. Events are generated as result of user interaction
with the graphical user interface components. For example, clicking on a button, moving the
mouse, entering a character through keyboard, selecting an item from list, scrolling the page are
the activities that causes an event to happen.
• Event Handling is the mechanism that controls the event and decides what should happen if an
event occurs.This mechanism have the code which is known as event handler that is
executed when an event occurs. Java Uses the Delegation Event Model to handle the
events.This model defines the standard mechanism to generate and handle the events.
71. T H E
DELEGATION
EVENT
MODEL
H A S T H E F O L L OW I N G
K E Y PA RT I C I PA N T S
N A M E LY
• Source - The source is an object on which event occurs.
Source is responsible for providing information of the
occurred event to it's handler.
• Listener - It is also known as event handler. Listener is
responsible for generating response to an event. Listener
waits until it receives an event. Once the event is received ,
the listener process the event an then returns.
72. T H E
EVENTS
C A N B E B ROA D LY
C L A S S I F I E D I N TO T W O
C AT E G O R I E S :
• Foreground Events -Those events which require the
direct interaction of user.They are generated as
consequences of a person interacting with the graphical
components in Graphical User Interface. For example, clicking
on a button, moving the mouse, entering a character through
keyboard, selecting an item from list, scrolling the page etc.
• Background Events - Those events that require the
interaction of end user are known as background events.
Operating system interrupts, hardware or software failure,
timer expires, an operation completion are the example of
background events.
78. MULTITHREADING
• A multi-threaded program contains
two or more parts that can run
concurrently and each part can handle
different task at the same time making
optimal use of the available resources
specially when your computer has
multiple CPUs.
80. CREATE THREAD BY IMPLEMENTING
RUNNABLE INTERFACE
• Step 1 you need to implement a run() method provided by Runnable interface.
• Step 2 you will instantiate a Thread object using the following constructor:
• Step 3 OnceThread object is created, you can start it by calling start( ) method, which
executes a call to run( ) method.
public void run( )
Thread(Runnable threadObj, String threadName);
void start( );
84. JAVA NETWORKING
• The term network programming refers
to writing programs that execute across
multiple devices (computers), in which the
devices are all connected to each other
using a network.
85. T H E J AVA . N E T
PA C K A G E P ROV I D E S
S U P P O RT F O R T H E
T W O C O M M O N
NETWORK
PROTOCOLS
• TCP:Transmission Control Protocol, which allows for
reliable communication between two applications.TCP is
typically used over the Internet Protocol, which is referred to
asTCP/IP.
• UDP: User Datagram Protocol, a connection-less protocol
that allows for packets of data to be transmitted between
applications.
86. SOCKET PROGRAMMING
• Sockets provide the communication mechanism between two computers usingTCP.A client program
creates a socket on its end of the communication and attempts to connect that socket to a server.
90. SIMPLE TASK
• Design a class named Person and its two subclasses named Student and Employee.
• Make Faculty member and Staff subclasses of Employee.
• A person has a name, address, phone number, and email address.
• A student has a class status (freshman, sophomore, junior, or senior).
• Define the status as a constant.
• An employee has an office, salary, and date hired.
• Define a class named MyDate that contains the fields year, month, and day.
• A faculty member has office hours and a rank.
• A staff member has a title.
• Override the toString method in each class to display the class name and the person’s name.
91. SIMPLE TASK
• Deliverables:
– Optional: Draw the UML diagram for the classes.
– Implement the classes.
– Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.
– Build GUI to Add Employee andView all Employees
– Bonus: Save and load employees data to/from File