Results 1 to 20 of 20
Like Tree4Likes
  • 1 Post By awinston
  • 1 Post By JosAH
  • 1 Post By awinston
  • 1 Post By JosAH

Thread: Problems with my objects - Incorrect Refferencing

  1. #1
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Problems with my objects - Incorrect Refferencing

    Could someone please explain to me how to refference multiple classes in my main method.

    My main program is as follows, all classes are working fine. But I cant figure out how to change my output.

    Java Code:
    import java.util.Scanner;
    
    public class DateTester{
    	public static void main(String[] args) {
    	
    	String date = "10/23/2009";
    	ComponentA monthObject = new ComponentA(date);
    	ComponentB dayObject = new ComponentB(date);
    	ComponentC yearObject = new ComponentC(date);
    	
    	
    	System.out.println("The date is: " + date);
    	System.out.println("The month is: " + date);
    	System.out.println("The day is: " + date);
    	System.out.println("The year is: " + date);
    This ofcourse prints:
    Java Code:
    The date is: 10/23/2009
    The month is: 10/23/2009
    The day is: 10/23/2009
    The year is: 10/23/2009
    But I want to change the output so it prints like this:
    Java Code:
    The date is: 10/23/2009
    The month is: 10
    The day is: 23
    The year is: 2009

    Please help, I know its something as simple as refferencing the classes correctly, im just not sure how.
    Can post up my individual classes if needed.

    Thanks heaps.
    Ben
    Last edited by gnarkill10; 08-12-2012 at 07:15 PM. Reason: removed comments for easier reading

  2. #2
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    wow thats embarrassing. Reference*

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Problems with my objects - Incorrect Refferencing

    Quote Originally Posted by gnarkill10 View Post
    Could someone please explain to me how to refference multiple classes in my main method.

    My main program is as follows, all classes are working fine. But I cant figure out how to change my output.

    Java Code:
    import java.util.Scanner;
    
    public class DateTester{
    	public static void main(String[] args) {
    	
    	String date = "10/23/2009";
    	ComponentA monthObject = new ComponentA(date);
    	ComponentB dayObject = new ComponentB(date);
    	ComponentC yearObject = new ComponentC(date);
    	
    	
    	System.out.println("The date is: " + date);
    	System.out.println("The month is: " + date);
    	System.out.println("The day is: " + date);
    	System.out.println("The year is: " + date);
    This ofcourse prints:
    Java Code:
    The date is: 10/23/2009
    The month is: 10/23/2009
    The day is: 10/23/2009
    The year is: 10/23/2009
    But I want to change the output so it prints like this:
    Java Code:
    The date is: 10/23/2009
    The month is: 10
    The day is: 23
    The year is: 2009

    Please help, I know its something as simple as refferencing the classes correctly, im just not sure how.
    Can post up my individual classes if needed.

    Thanks heaps.
    Ben
    You print date 4 times. You need to print monthObject, dayObject, and yearObject if you want their corresponding values to be outputted.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Yeh I thought that but I was getting the below when I run the program and thought I must have been doing something wrong.

    Java Code:
    The date is: 10/23/2009
    The month is: ComponentA@42e816
    The day is: ComponentB@9304b1
    The year is: ComponentC@190d11

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Problems with my objects - Incorrect Refferencing

    Quote Originally Posted by gnarkill10 View Post
    Yeh I thought that but I was getting the below when I run the program and thought I must have been doing something wrong.

    Java Code:
    The date is: 10/23/2009
    The month is: ComponentA@42e816
    The day is: ComponentB@9304b1
    The year is: ComponentC@190d11
    Have you over-rided the toString() method of ComponentA, ComponentB, and ComponentC?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Ahh ok, yeh I dont think so. These are my Component Classes.

    ComponentA
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the month, and return the  field**/
    public class ComponentA
    {
    	private String month;  //Variable to hold the value for month.
    	
    	//create the month constructor
    	public ComponentA(String dateStr)
    	{
    		//Create a string tokenizer object.
    		
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    		
    		//Extract the month token
    		month = strTokenizer.nextToken();
    		
    		System.out.println("This is the month Class");
    	}
    
    	//getmonth Method and return the month field
    		public String getMonth()
    		{
    			return month;
    		}
    	
    }
    ComponentB
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the day, and return the  field**/
    public class ComponentB
    {
    	private String day;  //Variable to hold the value for day.
    	
    	//create the day constructor
    	public ComponentB(String dateStr)
    	{
    		//Create a string tokenizer object.
    		
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    		
    		//Extract the day token
    		day = strTokenizer.nextToken();
    		
    		System.out.println("This is the day Class");
    	}
    
    	//getDay Method and return the day field
    		public String getDay()
    		{
    			return day;
    		}
    }
    ComponentC
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the year, and return the year field**/
    public class ComponentC
    {
    	private String year;  //Variable to hold the value for year.
    	
    	//create the year constructor
    	public ComponentC(String dateStr){
    		//Create a string tokenizer object.
    		
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    		
    		//Extract the year token
    		year = strTokenizer.nextToken();
    		
    		System.out.println("This is the Year Class");
    	}
    
    	//getYear Method and return the year field
    		public String getYear()
    		{
    			return year;
    		}
    }

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Problems with my objects - Incorrect Refferencing

    Yes, you need to over-ride the toString() method so that it returns what you want to be printed.
    gnarkill10 likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    sweet thanks man!

  9. #9
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Having problems again :/

    I referenced the this keyword, as follows:
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the month, and return the  field**/
    public class ComponentA
    {
    	private String month;  //Variable to hold the value for month.
    	
    	//create the month constructor
    	public ComponentA(String dateStr)
    	{
    		this.month = month;
    		//Create a string tokenizer object.
    		
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    		
    		//Extract the month token
    		month = strTokenizer.nextToken();
    		
    	}
    
    	//getmonth Method and return the month field
    		public String getMonth()
    		{
    			return month;
    		}
    }
    But when I wrote the toString method in my main, I couldn't work out how to reference it...
    Java Code:
    import java.util.Scanner;
    
    public class DateTester{
    	public static void main(String[] args) {
    	
    	String date = "10/23/2009";
    	ComponentA monthObject = new ComponentA(date);
    	ComponentB dayObject = new ComponentB(date);
    	ComponentC yearObject = new ComponentC(date);
    	
    	
    	System.out.println("The date is: " + date);
    	System.out.println("The month is: " + monthObject);
    	System.out.println("The day is: " + dayObject);
    	System.out.println("The year is: " + yearObject);
    	
    	}
    	
    	public String toString() {
    		return ("The month is: " + ComponentA.month);
    	}
    }

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Problems with my objects - Incorrect Refferencing

    For your ComponentA objects (the months) insert the following method:

    Java Code:
    public String toString() { return month; }
    That method will be called every time a String representation is needed for a ComponentA object. You can do similar things with the other classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Insert this into main, or into the class?

    It wont work if I put in my main method because I initialised the variable "month" in the class "ComponentA". Therefore I should reference this class somehow shouldn't I?

  12. #12
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    HAHAHA SICK ONE!

    I think I answered my own question...
    It wont work if I put in my main method because I initialised the variable "month" in the class "ComponentA".
    yeh derrr so put the method in the class you TWAT!

    Thanks heaps :)

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Problems with my objects - Incorrect Refferencing

    It is simple if you know the mechanism: anything that needs to know the String representation of an object calls its toString() method. When that method isn't overridden the toString() method of the Object class is called and you get those funny "MyClass@deadbeef" strings; otherwise the return value of your toString() value is used and (hopefully) it returns a more sensble value. Note that PrintStreams (such as System.out) call that method when they have to print an (any) object. Aren't virtual methods a wonderful thing?

    kind regards,

    Jos
    gnarkill10 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Problems with my objects - Incorrect Refferencing

    @JosAH
    What do you mean by "virtual method"?
    gnarkill10 likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  15. #15
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    :) haha im going to say yes.

    I wonder if you could help me with one more thing. My code is working correctly however dayObject and yearObject are printed to the screen the same as monthObject. I have used tokenizing in my classes, have I done it correctly?

    Output to the screen
    Java Code:
    The date is: 10/23/2009
    The month is: 10
    The day is: 10
    The year is: 10
    I want it to look like this:
    Java Code:
    The date is: 10/23/2009
    The month is: 10
    The day is: 23
    The year is: 2009

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default Re: Problems with my objects - Incorrect Refferencing

    Quote Originally Posted by awinston View Post
    @JosAH
    What do you mean by "virtual method"?
    A virtual method is any non-static and non-private method; it can be overridden or inherited.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default Re: Problems with my objects - Incorrect Refferencing

    The day is: 10
    The year is: 10
    Where does the "10" come from that is being printed? Look there to see why the desired values are not returned.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    I am having trouble understanding why I cannot grab the second number as I have listed a stringTokenizer object, not sure if I have done it correctly though.

    This is my main code:
    Java Code:
    import java.util.Scanner;
    
    public class DateTester{
    	public static void main(String[] args) {
    	
    	//Scanner keyboard = new Scanner(System.in);
    	String dateTest = "10/23/2009";
    	Date monthObject = new Date(dateTest);
    	Date dayObject = new Date(dateTest);
    	Date yearObject = new Date(dateTest);
    	
    	
    	System.out.println("The date is: " + dateTest);
    	System.out.println("The month is: " + monthObject);
    	System.out.println("The day is: " + dayObject);
    	System.out.println("The year is: " + yearObject);
    	}
    }
    This is my class file, I moved individual classes into one file. You may notice the commented out methods toward the bottom, not sure what I need to do here.

    Class code:
    Java Code:
    import java.util.StringTokenizer;
    
    /**Get the month, and return the  field**/
    public class Date
    {
    	private String month;  //Variable to hold the value for month.
    	private String day;  //Variable to hold the value for day.
    	private String year;  //Variable to hold the value for year.
    	
    	/**************************************************
    	***     This is the Month Exception				***
    	***************************************************/
    	
    	//create the month constructor
    	public Date(String dateStr)
    	{
    		this.month = month;
    		this.day = day;
    		this.year = year;
    		//Create a string tokenizer object.
    		//identify the delimeter for the string as "/"
    		StringTokenizer strTokenizer = new StringTokenizer(dateStr, "/");
    
    		
    		//Extract the month token
    		month = strTokenizer.nextToken();
    		//Extract the day token
    		day = strTokenizer.nextToken();
    		//Extract the year token
    		year = strTokenizer.nextToken();
    		
    	}
    
    
    	/*************************************************
    	**		getMonth, getDay, & getYear Methods		**
    	*************************************************/
    	
    	//getMonth Method and return the month field
    		public String getMonth()
    		{
    			return month;
    		}
    		public String toString() 
    		{
    			return month;
    		}
    	
    	/** Working on this section, this is commented out!! **
    	//getDay Method and return the day field
    		public String getDay()
    		{
    			return day;
    		}
    		
    	//getYear Method and return the year field
    		public String getYear()
    		{
    			return year;
    		}
    	
    	//toString Methods	
    		public String toString() 
    		{
    			return month;
    		}
    		
    		public String toString() 
    		{
    			return day;
    		}
    	
    		public String toString() 
    		{
    			return year;
    		}
    	**/
    }

  19. #19
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Ahh I worked it out, I had three objects in main when I only needed one. On to the next goal :)

  20. #20
    gnarkill10 is offline Member
    Join Date
    Aug 2012
    Location
    Tamworth, Australia
    Posts
    42
    Rep Power
    0

    Default Re: Problems with my objects - Incorrect Refferencing

    Im so excited, my first assignment that I have almost fully completed. Just need to implement the exceptions!! Thank you so much for your help everyone, and not just doing it for me :)

Similar Threads

  1. Incorrect information?
    By santa in forum New To Java
    Replies: 8
    Last Post: 02-27-2012, 01:35 AM
  2. Incorrect formulas
    By kprofgold in forum New To Java
    Replies: 46
    Last Post: 01-31-2012, 03:27 PM
  3. Refferencing a string to an existing object
    By Aaron D in forum New To Java
    Replies: 7
    Last Post: 04-21-2011, 04:32 PM
  4. problems generating random objects
    By mandangalo in forum New To Java
    Replies: 8
    Last Post: 07-30-2010, 04:42 AM
  5. What in my function incorrect?
    By artemff in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 04:25 PM

Tags for this Thread

Posting Permissions

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