Question 9

a) A class Student defines the personal data of a student whie another class Marks defines the register number, name of subject and marks obtained by the student. The details of both the classes are given below:
Class name        Student
Data members/instance variables:
name           :           variable to store the name
sex             :           character variable to store sex ‘F’ or ‘M’
age              :           integer variable to store the age           
Member functions/methods
Student(……)           :           parameterised constructor
void inpdetailS()     :           to accept values for data members
void studDisplay () :           to display personal data of student
Class name    Marks
Data members/instance variables:
subject        :           variable to store the subject
regno                      :           integer variable to store register number
mark                       :           variable to store marks
Member functions/methods
Marks(….)               :           parameterised constructor
void inpdetailM()                :           to accept values for data members
void markDisplay ()            :           to display exam details of Student and            Marks
Specify the class Student giving details of the constructor, void impdetailS() and void studDisplay (). Using the concept of inheritance specify the class Marks, giving details of the constructor function void inpdetailM() and void markDisplay ().  In the main SMOut class define the  main function, create object and call  markDisplay() and studentDisplay() functions.  

import java.util.Scanner;
public class Student
{
    String name;
    char sex;
    int age;  
    //parameterised Constructor to assign nam to name and rollNo to roll number
    public Student(String nam, char ch, int a) 
    {

        name = nam;
        sex=ch;
        age= a;
        
    }   
    public void studDisplay()// display roll number  and name
    {
         System.out.println("Name : "+name);
         System.out.println("Sex : "+sex);
         System.out.println("Age :"+age);
    }
    public void inpdetailS()
    {
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter name ");
        name = sc.nextLine(); 
        System.out.println("Enter gender m/f  ");
        sex = sc.next().charAt(0); 
        System.out.println("Enter age");
        age = sc.nextInt(); 
    }
}
import java.util.Scanner;
public class Marks extends Student
{
    String subject;
    int regno;
    int mark;
    public Marks(String na, char s,int a, int r, String sub, int m)
    {
        super(na,s,a );
        regno=r;
        subject = sub;
        mark = m;
    }
    public void inpdetailM()
    {
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter Subject ");
        subject = sc.nextLine(); 
        System.out.println("Enter mark ");
        mark = sc.nextInt();
        System.out.println("Enter reg no ");
        regno = sc.nextInt();
        
    }
    public void markDisplay()
    {
        studDisplay();
        System.out.println("Reg no = "+regno);
        System.out.println("Subject  = "+subject);
        System.out.println("Mark = "+mark);
    }
}
import java.io.*;
public class SMOut
{
  public void main() throws IOException
  {
      Marks M1 = new Marks("Raman", 'M',17, 1001, "English", 89);
      M1.inpdetailS();
      M1.inpdetailM();
      System.out.println("Details ");
      M1.markDisplay();
    }
}
This entry was posted in Term 2. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *