int assignment to object incompatibility issue - Java

This is a discussion on int assignment to object incompatibility issue - Java within the General Discussions forums, part of the Community Boards category; This isn't C related, but I figured there could be help here for this. I'm taking an objects oriented class ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    19

    int assignment to object incompatibility issue - Java

    This isn't C related, but I figured there could be help here for this.

    I'm taking an objects oriented class and have an asignment which instructs, in two different places "Create a class called DayDisplay whose only instance variable is a NumberDisplay object called dayNumber", and then later "The second constructor will expect an integer representing the day of the week to store in dayNumber (eg. 5): public DayDisplay(int theDayNumber). ... Use the parameter to initialize the dayNumber instance variable, but only if it is an integer between 0 and 6; otherwise set the day to 0".


    I believe I've done this, however, I'm receiving a conflict between NumberDisplay dayNumber (line 4) and int dayNumber (line 37) when I try to assign the latter to the former, as one is an int, and the other a NumberDisplay object. Help is appreciated, my code is as follows.


    Code:
    public class DayDisplay{
    
    
        private NumberDisplay dayNumber;
        
        public static final int NUMBER_OF_DAYS_IN_WEEK = 7;
        
        public static final int SUNDAY    = 0;
        public static final int MONDAY    = 1;
        public static final int TUESDAY   = 2;
        public static final int WEDNESDAY = 3;
        public static final int THURSDAY  = 4;
        public static final int FRIDAY    = 5;
        public static final int SATURDAY  = 6;
    
    
        public static final String SUNDAY_STRING    = "Sunday";
        public static final String MONDAY_STRING    = "Monday";
        public static final String TUESDAY_STRING   = "Tuesday";
        public static final String WEDNESDAY_STRING = "Wednesday";
        public static final String THURSDAY_STRING  = "Thursday";
        public static final String FRIDAY_STRING    = "Friday";
        public static final String SATURDAY_STRING  = "Saturday";
        
        
        /**
         * default constructor
         */
        public DayDisplay(){
        }
        
        /**
         * second constructor
         * 
         * @param theDayNumber
         */
        public DayDisplay(int dayNumber){
            if(dayNumber <= 6 && dayNumber >= 0){
                this.dayNumber = dayNumber;
            }else{
                dayNumber = 0;
            }
        }
        
        /**
         * third constructor
         */
        public DayDisplay(String dayOfWeek){
            if(dayOfWeek != null){
                if(dayOfWeek.equalsIgnoreCase(SUNDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SUNDAY);
                }else if(dayOfWeek.equalsIgnoreCase(MONDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(MONDAY);
                }else if(dayOfWeek.equalsIgnoreCase(TUESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(TUESDAY);
                }else if(dayOfWeek.equalsIgnoreCase(WEDNESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(WEDNESDAY);
                }else if(dayOfWeek.equalsIgnoreCase(THURSDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(THURSDAY);
                }else if(dayOfWeek.equalsIgnoreCase(FRIDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(FRIDAY);
                }else if(dayOfWeek.equalsIgnoreCase(SATURDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SATURDAY);
                }else{
                    dayNumber = 0;
                }
            }        
        }
        
        /**
         * fourth constructor
         */
        public DayDisplay(NumberDisplay day){
            dayNumber = day;
            
        }
    
    
    }
    Last edited by Delicieuxz; 11-15-2013 at 05:52 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,857
    You have two things with the same name. Rename one of them. Your problem description, if you look carefully, did encourage you to have them named differently.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Perhaps I am bad at looking, but I didn't see any reference to NumberDisplay being a standard Java thing. Is it a standard thing, or did you create it? Either way, presumably there is some method for setting its value, and you should then use that method.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    19
    The two same names shouldn't conflict, as the instance variable is references using this.dayNumber, wheres the local variable is simply referred to as dayNumber. The directions did encourage different naming, but after this the "this" keyword was introduced, and so now it has been taught that using "this" is proper practice.
    NumberDisplay is another custom class that this class references.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,857
    Quote Originally Posted by Delicieuxz View Post
    The two same names shouldn't conflict, as the instance variable is references using this.dayNumber, wheres the local variable is simply referred to as dayNumber.
    Yeah, but if you have a function argument named dayNumber in a class with a member named dayNumber, there is potentially ambiguity as you encountered. The "this." syntax in Java is one way of addressing that. Technically, using the "this." syntax IS simply a less ambiguous naming mechanism.

    Incidentally, "proper practice" is subject of opinion. Different people have different opinions about what is "proper".
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Delicieuxz View Post
    NumberDisplay is another custom class that this class references.
    So in that case, use the method that the NumberDisplay class provides to cram an integer down its throat.

    And grumpy's point can perhaps be summarized by "line 41 doesn't trigger an error because it's wrong" -- consider why line 41 doesn't trigger an error, and what that means about what is happening....

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    Let's see if I remember my Java...

    The conflict exists because you aren't obeying the variable type. The error goes away if you alter your constructor logic to take a NumberDisplay dayNumber, instead of an int dayNumber.

    Alternatively, take an int but call it something else.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    19
    It ended up being that this was the solution.

    Code:
    class DayDisplay{
        private NumberDisplay dayNumber;
    
    
        public static final int NUMBER_OF_DAYS_IN_WEEK = 7;
    
    
        public static final int SUNDAY      = 0;
        public static final int MONDAY      = 1;
        public static final int TUESDAY     = 2;
        public static final int WEDNESDAY   = 3;
        public static final int THURSDAY    = 4;
        public static final int FRIDAY      = 5;
        public static final int SATURDAY    = 6;
    
    
        public static final String SUNDAY_STRING    = "Sunday";
        public static final String MONDAY_STRING    = "Monday";
        public static final String TUESDAY_STRING   = "Tuesday";
        public static final String WEDNESDAY_STRING = "Wednesday";
        public static final String THURSDAY_STRING  = "Thursday";
        public static final String FRIDAY_STRING    = "Friday";
        public static final String SATURDAY_STRING  = "Saturday";
    
    
        /**
         * Default Constructor
         */
        public DayDisplay(){
            dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
        }
        
        /**
         * Another Constructor
         * @param theDayNumber - the number of the day
         */
        public DayDisplay(int theDayNumber){
            dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
            dayNumber.setValue(theDayNumber);
        }
        
        /**
         * Another Constructor
         * @param nameOfTheDay - the name of the day
         */
        public DayDisplay(String nameOfTheDay){
            if(nameOfTheDay !=null){
                if(nameOfTheDay.equalsIgnoreCase(SUNDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SUNDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(MONDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(MONDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(TUESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(TUESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(WEDNESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(WEDNESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(THURSDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(THURSDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(FRIDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(FRIDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(SATURDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SATURDAY);
                }else{
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(0);
                }
            }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    It looks like this:
    Code:
            if(nameOfTheDay !=null){
                if(nameOfTheDay.equalsIgnoreCase(SUNDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SUNDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(MONDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(MONDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(TUESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(TUESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(WEDNESDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(WEDNESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(THURSDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(THURSDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(FRIDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(FRIDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(SATURDAY_STRING)){
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(SATURDAY);
                }else{
                    dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                    dayNumber.setValue(0);
                }
            }
    can be simplified to:
    Code:
            if(nameOfTheDay !=null){
                dayNumber = new NumberDisplay(NUMBER_OF_DAYS_IN_WEEK);
                if(nameOfTheDay.equalsIgnoreCase(SUNDAY_STRING)){
                    dayNumber.setValue(SUNDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(MONDAY_STRING)){
                    dayNumber.setValue(MONDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(TUESDAY_STRING)){
                    dayNumber.setValue(TUESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(WEDNESDAY_STRING)){
                    dayNumber.setValue(WEDNESDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(THURSDAY_STRING)){
                    dayNumber.setValue(THURSDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(FRIDAY_STRING)){
                    dayNumber.setValue(FRIDAY);
                }else if(nameOfTheDay.equalsIgnoreCase(SATURDAY_STRING)){
                    dayNumber.setValue(SATURDAY);
                }else{
                    dayNumber.setValue(0);
                }
            }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    19
    That's a good increase of code cleanliness. Adopted, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with .so (Shared Object) Files
    By in_ship in forum C Programming
    Replies: 15
    Last Post: 09-16-2012, 07:57 AM
  2. Replies: 27
    Last Post: 09-07-2011, 08:39 AM
  3. Operator Overloading - RHS object is modified by assignment operation
    By IdioticCreation in forum C++ Programming
    Replies: 16
    Last Post: 12-30-2010, 06:33 AM
  4. g++ - .NET incompatibility??
    By alphaoide in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 12:26 PM
  5. JAVA issue driving me insane
    By axon in forum Tech Board
    Replies: 0
    Last Post: 02-23-2004, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21