Design and implement the class Day that implements the day ofthe week in a program.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skykier
    New Member
    • Apr 2014
    • 3

    Design and implement the class Day that implements the day ofthe week in a program.

    Design and implement the class Day that implements the day ofthe week in a program. The class Day should store the day,such as Sun for Sunday. The program should be able to performthe following operations on the 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.. Such as if today is monday and we add four daysit will return Friday
    g. add the appropriate constructors
    h.write the definitions of the methods to implement a -g
    i.write a program to test the operations of class Day.

    i have this codes but i cant make the previous, next and adding days methods to work

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication5;
    
    import java.util.Scanner;
    
    /**
     *
     * @author paciente
     */
    public final class Day {
    
        private int Day;
        private String Days;
        
      
      
        @Override
        public String toString()
        {
            return (Days);
        }
        public void setDay(int Day)
        {
            if (Day == 0)
               Days = "Sun";
            if (Day == 1)
               Days = "Mon";
            if (Day == 2)
               Days = "Tue";
            if (Day == 3)
               Days = "Wed";
            if (Day == 4)
               Days = "Thur";
            if (Day == 5)
               Days = "Fri";
            if (Day == 6)
               Days = "Sat";
         }
         public Day setNameDay(String Day)
         {
             Day = Days;        
             return this;
         }
       
         public void printDay()
         {      
                System.out.println(Days);
         }
        
        
        
         public void previousDay()
         {
             
             
         }
            
          public void nextDay()
         {        
           Day=Day+=2;
           
            setDay(Day);
            printDay();
            
    //         if (Day <1)
    //            Day = 1;
             
            
         }
        
         public void calculateDay()
         {
             int calc = 0;
             int dayAdd =0;
            
             Scanner scanner = new Scanner(System.in);
             System.out.println("Enter number of days to add: ");
             calc =scanner.nextInt();
            
             dayAdd = Day +(calc);
            
             Day = dayAdd %7;
            
            setDay(Day);
            printDay();
         }
         
        public Day()
        {
            setDay(0);
        }
        public Day (int Day)
        {
            setDay(Day);
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("What is your initial day?\nSunday\t\t=\t0\nMonday\t\t=\t1\nTuesday\t\t=\t2\nWednesday\t=\t3\nThursday\t=\t4\nFriday\t\t=\t5\nSaturday\t=\t6");
            System.out.print("Enter the number:");
            Scanner sc = new Scanner(System.in);
            int x= sc.nextInt();
            Day myDay = new Day(x);
            System.out.println("");
            
            System.out.print("The day of the week is: ");
            myDay.printDay();
           System.out.println();
           
           System.out.print("The previous day is: ");
            myDay.previousDay();
            
           System.out.println();
           
           System.out.print("The next day is: ");
            myDay.nextDay();
           
           System.out.println();
           
           myDay.calculateDay();
           System.out.println();
        }
    }
    Last edited by Rabbit; Apr 10 '14, 03:12 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    First, don't name the class and the variable the same!
    Java coding conventions tells you to use camelCase for variables.
    So in Line 15, rename "Day" to "dayNumber" .
    Also rename "Days" to dayName", that will give you less confusion.

    Now up to the adding: read about the modulo operator "%".
    For example (5 + 4) % 7 is 2. That means if you add 4 days to Friday(dayNumbe r of Friday is 5), you will get dayNumber 2 which means guess what?

    The next and previous day are just computed by adding or subtracting 1 to the dayNumber (Of course don't forget to use the modulo operator here, too). You are calculating the next day with "Day=Day+=2 ;", but that makes no sense. Change it to "dayNumber = dayNumber + 1" or to the following expressions which are doing the same: "dayNumber +=1" or "dayNumber+ +".
    Think about NOT modifying the current day this way, else you can't re-use it.
    You want to make a new Day and leave the old day as it is.
    So calculate the new day number and get a new Day instance with "Day nextDay = new Day(newDayNumbe r)". Then you can use the printDay() method of this new day instance.

    Comment

    • skykier
      New Member
      • Apr 2014
      • 3

      #3
      in your reply
      "You want to make a new Day and leave the old day as it is.
      So calculate the new day number and get a new Day instance with "Day nextDay = new Day(newDayNumbe r)". Then you can use the printDay() method of this new day instance."


      i cant understand it, where should i apply that one

      and i change the codes under the methods next and previous days yet the answer is not correct :(

      Code:
      public void previousDay()
           {        
               dayNumber=(dayNumber-1)%7;     
              setDay(dayNumber);
              printDay(); 
           }
       
            public void nextDay()
           {   
              
             dayNumber=(dayNumber+1)%7; 
              setDay(dayNumber);
              printDay();
           }
      public void calculateDay()
           {
               int calc = 0;
               int dayAdd =0;
       
               Scanner scanner = new Scanner(System.in);
               System.out.println("Enter number of days to add: ");
               calc =scanner.nextInt();
       
               dayAdd = dayNumber +(calc);
       
               dayNumber = dayAdd %7;
       
              setDay(dayNumber);
              printDay();
           }

      this is the output (i chose 1 or monday)

      What is your initial day?
      Sunday = 0
      Monday = 1
      Tuesday = 2
      Wednesday = 3
      Thursday = 4
      Friday = 5
      Saturday = 6


      Enter the number: 1

      The day of the week is: Mon

      The next day is: Mon

      The previous day is: Sun

      Enter number of days to add:
      1
      Mon

      BUILD SUCCESSFUL (total time: 11 seconds)

      Comment

      • skykier
        New Member
        • Apr 2014
        • 3

        #4
        in your reply
        "You want to make a new Day and leave the old day as it is.
        So calculate the new day number and get a new Day instance with "Day nextDay = new Day(newDayNumbe r)". Then you can use the printDay() method of this new day instance."


        i cant understand it, where should i apply that one

        and i change the codes under the methods next and previous days yet the answer is not correct :(

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          Apply it to line 11 which states:
          "dayNumber=(day Number+1)%7;"
          Line 11 sets the dayNumber of the current day. If you use "dayNumber= ..." it means "this.dayNumber =...", that means the dayNumber of the current day. So if the current day is Monday, you set it to Tuesday! And it stays Tuesday, so when you call previousDay(), you get Monday (instead of Sunday). That's what I mean with "You want to make a new Day and leave the old day as it is.". Notice that the variable name "newDayNumb er" is different from "dayNumber" ?
          So what I mean is, change line 11 to "var newDayNumber=(d ayNumber-1)%7;".
          Now you have calculated the next day as a number, but you need it as an object (that means instance of Day)
          So add "Day nextDay = new Day(newDayNumbe r)" below line 11.
          Now you can use "printDay() " in line 13 which prints the current day, but not the new day, or you use "nextDay.printD ay()" there, which prints the new day, which is what you want. That's what I meant with "Then you can use the printDay() method of this new day instance."
          Delete line 12 "setDay(dayNumb er)", because it will manipulate the current day, which you don't want if you need to re-use it later on.

          Do the same with the previousDay() method.

          Comment

          Working...