Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
THis is the assignment:
Write a program that does the following:

Declare a set of seven integer constants named MONDAY, TUESDAY, ... , SUNDAY. Set MONDAY to 1, TUESDAY to 2, and so forth. Then, create an integer variable named day_of_week and initialize it with some integer value. If the value of day_of_week is 1, the program should display Today is Monday. If the value is 7, the program should display Today is Sunday, and so forth. If the value is not in the range 1-7, the program should display the message Invalid Data. Use a switch construct and the defined constants.

This is what I did could you check?

C#
public class DaysOfWeek

{
    public static void main (String [] args)
    {

        int Monday=1;
        int tuesday=2;
        int wednesday=3;
        int thursday=4;
        int friday=5;
        int saturday=6;
        int sunday=7;
        int day_of_week = 1;


         switch(day_of_week)

         {
          case 1:
            System.out.println("today is monday");
            break;

          case 2:
            System.out.println("today is tuesday");
            break;

          case 3:
            System.out.println("today is wednesday");
            break;

          case 4:
            System.out.println("today is thursday");
            break;

          case 5:
            System.out.println("today is friday");
            break;

          case 6:
            System.out.println("today is saturday");
            break;

          case 7:
            System.out.println("today is sunday");
            break;

          default:
            System.out.println("Invalid Data");
            break;
         }
    }
}
Posted
Updated 31-Aug-12 7:06am
v4
Comments
ridoy 31-Aug-12 12:50pm    
i think your code is ok,can improve it's efficiency by following bellow solutions.

Well, if you have to define a constant you should use the "final" keyword or an "enum", use google to find out extended information on both of them, using an int makes it a "variable" which is not constant. This will be a problem only if your teacher is picky...

Apart of that the code looks good.

You should definitely compile and execute it using the debugger to see how it behaves.

;)
 
Share this answer
 
What is the point of declaring a constant value and then not using it? Your case statements should use the days of the week, not the integer values, something like:
Java
        switch(day_of_week)
        {
         case Monday:
           System.out.println("today is monday");
           break;
...

You may also like to capitalise all the day names and the messages you are using.
 
Share this answer
 
Comments
TorstenH. 19-Sep-12 5:49am    
ARRRR! Only if using Javaaaaaaaa 7 ARRRR!
Becauuuuuuuse Java6 does not like thaaaat! ARRRR!
Richard MacCutchan 19-Sep-12 5:58am    
But he was noooot using an eeeeeeenum!
Java
public class Test
{
    public static void main(String[] args)
    {
        String[] day_names = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
        int day = 1;
        System.out.println("Today is " + day_names[day-1]);
    }
}

If you are using java 1.5 or later then you should utilize an enum.
This is a much better solution:
Java
public class Test
{
    enum DayOfWeek
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday,
    }

    public static void main(String[] args)
    {
        int day = 1;
        System.out.println("Today is " + DayOfWeek.values()[day-1]);
        // or if you can omit the usage of an int type then you can say:
        //DayOfWeek day = DayOfWeek.Monday;
        //System.out.println("Today is " + day);
    }
}

OR
Java
public class Test
{
    enum DayOfWeek
    {
        Monday(1),
        Tuesday(2),
        Wednesday(3),
        Thursday(4),
        Friday(5),
        Saturday(6),
        Sunday(7);

        public static DayOfWeek fromInt(int i)
        {
            assert i>=1 && i<=7;
            return values()[i-1];
        }

        private DayOfWeek(int customValue)
        {
            _customValue = customValue;
        }

        public int getCustomValue()
        {
            return _customValue;
        }

        // You can store any kind of custom info for each enum member, not only an integer.
        // For example some kind of textual description, or fancy name, or references to data objects.
        private int _customValue;
    }

    public static void main(String[] args)
    {
        int day = DayOfWeek.Monday.getCustomValue();
        System.out.println("Today is " + DayOfWeek.fromInt(day));

        /*
        // This is much more elegant if you don't have to use an int type:
        DayOfWeek day = DayOfWeek.Monday;
        System.out.println("Today is " + day);
        */
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900