Write a program that implements the concept of inheritance F
Solution
ANS:
import java.util.Scanner;
abstract class AirUniversity
{
int i;
public abstract void id();
}
//student class
class Student extends AirUniversity
{
double g;//to store gpa
String e;//to store enrolled course
double p;//to store paidfee
public void gpa(double g){
this.g=g;
}
public void enroll(String e){
this.e=e;
}
public void pay(double amount){
p=amount;
}
public void id(){
i=(int)(Math.random()*10000);//generates random number for id
}
void display(){
System.out.println(\"ID : \"+i);
System.out.println(\"Gpa: \"+g);
System.out.println(\"Enrolled Course Name: \"+e);
System.out.println(\"Fee Paid :\"+p);
}
}
//Instructor class
class Instructor extends AirUniversity
{
String g;//to store grade
String t;//to store course name
double pay;//to stor pay
public void grade(String g){
this.g=g;
}
public void teach(String t){
this.t=t;
}
public void getPaid(double pay){
this.pay=pay;
}
public void id(){
i=(int)(Math.random()*10000);//generates random number for id
}
void display(){
System.out.println(\"ID : \"+i);
System.out.println(\"Grade: \"+g);
System.out.println(\"Course Name: \"+t);
System.out.println(\"Paid :\"+pay);
}
}
public class MainDemo
{
public static void main(String args[])
{
Student st=new Student();
Instructor i1=new Instructor();
int choice;
Scanner sc=new Scanner(System.in);//scanner object to read input values
System.out.println(\"Enter 1 for student 2 for Instructor \");
choice=sc.nextInt();
if(choice == 1)
{
System.out.println(\"Enter GPA\");
double g=sc.nextDouble();
st.gpa(g);
System.out.println(\"Enter Enrolled course name\");
String en=sc.next();
st.enroll(en);
System.out.println(\"Enter any fee paid\");
double p=sc.nextDouble();
st.pay(p);
st.id();//inherited from base class
System.out.println(\"Do you want to display Details, enter Y for (Y)es or N for (N)\");
String ch=sc.next();
if(ch.equalsIgnoreCase(\"y\"))
st.display();
}
else if(choice == 2)
{
System.out.println(\"Enter GRADE\");
String g=sc.next();
i1.grade(g);
System.out.println(\"Enter course Name\");
String en=sc.next();
i1.teach(en);
System.out.println(\"Enter fee paid\");
double p=sc.nextDouble();
i1.getPaid(p);
i1.id();//inherited from base class
System.out.println(\"Do you want to display Details, enter Y for (Y)es or N for (N)\");
String ch=sc.next();
if(ch.equalsIgnoreCase(\"y\"))
i1.display();
}
else
System.out.println(\"Invalid choice,Try again\");
}
}

