Showing posts with label Date. Show all posts
Showing posts with label Date. Show all posts

Applets and Web Pages

So far I have been concentrating on applets. The applet is normally the entire web page. The HTML for the web page is merely to name the applet, and give it a size. However this is not normal. You usually have some other presentation in the HTML page. Therefore I did a more realistic project that integrated applet output with the markup from the HTML source.

I had an exercise from my textbook dealing with times. In Java, you can get the number of milliseconds since 1970. This feature has been encapsulated with the Date class. The default constructor of Date initializes the object to the current date and time. Use DateFormat to choose how you want to show the date. Even though the class is abstract, you can still call the static members.

Private Date

I was trying to write a Java class for an exercise in my class textbook. The job was to model a credit card. I figured an important characteristic of the credit card is the expiration date. At first I just created an instance variable of type String. Then I figured I would need a member isExpired(). That made me thing String was the wrong type for this variable.

Off I went to the Java API. The first Date class I found had something to do with SQL. That did not seem right. Then I found another Date class in java.util. However the docs for this class did not seem intuitive. I want to set a day, month, and year. There used to be a way to construct a Date object with such properties. But it had been deprecated to favor the number of milliseconds since some start date. How random.

Since I am a programmer, I decided to roll my own simple Date class. This was just for instructive purposes. Following good programming practices, I made the day/month/year private. I got down to writing my own comparison member. That's when I really got tested with my knowledge of what private means. I pass another Date object reference into my compareTo member. Can my member access the private instance variables of the Date passed in? It compiles fine. But it seems wrong. My gut tells me I should only be able to access private members of "this" object. Time to ask my professor about these details.