Is my understanding of writing pseudocode (java) correct?
So I just learned how to write pseudo codes and so let's say this is my code
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
class Watever{
public static final String foo="12345151";
public static String today;
public static String expiry;
public static void date(){
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yy");
Calendar calen = Calendar.getInstance();
Date todayDate = calen.getTime();
today = DATE_FORMAT.format(todayDate);
calen.add(Calendar.MONTH, 4);
Date expirationDate = calen.getTime();
expiry = DATE_FORMAT.format(expirationDate);
}
public static void main(String[] args){
date();
System.out.println(today);
int bar = Integer.parseInt(foo);
System.out.println(bar);
}
}
Would this be acceptable pseudocode (considering we are going to write the code in java)
PROGRAM Watever:
Declare public final String foo="12345151";
Declare public String today;
method date():
GET Today date;
today= Today date;
expiry= today+ 4 MONTHS;
Format today AND expiry to "dd/MM/yy"
method main(String[] args):
CALL date();
PRINT today;
Convert String foo to int bar;
PRINT bar;
END
and say if we are using scanner object for system.in we have to say we used it or just use
PROMPT user "Enter number: ";
GET num;
also how we write pseudocode for printf (formatted string)
source to share
If someone asks for pseudocode, it usually means a quick sketching algorithm. Still carefully crafted, of course. It looks at what it really means and comes up with a choice of steps and data structures. See here for how a fuzzy requirement can be refined and put into code.
The pseudocode usually looks a little better than what you've tried. So,
- Be consistent in style: if you use reserved words with small caps (or bold), do it everywhere.
- IDs with spaces are ok, but (from my experience with Algol68) I would prefer they had small letters and quotes.
- Function calls, individual parameters are best read when the separation is clear, with rounds and commas.
So:
PROGRAM Whatever:
CONSTANT String foo = "12345151";
FIELD String today;
METHOD date ():
today := LIBRARY todays date();
expiry := today + 4 MONTHS;
today := date format(today, expiry, "dd/MM/yy").
BEGIN
date();
PRINT(today);
bar := convert string to int(foo);
PRINT(bar);
END
About "Convert String foo to int bar": This is also a top-down delete method. It declares the intent of a piece of code and documents the code nicely with it.
whatever:
String foo;
...
convert string foo to integer bar;
...
... .
convert string foo to integer bar:
int bar = atoi(foo).
And last but not least, about the content, the pseudo-algorithm itself. In your case, you only act when called date
without parameters and result, and this does the read and as a side effect stores something calculated in the field today
. More useful would be a function without a side night. The best name of course.
source to share