Question:
Why isn't list a parameter of the method (JAVA programming language)?
Unmanned
2012-02-12 16:39:27 UTC
static LinkedList Create(String nameFile)
throws IOException
{ LinkedList list = new LinkedList();
boolean Fileexists = true ;

FileReader fi = null;


try {
fi = new FileReader (nomFichier) ;
}

catch ( java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nameFile);
Fileexists = false ;
}

if (Fileexists) {


BufferedReader enter = new BufferedReader(fi);
boolean endFile = false ;


while ( !endFile ) {


String aLine = enter.readLine();


if (aLine == null)
endFile = true ;
else {

String name = aLine.substring(0, 10);
char sex = aLine.charAt(30);
double height = Double.parseDouble(aLine.substring(37, 41));
double weight = Double.parseDouble(aLine.substring(51, 56).trim());
int number = Integer.parseInt(aLine.substring(64));

liste.add(new People(name, sex, height, weight, number));
}
}
enter.close();
}

return list;
}
Three answers:
Ratchetr
2012-02-12 16:49:06 UTC
It is easier for the caller. When Create returns a list, the caller just has to write:



LinkedList p = foo.Create("PeopleFile");



If the list is passed in as a parameter, the caller needs to do:



LinkedList p = new LinkedList();

foo.Create("PeopleFile",p);



It's not a huge difference, but the first form seems more natural to me. The one advantage of the second form is that you could create users from multiple files more easily:





LinkedList p = new LinkedList();

foo.Create("PeopleFile",p);

foo.Create("AnotherFile",p);



But if you don't expect to need that functionality, go with the first form.
deonejuan
2012-02-12 16:54:41 UTC
ambiguious question.

1. Create() is cap and against code style convention, but let's that is a method and that it is a member of some UnNamedClass.

2. Create() only has one parameter( String nameFile)

3. There is no pointer to nomFichier as in: fi = new FileReader (nomFichier) ;

That probably needs to be nameFile



I don't see where list needs to be passed into Create() method. You probably want to make other code with list. You should make LinkedList a class member



public class TelephoneBook {

private final LinkedList list;

...

void LinkedList Create( String nomFichier ) {

list = new LinkedList();
anonymous
2016-05-16 08:18:28 UTC
Method-a.k.a as functions in C language. part / subprograms. Object- is the graphical representation of real world objects to object-oriented programming Encapsulation- information hiding, replacing information to hide original state, behavior ect. Signal- is a condition that may be reported during program execution, and can be ignored, handled specially, or, as is the default, used to terminate the program Operation- how the codes will be executed. Data Element- element within that data. example: x=0; [0 is the data inside x-element.] Data Structure- study of how data are manipulated. List- or linked-list: elements pointing to another element within an array [array implemented linked-list]. Stack-can have any abstract data type as an element, but is characterized by only two fundamental operations, the push (add) and the pop (delete), last-in first-out [LIFO]. Queue- rear(insert) and front (delete) first-in first-out [FIFO].


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...