473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package javaapplication5;
  6.  
  7. import java.util.Scanner;
  8.  
  9. /**
  10.  *
  11.  * @author paciente
  12.  */
  13. public final class Day {
  14.  
  15.     private int Day;
  16.     private String Days;
  17.  
  18.  
  19.  
  20.     @Override
  21.     public String toString()
  22.     {
  23.         return (Days);
  24.     }
  25.     public void setDay(int Day)
  26.     {
  27.         if (Day == 0)
  28.            Days = "Sun";
  29.         if (Day == 1)
  30.            Days = "Mon";
  31.         if (Day == 2)
  32.            Days = "Tue";
  33.         if (Day == 3)
  34.            Days = "Wed";
  35.         if (Day == 4)
  36.            Days = "Thur";
  37.         if (Day == 5)
  38.            Days = "Fri";
  39.         if (Day == 6)
  40.            Days = "Sat";
  41.      }
  42.      public Day setNameDay(String Day)
  43.      {
  44.          Day = Days;        
  45.          return this;
  46.      }
  47.  
  48.      public void printDay()
  49.      {      
  50.             System.out.println(Days);
  51.      }
  52.  
  53.  
  54.  
  55.      public void previousDay()
  56.      {
  57.  
  58.  
  59.      }
  60.  
  61.       public void nextDay()
  62.      {        
  63.        Day=Day+=2;
  64.  
  65.         setDay(Day);
  66.         printDay();
  67.  
  68. //         if (Day <1)
  69. //            Day = 1;
  70.  
  71.  
  72.      }
  73.  
  74.      public void calculateDay()
  75.      {
  76.          int calc = 0;
  77.          int dayAdd =0;
  78.  
  79.          Scanner scanner = new Scanner(System.in);
  80.          System.out.println("Enter number of days to add: ");
  81.          calc =scanner.nextInt();
  82.  
  83.          dayAdd = Day +(calc);
  84.  
  85.          Day = dayAdd %7;
  86.  
  87.         setDay(Day);
  88.         printDay();
  89.      }
  90.  
  91.     public Day()
  92.     {
  93.         setDay(0);
  94.     }
  95.     public Day (int Day)
  96.     {
  97.         setDay(Day);
  98.     }
  99.     /**
  100.      * @param args the command line arguments
  101.      */
  102.     public static void main(String[] args) {
  103.         // TODO code application logic here
  104.         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");
  105.         System.out.print("Enter the number:");
  106.         Scanner sc = new Scanner(System.in);
  107.         int x= sc.nextInt();
  108.         Day myDay = new Day(x);
  109.         System.out.println("");
  110.  
  111.         System.out.print("The day of the week is: ");
  112.         myDay.printDay();
  113.        System.out.println();
  114.  
  115.        System.out.print("The previous day is: ");
  116.         myDay.previousDay();
  117.  
  118.        System.out.println();
  119.  
  120.        System.out.print("The next day is: ");
  121.         myDay.nextDay();
  122.  
  123.        System.out.println();
  124.  
  125.        myDay.calculateDay();
  126.        System.out.println();
  127.     }
  128. }
Apr 9 '14 #1
4 11330
chaarmann
785 Expert 512MB
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(dayNumber 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(newDayNumber)". Then you can use the printDay() method of this new day instance.
Apr 9 '14 #2
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(newDayNumber)". 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 :(

Expand|Select|Wrap|Line Numbers
  1. public void previousDay()
  2.      {        
  3.          dayNumber=(dayNumber-1)%7;     
  4.         setDay(dayNumber);
  5.         printDay(); 
  6.      }
  7.  
  8.       public void nextDay()
  9.      {   
  10.  
  11.        dayNumber=(dayNumber+1)%7; 
  12.         setDay(dayNumber);
  13.         printDay();
  14.      }
  15. public void calculateDay()
  16.      {
  17.          int calc = 0;
  18.          int dayAdd =0;
  19.  
  20.          Scanner scanner = new Scanner(System.in);
  21.          System.out.println("Enter number of days to add: ");
  22.          calc =scanner.nextInt();
  23.  
  24.          dayAdd = dayNumber +(calc);
  25.  
  26.          dayNumber = dayAdd %7;
  27.  
  28.         setDay(dayNumber);
  29.         printDay();
  30.      }
  31.  

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)
Apr 10 '14 #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(newDayNumber)". 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 :(
Apr 10 '14 #4
chaarmann
785 Expert 512MB
Apply it to line 11 which states:
"dayNumber=(dayNumber+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 "newDayNumber" is different from "dayNumber"?
So what I mean is, change line 11 to "var newDayNumber=(dayNumber-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(newDayNumber)" 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.printDay()" 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(dayNumber)", 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.
Apr 11 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Cezar | last post by:
Hello, Can anyone please help me with this matter. I try to find the interfaces implemented by a class using Reflection. e.g: public class A : InterfaceB, InterfaceC how can I get (the...
5
by: Joe Rattz | last post by:
Ok, I am trying to figure out how to tell if a class (not object) implements a particular interface. Using the "is" operator does not seem to work for me and I assume its because I have a class,...
2
by: Terentius Neo | last post by:
I would like to browse all the classes in an assembly and see which implement a certain interface. Is there a way to do this? I know how to get an array of types in an assembly, but can't see how...
2
by: lenin42001 | last post by:
Please can you help.......... We have to design a Day of Week calculator two text boxes & a day of week command button inbetween The instruction is use the Isdate function in the vallidating( )...
1
by: Krish | last post by:
I have an object of a class. Now I want to know whether this class implements a particular interface or now. How do i do that. Interface ISomeInterface { } MyClass objCls= new MyClass();
5
by: Harinezumi | last post by:
Hello, I have a base class Base with given member variables, getters and setters, but it doesn't do much on its own. Then there are functionalities (Func) which can use this base class's member...
2
by: qambary | last post by:
In this Assignment, you will design a class member Type. A. Each object of member Type can hold the name of a person, member ID, number of books bought, and amount spent. B. Include the member...
7
by: Calek916 | last post by:
The problem that I am having is when I run my program my Benefit class starts running and I have not called it my main but for some reason it is running first instead of my Employee class. This whole...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.