Pages

Monday, 7 March 2011

SEM1 java ass 2

****************************Ass:2.Set:A.Que:1*********************************
Que)        Define a Student class (roll number, name, percentage). Define a default and
parameterized constructor. Override the toString method. Keep a count objects created.
Create objects using parameterized constructor and display the object count after each
object is created. (Use static member and method). Also display the contents of each
object.



*********************************Java Code***********************************

import java.io.*;
class sort
{
 int rollno;
 String name;
 float percent;

 static void sortStudent(sort[]studentArray,int n)
  {
    sort temp;
    for(int i=0;i<n;i++)
     {
       for(int j=i+1;j<n;j++)
       if(studentArray[i].percent<studentArray[j].percent)
        {
          temp=studentArray[i];
          studentArray[i]=studentArray[j];
          studentArray[j]=temp;
        }
     }
  }
 public String toString()
  {
   return "["+name+","+percent+","+rollno+"]";
  }
 public static void main(String args[])throws IOException
  {
   int n,i;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   System.out.println("enter value of n");
   n=Integer.parseInt(br.readLine());
   sort[] studentArray=new sort[20];
   for(i=0;i<n;i++)
    {
     studentArray[i]=new sort();
     studentArray[i].rollno=i+1;
     System.out.println("enter your name");
     studentArray[i].name=br.readLine();
     System.out.println("enter your percentage");
     studentArray[i].percentage=Float.parseFloat(br.readLine());
     System.out.println("rollno="+studentArray[i].rollno);
     System.out.println("name="+studentArray[i].name);
     System.out.println("percent="+studentArray[i].percent);
    }
   sort.sortStudent(studentArray,n);
   for(i=0;i<n;i++)
    {
      System.out.println(studentArray[i]);
    }
   }
}

//Output
[root@localhost~]#java sort
enter value of n
2
enter your name
Vinaya
enter your percent
65.52
rollno=1
name=Vinaya
percent=65.52
enter your name
Rutuja
enter your percent
70
rollno=2
name=Rutuja
percent=70
[Rutuja,70.0,2]
[Vinaya,65.52,1]
     
******************************************************************************

****************************Ass:2.Set:A.Que:2*********************************
Que)Write a java program to create n objects of the Student class. Assign roll numbers in
the ascending order. Accept name and percentage from the user for each object. Define a
static method “sortStudent” which sorts the array on the basis of percentage.



*********************************Java Code***********************************

import java.io.*;
class NStudents
{
 int rollno;
 String sname;
 float per;
 public void accept()
  {
   try
    {
      BuferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter student name:");
      sname=br.readLine();
      System.out.println("Enter student percentage:");
      per=Float.valueOf(br.readLine()).floatValue();
    }
   catch(Exception e){}
  }
 public static void sortStudent(NStudents s[],int n)
  {
    float.temp;
    String temp1;

    for(int i=0;i<n;i++)
     {
      for(int j=i+1;j<n;j++)
       {
        if(s[i].per > s[j].per)
         {
          temp=s[i].per;
          s[i].per=s[j].per;
          s[j].per=temp;

          temp1=s[i].sname;
          s[i].sname=s[j].sname;
          s[j].sname=temp1;
         }
       }
     }
    for(int i=0;i<n;i++)
     {
      s[i].rollno=i+1;
      System.out.println("\n"+s[i].rollno+"\t"+s[i].sname+"\t"+s[i].per);
     }
  }
public static void main(String a[])
{
 int n;
 try
  {
   BuferedReader br=new BufferedReader(new InputStreamReader(System.in));  
   System.out.println("How many objects you want to create:");
   n=Integer.parseInt(br.readLine());

   NStudent s[]=new NStudent[n];
   for(int i=0;i<n;i++)
    {
     s[i]=new NStudent();
     s[i].accept();
    }
   System.out.println("\nStudent Details:");
   System.out.println("RollNo\tName\tPercentage");
   for(int i=0;i<n;i++)
    {
     s[i].rollno=i+1;
     System.out.println("\n"+s[i].rollno+"\t"+s[i].sname+"\t"+s[i].per);
    }
   NStudent s1=new NStudents();
   System.out.println("\nStudent details in ascending order of their percentages:");
   System.out.println("Roll No\tName\tPercentage");
  
   s1.sortStudent(s,n);
  }
  catch(Exception e)
  { }
 }
}


//Output
[root@localhost Set-A]# java NStudents
How many objects you want to create:
2
Enter student name:
Priya
Enter student percentage:
88.88
Enter student name:
Vinaya
Enter student percentage:
77.78

Student Details:
Roll No  Name   Percentage
1        Priya  88.88
2        Vinaya 77.78

Student details in ascending order of their percentage:
Roll No  Name   Percentage
2        Vinaya  77.78
1        Priya   88.88
******************************************************************************


****************************Ass:2.Set:B.Que:1*********************************
Que)      Create a package named Maths. Define class MathsOperations with static methods
to find the maximum and minimum of three numbers. Create another package Stats.
Define class StatsOperations with methods to find the average and median of three
numbers. Use these methods in main to perform operations on three integers accepted
using command line arguments.

********************************setb1.java**********************************

import Maths.MathsOperations;
import Stats.StatsOperations;

class setb1
{
 public static void main(String a[])
  {
    int n1=Integer.parseInt(a[0]);
    int n2=Integer.parseInt(a[1]);
    int n3=Integer.parseInt(a[2]);

    MathsOperations.maximum(a,b,c);
    MathsOperations.minimum(a,b,c);
    StatsOperations.Average(a,b,c);
    StatsOperations.Median(a,b,c);
  }
}
**********************************MathsOperations.java**************************

