Results 1 to 9 of 9
- 04-13-2009, 10:03 AM #1
Passing objects into constructors
Hi all first post for me as I'm a real beginer.
I have to make a student class with various data members all primative. The only problem is that one of the data members has to "Date of Birth" DOB and it has to be GregorianCalendar. What I don't understand is how I define this in the Student Class or in the Student Constructor.
How do I pass the Calendar object into the Student constructor. Anyway any help would be appreciated.
Aaron.
- 04-13-2009, 05:42 PM #2
If you need to pass it through the constructor you can go like this...
Java Code:public Concstructor(GregorianCalendar c) { //Do Stuff with Calendar... }Who Cares... As Long As It Works...
- 04-14-2009, 05:36 AM #3
Ok that didn't really make it any clearer to me.
Here is my Student Constructor without the DOB:
/************** Constructor *******************/
public Student(int studentNum, String firstName, String lastName,
String gender, String contactPhone,
int commenceYear)
{
stuNum = studentNum;
fName = firstName;
lName = lastName;
gendr = gender;
phone = contactPhone;
comYear = commenceYear;
}
/**************** Data Members *****************/
private int stuNum;
private String fName;
private String lName;
private String gendr;
private String phone;
private int comYear;
}
Now, as part of the constructor for the Student Class, I have to include a DOB, this has to be of GregorianCalendar object.
How do I define this in the Student Constructor? So when a new student object is created in another class it incorporates the DOB?
eg.
Student Aaron = new Student(12345678, "Aaron", "Simons", "male", "02 9568 7754", 2008, DOB);
How do I pass this (DOB) GregorianCalenser object into the Student constructor so the newly created instance has, as part of it, a feild called DOB that is a GregorianCalender object?
If someone could help me out with some actual code I would be forever greatful. Like I said I'm new to all this.
Aaron:confused:
- 04-14-2009, 05:43 AM #4
1st declare your GregorianCalendar class and set a date
then use it in your classJava Code:String year = "1987"; String month = "08"; String day = "19"; GregorianCalendar DOB = new GregorianCalendar( Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day) );
Java Code:Student Aaron = new Student(12345678, "Aaron", "Simons", "male", "02 9568 7754", 2008, DOB);
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-14-2009, 05:58 AM #5
Thanks for a quick reply. I have one more question, how would I then make the DOB different for each new Student object?
I want to be able to declare the DOB when I create a new Student Object, If I'm not mistaken doesn't your code set the DOB to a permanent value?
I understand how you code will use the GregorianCalendar object reference in the Student Constructor, but that doesn't really solve my problem, Each student object will have a different DOB, surely I don't have to declare a new DOB object for each instance of Student I create?
Anyway Thanks for helping me I thought I was going to have to go it alone, which isn't good.....:D
- 04-14-2009, 06:12 AM #6
you can create a new class GregorianCalendar then set a new values
String year = "1987"; --|
String month = "08"; | ---- set new value here
String day = "19"; --|
hmmm you do have to declare a new GregorianCalendar object in each student object you will create
GregorianCalendar is a class that needs to be properly initialize for it to contain your desired valueLast edited by azzaiel; 04-14-2009 at 06:19 AM.
It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-14-2009, 06:56 AM #7
This is getting crazy, now I'm more confused. Look I'll post my code for the Student Class:
import java.util.*;
public class Student
{
/********************* Methods ****************************/
/***** Student Number *****/
public void setStudNum(int stnum){
stuNum = stnum;
}
public int getStudNum(){
return stuNum;
}
/***** First Name *****/
public void setFName(String newFName)
{
fName = newFName;
}
public String getFName(){
return fName;
}
/***** Last Name *****/
public void setLName(String newLName)
{
lName = newLName;
}
public String getLName(){
return lName;
}
/***** Gender *****/
public void setGender(String newGender)
{
gendr = newGender;
}
public String getGender(){
return gendr;
}
/***** Day *****/
/***** Month *****/
/***** Year *****/
/***** Phone *****/
public void setPhone(String newPhone)
{
phone = newPhone;
}
public String getPhone(){
return phone;
}
/***** Commencement Year*****/
public void setComYear(int newComYear)
{
comYear = newComYear;
}
public int getComYear(){
return comYear;
}
public void printDetails(){
System.out.println("Student Number: " + stuNum);
System.out.println("First Name: " + fName);
System.out.println("Last Name: " + lName);
System.out.println("Gender: " + gendr);
System.out.println(c.DATE);
System.out.println("Phone number: " + phone);
System.out.println("year studies commenced: " + comYear);
}
/************** Constructor *******************/
public Student(int studentNum, String firstName, String lastName,
String gender, GregorianCalendar dob, String contactPhone,
int commenceYear)
{
stuNum = studentNum;
fName = firstName;
lName = lastName;
gendr = gender;
c = dob;
phone = contactPhone;
comYear = commenceYear;
}
/**************** Data Members *****************/
private int stuNum;
private String fName;
private String lName;
private String gendr;
private GregorianCalendar c;
private String phone;
private int comYear;
}
And here is the the second class "TestStudent"
import java.util.*;
public class TestStudent {
public static void main(String[] args) {
int year = 1976;
int month = 8;
int day = 30;
GregorianCalendar DOB = new GregorianCalendar(year, month, day);
Student aaron = new Student(12345678, "Aaron", "Simons", "male", DOB, "02 9568 7754", 2008);
aaron.printDetails();
}
}
I'm trying create an instance of student with a DOB in a separate class. When I print it does not recognise the DOB object. What the HELL have I done! I'm soooo lost.
Thanks for all your help so far.
- 04-14-2009, 11:27 AM #8
Sorry for the late rply been bcy
here i have modified your code:
andJava Code:import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; public class Student { private int studentNumber; private String firstName; private String lastName; private String gender; private GregorianCalendar dateOfBirth; private String phoneNumber; private int commencedYear; public Student() { super(); } public Student(int studentNumber, String firstName, String lastName, String gender, GregorianCalendar dateOfBirth, String phoneNumber, int comYear) { super(); this.studentNumber = studentNumber; this.firstName = firstName; this.lastName = lastName; this.gender = gender; this.dateOfBirth = dateOfBirth; this.phoneNumber = phoneNumber; this.commencedYear = comYear; } public int getStudentNumber() { return studentNumber; } public void setStudentNumber(int studentNumber) { this.studentNumber = studentNumber; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public GregorianCalendar getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(GregorianCalendar dateOfBirth) { this.dateOfBirth = dateOfBirth; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public int getComYear() { return commencedYear; } public void setComYear(int comYear) { this.commencedYear = comYear; } public void printDetails(){ SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy"); Date dateOfBirth = new Date(this.dateOfBirth.getTimeInMillis()); System.out.println("Student Number: " + this.studentNumber); System.out.println("First Name: " + this.firstName); System.out.println("Last Name: " + this.lastName); System.out.println("Gender: " + this.gender); System.out.println("Date Of birth: " + formatter.format(dateOfBirth)); System.out.println("Phone number: " + this.phoneNumber); System.out.println("year studies commenced: " + this.commencedYear); } }
Java Code:import java.util.GregorianCalendar; import com.imperim.splitter.model.Student; public class Runner { public static void main(String[] args) { int year = 1976; int month = 8; int day = 30; int studentNumber = 12345678; String firstName = "Aaron"; String lastName = "Simons"; String gender = "male"; GregorianCalendar dateOfBirth = new GregorianCalendar(year, month, day); String phoneNumber = "02 9568 7754"; int commencedYear = 2008; Student student = new Student(studentNumber, firstName, lastName, gender, dateOfBirth, phoneNumber, commencedYear); student.printDetails(); } }
so this code creates a formater and converts your gcalendar to date
and this prints the date into the specified formatJava Code:SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy"); Date dateOfBirth = new Date(this.dateOfBirth.getTimeInMillis());
Java Code:System.out.println("Date Of birth: " + formatter.format(dateOfBirth));It's easy to write a code that computers can understand...
... the challenge is to write a code that humans can understand
- 04-14-2009, 12:08 PM #9
Similar Threads
-
help with passing objects between classes
By aruna1 in forum New To JavaReplies: 7Last Post: 03-22-2009, 02:41 PM -
Help with constructors
By Minime in forum New To JavaReplies: 3Last Post: 04-09-2008, 07:59 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM -
Passing objects in Java
By jbostjr in forum New To JavaReplies: 1Last Post: 10-30-2007, 10:15 PM -
Passing objects in Java
By jbostjr in forum Advanced JavaReplies: 1Last Post: 10-30-2007, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks