Wednesday 8 January 2014

Java : Make a HashMap for the class Student. class Student { String name; Date dateOfBirth; Integer fees; }



Ques2.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ques2;

/**
 *
 * @author Sunshine
 */
public class Ques2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Student_HM_Demo s=new Student_HM_Demo();
        s.add(new Student("ravi",10,125));
        s.add(new Student("Kiran",907,12));
        //s.print();
    }
}


 Student .java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ques2;

/**
 *
 * @author Sunshine
 */
class Student {
     private String Name;
    private double DOB;
    private int Fee;

    public Student(String name, double dob, int fee) {
        this.Name = name;
        this.DOB = dob;
        this.Fee = fee;
    }
    // Getter-setter

    Student() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public String get_student_name() {
        return this.Name;
    }

    public double get_student_D0B() {
        return this.DOB;
    }

    public int get_student_fee() {
        return this.Fee;
    }

    public void set_student_name(String name) {
        this.Name = name;
    }

    public void set_student_D0B(double DOB) {
        this.DOB = DOB;
    }

    public void set_student_fee(int fee) {
        this.Fee = fee;
    }

    public String toString() {
        return "Student[+Name+DOB+Fee]";
    }
}

Student_HM_Demo.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ques2;

import java.util.HashMap;

/**
 *
 * @author Sunshine
 */
public class Student_HM_Demo {
     HashMap<String, Student> map;

    public Student_HM_Demo() {
        map = new HashMap<String, Student>();
    }

    public void add(Student obj) {
        map.put(obj.get_student_name(), obj);
    }

    public void print() {
        for (Student s : map.values()) {
            System.out.println(""+s.get_student_name());
            System.out.println(""+s.get_student_D0B());
            System.out.println(""+s.get_student_fee());
        }
    }
}

No comments:

Post a Comment