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. Display details of all the students.
package lab0;
import java.util.Scanner;
class Ques_5 {
public static void main(String[] args) {
try {
String name;
int rollno = 1;
double percentage;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of students:");
int n = Integer.parseInt(sc.nextLine());
Student s[] = new Student[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter name of student" + (i + 1) + ":");
name = sc.nextLine();
System.out.print("Enter percentage of student" + (i + 1) + ":");
percentage = Double.parseDouble(sc.nextLine());
rollno = i + 1;
s[i] = new Student(name, rollno, percentage);
}
sc.close();
for (int i = 0; i < n; i++) {
s[i].showData();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class Student {
String name;
int rollno;
double percentage;
public Student(String Name, int roll, double per) {
name = Name;
rollno = roll;
percentage = per;
}
public void showData() {
System.out.println("Student Rollno : " + rollno + " Name : " + name + " Percentage : " + percentage);
}
}
import java.util.Scanner;
class Ques_5 {
public static void main(String[] args) {
try {
String name;
int rollno = 1;
double percentage;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of students:");
int n = Integer.parseInt(sc.nextLine());
Student s[] = new Student[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter name of student" + (i + 1) + ":");
name = sc.nextLine();
System.out.print("Enter percentage of student" + (i + 1) + ":");
percentage = Double.parseDouble(sc.nextLine());
rollno = i + 1;
s[i] = new Student(name, rollno, percentage);
}
sc.close();
for (int i = 0; i < n; i++) {
s[i].showData();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class Student {
String name;
int rollno;
double percentage;
public Student(String Name, int roll, double per) {
name = Name;
rollno = roll;
percentage = per;
}
public void showData() {
System.out.println("Student Rollno : " + rollno + " Name : " + name + " Percentage : " + percentage);
}
}
0 Comments