The Programming Project: Class
Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Thursday, April 10, 2014

Rational Numbers representation Using classes and structures


Basic operations on Rational Numbers using C, C++ and Java.
The same program is written in the above languages, helping you to grasp the basic principles of classes and objects in C++ and Java. Students moving from C++ to Java will be able to relate the similarities at the same time the major differences between them.

Code in C
#include<stdio.h>
#define TRUE 1
#define FALSE 0
typedef struct
{
int numerator;
int denominator;
} RATIONAL;
void multiply(RATIONAL *frst,RATIONAL *scnd);
void add(RATIONAL *frst,RATIONAL *scnd);
void subtract(RATIONAL *frst,RATIONAL *scnd);
int equality(RATIONAL *frst,RATIONAL *scnd);
void divide(RATIONAL *frst,RATIONAL *scnd);
void reduce(RATIONAL *rat);

int main()
{
RATIONAL x,y;
int choice;
printf("\n Enter the numerator and denominator of the first rational:");
scanf("%d %d",&x.numerator,&x.denominator);
printf("\n Enter the numerator and denominator of the second rational:");
scanf("%d %d",&y.numerator,&y.denominator);
//printf("\n Reduced form of the input rationals are:");
reduce(&x);
//printf("\n %d / %d ",x.numerator,x.denominator);
reduce(&y);
//printf("\n %d / %d ",y.numerator,y.denominator);
printf("\n Press 1 for addition:");
printf("\n Press 2 for subtraction:");
printf("\n Press 3 for multiplication:");
printf("\n Press 4 for divison:");
printf("\n Press 5 to check for equatilty");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
add(&x,&y);
break;
case 2:
subtract(&x,&y);
break;
case 3:
multiply(&x,&y);
break;
case 4:
divide(&x,&y);
break;
case 5:
equality(&x,&y)==TRUE ? printf("\n They are equal:\n") : printf("\n They are unequal:\n");
break;
default:
break;
}
return 0;
}
void divide(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL sum;
sum.numerator = (frst->numerator)*(scnd->denominator);
sum.denominator = (frst->denominator)*(scnd->numerator);
reduce(&sum);
printf("\n After dividing the new rational is: %d / %d \n",sum.numerator,sum.denominator);
}
void multiply(RATIONAL *frst,RATIONAL *scnd)
{
RATIONAL sum;
sum.numerator = (frst->numerator)*(scnd->numerator);
sum.denominator = (frst->denominator)*(scnd->denominator);
reduce(&sum);
printf("\n Product of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
int equality(RATIONAL *frst,RATIONAL *scnd)
{
return ( (frst->numerator == scnd->numerator && frst->denominator == scnd->denominator) ? TRUE : FALSE );
}
void subtract(RATIONAL *frst,RATIONAL *scnd)
{
int lcm=1,i;
for(i=1;;i++)
{
if (frst->denominator % i == 0 && scnd->denominator % i== 0)
break;
}
lcm = ((frst->denominator)*(scnd->denominator))/i;
RATIONAL sum;
sum.numerator = (frst->numerator)*(lcm/frst->denominator)-(scnd->numerator)*(lcm/scnd->denominator);
sum.denominator = lcm;
reduce(&sum);
printf("\n Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void add(RATIONAL *frst,RATIONAL *scnd)
{
int lcm=1,i;
for(i=1;;i++)
{
if (frst->denominator % i == 0 && scnd->denominator % i== 0)
break;
}
lcm = ((frst->denominator)*(scnd->denominator))/i;
RATIONAL sum;
sum.numerator = (frst->numerator)*(lcm/frst->denominator)+ (scnd->numerator)*(lcm/scnd->denominator);
sum.denominator = lcm;
reduce(&sum);
printf("\n Sum of the rationals is: %d / %d \n",sum.numerator,sum.denominator);
}
void reduce(RATIONAL *rat)
{
int a,b,rem;
if(rat->numerator > rat->denominator) {
a = rat->numerator;
b = rat->denominator;
}
else {
a = rat->denominator;
b = rat->numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
rat->numerator /= a;
rat->denominator /= a;
}



Code in C++

#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
class Rational {
private:
int numerator;
int denominator;
public:
Rational() { numerator = 1; denominator = 1;}
void setValues()
{
cout<<"\n Enter the numerator:";
cin>>numerator;
cout<<"\n Enter the denominator:";
cin>>denominator;
}
void dispValue()
{
cout<<"\n"<<numerator<<"/"<<denominator<<"\n";
}
void reduce();
void add(Rational r);
void subtract(Rational r);
void multiply(Rational r);
void divide(Rational r);
int equality(Rational r);
};
int Rational::equality(Rational r)
{
return ( (numerator == r.numerator && denominator == r.denominator) ? TRUE : FALSE );
}
void Rational::divide(Rational r)
{
Rational sum;
sum.numerator = (numerator)*(r.denominator);
sum.denominator = (denominator)*(r.numerator);
sum.reduce();
cout<<"\n On divison the rationals:";
sum.dispValue();
}
void Rational::multiply(Rational r)
{
Rational sum;
sum.numerator = (numerator)*(r.numerator);
sum.denominator = (denominator)*(r.denominator);
sum.reduce();
cout<<"\n Product of the rationals is:";
sum.dispValue();
}
void Rational::subtract(Rational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
Rational sum;
sum.numerator = numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
cout<<"\n Difference of the rationals is:";
sum.dispValue();
}
void Rational::add(Rational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
Rational sum;
sum.numerator = numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
cout<<"\n Sum of the rationals is:";
sum.dispValue();
}
void Rational::reduce()
{
int a,b,rem;
if(numerator > denominator) {
a = numerator;
b = denominator;
}
else {
a = denominator;
b = numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
numerator /= a;
denominator /= a;
}
int main()
{
int choice;
Rational x,y;
x.setValues();
y.setValues();
x.reduce();
y.reduce();
cout<<"\n Press 1 for addition:";
cout<<"\n Press 2 for subtraction:";
cout<<"\n Press 3 for multiplication:";
cout<<"\n Press 4 for divison:";
cout<<"\n Press 5 to check for equatilty";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
x.add(y);
break;
case 2:
x.subtract(y);
break;
case 3:
x.multiply(y);
break;
case 4:
x.divide(y);
break;
case 5:
x.equality(y)==TRUE ? cout<<"\n They are equal:\n" : cout<<"\n They are unequal:\n";
break;
default:
break;
}
return 0;
}


Code in JAVA

import java.util.*;
public class Rational
{
public static void main(String[] args)
{
int choice;
Scanner in = new Scanner(System.in);
OperationRational x = new OperationRational();
OperationRational y = new OperationRational();
x.setValues();
y.setValues();
x.reduce();
y.reduce();
System.out.println("You have entered the following rationals:");
x.dispValue();
y.dispValue();
System.out.println("Press 1 for addition:");
System.out.println("Press 2 for subtraction:");
System.out.println("Press 3 for multiplication:");
System.out.println("Press 4 for divison:");
System.out.println("Press 5 to check for equatilty");
System.out.println("Enter your choice:");
choice = in.nextInt();
switch(choice)
{
case 1:
x.add(y);
break;
case 2:
x.subtract(y);
break;
case 3:
x.multiply(y);
break;
case 4:
x.divide(y);
break;
case 5:
if (x.equality(y) == true)
System.out.println("They are equal:");
else
System.out.println("They are unequal:");
break;
default:
break;
}
}
}
class OperationRational
{
OperationRational()
{
numerator = 1;
denominator = 1;
}
public void setValues()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the numerator:");
numerator = in.nextInt();
System.out.println("Enter the denominator:");
denominator = in.nextInt();
}
public void dispValue()
{
System.out.println(numerator+"/"+denominator);
}
public void reduce()
{
int a,b,rem;
if(numerator > denominator) {
a = numerator;
b = denominator;
}
else {
a = denominator;
b = numerator;
}
while(b!=0) {
rem = a%b;
a = b;
b = rem;
}
numerator /= a;
denominator /= a;
}
public void add(OperationRational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
OperationRational sum = new OperationRational();
sum.numerator = numerator*(lcm/denominator)+(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
System.out.println("Sum of the rationals is:");
sum.dispValue();
}
public void subtract(OperationRational r)
{
int lcm=1,i;
for(i=1;;i++)
{
if (denominator % i == 0 && r.denominator % i== 0)
break;
}
lcm = ((denominator)*(r.denominator))/i;
OperationRational sum = new OperationRational();
sum.numerator = numerator*(lcm/denominator)-(r.numerator)*(lcm/r.denominator);
sum.denominator = lcm;
sum.reduce();
System.out.println("Difference of the rationals is:");
sum.dispValue();
}
public void multiply(OperationRational r)
{
OperationRational sum = new OperationRational();;
sum.numerator = (numerator)*(r.numerator);
sum.denominator = (denominator)*(r.denominator);
sum.reduce();
System.out.println("Product of the rationals is:");
sum.dispValue();
}
public void divide(OperationRational r)
{
OperationRational sum = new OperationRational();;
sum.numerator = (numerator)*(r.denominator);
sum.denominator = (denominator)*(r.numerator);
sum.reduce();
System.out.println("On divison the rationals:");
sum.dispValue();
}
public boolean equality(OperationRational r)
{
return ( (numerator == r.numerator && denominator == r.denominator) ? true : false );
}
private int numerator;
private int denominator;
}

Get the pdf file, click here

Sunday, September 8, 2013

Simple Java Program to Demonstrate ArrayList and use of gc()

Inherit the STUDENT Class(Program here http://thecprojectt.blogspot.in/2013/09/simple-java-program-to-demonstrate.html)  and override the input method so as to input the department of each student. Search and display a sorted list of students one department or students based on scoring criteria. Create an arraylist of students and remove a student based on scoring criteria and then call gc() and check for free memory



import java.util.*;
public class ListStudent
    {
    public static void main(String[] args)
    {
      int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
int ch,k=0,n;
String d1;
boolean lp,fg=true;
Runtime r = Runtime.getRuntime();
    Scanner in = new Scanner(System.in);
    Course co = new Course();
    extendStudent[] students = new extendStudent[6];
    for(int i=0;i<6;i++)
    students[i]=new extendStudent();
    ArrayList<extendStudent> L = new ArrayList<extendStudent>();
    System.out.println("Courses available are Graduation, PG and PhD with 3,2 and 1 seats respectively:");
    String choice1="";
        do
    {
   
    System.out.println("\n Enter the course name:");
    String S = in.next();
    if(co.getCourse(S).equals("XX"))
    {
    System.out.println("Invalid course:");
    continue;
    }
    if(co.getSeat(S)>co.getSeatLimit(S))
    {
    System.out.println("Seats for the course "+S+" is exhausted:");
    continue;
    }
      System.out.println("Enter the Record for student :"+(k+1));
      students[k].setCourse(S);
    students[k].inputData();
    L.add(students[k]);
    k++;
    System.out.println(" Want to add another record yes/no:");
    choice1 = in.next();
    }while("YES".equalsIgnoreCase(choice1));
    n=k;
    System.out.println(" Intial List Size is: "+L.size());
    System.out.println("Press 1 to display Records of all STUDENTS:");
    System.out.println("Press 2 to see the result of all STUDENTS:");
    ch = in.nextInt();
    switch(ch)
    {
    case 1:
    for(int i=0;i<L.size();i++)
    L.get(i).displayAll();
    break;
    case 2:
        for(int i=0;i<L.size();i++)
        L.get(i).resultBuild();
        break;
        default:
        System.out.println("Wrong choice:");
        break;
        }
        System.out.println(" Intial List Size is: "+L.size());
        System.out.println(" Total free memory is: "+r.freeMemory()+" Bytes");
        System.out.println(" Want to search by Admission Date(dd/mm/yyyy)? yes/no:");
        String choice = in.next();
        if("YES".equalsIgnoreCase(choice))
        {
        int d=0,m=0,y=0;
         
          do {
fg=true;
int i=0;
int j=0;

String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
d1=in.next();
while(d1.charAt(i)!='/' && i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
fg=false;
break;
}
} //d extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
  if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 )
{
j=0;
d = Integer.valueOf(temp);
i=i+1;
temp="";
while(d1.charAt(i)!='/' && i<d1.length() )
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
fg=false;
break;
}
} //m extraction
m = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);

if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 && m <=12 && 1<=m)
{
j=0;
//m = Integer.valueOf(temp);
//System.out.println(d);
i=i+1;
temp="";
while(i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
fg=false;
i++;
break;
//j++;

}
} //y extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
if(fg==true && j==4)
{
y = Integer.valueOf(temp);
fg=true;
}//end of else of y
else
fg=false;
if(fg==true)
{
if(y%400==0)
    lp=true;
else if (y%100==0)
    lp=false;
else if (y%4==0)
    lp=true;
else
    lp=false;
   
if(lp==false)
{
if(d>mdays[m-1] || d < 1 )
{
System.out.println("Invalid d of a m:");
fg=false;
}
}//non-lp y checking
else
{
if(d>ldays[m-1] || d < 1)
{
System.out.println("Invalid d of a m:");
fg=false;
}
} //lp y checking
} // checking for vaild date in months
}while(fg==false);
d1="";
d1=d+"/"+m+"/"+y;
for(int i=0;i<L.size();i++)
        {
        if(L.get(i).searchByAdmissionDate().equals(d1))
        L.get(i).displayAll();
        }
       
}
        System.out.println("Press 1 to display Records on basis of Departments:");
    System.out.println("Press 2 to display Records on basis of Scoring Criteria:");
    ch = in.nextInt();
    switch(ch)
    {
    case 1:
    System.out.println("Enter the Department name:");
    String dept = in.next();
    for(int i=0;i<L.size();i++)
        if(L.get(i).deptName().equals(dept))
        {
        System.out.println("Department name: "+dept+" Student Name: "+L.get(i).getName());
        }
    break;
    case 2:
    System.out.println("Enter the cut-off percentage:");
    double per = in.nextDouble();
    extendStudent S = new extendStudent();
    for(int i=0;i<n-1;i++)
    {
    for(int j=0;j<n-i-1;j++)
    {
    if(L.get(j).getPercentage()<L.get(j+1).getPercentage())
    {
    S=L.get(j);
    L.set(j,L.get(j+1));
    L.set(j+1,S);
    }
    }
    }
    System.out.println(" Sorted List is:");
    for(int i=0;i<n;i++)
    {
    System.out.println("Department name: "+L.get(i).deptName()+" Student Name: "+L.get(i).getName()+" Percentage Marks: "+L.get(i).getPercentage());
      }
   
    System.out.println("List after imposing the cut-off percentage:");
    for(int i=0;i<L.size();i++)
    {
    if(L.get(i).getPercentage()>=per)
      System.out.println("Department name: "+L.get(i).deptName()+" Student Name: "+L.get(i).getName()+" Percentage Marks: "+L.get(i).getPercentage());
    else
    {
    L.remove(i);
    r.gc();
    }
    }
   
    break;
    default:
    System.out.println("Wrong choice:");
    break;
    }
        in.close();
        System.out.println(" List Size after elimination is: "+L.size());
       
        System.out.println(" Total free memory after removing unwanted objects: "+r.freeMemory()+" Bytes");
    }
    }
class StudentRecord
{

StudentRecord()
{
roll=0;
name="";
co="";
for(int i:marks)
marks[i]=0;
}
public String searchByAdmissionDate()
{
return (dte);
}
public String getName()
{
return name;
}
public void resultBuild()
{
int sum=0;
System.out.println("----------------------------------------------------");
System.out.println("Name :"+name);
System.out.println("Course :"+co);
System.out.println("Roll Number :"+roll);
System.out.println("----------------------------------------------------");
for(int i=0;i<5;i++)
{
System.out.println("Subject "+(i+1)+" "+marks[i]);
sum +=marks[i];
}
System.out.println("Total Marks: "+sum);
System.out.println("Percentage: "+sum/5);
percentage= sum/5;
System.out.println("");
}
public void inputData()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name:");
name=in.nextLine();
System.out.println("Enter the roll number:");
roll=in.nextInt();
do {
flag=true;
int i=0;
int j=0;
String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
dte=in.next();
while(dte.charAt(i)!='/' && i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
flag=false;
break;
}
} //day extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
  if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 )
{
j=0;
day = Integer.valueOf(temp);
i=i+1;
temp="";
while(dte.charAt(i)!='/' && i<dte.length() )
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
flag=false;
break;
}
} //month extraction
month = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);

