0

I am new to Java and still trying to understand the basics. I can write code within a class and method but really struggling with working across classes. To help I am trying to write a very simple programme that can add or subtract two numbers. This program will have three classes:

Class 1

package christmas;

public class addintegers 

{

public static int add2numbers(int a, int b)

{
    return (a+b);

} }

and the second class

package christmas;

public class subtractintegers {


    public static int sub2numbers(int a, int b)

    {
        return (a-b);
    }
    }

what I now want to do is to write a third class that contains a main method that can read two numbers and an operator (+ or -) from the keyboard and then depending on the operand call the addintegers or subtractintegers classes.

This seems basic and it is not homework, I am simply trying to understand through a basic example how to build more complex code to work across mumtiple classes.

I know how to read from the keyboard, the help I need is how to call the different classes from a main method that is in a class of its own.

Thank you in advance - I am sure this is easy for many but hard for me to understand.

Sooraj
  • 565
  • 3
  • 8
  • 1
    This is really super basic stuff. Calling other methods is something that each and any book or tutorial will explain to you on page two. Sorry, but you are expected to do serious research prior posting a question. – GhostCat Dec 19 '19 at 12:32
  • Because you've defined your methods as static, you don't need an instance of the classes within to use them so taking one of them as an example you can simply do subtractintegers.sub2numbers(2,1); from your class with the main method. As to whether this is a good way of designing it is another question but you should be able to use what you've done here. – Old Nick Dec 19 '19 at 12:33
  • Well, in the case of static methods the classes are more of a namespace, so you'd call them like `int result = addintegers.add2numbers( valueA, valueB);` etc. - note that while progressing with your studies you'll want to learn about the naming conventions (e.g. class names should start with an upper case letter and use camel case - so at least names them `AddIntegers` etc.). Also note that things will change with instance method (non-static) as you'll then have to deal with polymorphism etc., but you _will_ have to learn that because that's where Java really starts :) – Thomas Dec 19 '19 at 12:33
  • As you are defining method as static you don't need to create the object for both the classes.First read the operator and two numbers.If it is "+" then call ```addintegers.add2numbers(number1,number2)``` if it is "-" then call ```subtractintegers.sub2numbers(number1,number2)```.But It is better not to define the class as static. – Priya Sri Dec 19 '19 at 13:53
  • thank you to everyone for your patience and help. This has helped me move forward. Thank you. –  Dec 19 '19 at 15:17

1 Answers1

-2

To call the christmas.addintegers.add2numbers method from another class, you just do christmas.addintegers.add2numbers(foo, bar) (where foo and bar are some already initialized integers.)

ansjob
  • 175
  • 7