Wednesday, April 19, 2017

Day 2 Java Training

Day 2 Java Training


//INHERITANCE

//Classroom.java

class Classroom{
String name;
int size;
int noOfChairs;

void display(){
System.out.println("Name:"+ name);
System.out.println("Size:"+ size);
System.out.println("No. of Chairs:"+ noOfChairs);
}
}

//TestClassroom.java

class TestClassroom{
public static void main(String[] args){
ComputerLab lab1 = new ComputerLab();
lab1.name="ICT Lab";
lab1.size=40;
lab1.noOfChairs=20;
lab1.noOfComputers=14;
lab1.display();
lab1.displayComputers();
}
}

//ComputerLab.java

class ComputerLab extends Classroom{
int noOfComputers;

void displayComputers(){
System.out.println("Computer:" + noOfComputers);}
}
______________________________________________________
//INHERITANCE
//Classroom.java

class Classroom{
String name;
int size;
int noOfChairs;

void display(){
System.out.println("Name:"+ name);
System.out.println("Size:"+ size);
System.out.println("No. of Chairs:"+ noOfChairs);
}
void setName(String n){
name=n;
}

void setSize(int s){
size=s;
}

void setChairs(int c){
noOfChairs=c;
}
}

// TestClassroom.java

class TestClassroom{
public static void main(String[] args){
ComputerLab lab1 = new ComputerLab();
lab1.setName("ICT Lab");
lab1.setSize(40);
lab1.setChairs(20);
lab1.setComputers(14);
lab1.display();
//lab1.displayComputers();
}
}

//ComputerLab.java

class ComputerLab extends Classroom{
int noOfComputers;

void displayComputers(){
System.out.println("Computer:" + noOfComputers);}

void setComputers(int comp){
noOfComputers=comp;
}

}
______________________________________________________

// Constructor

class Classroom1{
String name;
int size;
int noOfChairs;

Classroom1(String classN, int classSize, int classChair){
name=classN;
size=classSize;
noOfChairs=classChair;
}

void display(){
System.out.println("Name:"+ name);
System.out.println("Size:"+ size);
System.out.println("No. of Chairs:"+ noOfChairs);
}
public static void main(String[] args)
{
Classroom1 room2 = new Classroom1("St. Ignacius", 20,10);
room2.display();

}
}
_______________________________________________________________

// Computer Lab 1.java

class ComputerLab1 extends Classroom1{
int noOfComputers;

ComputerLab1(String a, int b, int c, int d){
super(a,b,c);
noOfComputers=d;
}

void displayComputers(){
System.out.println("Computer:" + noOfComputers);}

void setComputers(int comp){
noOfComputers=comp;
}
}

//TestClassroom.java

class TestClassroom2{
public static void main(String[] args){
ComputerLab1 lab2= new ComputerLab1("Maharlika", 20, 20, 40);
lab2.display();
lab2.displayComputers();
}
}
______________________________________________________________
// Overriding

//a.java
class a{
int a;
int b;
public void printTest(){
System.out.println("This is a " + a);
System.out.println("This is b " + b);
}
}

//b.java

class b extends a{
int c;
public void printTest(){
super.printTest();
System.out.println("This is c:" +c);
}
public static void main(String[] arg){
b myObj=new b();
myObj.a=1;
myObj.b=2;
myObj.c=3;
myObj.printTest();
}
}

_____________________________________________________
//bicycles.java

class bicycles
{
int gear;
int cadense;
int speed;
bicycles(int a, int b, int c){
gear=a;
cadense=b;
speed=c;
}
void printTest(){
System.out.println("This is a gear status:" + gear);
System.out.println("This is a cadense status:" + cadense);
System.out.println("This is a speed status:" + speed);
}
}

// bicycles1.java
class bicycles1 extends bicycles{
int height;
bicycles1(int a, int b, int c, int d){
super(a,b,c);
height=d;
}
void printTest(){
super.printTest();
System.out.println("This is a height: " + height);
}
}

// Testbicycle.java
class TestBicycle{
public static void main(String[] args)
{
bicycles1 myObj=new bicycles1(2,3,5,65);
myObj.printTest();
}
}
________________________________________________

download more info

No comments:

Post a Comment