CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Posts
    2

    Simple Addition Progs

    Hi I'm in an introductory Java course with a bad professor and I'm trying to get a hang of these basic concepts for an assignment using my notes and book, but im having lots of trouble compiling these things. If someone could quickly type out what a couple of these programs would look like, I could probably figure out the rest. Thanks so much.

    "Write a program that reads two numbers, let's say num1 and num2, each time using JOptionPane.showInputDialog, Then use, for instance,

    int value = Integer.parseInt(num1)

    to convert each number to an int. Then add them and print the results."

    Thats the most basic I think, but I keep getting errors like cannot resolve symbol.

    This next one seems much more difficult and I'm not sure where to begin:
    "Write a program that reads a 4-digit int, for instance, 3612, call it number. The program should separate the first two digits from the last two, and then add them. Thus in our example the program would assign 36 to a variable, let's say firstOne, and then assign 12 to a second variable, let's say secondOne. Finally, it would add the sum of firstOne and secondOne (here, 36 + 12, or 48) and assign it to a third variable, let's say sum.

    The output of your program should show the 4-digit integer read, the integer formed from the first two digits, the integer formed from the last two and then their sum. Hint: Use the %(mod) and /(intger division).

    Use JOptionPane.showInputDialog to read your data and assign the input to the string number. Then use

    int value = Integer.parseInt(number)

    to convert number to an int."

    If I can get these basics I feel like I could figure out the rest, but the info I have is really not helping, and I have never programmed before. Thanks again.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Simple Addition Progs

    Thats the most basic I think, but I keep getting errors like cannot resolve symbol.
    Every class exists within a package (in other languages it's also called a namespace). For instance, the JOptionPane is located within the "javax.swing" package. So, to use the JOptionPane in your code you'll have a couple of choices:

    1. Import all classes in the "javax.swing" package:
    Code:
    import javax.swing.*; 
    
    publc class YourClass
    {
        public void yourFunction()
        {
            // your JOptionPane here
            JOptionPane pPane = new JOptionPane(...);
        }
    }
    2. If you don't want to import all the classes in "javax.swing", but just the JOptionPane, then replace your import with this:
    Code:
    import javax.swing.JOptionPane;
    3. You can also refer to the class with it's "full" name:
    Code:
    class YourClass
    {
        public void yourFunction()
        {
            javax.swing.JOptionPane pPane = new javax.swing.JOptionPane(...);
        }
    }
    Hope this help you solve some of those "cannot resolve symbol" errors.

    - petter

  3. #3
    Join Date
    Sep 2005
    Posts
    2

    Re: Simple Addition Progs

    That seems to be a good start, but I still have no idea how to assemble this thing. What I have so far looks like this:

    import javax.swing.JOptionPane;

    public class Prog1
    {
    public static void main(String {} asd)
    {

    I dont know how to incorporate the actual JOptionPane to get integers, add them, and print a sum. He showed us something that I copied directly like this:

    char digit1 = '1';
    char digit2 = '2';
    int num1 = digit1;
    int num2 = digit2;
    int sum = num1 + num2;

    not sure if thats actually useful here?

  4. #4
    Join Date
    Aug 2003
    Posts
    102

    Re: Simple Addition Progs

    http://java.sun.com/j2se/1.5.0/docs/api/

    check the api documentation.................

  5. #5
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Simple Addition Progs

    Using JOptionPane to get input is pretty simple:
    Code:
    String input = JOptionPane.showInputDialog("Enter a number");
    int number = Integer.parseInt(input);
    That's your input routine for reading in 1 number, plain and simple (I havn't worked with JOptionPane for quite a while so it may need some tweaking in terms of what parameters to pass).

    As for separating the 4 digit number, you have to use a little math cleverness. If you mod(%) the number by 100, the result is the last two numbers, if you divide(/) a number by 100, you get the first two numbers.
    Last edited by cma; September 26th, 2005 at 09:34 AM.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured