Craig Mattson (Personal Website)
Home - Blog, News, About MePrograms - C#.Net, Java, VB6MusicWebsites

Viewing News Article

Programming Concepts Part 2 (02/06/2007 09:22:49 AM)

If you have missed part 1, you can view that by clicking here!

So, this post is a direct follow on from the program that is a Phone Book and hopefully educating some of the viewers some design concepts in creating small portions of code. Back in the previous post, we had implemented a menu, and a Person object. Continuing on from Step 4 which precisely defines what happens during the method, we can now interperate this now into Java syntax. To enter a person to the database, we assumed the following steps:

  1. Enter a users name
  2. Enter a users phone number
  3. Enter a users e-mail address
  4. Store the name
  5. Store the phone number
  6. Store the e-mail address

Lets convert this into Java code step by step:

//We firstly want to display some text for the user for the fields we wish to enter
System.out.println("Enter a Persons Name:");
//Now ask for the data using the keyboard we defined
String un = this.kb.nextLine();
//Ask for the users phone number
System.out.println("Enter a Persons Phone Number:");
//Again, get the data from the user
String ph = this.kb.nextLine();
//Ask for the users e-mail address
System.out.println("Enter a Persons Address");
//Get the input again from the keyboard
String ad = this.kb.nextLine();
//Now, store all the values - there's a number of ways we can approach it
//As we have a constructor class, we may aswell use it:
Person newPerson = new Person(un, ph, ad);

Above is the entire method for adding a user. In the case of adding an array, we simply require a counter (we are not assuming ArrayList here), so basically it works as such:

arrayName[position] = newPerson;

This method above would (assuming arrayName was already set up for the Person object; Person arrayName[];) simply store into the array of Person objects the new person created. The position incrementing simply says that the next time you add a person, you would store it in a new slot.

I'll post the final method tomorrow which will involve the use of a structured for loop, but right now there is something more important that I feel needs to be expressed:

There is NOTHING stopping you using multiple namespaces! Alot of people seem to be confused and think that it's only possible to use one namespace which is false. You CAN use multiple namespaces. For instance, providing we have say a Person that has a Lesson object and Lesson has a Time object, then there is nothing wrong with using:


Or, you may even have methods associated with it to return the Lesson Object, and the Time Object (especially where arrays are used):


This is exactly the same (that is if getLesson returns a Lesson and getTime returns a Time) as the one above it - it does not matter how many you have ;) Just don't get confused.

Anyway, more to come at a later date!

[Print View]