Вы находитесь на странице: 1из 41

Introducton to |ava Language 09/12/2013

Idea behnd programmng


The IDE
The IDE (Where to downoad, how to nsta, and openng)
Good about |ava when Compared to other programmng anguages.
nterpret
bytecode
vrtua machne
The C anguage
#ncude <stdo.h>
struct student {
nt d;
char name|20|;
oat percentage;
};

nt man() {
struct student record = {0}; //Intazng to nu
record.d=1;
strcpy(record.name, "Ra|u");
record.percentage = 86.5;

prntf(" Id s: %d \n", record.d);
prntf(" Name s: %s \n", record.name);
prntf(" Percentage s: %f \n", record.percentage);
return 0;
}
Note: arrays n C are not assgnabe so
record.name="Ra|u" w not work.
The rea use of the structure
#ncude <stdo.h>
struct vaue {
nt ;
oat |;
};
struct vaue f(nt x, oat y) {
struct vaue r;
r.=x+1;
r.|=y+1.0;
return r;
}
nt man(vod) {
nt x=10;
oat y=10.0;
struct vaue z;
z = f(x,y);
prntf("The nteger s %d, and the oat s %.2f\n", z.,z.|);
return 0;
}
What woud be much better?
What woud be the obvous extenton of the structure?
Expaned usng a structure
data/methods etc
The three concepts of OOP
human (hand,feet)
One of the most fundamenta prncpes
Data encapsuaton.
Lets earn the other
two concepts
ater
The rst |ava program - Heo word!!
pubc cass HeoWord {
pubc statc vod man(Strng|| args) {
System.out.prntn("Heo Word");
}
}
Note the man functon creaton shoud be
done wthout the name heoword n front
Moders: pubc, statc,
Entrance/ext; one man etc.
ncude s ke |ava brares (ater) obtaned
va mport keyword
It shoud be
pubc
need of the semcoon
#ncude<stdo.h>
vod man() {
prntf(Heo Word\n);
}
Namng Conventons
Casses shoud be Came case
cass ThsIsMyCass
Methods/varabes shoud be n mxed case
vod thsIsMyFuncton()
nt thsIsAnInteger;
USe names whch are meanngfu
Constants shoud be UPPERCASE
|ava s case senstve ke other anguages.
Keywords
There are many other thngs such as teras etc whch we w not touch due to tme restrctons.
We w not ook at denng varabe and ther szes as ts amost smar to C anguage.
Other |anguage constructs.
Exampe of the for oop
pubc cass TheLoop {
pubc statc vod man(Strng args||) {
System.out.prntn("Screen dspay");
for (nt =1; <=9; ++) {
for (nt |=1; |<=; |++) {
System.out.prnt(" ");
System.out.prnt();
}
System.out.prnt("\n");
}
System.out.prntn("Screen dspay done");
}
}
The pubc cass name shoud match
the name of the |ava e. Try wthout pubc
Try wth prvate. Expan the derence.
What s the dot notaton?
WheDemo.|ava
package whedemo;
pubc cass WheDemo {
pubc statc vod man(Strng|| args){
nt count = 1;
whe (count < 11) {
System.out.prntn("Count s: " + count);
count++;
}
}
}
What are packages
namng conventon
mportng a package
http://www.|artces.com/package/package_eng.htm
Operators and Expressons
Arthmatc, reatona and ogca operators are smar to C programmng anguage.
Increment/decrement operators are aso smar to C programmng anguage. And so are the condtona and btwse
operators.
You mght aso want to ook at the oca |ava tutora
http://docs.orace.com/|avase/tutora/|ava/ndex.htm
Ex.
package ndfrequency;
cass Frequency {
doube cacuateFrequency(doube L, doube R, doube c) {
doube frequency;
frequency=Math.sqrt((1.0f/(L*c))-(0.25*Math.pow(R/c,2)));
return(frequency);
}
}
pubc cass FndFrequency {
pubc statc vod man(Strng|| args) {
doube L=0.01;
doube R=1.0;
Frequency f=new Frequency();
for (doube c=0.01f; c<= 0.1; c+=0.01) {
System.out.prntn("When the capactance s " + Strng.format("%.2f",c) + " The frequency s "+
f.cacuateFrequency(L,R,c));
}
}

}
Another exampe of how ob|ects can be used n programmng.
package areacacuaton;
cass Rectange {
nt ength=1;
nt wdth=1;

nt areaOf() {
nt area = ength*wdth;
return(area);
}
}
pubc cass AreaCacuaton {
pubc statc vod man(Strng|| args) {
Rectange rect = new Rectange();
System.out.prntn("The area of the rectange s "+rect.areaOf());
rect.ength=10;
System.out.prntn("The area of the rectange s now "+rect.areaOf());
}

}
Lets do a quck recap
Three types of varabes n |ava
Instance varabes: The varabes dened wthn the cass. Each tme an ob|ect s created, an nstance of ths s
created adn memory aocated for t.
cass varabes: These are dened wth the keyword statc, one memory ocaton s aocated for a the nstances.
Loca varabes: These are varabes whch are found wthn the methods and are removed once the method
nshes executon.
package varabedentons;
cass TestCass {
nt x=5;
statc nt y=10;

nt returnX () {
return(x);
}

nt returnY () {
return(y);
}

}
pubc cass VarabeDentons {
pubc statc vod man(Strng|| args) {
TestCass c1 = new TestCass();
TestCass c2 = new TestCass();
System.out.prntn("c1.x before the change s "+c1.x);
System.out.prntn("c1.y before the change s "+c1.y);
System.out.prntn("c2.x before the change s "+c2.x);
System.out.prntn("c2.y before the change s "+c2.y);
c1.x=3;
c1.y=8;
System.out.prntn("c1.x after the change s "+c1.x);
System.out.prntn("c1.y after the change s "+c1.y);
System.out.prntn("c2.x after the change s "+c2.x);
System.out.prntn("c2.y after the change s "+c2.y);
}

}
Where woud ths be used?
Use the prvate moder for nt x=5, now how can
we access the varabe?
package varabedentons;
cass TestCass {
prvate nt x=5;
statc nt y=10;

nt returnX () {
return(x);
}

nt returnY () {
return(y);
}

nt seeX() {
return(x);
}
nt changeX(nt z) {
x=z;
return(0);
}

}
pubc cass VarabeDentons {
pubc statc vod man(Strng|| args) {
TestCass c1 = new TestCass();
TestCass c2 = new TestCass();
System.out.prntn("See X: "+c1.seeX());
c1.changeX(3);
System.out.prntn("Now X: "+c1.seeX());


}

}
package varabedentons;
cass TestCass {
prvate nt x=5;
statc nt y=10;

nt returnX () {
return(x);
}

nt returnY () {
return(y);
}
vod modfyX (nt z) {
x=z;
}

}
pubc cass VarabeDentons {
pubc statc vod man(Strng|| args) {
TestCass c1 = new TestCass();
TestCass c2 = new TestCass();
System.out.prntn("c1.x before the change s "+c1.returnX());
System.out.prntn("c1.y before the change s "+c1.returnY());
System.out.prntn("c2.x before the change s "+c2.returnX());
System.out.prntn("c2.y before the change s "+c2.returnY());
c1.modfyX(3);
c1.y=8;
System.out.prntn("c1.x after the change s "+c1.returnX());
System.out.prntn("c1.y after the change s "+c1.returnY());
System.out.prntn("c2.x after the change s "+c2.returnX());
System.out.prntn("c2.y after the change s "+c2.returnY());
}

}
Is ths better than drecty accessng data? Good encapsuaton?
You aow the manner n whch the data s accessed.
Encapsuaton eves
Inhertance
So ets ook at exampes
package testnhertance;
cass Rectange {
prvate nt ength=2;
prvate nt wdth=3;
nt areaOfRectange() {
return(ength*wdth);
}
}
cass Cube extends Rectange {
prvate nt heght=4;
nt voumeOfCube() {
return(heght*areaOfRectange());
}
}
pubc cass TestInhertance {
pubc statc vod man(Strng|| args) {
Cube cubeInstance = new Cube();
System.out.prntn("The cube voume s "+cubeInstance.voumeOfCube());
}

}
why cant we use ength*wdth*heght
nsde Cube?
package testnhertance;
cass Rectange {
prvate nt ength=2;
prvate nt wdth=3;
nt areaOfRectange() {
return(ength*wdth);
}
}
cass Cube extends Rectange {
prvate nt heght=4;
nt voumeOfCube() {
return(heght*areaOfRectange());
}
}
pubc cass TestInhertance {
pubc statc vod man(Strng|| args) {
Cube cubeInstance = new Cube();
System.out.prntn("The cube voume s "+cubeInstance.voumeOfCube());
System.out.prntn("The rectange area s "+cubeInstance.areaOfRectange());
Rectange rectangeInstance = new Rectange();
System.out.prntn("The rectanguar area s "+rectangeInstance.areaOfRectange());
}

}
package vehcetypes;
cass Car {
Strng typeOfCar() {
return("Its a generc car");
}

nt numberOfDoors(){
return(4);
}
}
cass Sedan {
Strng typeOfCar() {
return("Its a Sedan");
}
Strng mutmedaSystem() {
return("Has a Caskar MMS");
}
}
pubc cass VehceTypes {
pubc statc vod man(Strng|| args) {
Car myCar = new Car();
Sedan mySedan = new Sedan();
System.out.prntn("Cang Car cass: "+myCar.typeOfCar());
System.out.prntn("Cang Sedan cass: "+mySedan.typeOfCar());

}

}
package vehcetypes;
cass Car {
Strng typeOfCar() {
return("Its a generc car");
}
}
cass Sedan extends Car{
Strng typeOfCar() {
return("Its a Sedan");
}
}
pubc cass VehceTypes {
pubc statc vod man(Strng|| args) {
Car myCar = new Car();
Sedan mySedan = new Sedan();
System.out.prntn("Cang Car cass: "+myCar.typeOfCar());
System.out.prntn("Cang Sedan cass: "+mySedan.typeOfCar());

}

}
Remove tyOfCar n Sedan adn see
the resut. Expan.
package vehcetypes;
cass Car {
Strng typeOfCar() {
return("Its a generc car");
}
}
cass Sedan extends Car{
Strng typeOfCar(nt ) {
return("Its a Sedan");
}
}
pubc cass VehceTypes {
pubc statc vod man(Strng|| args) {
Car myCar = new Car();
Sedan mySedan = new Sedan();
System.out.prntn("Cang Car cass: "+myCar.typeOfCar());
System.out.prntn("Cang Sedan cass: "+mySedan.typeOfCar());
System.out.prntn("Cang Sedan cass: "+mySedan.typeOfCar(1));

}

}
IS t overrdden now?
overrdng gves you a way to modfy an
aready exstng cass method
Lets do an exampe now.

Вам также может понравиться