if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 && month <=12 && 1<=month)
{
j=0;
//month = Integer.valueOf(temp);
//System.out.println(day);
i=i+1;
temp="";
while(i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
flag=false;
i++;
break;
//j++;

}
} //year extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
if(flag==true && j==4)
{
year = Integer.valueOf(temp);
flag=true;
}//end of else of year
else
flag=false;
if(flag==true)
{
if(year%400==0)
    leap=true;
else if (year%100==0)
    leap=false;
else if (year%4==0)
    leap=true;
else
    leap=false;
   
if(leap==false)
{
if(day>mdays[month-1] || day < 1 )
{
System.out.println("Invalid day of a month:");
flag=false;
}
}//non-leap year checking
else
{
if(day>ldays[month-1] || day < 1)
{
System.out.println("Invalid day of a month:");
flag=false;
}
} //leap year checking
} // checking for vaild date in months
}while(flag==false);
dte="";
dte=day+"/"+month+"/"+year;
System.out.println("Enter the marks:");
for(int i=0;i<5;i++)
{
System.out.println("Enter the marks in Subject:"+(i+1));
marks[i]=in.nextInt();
}
}
public void setCourse(String S)
{
co=S;
}
public void displayAll()
{
System.out.println("Roll No. = "+roll+" Name= "+name+" Course ="+co);
System.out.println("Admission date: "+dte);
for(int i=0;i<5;i++)
{
System.out.println("Marks in Subject "+(i+1)+"= "+marks[i]);
}
System.out.println("");
}
public double getPercentage()
{
return percentage;
}
private int roll;
private String name;
private String co;
private String dte;
private boolean flag=true;
private boolean leap;
private double percentage;
private int day;
private int month;
private int year;
int[] marks = new int[5];
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
}
class extendStudent extends StudentRecord
{
public extendStudent()
{
super();
Department="";
}
public void inputData()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the department:");
Department = in.nextLine();
super.inputData();
}
public String deptName()
{
return Department;
}
public double getPercentage()
{
return super.getPercentage();
}

