1. Give a definition of a class named Arithmetic whose methods are three add() and multiply() and one subtract() and divide().
  2. Parameter for add() and multiply() should be two, three and four int respectively.
  3. Parameter for subtract() and divide() should be two int.
  4. Applying method calling in the main(), test your program by calling the following methods:
     add(1,3)
     add(1,3,4,5)
     multiply(2,3,4)
     multiply(1,1)
     subtract(3,1)
     divide(5,2)

can anyone please assist me in doing this?

Recommended Answers

All 10 Replies

show us what you've done, and tell us where you're stuck, and who knows, someone might enlighten you.

But we're NOT here to do your homework for you (and yes, we can tell that this is a homework assignment, you didn't even bother reformatting it before you pasted it here verbatim).

can you please give me a tip ? this assignment is all about polymorphism,overriding and overloading. can you tell if i've done it right?

//class one

  public class ClassOne {

        public int add(int num1,int num2){
            return(num1+num2);
        }
        public int add(int num1,int num2,int num3,int num4){
            return(num1+num2+num3+num4);
        }
        public int multiply(int num1,int num2,int num3){
            return(num1*num2*num3);
        }
        public int multiply(int num1,int num2){
            return(num1*num2);
        }
        public int subtract(int num1,int num2){
            return(num1-num2);
        }
        public float divide(float num1,float num2){
            return(num1/num2);
        }
    }






`//test driver`
    public class Arithmethic {
        public static void main(String[] args) {
            ClassOne classOne = new ClassOne();
            System.out.println(classOne.add(1,3));
            System.out.println(classOne.add(1,3,4,5));
            System.out.println(classOne.multiply(2,3,4));
            System.out.println(classOne.multiply(1,1));
            System.out.println(classOne.subtract(3,1));
            System.out.println(classOne.divide(5,2));
        }
    }

Before asking someone else to check your code try to compile and run it. If it runs and gives the correct answers then it's probably OK.

@JamesCherrill im just a beginner here. self studying ofcourse. i just want the opinion of others. ofcourse it is running. but i want to know if i applied polymorphism,overriding or overloading. thats all. thankyou

overriding, no, since there is no inheritance, overloading, yes, polymorphism ... well, overloading is considered to be "static polymorphism" so ... I guess, yes, but it's not something like dynamic polymorphism, which can be achieved through extending a class or implementing an interface.

im just a beginner here. self studying ofcourse. i just want the opinion of others. ofcourse it is running. but i want to know if i applied polymorphism,overriding or overloading. thats all.

OK, no problem. Your add and mult methods, which have the same names but different parameter lists are a good example of overloading. For the rest you will need a more complex setup that has more than one one class in some kind of class hierarchy.

I assume you are following some kind of on-line course or set of tutorials? If so they should introduce all those examples in some kind of sensible order. In any case, please feel free to come here for help and advice.

Your code is a good example of overloading. If you want to practice with overriding, you should create new class and this class inherits some properties from ClassOne class. And re-write methods such as add, substract... That is example of overriding.

i observe all examples in this website but i recognize that there is no word printf and scanf why??

i am filipino and im only first year student.maraming salamat (thank you)

Then you need to read the forum rules. Please do NOT resurrect old thread if it is not the same issue. Also, printf & scanf are for C++ if I remember correctly (haven't coded in the language for a while). This is Java...

If you have a question like this, please start a new thread rather than resurrecting a long-dead one like this.

As for why printf() and scanf() aren't used in Java very often, well, to begin with, ther is no scanf() method in the standard Java library. Rather, the Scanner class is used for the same purpose, which not only has fewer issues than the C style scanf(), but gives better control of the input.

As for printf(), it was introduced into the language fairly late on, and is usually only used for fairly complex string formatting; most of the things which you would use printf() for in C are handled better by the overloaded forms of print() and println(). Even with complex formatting, it is more common to use the String.format() mathod to do the string interpolation, then display the generated string with print().

I should add that even in C, scanf() has fallen into disuse for most modern programming, as it has a number of pitfalls to it. Today, most experienced programmers use fgets() to get the input and then parse the string using sscanf(), which is more secure and less prone to buffer overruns and newline issues.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.