• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Assignment Please  RSS feed

 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, this is my first post here. Hopefully, I'm posting this properly. I'm a new student learning Java on BlueJ. I would like help with this assignment. We are learning object interaction, abstraction, and modularization. This Java project has a main class called ClockDisplay. And there is NumberDisplay which we can't touch. This task is to create a class called DayDisplay whose only instance variable is a NumberDisplay object called dayNumber. There are also symbolic constants I need that was provided. There are also 4 constructors needed: a default, "public DayDisplay(int theDayNumber)", "publicDayDisplay(String nameOfTheDay)", and "public DayDisplay(NumberDisplay day)" that simply assigns the parameter to the dayNumber instance variable.
Further on, the instructions continue: "The DayDisplay class must have the following methods(note: use the dayNumber object's getValue() method:

public String getDayOfTheWeek();             //e.g. returns "Friday" if dayNumber is 5
public void incrementDay();                     //calls dayNumber's increment() method
public String getWhatDayIsTomorrow();    //e.g. returns "Saturday" if dayNumber is 5

Also add the following method: public void printDetails(): Which prints in the following format, exactly:

"Today is day 5 which is Friday!"

Test your project by compiling your class and then creating a DayDisplay object. Use the BlueJ inspector to check the contents of the field(right click the red-box object at the the bottom of the screen, in the object bench). Call each method to be sure it is doing what you expect.

Ok so as you can see below, I'm not done yet but when I compile, this is the warning I get: "constructor DayDisplay(NumberDisplay) is already defined in class DayDisplay"(specifically line 105). This is my fourth constructor, but I entered what was asked of me. There is something I'm missing. Can you help? Thanks gang!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"constructor DayDisplay(NumberDisplay) is already defined in class DayDisplay"(specifically line 105)

The instructions ask for these 4 constructors:
1) a default,
2) public DayDisplay(int theDayNumber)
3) publicDayDisplay(String nameOfTheDay)
4) public DayDisplay(NumberDisplay day)

Match each of the 4 with a line number in the code where it is defined.  Are any missing?
The compiler sees 2 constructors for required # 4)
 
Harry Peters
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norm, thanks for the reply. As for the constructors:

default: line 36
second constructor: line 45
third constructor: line 61
fourth constructor: line 105

And I figured there was a repeated constructor but the instructions told me to write them that way. Maybe there was something I was supposed to catch in what my instructor was asking for: "The fourth constructor is public DayDisplay(NumberDisplay day) and simply assigns the parameter to the dayNumber instance variable."
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pendatic]

It is not correct to refer to an explicit constructor that does not have any arguments as a "default" constructor.

A default constructor is one that is provided automatically by the compiler when there is no explicit constructor declared for the class. The default constructor that Java provides has no arguments so it is often referred to as the "default no-argument constructor."

If you explicitly provide a constructor that takes no arguments, it is correct to refer to it as a "no-argument" constructor but incorrect to call it the "default" constructor.

[/pendantic]
 
Susan Hart
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susan Hart wrote:I do not understand

Welcome to the Ranch!

Can you be more specific about what you don't understand?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Neal Parrett wrote:. . . There are also 4 constructors needed: a default, "public DayDisplay(int theDayNumber)", "publicDayDisplay(String nameOfTheDay)", and "public DayDisplay(NumberDisplay day)" . . .
Apart from the fact that is it impossible to have a default constructor in parallel with another constructor, that requirement looks surprisingly difficult to implement. You are going to have to pass
  • A number, e.g. 3. Really easy to implement.
  • Nothing. Quite easy to implement. Simply guess a default day of the week Don't leave that constructor empty. Initialise your field to something.
  • A NumberDisplay instance. Quite easy to implement if it has a getDayNumber() method
  • A day name. Rather awkward to implement. Have you not come across arrays yet? Don't you know how to write a switch-case statement? Look in the Java Tutorials. You can use the day names directly after case.
  • Your no‑arguments constructor and your constructor taking NumberDisplay don't initialise the field. That is potentially dangerous; you can crash your app.
    There are all sorts of other things I can see which I am not happy about, but sort out those few things first and consider the other things later.
     
    Harry Peters
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Campbell, thanks for the reply. So as far as "default" constructors, this is what our instructor calls them and has us implement in every lab assignment and in exactly that way. And we haven't come across arrays yet but definitely switch-case statements. Our instructions don't mention it but I'll keep at this and see how I can make it work.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Look at your comment on line 40 and compare it with the code you wrote on line 44. Does the comment correctly reflect what the code does? Now, look at the constructor declared on line 105 and compare it with what you did on line 44. Can you see why the compiler is complaining?
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your instructor is mistaken in referring to an explicit no-argument constructor as a default constructor. The default constructor is what the compiler will provide by default when no explicit constructor is defined in the class. Just keep that in mind. I would bring up that point with him so he can correct himself.
     
    Harry Peters
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok I seemed to have worked through my constructors and now I'm almost done with this first half of the lab assignment. Now I need to increment the day with a void incrementDay() method which calls dayNumber's increment() method. I also then need to make a "public String getWhatDayIsTomorrow()" to return "Saturday" if the dayNumber is 5. So I wonder if did the incrementDay method right. And then I need help with the get tomorrow method. How would I do it(basically lines 139 to 151)? My end goal is line 155 which is print "Today is day 5 which is Friday!" I updated my class below:
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Lines 51-52 are pointless. Why do you think you need to set the parameter to 0?
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Make sure not to take "return 'Saturday' if dayNumber is 5" too literally. That's just an example. If dayNumber is anything other than five, it still need to return the day after the current day.
     
    Harry Peters
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ya about lines 51 and 52, the idea is if someone were to enter a false day like 8, then it is supposed to show zero. There is no eighth day of the week. Look at my symbolic constants. And about lines 148 to 151, I'm supposed to create a method that can tell me what tomorrow is with a day number. If 5, which is Friday, then the method would know its Saturday. Or 2, then Wednesday. That is an example. But I can't figure out how to do it. I'm guessing I have to properly increment day first on the method above it.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Neal Parrett wrote:Ya about lines 51 and 52, the idea is if someone were to enter a false day like 8, then it is supposed to show zero. There is no eighth day of the week. Look at my symbolic constants.

    You're missing my point. You accomplish nothing by setting the parameter value. The parameter is local to the method, anything you do to it will not be seen outside the method, so setting it to zero does not help you accomplish your goal. If your program somehow ends up showing zero as you intend, it's not because of the assignment of 0 to the parameter on line 51. You can see this for yourself by commenting out those lines; the behavior of your program will not change when you do that.

    Also, the more common practice for handling invalid values is to throw an IllegalArgumentException and keep the current value instead of changing it to some default. It would be quite surprising to have a DayDisplay object that represents Wednesday suddenly represent Sunday after somebody tries to change it to an invalid day.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Neal Parrett wrote:And about lines 148 to 151, I'm supposed to create a method that can tell me what tomorrow is with a day number. If 5, which is Friday, then the method would know its Saturday. Or 2, then Wednesday. That is an example. But I can't figure out how to do it. I'm guessing I have to properly increment day first on the method above it.

    Guessing is seldom a good strategy when it comes to writing correct programs; there are just way too many ways to get it wrong so the odds are against you being right. Your guess that you need to "properly increment day first on the method above it", for example, is wrong.

    Understanding what your code does will give you a better chance at getting it right.

    From what I can tell, you seem to have a fundamental misunderstanding of what the NumberDisplay(int) constructor does. These lines and others similar to it don't make sense:

    I would assume the following behavior from NumberDisplay:

    If this is so, then it makes no sense to pass NUMBER_OF_DAYS_IN_WEEK to the NumberDisplay constructor on line 66 only to immediately overwrite the value with something different on the very next line.  That's like saying "Ok, I'm creating you so you can display the number 7 but when somebody asks you what value you represent, tell them it's 0."  That does not make sense at all.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here are some observations about the DayDisplay class:

    1. It has a NumberDisplay instance variable, dayNumber. The NumberDisplay class can represent any number and is used internally by the DayDisplay class.
    2. DayDisplay objects represent information about days of the week specifically. Days are numbered 0-6 (Sunday-Saturday)
    3. An instance of DayDisplay will represent exactly one day of the week at any given moment.
    4. You can see which day of the week an instance of DayDisplay represents via its printDetails method.
    5. An instance of DayDisplay can be made to represent a different day of the week via the setValue() and increment() methods.
    6. The setValue() method makes an instance of DayDisplay represent a specific day of the week, regardless of what day it currently represents.
    7. The increment() method makes an instance of DayDisplay represent the day of the week that comes after the day it currently represents.
    8. The getWhatDayIsTomorrow() method of a DayDisplay object returns the name of the day after the one that it currently represents. Calling the getWhatDayIsTomorrow() method will NOT change the day the DayDisplay object represents.

    In point 8 above, this means this is the behavior that you should expect:

    That is, it's reasonable to expect a DayDisplay object not to change which day it represents when you call its getWhatDayIsTomorrow() method. It would not make sense otherwise.

    Likewise, it would be reasonable to expect this behavior:

    Every call to increment() will make a DayDisplay object represent the day after the one it currently represents.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Regarding your getDayOfTheWeek() method, its behavior is not very consistent.  If the dayNumber is valid, it will return the name of the day that's represented. However, if the dayNumber is not valid, it will return "0" -- so "Sunday", "Monday", ... "Saturday" if valid but "0" if not valid. That's not consistent. Something like "Invalid day" would make more sense, IMO.

    At any rate, your class should validate any values before using them to set its internal state so a method like getDayOfTheWeek can operate under the assumption that the dayNumber instance variable is valid. In real-world practice, you'd throw a RuntimeException like IllegalStateException to flag that there's a programming/design error that needs to be addressed because getDayOfTheWeek tried to use an internal field that had an invalid value. 
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just noticed that line 107 has the same problem as line 51. Again, assigning a value to a parameter like that is pointless because it does absolutely nothing to change anything outside of that method.
     
    Harry Peters
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Junilu, thanks for taking the time for all of this. I'm still working at it. I can get it to compile now but can't call each method in the red box object because of a null pointer exception and its pointing at my getDayoftheWeek method starting at line 110. I'll see if I can make that work.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your dayNumber instance variable is null. Your constructors are the problem.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can also simplify the logic in the constructor that takes the name of the day as an argument. Since your symbolic constants are String objects, you can call String methods on them. That is, you can write the following without any chance of getting a NullPointerException:

    This will allow you to get rid of the initial check for null.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!