private String Department;
}
class Course
{
Course()
{
seats=new int[3];
seats[0]=0;
seats[1]=0;
seats[2]=0;
}
public String getCourse(String S)
{
for(int i=0;i<3;i++)
if(S.equalsIgnoreCase(course[i]))
{
seats[i]++;
return course[i];
}
return "XX";
}
public int getSeat(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return seats[i];
}
return -9;
}
public int getSeatLimit(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return fixSeat[i];
}
return -9;
}
private static String[] course = {"Graduation", "PG", "PhD"};
private static int[] fixSeat={3,2,1};
private int[] seats;
}


Simple Java Program to Demonstrate Class and Objects

Design a STUDENT class to store roll,name,admission date and marks in 5 subjects taken from user. Create an array of STUDENT objects. Provide methods corresponding to admission date and receiving marks, preparing mark sheet. Support also must be there to show number of students who have taken admission.





import java.util.*;
public class Student
    {
    public static void main(String[] args)
    {
      int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
int ch,n;
String d1;
boolean lp,fg=true;
    Scanner in = new Scanner(System.in);
        StudentRecord[] students = new StudentRecord[6];
        Course co = new Course();
        System.out.println("Courses available are Graduation, PG and PhD with 3,2 and 1 seats respectively:");
        for(int i=0;i<6;i++)
    students[i]=new StudentRecord();
    String choice1="YES";
    int k=0;
    do
    {
   
    System.out.println("\n Enter the course name:");
    String S = in.next();
    if(co.getCourse(S).equals("XX"))
    {
    System.out.println("Invalid course:");
    continue;
    }
    if(co.getSeat(S)>co.getSeatLimit(S))
    {
    System.out.println("Seats for the course "+S+" is exhausted:");
    continue;
    }
      System.out.println("Enter the Record for student :"+(k+1));
      students[k].setCourse(S);
    students[k].inputData();
    k++;
    System.out.println(" Want to add another record yes/no:");
    choice1 = in.next();
    }while("YES".equalsIgnoreCase(choice1));
    n=k;
    System.out.println("Press 1 to display Records of all STUDENTS:");
    System.out.println("Press 2 to see the result of all STUDENTS:");
    ch = in.nextInt();
    switch(ch)
    {
    case 1:
    for(int i=0;i<n;i++)
    students[i].displayAll();
    break;
    case 2:
        for(int i=0;i<n;i++)
        students[i].resultBuild();
        break;
        default:
        System.out.println("Wrong choice:");
        break;
        }
        System.out.println(" Want to search by Admission Date(dd/mm/yyyy)? yes/no:");
        String choice = in.next();
        if("YES".equalsIgnoreCase(choice))
        {
        int d=0,m=0,y=0;
         
          do {
fg=true;
int i=0;
int j=0;

String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
d1=in.next();
while(d1.charAt(i)!='/' && i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
fg=false;
break;
}
} //d extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
  if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 )
{
j=0;
d = Integer.valueOf(temp);
i=i+1;
temp="";
while(d1.charAt(i)!='/' && i<d1.length() )
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
fg=false;
break;
}
} //m extraction
m = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);

