Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. /**
  2.  * Student.
  3.  */
  4. public class Student {
  5.  
  6.     private int absences = 0;
  7.     private String name;
  8.     private String address;
  9.    
  10.     @Override
  11.     public boolean equals(Object o){
  12.         if(o == this){
  13.             return true;
  14.         }
  15.         if(!(o instanceof Student)){
  16.            return false;
  17.         }
  18.        
  19.         Student other = (Student) o;
  20.         return name == other.name && address == other.address;
  21.     }
  22.    
  23.     @Override
  24.     public int hashCode(){
  25.         int result = 7;
  26.         result = 31 * result + Double.valueOf(name.length()).hashCode();
  27.         result = 31 * result + Double.valueOf(address.length()).hashCode();
  28.         return result;
  29.     }
  30.    
  31.     /**
  32.      * Constructor for class Student
  33.      *
  34.      * @param name name of the student
  35.      * @param address address of the student
  36.      * @throws IllegalArgumentException if name or address are empty
  37.      */
  38.     public Student(String name, String address) {
  39.         if (name == null || name.isEmpty()) throw new IllegalArgumentException("name");
  40.         if (address == null || address.isEmpty()) throw new IllegalArgumentException("address");
  41.         this.name = name;
  42.         this.address = address;
  43.     }
  44.  
  45.     /**
  46.      * Add absence.
  47.      *
  48.      * @param hourse absence in hours
  49.      * @throws IllegalArgumentException if hours is <= 0
  50.      */
  51.     public void addAbsence(int hours){
  52.         if(hours <= 0)throw new IllegalArgumentException("hours <= 0");
  53.         this.absences += hours;
  54.     }
  55.    
  56.     /**
  57.      * @return absence (in hours)
  58.      */
  59.     public int getAbsences(){
  60.         return absences;
  61.     }
  62.    
  63.     /**
  64.      * @return name
  65.      */
  66.     public String getName() {
  67.         return name;
  68.     }
  69.    
  70.     /**
  71.      * @return address
  72.      */
  73.     public String getAddress() {
  74.         return address;
  75.     }
  76.    
  77.     @Override
  78.     public String toString(){
  79.         return name + " /" + address + "/: " + absences;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement