Advertisement
Hydrase

Array of Objects (Student Details)

Jul 3rd, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class student {
  4. int s_no;
  5. String n;
  6. float total = 0.0f;
  7. float marks[] = new float[3];
  8.  
  9. void getdata(Scanner sc) {
  10. System.out.print("Student name: ");
  11. n = sc.nextLine();
  12.  
  13. System.out.print("Roll no: ");
  14. s_no = sc.nextInt();
  15.  
  16. System.out.println("Enter marks of 3 subjects:");
  17. for (int i = 0; i < 3; i++) {
  18. System.out.print("Subject " + (i + 1) + ": ");
  19. marks[i] = sc.nextFloat();
  20. total += marks[i];
  21. }
  22. sc.nextLine();
  23. }
  24.  
  25. void display() {
  26. System.out.print(s_no + "\t" + n + "\t");
  27. for (int i = 0; i < 3; i++) {
  28. System.out.print(marks[i] + "\t");
  29. }
  30. System.out.println(total);
  31. }
  32.  
  33. public static void main(String args[]) {
  34. Scanner sc = new Scanner(System.in);
  35.  
  36. System.out.print("Enter the number of students: ");
  37. int n = sc.nextInt();
  38. sc.nextLine();
  39.  
  40. student s[] = new student[n];
  41.  
  42. for (int i = 0; i < n; i++) {
  43. System.out.println("\nEnter the details of student " + (i + 1));
  44. s[i] = new student();
  45. s[i].getdata(sc);
  46. }
  47.  
  48. System.out.println("\nSno\tName\tMark1\tMark2\tMark3\tTotal");
  49. System.out.println("------------------------------------------------------------");
  50.  
  51. for (int i = 0; i < n; i++) {
  52. s[i].display();
  53. }
  54.  
  55. sc.close();
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement