12,825,359 members (39,588 online)
Rate this:
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?

```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
Updated 31-Aug-12 8:06am
v4
ridoy 31-Aug-12 12:50pm

i think your code is ok,can improve it's efficiency by following bellow solutions.

Rate this:

## 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.

;)
Rate this:

## 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.
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!
Rate this:

## 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);
*/
}
}
```
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 210 ppolymorphe 155 AnvilRanger 110 Karthik Bangalore 105 Jochen Arndt 75
 OriginalGriff 5,367 Graeme_Grant 3,870 Karthik Bangalore 3,681 ppolymorphe 2,739 Jochen Arndt 2,666