Advertisement
Shavit

P. 95 Ex. 11.14

Feb 18th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Classroom {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner in = new Scanner(System.in);
  11.         int quantity;
  12.        
  13.         System.out.printf("How many students are studying in your class? ");
  14.         quantity = in.nextInt();
  15.        
  16.         Student[] students = new Student[quantity];
  17.        
  18.         for(int i = 0; i < quantity; i++)
  19.         {
  20.             System.out.printf("Student No. %d:\n", i + 1);
  21.             System.out.printf("Name: ");
  22.             String currentName = in.next();
  23.             System.out.printf("Favorite subject: ");
  24.             String currentFavoriteSubject = in.next();
  25.             System.out.printf("Age: ");
  26.             int currentAge = in.nextInt();
  27.             students[i] = new Student(currentName, currentFavoriteSubject, currentAge);
  28.         }
  29.         System.out.printf("The \"special\" students are:\n");
  30.         for(int i = 0; i < quantity; i++)
  31.         {
  32.             boolean special = true;
  33.             for(int j = 0; j < quantity; j++)
  34.             {
  35.                 if(i == j)
  36.                     continue;
  37.                 if(students[i].getName().equals(students[j].getName()))
  38.                     special = false;
  39.             }
  40.             if(special)
  41.                 students[i].getData();
  42.         }
  43.         in.close();
  44.     }
  45. }
  46.  
  47. // Next class
  48.  
  49. public class Student
  50. {
  51.     private String name;
  52.     private String favoriteSubject;
  53.     private int age;
  54.    
  55.     public Student (String name, String favoriteSubject, int age)
  56.     {
  57.         this.name = name;
  58.         this.favoriteSubject = favoriteSubject;
  59.         this.age = age;
  60.     }
  61.    
  62.     public String getName()
  63.     {
  64.         return name;
  65.     }
  66.    
  67.     public void getData()
  68.     {
  69.         System.out.printf("___________________________\n");
  70.         System.out.printf("Name: %s\n", name);
  71.         System.out.printf("Favorite Subject: %s\n", favoriteSubject);
  72.         System.out.printf("Age: %d\n", age);
  73.         System.out.printf("___________________________\n");
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement