I am in the process of learning java. I have previously learned ruby so I am taking some ruby code I know works and trying to convert to java, but I'm a little lost with this one so thanks in advance. I tried extends and that still isn't working :(
I have created a class Visitor which takes in name, gender, age, and citizenship. I am trying to have a new method AdmitNewVisitor in the House class where I can manually insert a new Visitor in a test. So, this code works below, but then the next code will not work and I can't figure out why, I've searched for hours and my book doesn't have an example.
What I am currently doing
public void AdmitNewVisitor( )
{
for (int i = 0; i < 2; i++)
{
numVisitors++;
visitors[numVisitors - 1] = new Visitor("Grates, Brill", "M", 66, "Alien");
}
}
What I want to do
I have changed i < numVisitors (max 100) defined in full code
public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship )
{
for (int i = 0; i < numVisitors; i++)
{
numVisitors++;
visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
}
}
New Test Method
t.AdmitNewVisitor("Grates, Brill", "M", 66, "Alien");
In ruby you would just do
def admit_new_visitor the_name, the_gender, the_age, the_citizenship
@num_visitors += 1
@visitors[@num_visitors - 1] = Visitor.new(the_name, the_gender,
the_age, the_citizenship)
end
Edit: Forgot to specify error, sorry long day the error is there nothing displayed when I do toString(), no error message, just prints my string and no visitors where visitors should appear. I now see that I should not run this in a loop as that doesn't make sense, when I try
public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship)
{
numVisitors++;
visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
}
I receive error:
error: constructor Visitor in class Visitor cannot be applied to given types
{
^
Required: String, String, int, String
found: no arguements
reason: actual and formal arguments differ in length
I believe it's referring to this line of code in Visitor class, all variables are public in Visitor
public Visitor(String theName, String theGender, int theAge, String theCitizenship)
Edit: Everyone has been very helpful but since I'm learning from scratch and haven't gotten to lists yet, is there any way to do this the way I am trying? The ant eats the elephant one bite at a time
Edit 3 Visitor Class
import java.util.*;
public class Visitor
{
// Define Instance Variables
public String name;
public String gender;
public int age;
public String citizenship;
public int currentRoom;
// Define Constructor
public Visitor(String theName, String theGender, int theAge, String theCitizenship)
{
name = theName;
gender = theGender;
age = theAge;
citizenship = theCitizenship;
currentRoom = 0;
}
public String toString( )
{
String output = String.format("%-20s %-15s %d", name, citizenship, currentRoom);
return output;
}
}
Edit 4 forgot this, included in HouseTour class where AdmintNewVisitor is located in
public Visitor[ ] visitors = new Visitor[100];
Edit: SOLVED
Well, I have been tinkering with my code for a bit and I think I know the problem. Thanks to all but specifically @Dan Temple, I should have probably worked on this when I was less tired so I know exactly where I went wrong but I ended up using the code below, numVisitors = 0 is defined previously so numVisitors++ comes first, then subtracts 1 to get place in array. I believe, and I could be wrong, the problem was listing my public String toString() method that involved visitor BEFORE listing my AdmitNewVisitor, or maybe I'm just tired. In ruby, I had no problems listing my to_s (toString() in java) before my new_visitor method. Thanks again, great to know I'm not in this alone :{o :{/ :)
public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship)
{
numVisitors++;
visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
}