JAVA Tutorial(Oni Blessing)
-
Upload
blessing-oni -
Category
Documents
-
view
230 -
download
0
Transcript of JAVA Tutorial(Oni Blessing)
-
8/8/2019 JAVA Tutorial(Oni Blessing)
1/17
JAVA TUTORIAL
By
Oni Blessing
-
8/8/2019 JAVA Tutorial(Oni Blessing)
2/17
What is JAVA?
JAVA is an object oriented language that
interprets using the JAVA virtual machine to
implement programs on the computer.
-
8/8/2019 JAVA Tutorial(Oni Blessing)
3/17
Basics Of JAVA Programing
JAVA Fundamentals
Classes an objects
Method inheritance
-
8/8/2019 JAVA Tutorial(Oni Blessing)
4/17
-
8/8/2019 JAVA Tutorial(Oni Blessing)
5/17
Classes and Object
Object:This are physical things suchascar,house,computer etc. but in JAVA objects
have (attribute and behavior)also called field
and method respectively.
Field: this is what make up an OBJECT
Method: this is the function ofan object
Class:a class describes an object. We can have
many classes that defines an object.
-
8/8/2019 JAVA Tutorial(Oni Blessing)
6/17
Method
As it is in the previous slide METHOD is also thebehavior ofan OBJECT. The function an objectperform.
They are subroutines used to describe an object,they are represented by name and they can becalled atany point in the program.
e.g. public static main void(String [] args).It simply means the behaviors ofan objectarerepresented as methods in a class.
-
8/8/2019 JAVA Tutorial(Oni Blessing)
7/17
TypesofMethod
Built-in: this are methods thatare part ofthe
compiler package e.g. System.out.println()
Userdefined:this are methods created by theprogrammer e.g. public int getDate()(in the
workshop below)
-
8/8/2019 JAVA Tutorial(Oni Blessing)
8/17
A Simple JAVA Program
This is a simple program to display HELLO
WORLD
1.Write the source code.
2.Compile the source code.
3.Run the program.
-
8/8/2019 JAVA Tutorial(Oni Blessing)
9/17
Sourcecodecan be written withany text editor e.g.Notepad. But it must be saved as a java file
(.Java)for example (helloworld.java)
Compiling:compiling will be done in the commandprompt using this syntax (javac space the javafile)e.g.(javachelloworld.java)
Running:this is done in the command prompt usingthis syntax (java space the file name without(.java)e.g.(java helloworld).this will only run iftheprogram was error free during compilation
Note: to run and compiling java programs you mustbe in the directory ofthe java document forexample(C:\javafiles)
-
8/8/2019 JAVA Tutorial(Oni Blessing)
10/17
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("HelloWorld!");
}
}
-
8/8/2019 JAVA Tutorial(Oni Blessing)
11/17
public class program-name
{
optional-variable-declarations-and-subroutines
public static void main(String[] args)
{
statements
}
optional-variable-declarations-and-subroutines
}
-
8/8/2019 JAVA Tutorial(Oni Blessing)
12/17
Workshop
How many fields does the Date class have? How
many methods?
-
8/8/2019 JAVA Tutorial(Oni Blessing)
13/17
Programpublicclass Date
{
publicintday,month,year;
publicintgetDay()
{
System.out.println(InsidegetDaymethod);
returnday;
}
public voidprintDate()
{
System.out.println(InsideprintDatemethod);
System.out.println(month+ / + day + / + year);
}
}
-
8/8/2019 JAVA Tutorial(Oni Blessing)
14/17
Anwser
The Date class has 3 fields and 3 Methods.
Field
int: day, month, and year,Method
getDay() and printDate()
-
8/8/2019 JAVA Tutorial(Oni Blessing)
15/17
-
8/8/2019 JAVA Tutorial(Oni Blessing)
16/17
publicclass DateProgram
{
publicstatic voidmain(String [] args)
{
System.out.println(Withinmain...);
Datetoday = new Date();
today.day = 25;
today.month = 12;
today.year = 2003;
System.out.println(Thedayis + today.getDay());
System.out.println(Printingthedate...);
today.printDate();
System.out.println(Whatisdisplayednext?);
today.getDay();
}
}
Fig: Method
-
8/8/2019 JAVA Tutorial(Oni Blessing)
17/17
Expalnation