< 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

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

 

public class 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. Free Java EE (J2EE) Training Online Course by Sang Shin

  4. NetBeans basics tutorial

  5. Swing (Book)

  6. The Java Tutorials

  7. The Really Big Index

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

  9. Sun Microsystems - Search - Sun Microsystems search.

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

  11. Java Developers Almanac Example -  Code Examples from The Java Developers Almanac 1.4

  12. Java Tips - Other API Tips - Java Tips -- Java, Java, and more Java.

  13. Java Tips Weblog : Weblog

  14. Java Examples - Latest Code Samples

  15. Open source - java communities

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

  17. Core GUI APIs - Swing

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

  19. Swing: A Quick Tutorial for AWT Programmers - A brief Swing tutorial for Java programmers who already know the AWT.

  20. Java Swing Tutorial | Swing Examples - Java swing tutorial contains programming tutorials for various components illustrated with different swing examples.

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

  22. Download SwingLabs Technology

  23. swinglabs: SwingLabs Home Page

  24. java.net Forums : SwingLabs

  25. JAva2.com.

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

  27. JavaReference.com -  JavaReference.com is a resource site for the Java Community. It provides Java Articles, Java Examples, Java Books, Java Forum, Test Center for Java Certification, Java Downloads, Latest Java News, and Book Reviews. Members of Java Reference can contribute to almost every section of the site.

  28. jGuru: Forum Home Page

  29. kickjava.com.

  30. Code Examples from Java Swing, 2e

  31. SwingLabs Demos

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

  33. The Java JDBC.

  34. Java Tutorial - Java Tutorial.

  35. JAVA DEVELOPER'S JOURNAL - The Java Developer's Journal.

  36. C#, Java, and JavaScript Programming, by Richard G Baldwin - Online C#, Java, and JavaScript tutorial lessons designed to teach you how to program. Content ranges from Introductory to Advanced.

  37. Overview of Java programming resources - An overview of the Java resources offered on this site.

  38. Advanced JDBC Samples

  39. Java ImageUpload

  40. Hans Muller's Java Blog

  41. community.java.net - JavaDesktop - JavaDesktop, a new gathering place for members of the Java graphical user interface (GUI) community.

  42. JARS.COM - Free html tutorials and help, images, color codes and more for the web developer. Featuring tutorials on HTML tags, programming, basics, JavaScript, frames, hex codes and more. Our weekly newsletter provides the latest information for developers.

  43. Annotations in Java 5.0 - Annotations in Java 5.0

  44. techdayscode: Documents & files: JPA

  45. Sun.com Developers - The Java developers zone.

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

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

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

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

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

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

  52. A Visual Guide to Swing Components (Java Look and Feel) (The Java™ Tutorials > Graphical User Interfaces > Swing Features)

  53. Java wtk2.5.2 Toolkit User Guide.

  54. Java and JavaScript Programming, by Richard G Baldwin  - OnlineJava and JavaScript tutorial lessons designed to teach you how to program in Java and JavaScript. Content ranges from Introductory to Advanced.

  55. Working with Common Page Bean Design Elements - These tips and design patterns will help you work with common page bean design elements. Some of the techniques described in this article involve writing custom code in a page bean, application bean, or session bean, but you can implement many of these techniques just by using IDE functions.

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

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

  58. Sun Java System Application Server Platform Edition 9.0 Developer's Guide

  59. Java tutorial trail map.

 

 

 

 

 

 

 

 

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 >