Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do u write algorithms/psuedo code

Status
Not open for further replies.

badbhoy

Programmer
Apr 11, 2002
12
GB
i have write this program and i am finding it very hard to document in the form off algorithms/ pseudo code
any help would be great

public class Booking implements Serializable
{
static BufferedReader keyboard = new
BufferedReader(new InputStreamReader(System.in));


private Vector readvector;
private Vector writevector;

public Booking()
{
readvector = new Vector(20);
}


public void recordticket(DataRecorder afine)
{
readvector.addElement(afine);
}

public String toString()
{
String tempString = "";

if(readvector.size() > 0)
{
for(int count = 0; count < readvector.size(); count = count + 1)
{
DataRecorder afine = (DataRecorder)readvector.elementAt(count);
tempString = tempString + afine.toString() + &quot;\n\n&quot;;
}
}
else
{
tempString = &quot;No Records Stored&quot;;
}

return tempString;
}

// Save data to file

public void save()
{
try
{
FileOutputStream ostream = new FileOutputStream(&quot;Tickets.dat&quot;);
ObjectOutputStream oos = new ObjectOutputStream(ostream);

for(int i = 0; i < readvector.size(); i++)
{
oos.writeObject(readvector.elementAt(i));
}

oos.flush();
ostream.close();

}
catch(Throwable e)
{
System.out.println(&quot;Error writing - &quot; + e.getMessage());
}
}

public void load()
{
writevector = new Vector();

try
{
FileInputStream istream = new FileInputStream(&quot;Tickets.dat&quot;);
ObjectInputStream ois = new ObjectInputStream(istream);
DataRecorder temp;

int i = 0;

do
{
temp = (DataRecorder)ois.readObject();
writevector.addElement(temp);
i++;
}

while(i<readvector.size());

istream.close();

for(int j = 0; j<readvector.size(); j++)
{
System.out.println(writevector.elementAt(j).toString());
keyboard.readLine();
}

}
catch(Throwable e)
{
System.out.println(&quot;Error reading- &quot; + e.getMessage());
}

}

}
 
{rant}
Hmm... the best code I write is the one I document before writing. :->

But don't let that stop you from writing it now! Here's some general stuff to keep in mind:

There are three kind of comments in java:
/**
* Javadoc with two stars
*/
/*
* multiline comments good for describing code flow and
* commenting out code
*/
//Single line comments good for personal comments or details
//use many if needed in a section if it is details!
//or to comment out just one line of code

Write a javadoc comment at the top of the class that describes it in general terms, followed by a code flow comment if needed.

Write a Javadoc comment for all the important methods (try to make their names obvious too!)... use @param a lot.

Document any weird algorythms and flag break points with the '//'

Your class is not very complex, so it shouldn't take you very long. But in the future, consider spending time before coding to develop the comments. Here's an example of code I was asked for last week:

/**
* Abstract class for String utilities
*/
public abstract class StringUtils
{
private static Category cat = Category.getInstance(&quot;StringUtils&quot;);

/**
* Replaces the first occurence of a substring
* @param source string that needs to have stuff replaced
* @param oldVal name of variable in Source string
* @param newVal replacement to put in String
* @return String
*/
public static String replace(String source, String oldVal, String newVal)
{
if (oldVal == null) return source;

//cat.debug(&quot;replacing:&quot; + oldVal + &quot;: by :&quot; + newVal + &quot;:&quot;);
StringBuffer SB = new StringBuffer(source);
int nameindex = source.indexOf(oldVal);
SB.replace(nameindex, nameindex + oldVal.length(), newVal);
//cat.debug(&quot;new StringBuffer: &quot;+SB);
return new String(SB);
}

/**
* Replaces all occurences of a substring
* @param source string that needs to have stuff replaced
* @param oldVal name of variable in Source string
* @param newVal replacement to put in String
* @return String
*/
public static String replaceAll(String source, String oldVal, String newVal)
{
while(source.indexOf(oldVal) != -1)
{
source = replace(source, oldVal, newVal);
}
return source;
}
...
}

As you can imagine, if you had those comments ready for you, it wouldn't take you much time to actually write the code. I wrote the comments with skeletal methods to see that what I was creating actually met the needs of the other members of my development team (ran javadoc and showed them that). That way I know for sure that what I do will result in code that gets used (and that means less code and less work -- yeah!)

{end rant}
 
Badbhoy,

I am not sure if you want to product a simplified psuedo code version of the above code, or an external documetation of the algorithms, or just JavaDocs. If it is JavaDocs, the Daniel's is a great start for you. If you want more detail JavaDoc information you can go to the following link:


Here you will find FAQ, HOWTO, and Requirements for JavaDocs produced by Sun themselves.

If JavaDocs is not what you were looking for please post again what you are looking for with some mroe details...

Rodney
 
i want to produce simple pseudo code for documentation
that explain each line number for some one to know what each line is doing in simple terms
thanx
 
I am not familar with anyone every really doing this. As for what pseudocode is, it is just a notation that is similar to a programming language, but will not compile. It combines some structor of a programming language with an informal natural-language description of what is going on, the computations to be done.

It is not ment as a line by line description of your code. It is mainly used for conseptualizing what you want to do before you write a class, or a method. So that you can structure it like the language you are using, but use plain english to describ what the class or method is going to do. It normally does not lower than the method level.

If you want to do a LINE BY LINE documentation of your code you should just open up a word processor, and start type it out, with your code opened in text editor.

Java Docs may still work for you and will produce a HTML file for your documentation. You may want to look at the above links about Java Docs. Also if you just want internal code documenation, Java Docs work well or you can just use the normal comments so that they are not outputted to a Java Doc HTML file.

Anyway, I am not sure I can be of more help than this... sorry if this information is not helpful.

Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top