Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
//I need the program to do what it is doing right now, but with less Print lines.

Java
public class AssignmentFour
{
    public static void main(String[] args)
    {
        final int MONDAY = 1;
        final int TUESDAY = 2;
        final int WEDNESDAY = 3;
        final int THURSDAY = 4;
        final int FRIDAY = 5;
        final int SATURDAY = 6;
        final int SUNDAY = 7;
        int day_of_week = 4;

        switch (day_of_week)
        {
        case MONDAY:
            System.out.println("Today's Monday!");
            break;
        case TUESDAY:
            System.out.println("Today's tuesday!");
            break;
        case WEDNESDAY:
            System.out.println("Today's wednesday!");
            break;
        case THURSDAY:
            System.out.println("Today's thursday!");
            break;
        case FRIDAY:
            System.out.println("Today's friday!");
            break;
        case SATURDAY:
            System.out.println("Today's Saturday!");
            break;
        case SUNDAY:
            System.out.println("Today's Sunday!");
            break;
        default:
            System.out.println("Invalid Data");
            break;
        }
    }
}
Posted
Updated 24-Sep-13 22:41pm
v3
Comments
pasztorpisti 25-Sep-13 5:17am    
The problem with your solution is not the too many lines but redundancy. Its usually not the shorter code is the better but the code that is easy to read and doesn't have redundancy/copy-paste.
Shubhashish_Mandal 25-Sep-13 13:32pm    
4 answers!! Surprise to see that there are so many people ready to do school assignment on request.

Simply make sure the imput value is between 1 and 7, then simply lookup the value from an array with the names of the days.

Good luck!
 
Share this answer
 
Java 1.5 and later has enum and this is the clean solution:
Java
public class AssignmentFour
{
    enum DayOfWeek
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday,
    }

    public static void main(String[] args)
    {
        DayOfWeek dayOfWeek = DayOfWeek.Thursday;
        System.out.println("Today's " + dayOfWeek + "!");
    }
}

But the teachers are probably interested in the array solution that was already recommended.
 
Share this answer
 
v2
This sounds like homework or something similar, but I'm going to give you the benefit of the doubt on that one.

Try storing your day names in an array, it reduces the number of lines:

Java
public static void main(String[] args)
{
    final String[] days = new String[] { "Monday", "tuesday", "wednesday", "thursday", "friday", "Saturday", "Sunday" };
    int day_of_week = 4;
    try {
       System.out.println(String.format("Today's %s!", days[day_of_week - 1]));
    }
    catch(IllegalArgumentException e) {
       System.out.println("Invalid Data");
    }
}

Hope this helps,
Fredrik
 
Share this answer
 
Comments
pasztorpisti 25-Sep-13 5:23am    
We know that IllegalArgumentException will never be thrown (since our code always satisfies "%s"). "Invalid Data" happens in case of IndexOutOfBoundsException and we handle the IllegalArgumentException only because we have to. You can also make printing simpler with System.out.printf but then the format string must end with "\n".
Simplest (primitive) approach :p

1 line:

Java
public class AssignmentFour{public static void main(String[] args){ final int MONDAY = 1, TUESDAY = 2, WEDNESDAY = 3, THURSDAY = 4, FRIDAY = 5, SATURDAY = 6, SUNDAY = 7;int day_of_week = 4; switch (day_of_week) { case MONDAY: System.out.println("Today's Monday!"); break; case TUESDAY: System.out.println("Today's tuesday!"); break; case WEDNESDAY: System.out.println("Today's wednesday!"); break; case THURSDAY: System.out.println("Today's thursday!"); break; case FRIDAY: System.out.println("Today's friday!"); break;case SATURDAY: System.out.println("Today's Saturday!"); break; case SUNDAY: System.out.println("Today's Sunday!"); break; default: System.out.println("Invalid Data"); break; } }}
 
Share this answer
 
Comments
pasztorpisti 25-Sep-13 5:16am    
+5 :-)

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