<Java Programming Part 3 |Java & Friends |Install, test and Use NetBeans 6.x.x >


 

 

Using jGRASP the Java IDE: Java Programming Part 4

 

 

Content:  Java Program Examples Part 4

 

 

Java Program Examples

 

14.   Define a class named MyDay that implements the day of the week in a program. The class MyDay should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of the type MyDay:

 

a.     Set the day.

b.    Print the day.

c.     Return the day.

d.    Return the next day.

e.     Return the previous day.

 

The class should also has the appropriate constructor. Write a program to test all the operations for class MyDay as described in a. to e. above.

 

MyDay class and object manipulation.

 

import java.util.*;

 

//------------------------------------------------------------------

// MyDay class. Playing with day of week. I use MyDay instead of Day

//------------------------------------------------------------------

 

publicclass MyDay

{

      // instantiate standard input object

      static Scanner readinput = new Scanner(System.in);

 

      // member variable, private by default

      String day;

      // constructor with "Sunday" value

      public MyDay(String day)

      {

         day = "Sunday";

      }

      // set the day

      public void setDay(String theDay)

      {

         day = theDay;

      }

      // print or return the day

      public String getDay()

      {

         return day;

      }

      // print the day, looks unnecessary

      public void printDay()

      {

         System.out.println("When printing, your day is " + day);

      }

      // return the next day

      public String getNextDay()

      {

         // using direct comparison will fail as day == "Saturday"?

        // so use compareTo() method...

        if((day.compareTo("Saturday") == 0) || (day.compareTo("Sat") == 0))

            return ("Sunday");

         else if((day.compareTo("Sunday") == 0) || (day.compareTo("Sun") == 0))

            return ("Monday");

         else if((day.compareTo("Monday") == 0) || (day.compareTo("Mon") == 0))

            return ("Tuesday");

         else if((day.compareTo("Tuesday") == 0) || (day.compareTo("Tue") == 0))

            return ("Wednesday");

         else if((day.compareTo("Wednesday") == 0) || (day.compareTo("Wed") == 0))

            return ("Thursday");

         else if((day.compareTo("Thursday") == 0) || (day.compareTo("Thu") == 0))

            return ("Friday");

         else if((day.compareTo("Friday") == 0) || (day.compareTo("Fri") == 0))

            return ("Saturday");

         else

           return ("\"Opps Error lol!\"");

      }

      // return the previous day

      public String getPreDay()

      {

         if((day.compareTo("Friday") == 0) || (day.compareTo("Fri") == 0))

            return ("Thursday");

         else if((day.compareTo("Thursday") == 0) || (day.compareTo("Thu") == 0))

            return ("Wednesday");

         else if((day.compareTo("Wednesday") == 0) || (day.compareTo("Wed") == 0))

            return ("Tuesday");

         else if((day.compareTo("Tuesday") == 0) || (day.compareTo("Tue") == 0))

            return ("Monday");

         else if((day.compareTo("Monday") == 0) || (day.compareTo("Mon") == 0))

            return ("Sunday");

         else if((day.compareTo("Sunday") == 0) || (day.compareTo("Sun") == 0))

            return ("Saturday");

         else if((day.compareTo("Saturday") == 0) || (day.compareTo("Sat") == 0))

            return ("Friday");

         else

           return ("\"Opps Error lol!\"");

      }

     

      // main execution point

      public static void main (String args[])

      {

       // One of its weakness is the case sensitive: sun, Sunday, sunday, SuNdAy...

      // need more codes to avoid this case sensitiveness...

 

       // instantiate testday object of type MyDay class

        // with "Sun" as initial value....

         MyDay testday = new MyDay("Sun");

         // prompt user to set his/her day

         System.out.print("Enter day to set your day: ");

         // read and store user's day

         String storeday = readinput.nextLine();

         // invoke setDay() method to set a day defined by user

         testday.setDay(storeday);

         // invoke getDay() method to get a day

         System.out.println("Your day is " + testday.getDay());

         // test printing by invoking printDay() method

         testday.printDay();

         // invoke getPreDay() method to get the previous day

         System.out.println("Your previous day is " + testday.getPreDay());

         // invoke getNextDay() method to get the next day

         System.out.println("Your next day is " + testday.getNextDay());

      }

}

 

An output samples:

 

Enter day to set your day: Wednesday

Your day is Wednesday

When printing, your day is Wednesday

Your previous day is Tuesday

Your next day is Thursday

 

Enter day to set your day: Sat

Your day is Sat

When printing, your day is Sat

Your previous day is Friday

Your next day is Sunday

 

Enter day to set your day: sun

Your day is sun

When printing, your day is sun

Your previous day is "Opps Error lol!"

Your next day is "Opps Error lol!"

 

Enter day to set your day: Saturday

Your day is Saturday

When printing, your day is Saturday

Your previous day is Friday

Your next day is Sunday

 

More Java programs, tutorials, books and related resources can be found in the following list

  1. Download The Java Tutorial

  2. Java Frequently Asked Questions ( FAQ )

  3. NetBeans basics tutorial

  4. The Java Tutorials

  5. The Really Big Index

  6. Trail: JDBC(TM) Database Access (The Java™ Tutorials)

  7. JavaRanch - Includes study material for Java certification, a discussion forum and articles geared towards Java beginners.

  8. Java Examples - Latest Code Samples

  9. Open source - java communities

  10. Trail: Creating a GUI with JFC/Swing (The Java™ Tutorials)

  11. JIDE Software - The Best Java and Swing Components Library - Over 100 Components - JIDE Software is a professional Java and Swing component provider. JIDE Software's products are focused on rich-client applications for software developers. Based on pure Java Swing technology, JIDE Software's products allow for maximum compatibility with industry standards.

  12. Swing (Java) - Wikipedia, the free encyclopedia

  13. Java2.com.

  14. Trail: 2D Graphics (The Java Tutorials)

  15. Online Training and Tutorials - Learn the various Java technologies -- from the fundamentals of the Java programming language to web services and the the J2EE platform--through a variety of online tutorials and training classes.

  16. Java2s Tutorial - Java Tutorial.

  17. Advanced JDBC Samples

  18. Java examples (example source code) Organized by topic - Java source code examples organized by topic.

  19. Java EE Code Samples & Apps - This page lists content under code samples and applications for J2EE.

  20. Sun Java Application Server - List of Sample Applications.

  21. Java Persistence API - The Java Persistence API provides a POJO persistence model for object-relational mapping.

  22. Reference Resources for Java Technology - Here are resources for Java technology, including API specifications, documentation, FAQs, Webcasts and audiocasts, code samples, blueprints, technical articles, white papers, case studies.

  23. Java BluePrints: Guidelines, patterns, and code for end-to-end applications - Java BluePrints index page.

  24. Sun Java Wireless Toolkit for CLDC User's Guide.

  25. NetBeans Docs & Support - NetBeans IDE Documentation, Support Resources, and Tutorials.

  26. Web Application Learning Trail - NetBeans Tutorials, Guides and Articles - A collection of tutorials, articles, and demos on creating web applications in NetBeans IDE.

  27. Apache Tomcat - Java Web server.

  28. Oracle Mobile Application Framework - Java mobile development.

 

 

 

 

jGRASP the Java IDE & Java Programming:Part 0 |Part 1 |Part 2 |Part 3 |Part 4


<Java Programming Part 3 |Java & Friends |Install, test and Use NetBeans 6.x.x >