Results 1 to 5 of 5

Thread: Need some tips.

  1. #1
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Need some tips.

    Hi I have some problems with express my self. I doing this kind of exercise: design the class Day that implements the day of week in a program. The program should store the day and be able to perform the following operation of type Day
    a. setDayOfWeek must not permit a change to an invalid value

    b. printDay prints the day from the receiving instance.

    c. getDayOfWeek returns the value of the dayOfWeek

    d. nextDay returns the String value of the day that comes after the receiving object.

    e. previousDay returns the String value of the day that comes before the receiving object.

    f. forwardDays returns the value of the day some integer days after the receiving object. What happens if the input parameter is negative?

    g. Default Day constructor sets dayOfWeek to Sunday. Day constructors must ensure that only valid day names are set.


    I'm interest if the program what I did to point of my knowledge it's good and I need some tips for the nextDay, previousDay and forwardDays. I think that I have to use a if statements.

    Thanks in advance
    This is what i did:

    package chapt08;

    public class Day {

    private static final String[] DAYS_OF_WEEK = { "Sunday", "Monday",
    "Tusday", "Wednesday", "Thursday", "Friday", "Saturday" };

    private String dayOfWeek;

    public Day() {
    dayOfWeek = DAYS_OF_WEEK[0];
    }

    public Day(String aString) {
    this();
    setDayOfWeek(aString);
    }

    public void setDayOfWeek(String dayName){

    for (int i = 0; i < DAYS_OF_WEEK.length; i++) {


    if(dayName.equals(DAYS_OF_WEEK[i]));
    }
    dayOfWeek = dayName;
    }


    public void printDay(){
    System.out.println("The day is " + dayOfWeek);
    }

    public String getDayOfWeek() {

    if (dayOfWeek == Sunday)
    System.out.println("Next Day is MONDAY");

    return dayOfWeek;
    }
    public previousDay(){

    }

    public forwardDays(){

    if (dayOfWeek

    }

    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    forwardDays , no matter is positive or not, % 7 to get the reminder, "add" the reminder to the day

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default == vs equals

    Java Code:
    if (dayOfWeek == Sunday)
    Don't use "==" to compare strings.... always use the string .equals() method:
    Java Code:
    if (dayOfWeek.equals("Sunday"))
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Thanks, but what to do for a forward day. I Do I have to set other array for forwardDays?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I would have my class contain an int variable that holds the numeric value of the current day of the week. I'd add my parameter int to this int and as mtyoung suggests, use mod 7 to get a realistic value. There is absolutely no need for a second array.

Similar Threads

  1. tips on crystal report
    By kishore in forum New To Java
    Replies: 1
    Last Post: 01-20-2009, 03:23 AM
  2. give interview tips....
    By thirumurugan.sethu in forum New To Java
    Replies: 4
    Last Post: 10-03-2008, 11:45 AM
  3. I need some advice and tips about my code...
    By sukatoa in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2008, 07:29 PM
  4. First project, need some tips..
    By Komala_aradhya in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 01:25 PM

Posting Permissions

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