Skip to main content

Program to implement single inheritance.


CODE
import java.io.*;
class Student 
{ BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int rollno;
String name;
public void getdata()throws IOException 
{ System.out.println("Enter rollno: ");
rollno=Integer.parseInt(obj.readLine());
System.out.println("Enter name: ");
name=obj.readLine();  } 
void putdata()
{ System.out.println("Rollno: "+rollno);
System.out.println("Name: "+name);
} }
class marks extends Student 
{ BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));             
double m1, m2, m3;
public void getdata()throws IOException 
{ super.getdata();
System.out.println("Enter marks for m1, m2 and m3: ");
m1=Double.parseDouble(obj.readLine());
m2=Double.parseDouble(obj.readLine());
m3=Double.parseDouble(obj.readLine()); }
void putdata()
{ super.putdata();
System.out.println("M1: "+m1);
System.out.println("M2: "+m2);
System.out.println("M3: "+m3);
System.out.println("Total (out of 300): "+(m1+m2+m3));
System.out.println("Percentage: "+((m1+m2+m3)/3));
} }
public class EXP18Q2
{ public static void main(String[] args)throws IOException
{ marks m1 = new marks();
m1.getdata(); 
m1.putdata();  } }


Comments

Popular posts from this blog

Program to calculate the room area and volume to illustrate the concept of single inheritance.

CODE: class Roomarea   { protected double l, b; Roomarea(double len, double br) { l=len; b=br; } void putdata() { System.out.println("Area of Room: "+(l*b)); } } class Roomvol extends Roomarea   { double h; Roomvol(double len, double br, double he) { super(len, br); h=he;   } void putdata() { super.putdata(); System.out.println("Volume of Room: "+(l*b*h)); } } public class EXP18Q4 { public static void main(String[] args) { Roomvol r1 = new Roomvol(10, 20, 10); r1.putdata();   } }

Define an exception called ‘NotMatchexception’ that is thrown when a string is not equal to ‘India’. Write a java program that uses this exception.

Code: import java.util.Scanner; class NotMatchException extends Exception {   public NotMatchException (String str)   {     System.out.println(str);   } } public class StringExc {    public static void main(String[] args)    {      Scanner scan = new Scanner(System.in);      System.out.print("Enter the string : ");      String input = scan.nextLine();      try      {        if(input.equalsIgnoreCase("India"))          System.out.println("String matched !!!");        else          throw new NotMatchException ("String not matched !!!");     }     catch (NotMatchException s)     {     ...

Develop a program using control loops in applets.

Code: import java.awt.*; import java.applet.*; public class Control extends Applet {      public void paint(Graphics g)      {      for(int i=1;i<=4;i++)     {          if(i%2==0)         {            g.fillOval(90,i*50+10,50,50);           g.setColor(Color.black);        }        else       {          g.fillOval(90,i*50+10,50,50);          g.setColor(Color.red);       }    }   } } /* <applet code=Control width=300 height=300> </applet> */ Output: