• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Assign a value to a numeric variable, then manipulate it, and return a new string.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

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!
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a good way to map (hint hint hint) a String value to a numeric value?
 
Michael Letendre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you suggest that I use something like



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.


Or would it be better to use something like this:

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem with using an enum is that you get constants, not ints. (Note: that enum won't compile.) I'd use a java.util.Map or an array.

String.equals() is a better choice. In your case, there is an even better kind of equals that is case-insensitive.
 
Michael Letendre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is along the lines of what I am looking for.
If I get the syntax correct it should look something like this
string.equalsIgnoreCase(value));

Which would be translated to (???)

string.equalsIgnoreCase(Monday));

I will need to look into using the map or array more. Everyone says to use an array, but I thought that it would not meet the requirements.

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Letendre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?"
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[1] so for thursday,
daysOfWeek[4], 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.
 
Michael Letendre
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 77894
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcoe to the Ranch, if it isn't too late

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!
 
Are we home yet? Wait, did we forget the tiny ad?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic