I’ve been working on Java homework. Here are my instructions:
Interface Programming Assignment
Create an interface named ISum. The interface should define 2 methods. The first method should take two integers as arguments and return their sum. The second method should take 2 strings as arguments and return their concatenation. The second method should be an overloaded version of the first one.
Define another interface named IAverage. The interface should define one method that takes 2 integer arguments and returns the average.
Create a class named Calculator that implements both the interfaces defined above. Your class MUST implement exception handling. Create a driver class that allows a user to call each of the methods in your class.
My work so far:
Main class:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Calculator calculatorObject = new Calculator();
Scanner scannerObject = new Scanner(System.in);
System.out.println("Enter 1 for integer or 2 for string");
int test1 = scannerObject.nextInt();
switch (test1){
case 1:
System.out.println("Enter 1st number");
int int1 = scannerObject.nextInt();
System.out.println("Enter 2nd number");
int int2 = scannerObject.nextInt();
System.out.println("Enter 1 for sum or 2 for average");
int test2 = scannerObject.nextInt();
switch (test2){
case 1:
int sum = calculatorObject.intSum(int1,int2);
System.out.println("The sum is " + sum);
break;
case 2:
int avg = calculatorObject.intAvg(int1,int2);
System.out.println("The avg is " + avg);
break;
default:
System.out.println("You entered an invalid option");
break;
}
break;
case 2:
System.out.println("Enter 1st string");
String string1 = scannerObject.nextLine();
System.out.println("Enter 2nd number");
String string2 = scannerObject.nextLine();
String stringConcat = calculatorObject.stringSum(string1,string2);
System.out.println("The sum is " + stringConcat);
break;
default:
System.out.println("You entered an invalid option");
break;
}
}
}
ISum interface:
public interface ISum {
public void intSum();
public void intAvg();
}
IAverage interface:
public interface IAverage {
public void intAvg();
}
Calculate class:
abstract class Calculator implements IAverage, ISum {
public int intSum (int1,int2){
int int1;
int int2;
int sum = int1 + int2;
return sum;
}
public String stringSum (string1,string2){
String string1;
String string2;
String stringConcat = string1.concat(string2);
return stringConcat;
}
public int intAvg(int1,int2){
int int1;
int int2;
int avg = (int1 + int2)/2;
return avg;
}
}
you redeclare and reinitialize your parameter variables before using them, so they are blank by the time you touch them.
For instance:
In the first line, public int intSum (int1,int2){, thats you declaring int1 and int2 as method-local variables. You do not need to then do int int1; or int int2;. By including these lines you overwrite them with nothing. For the above method, you just need:
Edit: derp, reading fail. you should also give the parameters a type, so