Thursday, February 13, 2014

Java Programming: Arrays of Objects

This Java program is made up of two files.
The file "Entry.java" holds the definition of class Entry 
and the file "DirectoryTest.java" holds the main program that uses the Entry class to 
define its objects.

"The class "Entry" contains 4 strings as instance variables, a constructor and the 
methods to access and modify the instance variables.
 -----------------------------------------------------------------------------------------------------------
/**********************Entry.java***************************************/

import java.io.*;

public class Entry{
//instance variables
  String name;
  String address;
  String email;
  String phone;

//constructor 
  public Entry(String Name, String Address, String Email, String Phone)
  {
    this.name = Name;
    this.address = Address;
    this.email = Email;
    this.phone = Phone;
  }
  //methods
 
   public void name(String Name){
      this.name = Name;
   }
   public void address(String Address){
      this.address = Address;
   }
   public void email(String Email){
      this.email = Email;
   }
   public void phone(String Phone){
      this.phone = Phone;
   }

   public void Print()
  {
    if(this.name != null)
      System.out.println("\n Name: " + this.name + " \n Address: " + this.address+
                      " \n email: " + this.email + "\n phone: " + this.phone);
    else
      System.out.println( "cell empty" );
   }
  }
 --------------------------------------------------------------------------------------------------------------
This is a simple Directory test program that creates an object of class Entry and
 initializes it during it's creation. It then calls the print function of class Entry to print 
the contents of the object.
 -------------------------------------------------------------------------------------------------------------
 /**********************DirectoryTest.java***********************************/

 import java.io.*;

public class DirectoryTest
{
   public static void main(String args[])
   {
     Entry Directory1= new Entry
                            ("suchita"," 1234 Timothy TX", "suchita@yahoo.com","512 567 8904");
     Directory1.Print();
  }
}
 ----------------------------------------------------------------------------------------------------------- 
After compiling both the java files and then running the Directory.test, the result:
 -----------------------------------------------------------------------------------------------------------
 Name: suchita
 Address:  1234 Timothy MA
 email: suchita@yahoo.com
 phone: 512 567 8904
  -----------------------------------------------------------------------------------------------------------
 Here is a modified version of DirectoryTest.java that creates an array of objects of
 class Entry with user input data.
------------------------------------------------------------------------------------------------------------
   
import java.io.*;
import java.util.Scanner;

public class DirectoryTest
{
   public static void main(String args[])
   {
     Scanner scan = new Scanner( System.in );
   
     Entry[] NewDirectory = new Entry[3];
   
     String nameIn, addressIn, emailIn, phoneIn;
   
     for (int i=0;i < (NewDirectory.length);i++)
    {
         System.out.println(" Enter Name: ");           nameIn = scan.nextLine();
         System.out.println(" Enter Street Address: ");         addressIn = scan.nextLine();
         System.out.println(" Enter email address: ");         emailIn = scan.nextLine();
         System.out.println(" Enter phone number: ");         phoneIn = scan.nextLine();
         Entry temp= new Entry(nameIn, addressIn, emailIn, phoneIn); 
         NewDirectory[i] = temp;
    }
    System.out.println("The directory is full.");
  
    for (int i=0;i < (NewDirectory.length);i++)
       NewDirectory[i].Print();
   }
}
 ----------------------------------------------------------------------------------------------------------

Java Programming: 2D Arrays and for-loops

Here is a simple Java program that uses an array to store Names and Addresses:
----------------------------------------------------------------------------------------------------
class NamesAndAddresses{

  public static void main(String args[])
  {        


    // a 2-D string array with four rows and four colomns.
   
    String[][] Names = new String[][]

            {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
              { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
              {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
              {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};
   
    // A for loop for printing the elements in order 


    System.out.println("\nPHONE BOOK: \n");       


    for (int i=0;i <(Names.length);i++)
          System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                            "  email: " + Names[i][2] + "  phone: " + Names[i][3]);
  }
}

-----------------------------------------------------------------------------------------------
Let's look at this one statement at a time:
The String array "Names" has 4 rows and 4 colomns. 
Each row corresponds to one entry.
---------------------------------------------------------------------------------------------

String[][] Names = new String[][]
         {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
          { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
           {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
           {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};


----------------------------------------------------------------------------------------------------

Here is a "for loop" that I use for printing the array.
--------------------------------------------------------------------------------------------------

System.out.println("\nPHONE BOOK: \n");  

for (int i=0;i <(Names.length);i++)
      System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                     "  email: " + Names[i][2] + "  phone: " + Names[i][3]);

 --------------------------------------------------------------------------------------------------
 When I compile and run the program,  the resulting output on my console is:
---------------------------------------------------------------------------------------------------

PHONE BOOK:

 Name: suchita  Address:  1234 Dallas TX  email: suchita@yahoo.com  phone: 512 567 8904
 Name: Maria  Address: 345 Houston TX  email: maria@gmail.com  phone: 456 890 2345
 Name: Ben  Address: 789 Boston MA  email: ben@tt.com  phone: 345 456 7890
 Name: John  Address: 890 San Francisco CA  email: john@pinterest.com  phone: 234 789 2415


--------------------------------------------------------------------------------------------------
 Here is a "for loop" for printing the array in reverse:
---------------------------------------------------------------------------------------------------

System.out.println("\nPHONE BOOK IN REVERSE: \n");

    for (int i=Names.length-1 ;i >=0 ;i--)
      System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                     "  email: " + Names[i][2] + "  phone: " + Names[i][3]);
 ----------------------------------------------------------------------------------------------------
 When I compile and run the program,  the resulting output on my console is:
-----------------------------------------------------------------------------------------------------

PHONE BOOK IN REVERSE:

 Name: John  Address: 890 San Francisco CA  email: john@pinterest.com  phone: 234 789 2415
 Name: Ben  Address: 789 Boston MA  email: ben@tt.com  phone: 345 456 7890
 Name: Maria  Address: 345 Houston TX  email: maria@gmail.com  phone: 456 890 2345
 Name: suchita  Address:  1234 Dallas TX  email: suchita@yahoo.com  phone: 512 567 8904

------------------------------------------------------------------------------------------------------
Here is the program with a nested for loop for printing out the array:
------------------------------------------------------------------------------------------------------
 class MultiDArray{
  public static void main(String args[])
  {      
    // a 2-D string array with four rows and four colomns.
  
    String myArray[][] = new String[][]

               {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
                 { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
                 {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
                  {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};
  
    String display[] = new String[]{" Name: ", " Address: ", " Email:", " Phone :"};
  
    // A for loop for printing out the elements in order
    System.out.println("\nPHONE BOOK: \n");     
    for (int i=0;i < (myArray.length);i++)
    {
      for (int j=0; j< myArray[i].length; j++){
        System.out.println(display[j] + myArray[i][j]);
      }
      System.out.println();
    }
  }
}

------------------------------------------------------------------------------------------------------
Here is a phone book array that is created and then the entries filled in at
 the console by the user.
------------------------------------------------------------------------------------------------------

import java.util.Scanner;

class NamesAndAddresses{

  public static void main(String args[])
  {        


    Scanner scan = new Scanner( System.in );

    String[][] UserInput = new String[4][4];  


    for (int i=0;i < (UserInput.length);i++)
    {
         System.out.println(" Enter Name: ");
         UserInput[i][0]=scan.nextLine();
         System.out.println(" Enter Street Address: ");
         UserInput[i][1]=scan.nextLine();
         System.out.println(" Enter email address: ");
         UserInput[i][2]=scan.nextLine();
         System.out.println(" Enter phone number: ");
         UserInput[i][3]=scan.nextLine();
    }


  // A for loop for printing the elements in order 

 
    System.out.println("\nPHONE BOOK: \n");      
    for (int i=0;i < (UserInput.length);i++)
      System.out.println(" Name: " + UserInput[i][0] +

                                  "  Address: " +  UserInput[i][1]+
                                  "  email: "  + UserInput[i][2] +
                                  "  phone: " + UserInput[i][3]);
   
     // A for loop for printing the elements in reverse order 

 
    System.out.println("\nPHONE BOOK IN REVERSE: \n");
    for (int i=UserInput.length-1 ;i >=0 ;i--)
      System.out.println(" Name: " + UserInput[i][0] +

                                   "  Address: " + UserInput[i][1]+
                                     "  email: "   + UserInput[i][2] + 
                                     "  phone: " + UserInput[i][3]);  
}
}

---------------------------------------------------------------------------------------------


Friday, March 4, 2011

Friday, February 25, 2011

Ammo room with a dirt floor



I was not happy with the texture of the floor, so I changed it to a dirt floor to match the feel of the ammo room in the middle of a forest.

Tuesday, February 15, 2011

Saturday, February 12, 2011

LOGO REDESIGN


I am experimeting with colors on the material editor, getting used to the different parameters. I set the renderer to the Mental Ray option and this has a much larger selection of material choices. I cant wait to try out all of them.