Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

convert Java code to pseudo code

843785Jul 24 2008 — edited Jul 24 2008
any kind soul can help me convert Java code to pseudo code??? My due is 20hrs later !!! please help

package assignment;

import javax.swing.JOptionPane;
import javax.*;
public class Array
{
public static void main (String args[])
{
int inputInt = 0;
String errorMsg;
String wanaPlayInput;

do{ //set the flag for input as true at first
boolean inputAgain = true;

while (inputAgain){
String tryInput = JOptionPane.showInputDialog("How many numbers do you want to enter");
try{
errorMsg = "";
inputInt = Integer.parseInt(tryInput);//try to change string into integer type
}
catch(Exception e){
errorMsg = e.getMessage();//input is not a number
//System.out.println("Exception!!" + errorMsg);
}
if(errorMsg.equals(""))//if no error msg, input is a number
inputAgain = false;//need not re-input
}

Double arrayInt [] = new Double [inputInt];

String numInputStr;
double numInput;
int pos;

for(int i=0; i<inputInt; i++){
pos = i + 1;
numInputStr = JOptionPane.showInputDialog("Enter number " + pos + " between 1 and 100");
numInput = Double.parseDouble(numInputStr);
while (numInput < 1 || numInput > 100){
//System.out.println("Number is less than 1 or more than 100");
numInputStr = JOptionPane.showInputDialog("Enter number " + pos + " between 1 and 100");
numInput = Double.parseDouble(numInputStr);
}
if(numInput > 0 && numInput < 101)
arrayInt[i] = numInput;
}

for(int j=0; j<inputInt; j++){
double number = arrayInt[j];

double squaredNum = calSquare(number);
//double squaredNum = Math.pow(number,2);
//System.out.println(number + " squared is : " + squaredNum);

double cubedNum = calCube(number);
//double cubedNum = Math.pow(number,3);
//System.out.println(number + " cubed is : " + cubedNum);

String msgBox = number + " squared is " + squaredNum + "\n" +
number + " cubed is " + cubedNum;

JOptionPane.showMessageDialog (null, msgBox);
}
//System.out.println("I am outside the loop");
wanaPlayInput = JOptionPane.showInputDialog("Do you want to continue (y/n)?");
}
while (wanaPlayInput.equalsIgnoreCase("y"));
}

public static double calSquare(double number){
double squaredNum = Math.pow(number,2);
//System.out.println("Using calSquare : " + number + " squared is : " + squaredNum);
return squaredNum;
}

public static double calCube(double number){
double cubedNum = Math.pow(number,3);
//System.out.println("Using calCube : " + number + " cubed is : " + cubedNum);
return cubedNum;
}

}

Comments

PhHein
Of course people here could help* you, but you should do the start and ask for directions when you get stuck.

I think I've never seen it this way, normally people ask to turn pseudo code into Java.
843785
Look at the block of code and describe in short phrases or words what it does.
And you'll probably have it good enough for your teacher.

