Click here to Skip to main content
11,410,584 members (59,062 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
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?

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 31-Aug-12 5:05am
Edited 31-Aug-12 8:06am
v4
Comments
ridoy at 31-Aug-12 12:50pm
   
i think your code is ok,can improve it's efficiency by following bellow solutions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.

Wink | ;)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
         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.
  Permalink  
Comments
TorstenH. at 19-Sep-12 5:49am
   
ARRRR! Only if using Javaaaaaaaa 7 ARRRR!
Becauuuuuuuse Java6 does not like thaaaat! ARRRR!
Richard MacCutchan at 19-Sep-12 5:58am
   
But he was noooot using an eeeeeeenum!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
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
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);
        */
    }
}
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100