aspose file tools*
The moose likes Beginning Java and the fly likes how to create 5 objects of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create 5 objects of a class" Watch "how to create 5 objects of a class" New topic
Author

how to create 5 objects of a class

Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
import java.io.*;

class Person
{
String ic;
String name;

void set_data(String i, String n)
{
ic = i;
name = n;
}

String get_ic()
{
return ic;
}

String get_name()
{
return name;
}

public static void main(String args[])
{
Person P[] = new Person[5];

P[0].set_data("123456", "Tony");
System.out.println("Employee No: "+P[0].get_ic());
System.out.println("Employee Salary: "+P[0].get_name());
}
}

is this the way to create 5 objects(Person P[] = new Person[5] ?
[ February 23, 2005: Message edited by: Garry Meax ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Please use the [ CODE ] ... [ /CODE ] tags (remove spaces) or use the button below the edit box to preserve your code's formatting so it's more readable.

When you create an array of objects, all the elements are initialized to null, so you must assign valid object references to them before accessing them.Also, in Java the standard is for classes to use CamelCase, methods and variables to use camelCase (first letter lowercase), and constants to use ALL_CAPS with underscores to separate words. For example, p[0].setData(...).
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
i have created 5 objects using (Person[] P = new Person[5] .

and i have a method which sorts those 5 objects according to the number(i am passing an integer value as one argument of method set_data()).

i tried to call that mathod(one which sorts) using the following code:
sort(P);

but this generates an error. how to call that sorting mathod?

[ February 23, 2005: Message edited by: Garry Meax ]
[ February 23, 2005: Message edited by: Garry Meax ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
What is the error you are getting?

Again, "Person[] P = new Person[5]" doesn't create 5 Person objects; it creates an array of 5 Person references all initialized to null.
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
i have set data using this code:
P[0] = new Person();
P[0].set_data(1,1000);

this is the error i am getting:
sor{int[]} in Person cannot be applied to {Person[]}

sor is the name of my sorting method.

[ February 23, 2005: Message edited by: Garry Meax ]
[ February 23, 2005: Message edited by: Garry Meax ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Can you post the class that contains the sort(Person[]) method and the code that calls it? If you provide more context, we'll have a chance at helping you. Make sure you use the CODE tags I described, please.Does Question6 extend the Person class? What are you trying to do at this line?
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31


no, it doesn't. its a mistake.

it should be:

[ February 23, 2005: Message edited by: Garry Meax ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Garry Meax:
This method requires an array of ints, not Persons. You can change it to "Person a[]", but then the other line I quoted won't compile because Java cant use > to compare objects -- only primitives (int, char, float).

Look up the java.util.Comparable interface in the JavaDocs for the standard way of comparing objects. It defines a single method that you can implement:
[ February 23, 2005: Message edited by: David Harkness ]
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
thank you for the help
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
On a side note, your bubble sort algorithm is doing some wasted work (will never have an effect). For example, consider this list of integers:The first time through the outer loop (i = 0), the first element is compared to every element (including itself) and swapped if the other element is smaller.It's trivial compared to all the comparisons being done, but comparing an element to itself will always fail the test, so it's wasted effort.

But more importantly, what do we know about element 0 at this point? It's the smallest value in the list and therefore will never be swapped again. Yet it will continue to be compared each time through the loop.

So on the first loop we know that you only need to compare element 0 to elements 1, 2, and 3. On the second loop, you must compare element 1 to only 2 and 3. Can you generalize this pattern?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Glad to help!
 
 
subject: how to create 5 objects of a class