If you cannot do that, then well, what can I say; maybe you should get a 0 on the assignment.
843785
PhHein wrote:
I think I've never seen it this way, normally people ask to turn pseudo code into Java.
Indeed. What's the next step? Pseudo code --> problem statement?
843785
lol... my teacher alway concentrate on the coding... so when come to pseudo code, I'm clueless what to do and write
843785
jacelyn_chia wrote:
lol... my teacher alway concentrate on the coding... so when come to pseudo code, I'm clueless what to do and write
What is your question: how to write pseudo code? [http://en.wikipedia.org/wiki/Pseudo_code]
843785
ok, maybe I should ask those part i need help...

import javax.swing.JOptionPane;
import javax.*;
public class Array <--- any pseudo code for this??

JOptionPane.showInputDialog <--- this?
843785
DrLaszloJamf wrote:
PhHein wrote:
I think I've never seen it this way, normally people ask to turn pseudo code into Java.
Indeed. What's the next step? Pseudo code --> problem statement?
College would have been fastly easier had I been handed code, and tasked with writing an assignment for it.
843785
newark wrote:
DrLaszloJamf wrote:
PhHein wrote:
I think I've never seen it this way, normally people ask to turn pseudo code into Java.
Indeed. What's the next step? Pseudo code --> problem statement?
College would have been fastly easier had I been handed code, and tasked with writing an assignment for it.
A sort-of Java-centric version of the game show 'Jeopardy'
843785
newark wrote:
DrLaszloJamf wrote:
PhHein wrote:
I think I've never seen it this way, normally people ask to turn pseudo code into Java.
Indeed. What's the next step? Pseudo code --> problem statement?
College would have been fastly_ easier had I been handed code, and tasked with writing an assignment for it.
^^^^

Come to think of it, I saw someone eating M&Ms with a spoon, yesterday.
843785
It won't work it stops in a never ending loop!
package assignment;

import javax.swing.JOptionPane;
import javax.*;
public class Array
{
public static void main (String args[])
{
int inputInt = 0;
String errorMsg;
String wanaPlayInput;

do{ //set the flag for input as true at first
boolean inputAgain = true;

while (inputAgain){
843785
jacelyn_chia wrote:
ok, maybe I should ask those part i need help...

import javax.swing.JOptionPane;
import javax.*;
public class Array <--- any pseudo code for this??
In psudo code you don't need to declare data types.
JOptionPane.showInputDialog <--- this?
show it

Note: if you are doing a line by line, then you totally miss the point of psudo code; you are looking to get a high level overview of what this thing is supposed to do--pretty much block by block or group by group (block/group are just several lines of code that seem to fit together--what do they do) and not a line by line conversion. For instance a whole input routine for reading Images in could be described as "read JPG image" or even better "read image".
843785
DrLaszloJamf wrote:
newark wrote:
DrLaszloJamf wrote:
PhHein wrote:
I think I've never seen it this way, normally people ask to turn pseudo code into Java.
Indeed. What's the next step? Pseudo code --> problem statement?
College would have been fastly_ easier had I been handed code, and tasked with writing an assignment for it.
^^^^

Come to think of it, I saw someone eating M&Ms with a spoon, yesterday.
Oh man, there's some irony. I just posted in another thread that "fastly" wasn't a word...and 5 posts later I typo 'vastly' as 'fastly'. Go me.
PhHein
newark wrote:
Oh man, there's some irony. I just posted in another thread that "fastly" wasn't a word...and 5 posts later I typo 'vastly' as 'fastly'. Go me.
LOL
843785
is they a standard formal for starting and ending the pseudo code??
843785
jacelyn_chia wrote:
is they a standard formal for starting and ending the pseudo code??
Pseudo code is informal in syntax. The whole point is not to get hung up on details, because it's not going to be machine-parsed or -analysed.
843785
I'm really stress now looking all over the internet for good sample of pseudo code... c
an you roughly show me example of writing pseudo code for array and for loop? (best use my code)

Edited by: jacelyn_chia on Jul 24, 2008 8:35 AM
843785
jacelyn_chia wrote:
can you roughly show me example of writing pseudo code for array and for while? (best use my code)
Heh heh nice try. There was a sample in the Wikipedia article.

Think of pseudo code as a cooking recipe -- that level of informality and brevity.
843785
wikipedia only show you else if sample...

array and for loop.... never !!! I swear !!!!!
796447

jacelyn_chia wrote:
wikipedia only show you else if sample...

array and for loop.... never !!! I swear !!!!!

Ok ok I'll give in and do it for you. Here's your psedo-code to hand in.

loop
  ask someone to do it for me
until someone-gave-in or told-me-to-get-lost
if someone-gave-in
  copy code
else
  fail-course
end-if
843785
nice
843785
How would you represent a loop:

In Computer Programming--there are several things which are not standard--psudo code is one of those things. Pretty much any way you can think of to represent a loop will do. In much of the assignments I can remember doing in College, the Prof insisted that the psudo code be programming language syntax free.
843785
morgalr wrote:
How would you represent a loop:

In Computer Programming--there are several things which are not standard--psudo code is one of those things. Pretty much any way you can think of to represent a loop will do. In much of the assignments I can remember doing in College, the Prof insisted that the psudo code be programming language syntax free.
How about "loop over this and do stuff" or a variant thereof?

OP, you're making this a bigger problem than it really is. Just write out the rough steps your code is taking
843785
As I remember, the loop type of syntax was not needed--since looping is pretty much a specific solution: he wanted a higher look than that--what ever the loop would calculate, then a line saying:

calculate factorial

No implementation details needed, if you put a loop or actually tried to break down the algo and show looping, then you go points off.

The biggest problem I've seen in psudo code is the tendency to show too much detail, as the OP is keanly showing, and the psudo code becomes a specific solution to the problem usually modeled after the programmer's favorite language syntax.
843785
I once got a ton of psudo code from a consulting firm in Texas; it was patterened after Pascal as far as the syntax goes and had some C flavor thrown in too. The problem with it became the author in many places depended on you knowing what the Pascal or C implementation did, but in others you had to totally ignor what the language specific syntax meant. That made for an extreamly confusing interpretation of what the algorith actually was supposed to do.

We had to end up writing higher level psudo code for it, that had much clearer meaning, and use that new psudo code for the reference.
843785
jacelyn_chia wrote:
is they a standard formal for starting and ending the pseudo code??
If you already have a working Java program why not just comment it. The details are in the code and you just summarize in general informal words what happens. That should do as pseudo-code. You could pretend you wrote it before you got down to coding it in Java, which is the normal order.
1 - 25
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 21 2008
Added on Jul 24 2008
25 comments
9,755 views