Q. Modify the above program to create n objects of the Student class. Accept details from the user for each object. Define a static method “sortStudent” which sorts the array on the basis of percentage.
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Student
{
int rollno;
String name;
float per;
static int count;
Student(){}
Student(String n,float p)
{
count++;
rollno=count;
name=n;
per=p;
}
void display()
{
System.out.println(rollno+"\t"+name+"\t"+per);
}
float getper()
{
return per;
}
static void counter()
{
System.out.println(count+" object is created");
}
public static void sortStudent(Student s[],int n)
{
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(s[j].getper()>s[j+1].getper())
{
Student t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
for(int i=0;i<n;i++)
s[i].display();
}
}
class Studentclass
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of Student:");
int n=Integer.parseInt(br.readLine());
Student p[]=new Student[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter Name:");
String name=br.readLine();
System.out.print("Enter percentage:");
float per=Float.parseFloat(br.readLine());
p[i]=new Student(name,per);
p[i].counter();
}
Student.sortStudent(p,Student.count);
}
}
Output:
Enter no. of Student:
3
Enter Name:amit
Enter percentage:37
1 object is created
Enter Name:suresh
Enter percentage:56
2 object is created
Enter Name:ramesh
Enter percentage:55
3 object is created
1 amit 37.0
3 ramesh 55.0
2 suresh 56.0 */
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Student
{
int rollno;
String name;
float per;
static int count;
Student(){}
Student(String n,float p)
{
count++;
rollno=count;
name=n;
per=p;
}
void display()
{
System.out.println(rollno+"\t"+name+"\t"+per);
}
float getper()
{
return per;
}
static void counter()
{
System.out.println(count+" object is created");
}
public static void sortStudent(Student s[],int n)
{
for(int i=n-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(s[j].getper()>s[j+1].getper())
{
Student t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
for(int i=0;i<n;i++)
s[i].display();
}
}
class Studentclass
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of Student:");
int n=Integer.parseInt(br.readLine());
Student p[]=new Student[n];
for(int i=0;i<n;i++)
{
System.out.print("Enter Name:");
String name=br.readLine();
System.out.print("Enter percentage:");
float per=Float.parseFloat(br.readLine());
p[i]=new Student(name,per);
p[i].counter();
}
Student.sortStudent(p,Student.count);
}
}
Output:
Enter no. of Student:
3
Enter Name:amit
Enter percentage:37
1 object is created
Enter Name:suresh
Enter percentage:56
2 object is created
Enter Name:ramesh
Enter percentage:55
3 object is created
1 amit 37.0
3 ramesh 55.0
2 suresh 56.0 */
1 Comments
Slip9_1: Define a “Clock” class that does the following ;
ReplyDeletea. Accept Hours, Minutes and Seconds
b. Check the validity of numbers
c. Set the time to AM/PM mode
Use the necessary constructors and methods to do the above task
import java.util.*;
class Clock
{
int hours,minutes,seconds;
Clock()
{
System.out.println("enter the time in HH MM SS format");
Scanner sc= new Scanner(System.in);
this.hours = sc.nextInt();
this.minutes = sc.nextInt();
this.seconds = sc.nextInt();
}
void isTimeValid()
{
if(hours>=0 && hours<24 && minutes>0 &&minutes<60
&&seconds>0 && seconds<60)
System.out.println("time is valid");
else
System.out.println("time is not valid");
}
void setTimeMode()
{
if(hours<12)
{
System.out.println("time ="
+hours+":"+minutes+":"+seconds +" AM");
}
else
hours = hours-12;
System.out.println("time ="
+hours+":"+minutes+":"+seconds +" PM");
}
public static void main(String args[])
{
Clock c = new Clock();
c.isTimeValid();
c.setTimeMode();
}
}