Search This Blog

Friday, 15 July 2022

5. NEP Java Program to define a class with the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class.

 //5.In a college first year class are having the following attributes Name of the class (BCA, BCom, BSc), Name of the staff No of the students in the class, Array of students in the class. Define a class called first year with above attributes and define a suitable constructor. Also write a method called best Student() which process a first-year object and return the student with the highest total mark. In the main method define a first-year object and find the best student of this class

import java.io.*;

class Program51

{

     public String cname, sname;

     public static int big,count=-1;

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

    {

          int n;  // Total number of students.

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

          DataInputStream in= new DataInputStream(System.in);

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

         FirstYear  student[] = new FirstYear[n];

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

         

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

          {

System.out.println("Enter the Class Name, Student Name and Total Marks");

              student[i] = new FirstYear("abc","xyz");

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

          }

       

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

          {

              student[i].BestStudent();

          }

          System.out.println("Best Student Details:");

    System.out.println("StudentName: "+student[count].sname+

"\nClass: "+student[count].cname+"\nTotalMarks: "+big);

     }

}

class FirstYear

{

          public int total=0;

          public String cname, sname;

         

          Program51 obj=new Program51();

          FirstYear(String cname, String sname)

          {

              this.cname = cname;

              this.sname = sname;

          }

       public void read(String cname, String sname,int total )

          {

              this.cname=cname;

              this.sname=sname;

              this.total=total;

         

          }

    

          public void BestStudent()

          {

              if(this.total>obj.big)

              {

                   obj.big=this.total;

                   obj.count++;

               }

          }

}

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();

}

}

3. NEP Java Program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in object’s member variable values.

3. Program with class variable that is available for all instances of a class. Use static variable declaration. Observe the changes that occur in object’s member variable values.

import  java.io.*;

class Prog3

{

     static int num1; //Class Variable

     int num2;    

     public static void  main(Stringargs[])

     {

          Prog3.num1=30;

          Prog3 obj1=new Prog3();

          Prog3 obj2=new Prog3();

          obj1.num2=25;

          obj2.num2=50;

          System.out.println("Assigning Prog3.num1=30, obj1.num2=25 and

obj2.num2=50");

          System.out.println("Prog3.num1="+Prog3.num1);

          System.out.println("obj1.num2="+obj1.num2);

          System.out.println("obj2.num2="+obj2.num2);

          obj1.num1=45;

          obj1.num2=67;

          System.out.println("\nAfter changing class variable with

obj1.num1=45 and obj1.num2=67");

          System.out.println("Prog3.num1="+Prog3.num1);

          System.out.println("obj1.num1="+obj1.num1);

          System.out.println("obj2.num1="+obj2.num1);

          System.out.println("obj1.num2="+obj1.num2);

          System.out.println("obj2.num2="+obj2.num2);

     }

}

Wednesday, 13 July 2022

2. NEP Program to perform mathematical operations.

 //2. NEP Program to perform mathematical operations.

import java.io.*;

class  AddSub

{   

     static int num1=25, num2=5;

     public static void add()

     {

          System.out.println("Sum="+(num1+num2));

     }

     public static void subtract()

     {

          System.out.println("Diff="+(num1-num2));

     }   

     public static void main(String args[])

     {

          AddSub obj1=new AddSub();

          MulDiv obj2=new MulDiv();

          System.out.println("Mathematical Operations:");

          obj1.add();

          obj1.subtract();

          obj2.multiply();

          obj2.divide();

     }

 

}

class MulDiv extends AddSub

{

     public static void multiply()

     {

          System.out.println("Div="+(num1/num2));

     }

     public static void divide()

     {

          System.out.println("Prod="+(num1*num2));

     }

}

1. NEP Java Program to find the sum of two integers and float numbers. Read through Command Line.

//1. NEP Java Program to find the sum of two integers and //float numbers.

import java.io.*;

class Program11

{   

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

     {

          int a=10,b=20;

          float c=40.2F,d=35.5F;

 

          try

          {

                   a=Integer.parseInt(args[0]);

                   b=Integer.parseInt(args[1]);

                    c=Float.parseFloat(args[2]);

                   d=Float.parseFloat(args[3]);

          }

          catch(Exception e){}

          System.out.println(a + " " + b + " " + c + " " + d);

 

          add(a,b);

          add(c,d);

  

          int sum = a + b;

 

     }

     public static void add(int a, int b)

     {

          int sum = a+b;

          System.out.println("The sum is: " + sum);

     }

     public static void add(float c, float d)

     {

          float sum = c+d;

          System.out.println("The sum is: " + sum);

     }

}

FREE Hit Counters