package Maths;
public class MathsOperations
{
        public static int Maximum(int a,int b,int c)
        {
                int max;
                if(a>b && a>c)
                        max=a;
                else if(b>a && b>c)
                        max=b;
                else
                        max=c;
                return max;
        }
        public static int Minimum(int a,int b,int c)
        {
                int min;
                if(a<b && a<c)
                        min=a;
                else if(b<a && b<c)
                        min=b;
                else
                        min=c;
                return min;
        }
}

**********************************StatsOperations.java***************************
package Stats;
public class StatsOperations
{
        public static float Average(int a,int b,int c)
        {
                return((c+b+c)/3);
        }
        public static int Median(int a,int b,int c)
        {
                int temp;
                if(a>b)
                {       temp=a;
                        a=b;
                        b=temp;
                }
                if(a>c)
                {
                        temp=a;
                        a=c;
                        c=temp;
                }
                if(b>c)
                {
                        temp=b;
                        b=c;
                        c=temp;
                }
                return b;
        }
}


*********************************Output**************************************
[root@localhost 1]#java setb1 4 6 2
The maximum among three numbers is 6
The minimum among three numbers is 2
The Average is 4.0
The Median is 4.0
*****************************************************************************

****************************Ass:2.Set:B.Que:2*********************************
Que)     Write a Java program to create a Package “SY” which has a class SYMarks
(members – ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package
TY which has a class TYMarks (members – Theory, Practicals). Create n objects of
Student class (having rollNumber, name, SYMarks and TYMarks). Add the marks of SY
and TY computer subjects and calculate the Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for
>= 50 , Pass Class for > =40 else ‘FAIL’) and display the result of the student in proper
format.

******************************SYMarks.java***********************************
package sy;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SYMarks
{
 public int ctotal,etotal,mtotal;

 public void get()
  {
   try
    {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
     ctotal=Integer.parseInt(br.readLine());
     etotal=Integer.parseInt(br.readLine());
     mtotal=Integer.parseInt(br.readLine());
    }
   catch(Exception e){ }
  }
}
***********************************TYMarks.java********************************
package ty;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class TYMarks
{
 public int theory,practicals;

 public void accept()
  {
   try
    {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
     System.out.println("Enter TY subject marks:");  

     System.out.println("Enter Theory marks out of 600:");         
     theory=Integer.parseInt(br.readLine());
         
     System.out.println("Enter Practical's marks out of 300:");    
     practicals=Integer.parseInt(br.readLine());
     System.out.println("Enter TY subject marks:");        
javaprac=Integer.parseInt(br.readLine());
    }
   catch(Exception e){ }
  }
}******************************student.java*************************************

import SY.SYMarks;
import TY.TYMarks;
import java.io.*;

class student
{
 int rollno;
 String name;

 public void get()
  {
   try
    {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
     rollno=Integer.parseInt(br.readLine());
     name=br.readLine();
    }catch(Exception e){}
  }
 public static void main(String a[])
  {
   try
    {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("How many object you want to create:");
     int n=Integer.parseInt(br.readLine());
  
     SYMarks sy[]=new SYMarks[n];
     TYMarks ty[]=new TYMarks[n];    
     student s[]=new student[n];

     for(int i=0;i<n;i++)
      {
       s[i]=new student();
       sy[i]=new SYMarks();
       ty[i]=new TYMarks();

       s[i].accept();
       sy[i].accept();
       ty[i].accept();

       s[i].tytotal=(ty[i].theory + ty[i].prac);
       s[i].sytotal=(sy[i].ctotal + sy[i].mtotal + sy[i].etotal);
       s[i].gtotal=(s[i].tytotal + s[i].sytotal);
       s[i].per=(((s[i].gtotal)/12)*100);

       if(s[i].per>=70)
         s[i].grade="A";
       elseif(s[i].per<70 && s[i].per>=60)
         s[i].grade="B";
       elseif(s[i].per<60 && s[i].per>=50)
         s[i].grade="C";
       elseif(s[i].per<50 && s[i].per>=40)
         s[i].grade="PASS";
       else
         s[i].grade="FAIL";
      }
      System.out.println("\nRoll No./tSname/tSYTotal/tTYTotal/tGTotal/tGPercentage/tGrade");
 
      for(int i=0;i<n;i++)
       {
         System.out.println("s[i].rollno+"/t"+s[i].sname+"/t"+s[i].sytotal+"/t"+s[i].tytotal+"/t"+s[i].gtotal+"/t"+s[i].per+"/t"+s[i].grade);
       }
      }catch(Exception e){}
     }
   }
*********************************Output************************************
[root@localhost 2]#java student
How many objects you want to create:
3
Enter student name:Pooja
Enter student Roll No:100
Enter SYMarks:
Enter Computer total out of 300:298
Enter Maths total out of 300:250
Enter Electronics total out of 300:270
Enter TY subject marks :
Enter Theory marks out of 600:500
Enter Practicals marks out of 300:250

Enter student name:Mrunal
Enter student Roll No:101
Enter SYMarks:
Enter Computer total out of 300:129
Enter Maths total out of 300:232
Enter Electronics total out of 300:123
Enter TY subject marks :
Enter Theory marks out of 600:534
Enter Practicals marks out of 300:267

Enter student name:Sonali
Enter student Roll No:102
Enter SYMarks:
Enter Computer total out of 300:287
Enter Maths total out of 300:154
Enter Electronics total out of 300:190
Enter TY subject marks :
Enter Theory marks out of 600:300
Enter Practicals marks out of 300:198

Roll No     SName  SYTotal  TYTotal  GTotal  GPercentage  Grade
100      Pooja  810      750      1040    86           A
101      Mrunal 484      801      930     77           A
102     Sonali    631     408      795     65           A

*****************************************************************************





No comments:

Post a Comment