Skip to content

Instantly share code, notes, and snippets.

@axelson
Created October 13, 2009 05:59
Show Gist options
  • Save axelson/209043 to your computer and use it in GitHub Desktop.
Save axelson/209043 to your computer and use it in GitHub Desktop.
import javax.swing.JOptionPane;
/**
* Takes in information about 2 people and displays it
* @author Axelson, Jason
* @assignment ICS 211 Assignment 11
* @date Wed Oct 7 22:39:31 HST 2009
* @bugs None known
*/
public class AxelsonJason11 {
public static void main( String[] args ) {
int age1 = -1, age2 = -1;
String name1 = "", name2 = "";
Person person1, person2;
// Check if correct number of command-line arguments passed
if ( args.length != 4 ) {
JOptionPane.showMessageDialog( null, "Sorry, this program requries 4 arguments, in this order:\nname (of person 1), an age (in years), name (of person 2), and age ( of person 2 in years)" );
System.exit( 1 );
}
// Parse command-line input
// Format should be name (of person 1), an age (in years), name (of person 2), and age ( of person 2 in years)
try {
name1 = args[0];
age1 = Integer.parseInt( args[1] );
name2 = args[2];
age2 = Integer.parseInt( args[3] );
} catch ( NumberFormatException e ) {
JOptionPane.showMessageDialog( null, "Sorry, you need to pass in age as an integer" );
System.exit( 1 );
}
// Check for sanity of arguments
if (( age1 < 0 ) || ( age2 < 0 ) ) {
JOptionPane.showMessageDialog( null, "Sorry, ages need to be positive numbers" );
System.exit( 1 );
}
// Create the Person objects
person1 = new Person( name1, age1 );
person2 = new Person( name2, age2 );
// Display the information about the people
JOptionPane.showMessageDialog( null, person2.toString() );
JOptionPane.showMessageDialog( null, person1.toString() );
}
}
/**
* Models a person
*/
class Person {
// Data fields
String name;
int age;
// Constructor
public Person( String name, int age ) {
this.name = name;
this.age = age;
}
// Returns information about the person as a string
public String toString() {
return this.name + " is " + this.age + " years old";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment