Page 1 of 1

Who wants to earn more java insipred VV's?

Posted: December 13, 2004, 11:05 pm
by Akaran_D
Ok, here's the deal. For my final project, my professor tosses out a list of things that I, sadly, was busy running a 102degree fever and missed, and he won't answer any questions. Even better, when running the examples he mailed to the class, they don't work... which doesn't surprise me that much.

Here's the code for the examples, then I'll ask the questions.

Code: Select all

public class PhoneRecord
{
  protected String Name, Phone;
  
  public PhoneRecord(String Name, String Phone)
  {
    this.Name = Name;
    this.Phone = Phone;
  }
  
  public void setName(String NewName)
  {
    Name = NewName;
  }
  
  public void setPhone(String NewPhone)
  {
    Phone = NewPhone;
  }
  
  public String getName()
  {
    return Name;
  }
  
  public String getPhone()
  {
    return Phone;
  }
}
  

Code: Select all

// The "PhoneBook" class.
import java.awt.*;
import hsa.Console;
import PhoneRecord;

public class PhoneBook
{
    static Console c;           // The output console
    
    public static void main (String[] args)
    {
        c = new Console ();
        
        PhoneRecord person1 = new PhoneRecord("Andrew","304-424-3434");
        PhoneRecord person2 = new PhoneRecord("Mike","304-863-5534");
        
    //    c.println(person1.Name + " " + person1.Phone);
    //    c.println(person2.getName() + " " + person2.getPhone());
        
        PhoneRecord TheClass[] = new PhoneRecord[10];
        
     //   TheClass[0] = new PhoneRecord("Andrew","304-424-3434");
     //   TheClass[1] = new PhoneRecord("Mike","304-863-5534");
        TheClass[2] = new PhoneRecord("Eric","304-275-1234");
        TheClass[3] = new PhoneRecord("Jenny","304-3l4-4454");

        c.println(TheClass[2].getName() + " " + TheClass[2].getPhone());
        c.println(TheClass[3].getName() + " " + TheClass[3].getPhone());
       
        c.println("\n\n\n\nLet's swap them\n\n\n\n\n");
        
        Swap(TheClass,2,3);
        
        c.println(TheClass[2].getName() + " " + TheClass[2].getPhone());
        c.println(TheClass[3].getName() + " " + TheClass[3].getPhone());  
        
        Swap(TheClass,2,3);
        
        c.println("\n\n\n\n\n\n");
        
        c.println(TheClass[2].getName() + " " + TheClass[2].getPhone());
        c.println(TheClass[3].getName() + " " + TheClass[3].getPhone());  
        
        
        
    } // main method
    
    
    public static void Swap(PhoneRecord List[], int i, int j)
    {
        PhoneRecord Temp = List[i];
        List[i] = List[j];
        List[j] = Temp;
    }   
     
} // PhoneBook class
Ok, now for the questions.
I'm supposed to read in a file into something like the above two examples and then print them. I'm ok on printing, that shouldn't be too hard. But there's a few things I don't understand and that the books don't explain.

What is the this. tag and what does it do?
What does it mean to have something "protected"?
How does it read in the information in the subclass that gets imported into the main class?


If someone could offer even a cusory explination on this, I'll donate VV's. If someone can get this example working so I can better undstand it, or even just document it out, I'll donate VV's. If anyone can offer a clear and concise deffinition, I'll donate a lot of VV's.

Posted: December 13, 2004, 11:50 pm
by Zaelath

Posted: December 14, 2004, 12:20 am
by Mr Bacon
While I have limited knowledge of java, I do know a few things.

Basically, whenever you create an instance variable up at the top of your program, it can go anywhere throughout. Occasionally, you want to write a variable in a method with the same name as the instance variable. There is a way to refer to the instance variable and not the local variable, and that's using "this."

Protected on the other hand i'm not as familiar with. From what I do know, is just another level of private, but it double checks with outside security and databases etc.

Hope this helps.

Posted: December 14, 2004, 12:37 am
by Fash
protected is related to variable scope. whether or not outside functions can access the variable data. in the posted example, the protected tag forces you to use the functions he included to get the underlying data.

Posted: December 14, 2004, 11:16 am
by Akaran_D
Points comming this afternoon or tommorow before / after work (I f'ing hate these 6-11 night shifts) when I have a chance to review this. :)

Posted: December 14, 2004, 1:16 pm
by Deward
Don't worry to much about "this". It pretty much just tells the code to look for answers "here". Half the time you don't need it as far as I know.

Protected, as mentioned previously, refers to scope of variables. I mostly write in c so I could be wrong but there are three levels of scope, Public, Private and Protected. Public means taht you could access the variables Name and Phone directly from your phonebook class. This woudl look like person1.Name or person2.Phone. Protected and Private means that they can only be accessed using the public functions of the class, i.e. person1.getName().

What errors are you getting in the code? Looks ok to me just glancing through it.

Posted: December 14, 2004, 7:29 pm
by Akaran_D
just a quick reponse bnefore i'm off to work (more detailed stuff comming tommorow, thanks in advance all):

Deward, it's not compiling. I don't have the right computer with it in front of me right now, but copy the code and paste it, then run it. I'd post the message if I could but I'm otw out..

VV's comming late tonight for those that replied so far. :)

Posted: December 16, 2004, 1:31 pm
by Akaran_D
Donations done, thanks all.

Posted: December 16, 2004, 3:49 pm
by Mr Bacon
thanks, love.

Posted: December 16, 2004, 4:07 pm
by Akaran_D
Any time sweetiekins.