BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Saturday, March 05, 2005

Assignment #2 Answer

Oh man, oh man. I can't believe I ate the whole thing.

After doing a little Google search to help me out on how to use SimpleDateFormat I spent a while scratching my head wondering why you wanted to me to use GregorianCalendar or Calendar. Now I see.

import java.text.*; 
import java.util.*;

public class DateParse {
public static void main(String[] arguments) {

int month = 2;
int day = 3;
int year = 1976;

if (2 < arguments.length) {
month = Integer.parseInt(arguments[0]);
day = Integer.parseInt(arguments[1]);
year = Integer.parseInt(arguments[2]);
}
GregorianCalendar gregCal = new GregorianCalendar(year, (month - 1), day);

SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy");
System.out.println(sdf.format(gregCal.getTime()));
}
}

4 Comments:

Blogger Sten said...

Cool. I'm glad you went back and did it, for completeness sake if nothing else.

A style issue I'd like to address, is this guy:

if (2 < arguments.length) {

is a C-ism. The intention behind it is if you put your constant expression on the left side you'll never acidentally assign the variable to the constant as in:

if (i == 4) { // does i equal 4?
vs.
if (i = 4) { // i just got assigned the value of 4

In C that was an issue and considered good practice to do what you've been doing since:

if (4 = i) { // compiler error

But in Java this problem isn't there since the if statement is required to take a boolean expression, and "i = 4" is not a boolean expression.

7:35 AM  
Blogger Brian T. Grant said...

You're saying, stylistically, its better form to say

if (arguments.length > 2) {

right?

Good point. For some reason, when I write that stuff out at first, it makes more sense for me to read it the way I wrote it, but I should go back and change that stuff, or just get it sorted out in my head and write it the way it should be written first.

7:49 AM  
Blogger Sten said...

Right. It's more readable because it's how you would naturally speak it.

7:52 AM  
Blogger Brian T. Grant said...

I got some work done on Bouncing Balls yesterday morning, but got hung up on something that I did not
expect to. I'm going to take a look at my code and make sure I'm adding
things in the right order and to the right JPanels, but basically I've got a panel where, if the button gets added, the black field for the balls
ends up being a tinly little box. What I'm attempting to do is:
- create a JPanel with a Y-axis oriented BoxLayout
- create a JPanel for the black field
- create a JPanel for the button which I want to appear below the black field

Now that I'm writing this, I wonder if something like a Grid or GridBag
might be a better choice; something with a bit more control over the
actual position and size of the elements.

2:35 PM  

Post a Comment

<< Home