Results 1 to 4 of 4
  1. #1
    whixpyl is offline Member
    Join Date
    Jun 2015
    Posts
    1
    Rep Power
    0

    Default Need help improving clock program

    Hi all. I've recently started learning Java, and I could really use some pointers from the community. One of my practice assignments was to create a clock app, which is copied below. Could really use some help streamlining and pointing out how I could make the code more efficient. Thanks!


    import java.util.Date;
    import java.util.ArrayList;

    public class clock
    {
    public static void main (String [] args)
    {
    Date d = new Date();

    String s = d.toString();
    String [] days = s.split(" ");
    String day1 = days[0];
    String time1 = days[3];
    String [] time2 = time1.split(":");
    String Day = "Thursday";
    String timeDay = "morning";

    int time3 = Integer.parseInt(time2[0]);

    if (time3 >=5 && time3<=11)
    {
    timeDay = "morning";
    } else if (time3 >= 12 && time3<=16) {
    timeDay = "afternoon";
    } else if (time3 >=17 && time3<=21) {
    timeDay = "evening";
    } else if (time3 >=22 && time3 <= 4) {
    timeDay = "night";
    }

    if (day1.equals("Sun")) { //set the day
    Day = "Sunday";
    } else if (day1.equals("Mon")) {
    Day = "Monday";
    } else if (day1.equals("Tue")) {
    Day = "Tuesday";
    } else if (day1.equals("Wed")) {
    Day = "Wednesday";
    } else if (day1.equals("Thu")) {
    Day = "Thursday";
    } else if (day1.equals("Fri")) {
    Day = "Friday";
    } else if (day1.equals("Sat")) {
    Day = "Saturday";
    }


    System.out.println("It is " + Day + " " + timeDay + ".");
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help improving clock program

    First, check out the Java Naming Conventions in my signature (e.g. class names begin with upper case, variables begin with lower case, etc). You may want to check out the Calendar class. You can pull the numeric values directly from class and use those values to index into an array or list of strings. If you want to do it with if/else, then you can ignore the last construct if you check for all possibilities. For example:

    Java Code:
    //instead of this:
    
    int val; // some assigned integer
    String str;
    if (val < 5) {
       str = "Value is less than 5";
    } else if (val >= 5) {
      str = "Value is greater than or equal to 5";
    }
    Java Code:
    // do it like this
    
    if (val < 5) {
       str = "Value is less than 5";
    } else { // only option left so no if required.
      str = "Value is greater than or equal to 5";
    }
    Finally, if you do your if statements in the appropriate order, some of your statements only need to check the upper or lower bound, but not both.

    Regards,
    Jim
    Last edited by jim829; 06-08-2015 at 03:22 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Need help improving clock program

    Since you've only just started, forget about the archaic java.util.Date/Calendar API and learn about the Date/Time API that was introduced in Java 8: Trail: Date Time (The Java™ Tutorials)

    Also go through BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help improving clock program

    Well, I just learned something. Looks like I need to check it out too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Need help starting world clock program
    By sebjavierw in forum New To Java
    Replies: 3
    Last Post: 05-17-2013, 01:02 PM
  2. A weirdish clock program.
    By linkxs in forum New To Java
    Replies: 6
    Last Post: 09-26-2011, 05:36 PM
  3. improving my code
    By daud in forum New To Java
    Replies: 2
    Last Post: 08-14-2009, 09:46 PM
  4. clock class program
    By jvasilj1 in forum New To Java
    Replies: 12
    Last Post: 02-01-2008, 08:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •