Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,774 people online right now. Registration is fast and FREE... Join Now!




Psuedocode to JAVA help

 
Reply to this topicStart new topic

Psuedocode to JAVA help, this has to be done using import javax.swing.JOptionPane instead of im

trayyrho
9 Oct, 2007 - 11:34 PM
Post #1

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 9


My Contributions
this is what i need help converting using import javax.swing.JOptionPane

BEGIN SIMPLEMATH
GET DATA
GET FIRST INTEGER FROM USER
GET SECOND INTEGER FROM USER
END GET DATA
PERORM MATH
CALCULATE SUM: FIRST + SECOND
CALCULATE DIFFERENCE: FIRST - SECOND
CALCULATE PRODUCT: FIRST * SECOND
CALCULATE QUOTIENT: FIRST / SECOND
END PERFORM MATH
DISPLAY ALL ANSWERS

END


this is what i have but was wrong because i used java.util instead of javax.swing.JOptionPane. I'm a beginner in JAVA and need help with this


CODE

import java.util.*;
public class SimpleMath
{
System.out.println("Type in the first integer");
Scanner sc = new Scanner(System.in);
double firstInteger = sc.nextInt();

System.out.println("Type in the second integer");
Scanner sc = new Scanner(System.in);
double secondInteger = sc.nextInt();

double sum = firstInteger + secondInteger;
double difference = firstInteger - secondInteger;
double product = firstInteger * secondInteger;
double quotient = firstInteger / secondInteger;

System.out.println("sum: " + sum);
System.out.println("difference: " + difference);
System.out.println("product: " + product);
System.out.println("quotient: " + quotient);
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Psuedocode To JAVA Help
10 Oct, 2007 - 12:46 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi trayyrho, welcome to </dream.in.code> smile.gif


Now here is a code that works, and I only had to do a few changes.


CODE
import java.util.*;
import javax.swing.JOptionPane;
public class SimpleMath
{
    public static void main(String [] args)
    {
        
        
        double firstInteger = Integer.parseInt(JOptionPane.showInputDialog(null,"Type in the first integer:"));
        
        
        double secondInteger = Integer.parseInt(JOptionPane.showInputDialog(null,"Type in the second integer:"));
        
        double sum = firstInteger + secondInteger;
        double difference = firstInteger - secondInteger;
        double product = firstInteger * secondInteger;
        double quotient = firstInteger / secondInteger;
        
        System.out.println("sum: " + sum);
        System.out.println("difference: " + difference);
        System.out.println("product: " + product);
        System.out.println("quotient: " + quotient);
        JOptionPane.showMessageDialog(null,"sum: "+sum+"\ndifference: "+difference+"\nproduct: "+product+"\nquotient: "+quotient);
    }

}



Now let me explain, what I did. First in the code you posted you don't have a main() function. In almost every program you'll have a main() function, so that the program will work, the function is:

CODE
public static void main(String [] args)
{
   // CODE HERE
}


And now in the code I used JOptionPane.

NOTE: for every new function or class try to read it's functionalities and constructs from the Java API, see the link below:

http://java.sun.com/javase/6/docs/api/index.html

Another very awesome alternative people use these last 9 yrs is Google wink2.gif

So I hope that you find this useful. And below is a code that works in the way you tried it:

CODE
import java.util.*;

public class SimpleMath
{
    public static void main(String [] args)
    {
        
        System.out.println("Type in the first integer");
        Scanner sc = new Scanner(System.in);
        double firstInteger = sc.nextInt();
        
        System.out.println("Type in the second integer");
        sc = new Scanner(System.in);// NO NEED FOR Scanner in front of sc
        double secondInteger = sc.nextInt();
        
        double sum = firstInteger + secondInteger;
        double difference = firstInteger - secondInteger;
        double product = firstInteger * secondInteger;
        double quotient = firstInteger / secondInteger;
        
        System.out.println("sum: " + sum);
        System.out.println("difference: " + difference);
        System.out.println("product: " + product);
        System.out.println("quotient: " + quotient);
            }

}


One more thing in the code you posted you define the same Scanner twice.
You shouldn't do that because there is no need and because it is an error for the compiler at least. I commented that in the last code.
So as I said I hope you find this useful and I hope you learn from it.

Enjoy smile.gif

This post has been edited by PennyBoki: 10 Oct, 2007 - 12:48 AM
User is offlineProfile CardPM
+Quote Post

trayyrho
RE: Psuedocode To JAVA Help
10 Oct, 2007 - 01:47 AM
Post #3

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 9


My Contributions
Thank you!!! JAVA is very intimidating yet interesting. I guess as I progress in JAVA, the programming rules will become more clearer.
r/



QUOTE(PennyBoki @ 10 Oct, 2007 - 01:46 AM) *

Hi trayyrho, welcome to </dream.in.code> smile.gif


Now here is a code that works, and I only had to do a few changes.


CODE
import java.util.*;
import javax.swing.JOptionPane;
public class SimpleMath
{
    public static void main(String [] args)
    {
        
        
        double firstInteger = Integer.parseInt(JOptionPane.showInputDialog(null,"Type in the first integer:"));
        
        
        double secondInteger = Integer.parseInt(JOptionPane.showInputDialog(null,"Type in the second integer:"));
        
        double sum = firstInteger + secondInteger;
        double difference = firstInteger - secondInteger;
        double product = firstInteger * secondInteger;
        double quotient = firstInteger / secondInteger;
        
        System.out.println("sum: " + sum);
        System.out.println("difference: " + difference);
        System.out.println("product: " + product);
        System.out.println("quotient: " + quotient);
        JOptionPane.showMessageDialog(null,"sum: "+sum+"\ndifference: "+difference+"\nproduct: "+product+"\nquotient: "+quotient);
    }

}



Now let me explain, what I did. First in the code you posted you don't have a main() function. In almost every program you'll have a main() function, so that the program will work, the function is:

CODE
public static void main(String [] args)
{
   // CODE HERE
}


And now in the code I used JOptionPane.

NOTE: for every new function or class try to read it's functionalities and constructs from the Java API, see the link below:

http://java.sun.com/javase/6/docs/api/index.html

Another very awesome alternative people use these last 9 yrs is Google wink2.gif

So I hope that you find this useful. And below is a code that works in the way you tried it:

CODE
import java.util.*;

public class SimpleMath
{
    public static void main(String [] args)
    {
        
        System.out.println("Type in the first integer");
        Scanner sc = new Scanner(System.in);
        double firstInteger = sc.nextInt();
        
        System.out.println("Type in the second integer");
        sc = new Scanner(System.in);// NO NEED FOR Scanner in front of sc
        double secondInteger = sc.nextInt();
        
        double sum = firstInteger + secondInteger;
        double difference = firstInteger - secondInteger;
        double product = firstInteger * secondInteger;
        double quotient = firstInteger / secondInteger;
        
        System.out.println("sum: " + sum);
        System.out.println("difference: " + difference);
        System.out.println("product: " + product);
        System.out.println("quotient: " + quotient);
            }

}


One more thing in the code you posted you define the same Scanner twice.
You shouldn't do that because there is no need and because it is an error for the compiler at least. I commented that in the last code.
So as I said I hope you find this useful and I hope you learn from it.

Enjoy smile.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month