Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
with Java
Topic
Lecture 2
Object Oriented Paradigms in Java
P r im it iv e D a t a T y p e s A b s tra c t D a ta T y p e s
N u m e r ic D a t a T y p e s N o n - n u m e r ic D a ta T y p e s C la s s e s A rra y s
In te rfa c e
In te g e r C h a ra c te r
F lo a t in g p o in t B o o le a n
Integer Type
Character Type
Boolean Type
class PrimitiveDataTypes
{
public static void main(String args[ ] ) {
DataInputStream in = DataInputStream(System.in);
boolean flag;
char c = ‘A’;
byte b = 63;
short shortInt;
int i;
long longInt;
float x;
double y;
m a rk s
m a r k s .le n g th = n
class Point
{
int x;
int y;
void getPoint ( int a, int b, ) {
x = a;
y = b;
}
}
class Points
{
float distance;
public static void main (String args[ ] {
Point p1 =new Point( );
Point p2 = p1;
Point p3 = new Point ( );
Point p4 = new Point ( );
p1.getPoint (5, 10 );
p2.getPoint (15, 20);
p3.getPoint (20, 30);
p4.getPoint (30, 40);
System.out .println ( " X1 = " + p1.x + "Y1 = " + p1.y );
System.out .printlin ( "X2=" + p2.x + "Y2 = " +p2.y );
int dx = p3.x - p4. x; // X2 - X1
int dy = p3.y - p4. y; // y2 - y1
distance = Math.sqrt (dx * dx + dy * dy ); // (X2-X1)2 + (Y2-Y1)2
System.out.println ( " Distance = "+ distance );
}
}
19.03.18 Short Term Course on C++ 13
Constructor
Automatic Initialization of Objects
class Point ( ) {
int x, y;
Point ( int x, int y ) {
this.x = x ;
this.y = y;
}
printPoint() {
System.out.println("X = "+ this.x + " Y= " + this.y);
}
class PointCreate {
public static void main ( String args [ ] ) {
Point p = new Point (10, 20 );
p.printPoint();
}
}
void printPoint() {
System.out.println("X = "+ this.x + " Y= " + this.y);
}
class PointCreate {
public static void main ( String args [ ] ) {
Point p1 = new Point (10, 20 );
p1.printPoint();
void displayPoint ( ) {
System.out.println ( "X=" + x + " Y = " + y + " Z = " + z ) ;
}
}
19.03.18 Short Term Course on C++ 19
Polymorphism in Java
class Point {
int x,y;
Point ( int x, int y ) { // It is a constructor
this.x = x;
this.y = y;
}