Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Exercise_Objects_and_Classes_4_Students {
  6.  
  7. static class Student {
  8.  
  9. private String firstName;
  10. private String lastName;
  11. private double grade;
  12.  
  13. public Student(String firstName, String lastName, double grade){
  14.  
  15. this.firstName = firstName;
  16. this.lastName = lastName;
  17. this.grade = grade;
  18. }
  19.  
  20. @Override
  21. public String toString() {
  22. return String.format("%s %s %.2f", this.firstName, this.lastName, this.grade);
  23. }
  24. }
  25.  
  26. public static void main(String[] args) {
  27.  
  28. Scanner scanner = new Scanner(System.in);
  29.  
  30. List<Student> students = new ArrayList<>();
  31. int number = Integer.parseInt(scanner.nextLine());
  32.  
  33. for (int i = 0; i < number; i++) {
  34. String[] tokens = scanner.nextLine().split("\\s+");
  35.  
  36. String firstName = tokens[0];
  37. String secondName = tokens[1];
  38. double grade = Double.parseDouble(tokens[2]);
  39.  
  40. Student student = new Student(firstName, secondName, grade);
  41. students.add(student);
  42. }
  43.  
  44.  
  45. for (Student student : students) {
  46.  
  47. System.out.println(student.toString());
  48. }
  49.  
  50.  
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement