Hello everyone, I have been lurking on here for a few weeks and have found a lot of the info to be helpful, so thank you.
I ran into a bit of an issue with one of my projects for a class. I am not looking for anyone to give me the answer but some of examples to a quick tutorial would be great!
I will put my code below, along with the requirements. At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.
Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:
A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
SO right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).
When I started to read your post I initially wanted to help you by giving you a straight shot answer. However, I decided otherwise. Instead, I would like to recommend that before you work on implementing your addDays method that you take a close look at your code. You may want to meander over to the String class in the Java API Documentation. There are methods in that class that will solve your case sensitivity issue and also some alternatives to comparing your user input String. There may also be a few other things in there that could help you quite a bit... You should post back with your improvements!
Again, welcome to the Ranch and I look forward to seeing your results!
I was originally looking to see if I could set values in the set day block
if((day.compareTo("sunday") == 0) || (day.compareTo("Sun") == 0)) and change the numeric value, howeve I was not sure if that would work.
My experience with instructors is that it is rare for them to fault you for learning something new in order to complete a project. It's true that you *should* be able to do it just with what was taught in class.
All things are lawful, but not all things are profitable.
I had asked about using an array and the response that I received was :
"This is considered a requirement and you'll have to think about what type would support a string of characters "
But perhaps I interpreted it wrong, looking back at my question I had asked
"At first I was looking at the final program and thinking ok easy enough, assign each of the days to a number and then use math to add or subtract, then get the answer to the math problem to be able to return the day that the program returns from the operation, however I was re reading the assignment and saw "The class Day should store the day, such as Sun for Sunday." so does this mean we have to store the day as Sun, Mon, Tues.... etc and not 1, 2, 3.... etc?"
Good job on finding a better method for your code. Just an idea...
You could do something where you use array notation to do the math.
This is, of course, if you decide to use array notation.
Let's say you're currently set at Monday and you want to get to
Thursday. And your array is something like:
Monday would be represented by daysOfWeek so for thursday,
daysOfWeek, you would add 3. daysOfWeek[1 + 3]. This way you
have a numeric representation of the day with array notation and
a String that also represents the day. There are a few ways to relate this to your code so it works well, too.
I just got home from work and received this email from my professor:
You have a few approaches below. My suggestion is to keep this very simple. You just need a way to map the String day with the integer value for a day. For example, if the dayAsString equals to "sun" then dayAsInt = 0, else if the dayAsString equals to "mon" then dayAsInt = 1, and so on.
First try to get a day set and printed before you go any further. Once you have the right integer value for the day, you can use a short algorithm to compute the new day value using mod 7."
Now can I change this part of my code to assign it a value?
Long blocks of if-elses are pretty bad. You had a really good idea earlier, that of using an enum, but you wrote it badly. Have a look at that link and look at the Planet example. You can do the same with Days and use SUNDAY(0), MONDAY(1) or similar. As well as that, read the Java Language Specification (=JLS) section about enums (and ignore the bits you can't understand:wink:) and the Enum class documentation. Every enum (small e) constant implicitly implements every method in Enum (big E). Note there is one which gives you its position in the declaration.
There is now a simpler way to get that sort of thing with an enum: look at this:-Then you can apply that to Days.
What's more, if you get that to work you can say you found a nice simple way by reading the documentation and the JLS!
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop