Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

array display class

  Asked By: Wayne    Date: Oct 23    Category: Java    Views: 946
  

Please please, can anyone help me I need to construct this array class
but it is not happening. I needs to print out 3 sets of details.


public class Passenger
{
String name;
String address;
int age;

Passenger(String n, String add, int a)
{
name = n;
address = add;
age = a;
}

public int getAge()
{
return age;
}

public String getAddress()
{
return address;
}
public String getName()
{
return name;
}


public void PrintList()
{
Passenger p1 = new Passenger;
p1.printList();

System.out.println("My name is" + name);
System.out.println("My address is" + address);
System.out.println("My age is" + age);
}
public static void main(String[] args)
{
Traveller[] travellerlist = new Traveller[3];

travellerlist[0] = ("Jim","London","48");
travellerlist[1] = ("Sue","Manchester","22");
travellerlist[2] = ("Matt","Bristol","31");

for (int i =0; i<3;i++)
{
travellerlist[i].Print();
System.out.println
("==========================");
}
}

}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Adaulfo Fischer     Answered On: Oct 23

Hey buddy, I don't have much time, but here are some things I noticed. You
should also move the main method to a different class.

public void  PrintList()
{
//Passenger p1 = new Passenger;
//p1.printList();

System.out.println("My name  is" + name);
System.out.println("My address  is" + address);
System.out.println("My age  is" + age);
}
public static  void main(String[] args)
{
//Traveller[] travellerlist = new Traveller[3];
Passenger[] travellerlist = new Passenger[3];

travellerlist[0] = ("Jim","London","48");
travellerlist[1] = ("Sue","Manchester","22");
travellerlist[2] = ("Matt","Bristol","31");

 
Answer #2    Answered By: Nicholas Wells     Answered On: Oct 23

I took the time to correct all the errors. The rewritten program
worked on my JVM. The main method is just a test method for the class
itself. Hope this helps.

public class  Passenger
{
String name;
String address;
int age;

Passenger (String n, String add, int  a)
{
name = n;
address = add;
age = a;
}


public int getAge ()
{
return age;
}


public String getAddress ()
{
return address;
}


public String getName ()
{
return name;
}


public void print  ()
{
System.out.println ("My name  is " + name);
System.out.println ("My address  is " + address);
System.out.println ("My age  is " + age);
}


public static void  main (String [] args)
{
Passenger [] travellerlist = new Passenger [3];

travellerlist [0] = (new Passenger ("Jim", "12 Birdie Street,
London", 48));
travellerlist [1] = (new Passenger ("Sue", "24 Baker Street,
Manchester", 22));
travellerlist [2] = (new Passenger ("Matt", "2 Shop Ave,
Bristol", 31));

for (int i = 0 ; i < 3 ; i++)
{
travellerlist [i].Print ();
System.out.println
("==========================");
}
}
}

 
Answer #3    Answered By: Lily Brown     Answered On: Oct 23

Try code below :

public class  Passenger
{
String name;
String address;
int age;

public Passenger(String n, String add, int  a)
{
name = n;
address = add;
age = a;
}


public int getAge()
{
return age;
}

public String getAddress()
{
return address;
}
public String getName()
{
return name;
}

public void  PrintList()
{
/*Passenger p1 = new Passenger();
p1.printList();*/

System.out.println("My name  is" + name);
System.out.println("My address  is" + address);
System.out.println("My age  is" + age);
}

public static  void main(String[] args)
{

/*Traveller[] travellerlist = new Traveller[3];*/
Passenger[] travellerlist = new Passenger[3];

travellerlist[0] = new Passenger("Jim","London",48);
travellerlist[1] = new Passenger("Sue","Manchester",22);
travellerlist[2] = new Passenger("Matt","Bristol",31);


for (int i =0; i<3; i++)
{
travellerlist[i].PrintList();
System.out.println ("==========================");
}
}
}

 
Didn't find what you were looking for? Find more on array display class Or get search suggestion and latest updates.




Tagged: