Hi,
I am facing a small problem in solving Lab 1014.
Below is my code:
*Student.java*
public class Student {
private int studentId = 1;
private static int studentCount;
public Student()
{
studentId++;
this.setStudentId(studentId);
}
public int getStudentId()
{
return studentId;
}
public void setStudentId(int Id)
{
this.studentId = Id;
}
private StudentRecord studentRecord;
public static int getStudentCount()
{
return studentCount;
}
public static void increaseStudentCount()
{
studentCount++;
}
public void setStudentName(Student s,String name)
{
studentRecord = new StudentRecord();
s.studentRecord.setName(name);
}
public String getStudentName()
{
return this.studentRecord.getName();
}
}
********************************************************************************************************
*Student.java*
public class StudentRecord {
private String name;
public String getName()
{
return name;
}
public void setName(String temp)
{
name = temp;
}
}
************************************************************************************************
StudentRecordExample.java (main)
public class StudentRecordExample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Student s1 = new Student();
s1.setStudentName(s1,"prathi");
Student.increaseStudentCount();
Student s2 = new Student();
s2.setStudentName(s2,"Manu");
Student.increaseStudentCount();
Student s3 = new Student();
s3.setStudentName(s3,"Shubha");
Student.increaseStudentCount();
System.out.println("The following is the list of students and their
respective details");
System.out.println("Student Name = " +s1.getStudentName());
System.out.println("Student Id = " +s1.getStudentId());
System.out.println("Student Name = " +s2.getStudentName());
System.out.println("Student Id = " +s2.getStudentId());
System.out.println("Student Name = " +s3.getStudentName());
System.out.println("Student Id = " +s3.getStudentId());
System.out.println("Total Number of Students = " +
Student.getStudentCount());
}
}
The output I get is the following
The following is the list of students and their respective details
Student Name = prathi
Student Id = *2*
Student Name = Manu
Student Id = *2*
Student Name = Shubha
Student Id =* 2*
Total Number of Students = 3
I tried to increment the StudentId in the constructor. But it gets
initialised every time I create a new object and that value gets
incremented. I am not sure where I went wrong !!! ... pls let me know
..what can be done.
Thanks in advance
regards,
Prathi
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---