if(fg==true && j<=2 && d1.charAt(i)=='/' && j >0 && m <=12 && 1<=m)
{
j=0;
//m = Integer.valueOf(temp);
//System.out.println(d);
i=i+1;
temp="";
while(i<d1.length())
{
if(0 <= d1.charAt(i)-48 && d1.charAt(i)-48 <= 9 )
{
temp=temp+d1.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
fg=false;
i++;
break;
//j++;

}
} //y extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"fg="+fg);
if(fg==true && j==4)
{
y = Integer.valueOf(temp);
fg=true;
}//end of else of y
else
fg=false;
if(fg==true)
{
if(y%400==0)
    lp=true;
else if (y%100==0)
    lp=false;
else if (y%4==0)
    lp=true;
else
    lp=false;
   
if(lp==false)
{
if(d>mdays[m-1] || d < 1 )
{
System.out.println("Invalid d of a m:");
fg=false;
}
}//non-lp y checking
else
{
if(d>ldays[m-1] || d < 1)
{
System.out.println("Invalid d of a m:");
fg=false;
}
} //lp y checking
} // checking for vaild date in months
}while(fg==false);
d1="";
d1=d+"/"+m+"/"+y;
for(int i=0;i<n;i++)
        {
        if(students[i].searchByAdmissionDate().equals(d1))
        students[i].displayAll();
        }
       
}
         
        in.close();
    }
    }
class Course
{
Course()
{
seats=new int[3];
seats[0]=0;
seats[1]=0;
seats[2]=0;
}
public String getCourse(String S)
{
for(int i=0;i<3;i++)
if(S.equalsIgnoreCase(course[i]))
{
seats[i]++;
return course[i];
}
return "XX";
}
public int getSeat(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return seats[i];
}
return -9;
}
public int getSeatLimit(String S)
{
for(int i=0;i<3;i++)
{
if(S.equalsIgnoreCase(course[i]))
return fixSeat[i];
}
return -9;
}
private static String[] course = {"Graduation", "PG", "PhD"};
private static int[] fixSeat={3,2,1};
private int[] seats;
}
class StudentRecord
{

StudentRecord()
{
roll=0;
name="";
co="";
for(int i:marks)
marks[i]=0;
}
public String searchByAdmissionDate()
{
return (dte);
}
public void resultBuild()
{
int sum=0;
System.out.println("Name :"+name);
System.out.println("Course :"+co);
System.out.println("Roll Number :"+roll);
System.out.println("------------------------------------------");
for(int i=0;i<5;i++)
{
System.out.println("Subject "+(i+1)+" "+marks[i]);
sum +=marks[i];
}
System.out.println("Total Marks: "+sum);
System.out.println("Percentage: "+sum/5);
System.out.println("");
}
public void inputData()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the name:");
name=in.nextLine();
System.out.println("Enter the roll number:");
roll=in.nextInt();
do {
flag=true;
int i=0;
int j=0;
String temp ="";
System.out.println("Enter the admission date(dd/mm/yyyy):");
dte=in.next();
while(dte.charAt(i)!='/' && i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
flag=false;
break;
}
} //day extraction
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
  if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 )
{
j=0;
day = Integer.valueOf(temp);
i=i+1;
temp="";
while(dte.charAt(i)!='/' && i<dte.length() )
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
i++;
//j++;
flag=false;
break;
}
} //month extraction
month = Integer.valueOf(temp);
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);

if(flag==true && j<=2 && dte.charAt(i)=='/' && j >0 && month <=12 && 1<=month)
{
j=0;
//month = Integer.valueOf(temp);
//System.out.println(day);
i=i+1;
temp="";
while(i<dte.length())
{
if(0 <= dte.charAt(i)-48 && dte.charAt(i)-48 <= 9 )
{
temp=temp+dte.charAt(i);
i++;
j++;
}
else
{
System.out.println("Incorrect format:");
flag=false;
i++;
break;
//j++;

}
} //year extraction
}
//System.out.println("Temp="+temp+"I="+i+"J="+j+"flag="+flag);
if(flag==true && j==4)
{
year = Integer.valueOf(temp);
flag=true;
}//end of else of year
else
flag=false;
if(flag==true)
{
if(year%400==0)
    leap=true;
else if (year%100==0)
    leap=false;
else if (year%4==0)
    leap=true;
else
    leap=false;
   
if(leap==false)
{
if(day>mdays[month-1] || day < 1 )
{
System.out.println("Invalid day of a month:");
flag=false;
}
}//non-leap year checking
else
{
if(day>ldays[month-1] || day < 1)
{
System.out.println("Invalid day of a month:");
flag=false;
}
} //leap year checking
} // checking for vaild date in months
}while(flag==false);
dte="";
dte=day+"/"+month+"/"+year;
System.out.println("Enter the marks:");
for(int i=0;i<5;i++)
{
System.out.println("Enter the marks in Subject:"+(i+1));
marks[i]=in.nextInt();
}
}
public void setCourse(String S)
{
co=S;
}
public void displayAll()
{
System.out.println("Roll No. = "+roll+" Name= "+name+" Course ="+co);
System.out.println("Admission date: "+dte);
for(int i=0;i<5;i++)
{
System.out.println("Marks in Subject "+(i+1)+"= "+marks[i]);
}
System.out.println("");
}
private int roll;
private String name;
private String co;
private String dte;
private boolean flag=true;
private boolean leap;
private int day;
private int month;
private int year;
int[] marks = new int[5];
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
}