I'm Overloading Sum

This week we introduced overriding, overloading, and interfaces. We will explore examples of all of these. We are tasked with overloading (creating multiple methods with the same name but different signatures) the sum method. We will consider the following types of inputs: short, int, float, and double. We will overload the sum method for all of these types for two and three parameters. A code skeleton has been provided below showing a method with two parameters. A method with three parameters would have something like int x, short y and double z. You should be implementing about 68 different versions of the sum method.

//This class overloads the sum method several ways.

public class OverloadingSum {
// int
public int sum(int x, int y) {
return x + y;
}
}

Zero Sums aren't Real Sums

Now that we have thoroughly explored overloading we are going to explore overriding. Create a class called ZeroSum.java. Take all of the methods from OverloadingSum.java and change them to return all zeros in ZeroSum.java. Next, create a class called RealSum.java. This class, RealSum, will extend the ZeroSum class. Having all zeros for our sums isn't very useful. We will need to override the parent, or super class methods to produce real results. Create the necessary methods to override all of those in the ZeroSum.java. When you are done run your classes against DrivingSum.java.

public class DrivingSum {
public static void main(String[] args) {
int x = 10;
int y = 20;
// x + y = 30....yay?
ZeroSum zero= new ZeroSum();
RealSum real= new RealSum();

System.out.println("Calling ZeroSum " + zero.sum(x, y) );
System.out.println("Calling ZeroSum " + real.sum(x, y) );
}
}

Implementing Sum Interfaces

Now that we completely understand the difference between overloading and overriding we are going to explore interfaces. Create an interface called InterSum.java. When creating an interface we do not use the keyword class but interface instead. Inside of InterSum.java copy all of the methods from OverloadingSum into InterSum.java. Having methods with a body will cause an error. Proceed to remove the body of each method. It should look something like:

public int sum(int x, int y);

Now that we have successfully created our interface we will need to implement it. Create a file called ImplementingSum.java. This file will be a class that implements the InterSum interface. To make the ImplemtingSum.java file work will need to add a body to each of those methods we just removed earlier.

Classes are not restricted with implementing just one interface. Implement the Multiply.java interface below in ImplementingSum.java. You will need to fill in the methods to produce the multiplication of the parameters.

Now that we have two interfaces implemented we should test them. Create a driver file called GettingSumImplements.java. Create an instance of ImplementingSum.java. Then call five sum methods with different parameter types and five multiply methods with different parameter types. This should let you get a feel for the class.

public interface Multiply {
// short, short x2 x3
public short multiply(short x, short y);

public short multiply(short x, short y, short z);

// int
public int multiply(int x, int y);

// int with short
public int multiply(short x, int y);

public int multiply(int x, short y);

// int
public int multiply(int x, int y, int z);

// int with short
public int multiply(short x, int y, int z);

public int multiply(int x, short y, int z);

public int multiply(int x, int y, short z);

public int multiply(short x, short y, int z);

public int multiply(short x, int y, short z);

public int multiply(int x, short y, short z);

// float with float, int, short by 2
public float multiply(short x, float y);

public float multiply(float x, short y);

public float multiply(float x, int y);

public float multiply(int x, float y);

public float multiply(float x, float y);

// float by 3
public float multiply(float x, float y, float z);

// float with int
public float multiply(int x, float y, float z);

public float multiply(float x, int y, float z);

public float multiply(float x, float y, int z);

public float multiply(int x, int y, float z);

public float multiply(int x, float y, int z);

public float multiply(float x, int y, int z);

// float with short
public float multiply(short x, float y, float z);

public float multiply(float x, short y, float z);

public float multiply(float x, float y, short z);

public float multiply(short x, short y, float z);

public float multiply(short x, float y, short z);

public float multiply(float x, short y, short z);

// float with short and int
public float multiply(short x, int y, float z);

public float multiply(int x, short y, float z);

public float multiply(short x, float y, int z);

public float multiply(int x, float y, short z);

public float multiply(float x, int y, short z);

public float multiply(float x, short y, int z);

// doubles with double, float, int, shorts by 2
public double multiply(short x, double y);

public double multiply(double x, short y);

public double multiply(int x, double y);

public double multiply(double x, int y);

public double multiply(float x, double y);

public double multiply(double y, float x);

public double multiply(double x, double y);

public double multiply(double x, double y, double z);

// double with float
public double multiply(float x, double y, double z);

public double multiply(double x, float y, double z);

public double multiply(double x, double y, float z);

public double multiply(double x, float y, float z);

public double multiply(float x, double y, float z);

public double multiply(float x, float y, double z);

// double with ints
public double multiply(int x, double y, double z);

public double multiply(double x, int y, double z);

public double multiply(double x, double y, int z);

public double multiply(int x, int y, double z);

public double multiply(int x, double y, int z);

public double multiply(double x, int y, int z);

// double with shorts
public double multiply(short x, double y, double z);

public double multiply(double x, short y, double z);

public double multiply(double x, double y, short z);

public double multiply(double x, short y, short z);

public double multiply(short x, double y, short z);

public double multiply(short x, short y, double z);

// double, float, with int or short
public double multiply(double x, float y, int z);

public double multiply(double x, float y, short z);

public double multiply(float x, double y, int z);

public double multiply(float x, double y, short z);

public double multiply(int x, float y, double z);

public double multiply(short x, float y, double z);
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.