Search This Blog

Friday 15 July 2022

4. NEP Java Program to create a student class with the following attributes; Enrollment No: Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks.

 4. Program to create a student class with the following attributes; Enrollment No: Name, Mark of sub1, Mark of sub2, mark of sub3, Total Marks. Total of the three marks must be calculated only when the student passes in all three subjects. The pass mark for each subject is 50. If a candidate fails in any one of the subjects his total mark must be declared as zero. Using this condition write a constructor for this class. Write separate functions for accepting and displaying student details. In the main method create an array of n student objects and display the details.

import java.io.*;

class Program4

{

 

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

    {

        int n;

        System.out.println("How many students?");

        DataInputStream in= new DataInputStream(System.in);

        n=Integer.parseInt(in.readLine());

        Student arr[] = new Student[n];

        System.out.println("n="+n);

        for(int i=0;i<n;i++)

        {

        System.out.println("Enter the ID, name, marks in sub1,

          sub2 and sub3 for Student-"+(i+1));

          arr[i] = new Student(0,"");

                       arr[i].read(Integer.parseInt(in.readLine()),in.readLine(),

Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine()),Integer.parseInt(in.readLine()));

             

          }

          for(int i=0;i<n;i++)

          {

              arr[i].display();

          }

    }

    

}

class Student

{

          public int id,sub1,sub2,sub3;

          public float totalmarks;

          public String name;

          Student(int id, String name)

          {

              this.id = id;

              this.name = name;

          }

public void read(int id, String name,int sub1, int sub2, int sub3 )

{

              this.id=id;

              this.name=name;

              this.sub1=sub1;

              this.sub2=sub2;

              this.sub3=sub3;

              if(this.sub1<50 || this.sub2<50 || this.sub3<50)

              {   

                   this.totalmarks=0.0F;

              }

              else

              {

                                                                              this.totalmarks=this.sub1+this.sub2+this.sub3;

              }

 

}

public void display()

{

     System.out.println("Student id: " + id + " "+ "name: "+      name);

     System.out.println("totalmarks:"+totalmarks);

     System.out.println();

}

}

No comments :

Post a Comment

FREE Hit Counters