Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Angry 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.

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    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...

  3. #3
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Question

    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:

  4. #4
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    1st declare your GregorianCalendar class and set a date
    Java Code:
     String year  = "1987";
     String month = "08";
     String day   = "19";
    
     GregorianCalendar DOB = new GregorianCalendar(
         Integer.parseInt(year),
         Integer.parseInt(month) - 1,
         Integer.parseInt(day)
     );
    then use it in your class
    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

  5. #5
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    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

  6. #6
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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 value
    Last 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

  7. #7
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    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.

  8. #8
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Sorry for the late rply been bcy

    here i have modified your code:

    Java 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);
    		
    		}
    
    }
    and

    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

    Java Code:
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
    		Date dateOfBirth = new Date(this.dateOfBirth.getTimeInMillis());
    and this prints the date into the specified format

    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

  9. #9
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

Similar Threads

  1. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 PM
  2. Help with constructors
    By Minime in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 07:59 AM
  3. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM
  4. Passing objects in Java
    By jbostjr in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 10:15 PM
  5. Passing objects in Java
    By jbostjr in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •