Slip20

 Q1) Write a Program to illustrate multilevel Inheritance such that country is inherited from continent. State is inherited from country. Display the place, state, country and continent. [10 marks] 

import java.io.*;

class Continent

{

   String con;

 

  BufferedReader r  = new BufferedReader(new InputStreamReader(System.in));

  

 void con_input() throws IOException

 {

       System.out.println("Enter Continent Name:  "); 

       con = r.readLine();    

 }

}

class Country extends Continent

{

 String cou ;

 void cou_input() throws IOException

 {

       System.out.println("Enter Country Name:  ");

       cou = r.readLine();    

 } 

}

 

class State extends Country

{

 String sta,place;

 void sta_input() throws IOException

 {

       System.out.println("Enter State Name:  ");

       sta = r.readLine(); 

System.out.println("Enter place Name:  ");

       place = r.readLine();

 }

 public static void main( String argsp[] )throws IOException

 {

  State s = new State();

s.con_input();

  s.cou_input();

  s.sta_input();

    System.out.println("Continent: "+s.con);

  System.out.println("Country: "+s.cou); 

  System.out.println("State: "+s.sta); 

System.out.println("Place: "+s.place);

 }

}

Q2) Write a package for Operation, which has two classes, Addition and Maximum. Addition has two methods add () and subtract (), which are used to add two integers and subtract two, float values respectively. Maximum has a method max () to display the maximum of two integers

//Addition.java

package operation;

public class Addition

{

public void add(int a,int b)

{

int c=a+b;

System.out.println("Addition of 2 integer is :"+c);

}

public void sub(float a,float b)

{

float s=a-b;

System.out.println("Substraction of 2 float values is :"+s);

}

}

//Maximum.java

package operation;

public class Maximum

{

public void max(int a,int b)

{

if(a>b)

System.out.println(a+" is Maximum number of"+a+" and "+b);

else

System.out.println(b+" is Maximum number of"+a+" and "+b);

}

}


//main program

import operation.*;

class Slip20_2

{

public static void main(String args[])

{

Addition a=new Addition();

a.add(2,6);

a.sub(20.40f,10.70f);

Maximum m =new Maximum();

m.max(25,30);

}

}


Comments

Popular posts from this blog

Slip1

Slip3

Slip2