Lance Hall
Lance Hall

Reputation: 202

Don't understand how to override parent function from subclass

I have a problem that I am stuck on. I have a menu driven calculator. My calculator class contains the method displayMenu(); which looks like

public static int displayMenu(){

    Scanner input=new Scanner(System.in);

    int choice=0;
    do{
    System.out.println("1.Add");
    System.out.println("2.Subtract");
    System.out.println("3.Multiply");
    System.out.println("4.Divide");
    System.out.println("5.Clear");
    System.out.println("6.Quit");
    System.out.println("What would you like to do?");
    }while(!input.hasNextInt());
    choice=input.nextInt();
    return choice;
}

I am being asked to create a subclass called ScientificMemCalc. The subclass should use power of inheritance to add new options to my menu. The new printout should look like

Menu 1. Add 2. Subtract 3. Multiply 4. Divide 5. Power 6. Logarithm 7. Clear 8. Quit

What would you like to do?

I know to start with

public class ScientificMemCalc extends MC{

}

but I do not understand how to override the displayMenu() function to show the new options. I get how to add functionality for the new options, I think, but how do I change the printout without rewriting the whole method in the subclass?

Upvotes: 1

Views: 87

Answers (1)

deanosaur
deanosaur

Reputation: 611

Try to keep the things you are displaying (i.e. the list of menu item strings) separate from the code which actually displays them.

In the code you've written, the list of menu items strings are hardcoded into your display code, which you can think of as the calls to System.out.println function.

System.out.println("1. ...");
System.out.println("2. ...");

There is no way to add another item to the list except by modifying the code that is calling System.out.println.

What you need to do is store the menu items (a list of strings) in some data structure (e.g. an array, list, or map). Then have some display code that reads the items from the data structure and displays them using System.out.println().

You be creating:

  1. A data structure that stores you menu item strings. You have lots of options: arrays, lists, maps

  2. A function that reads the data structure you've created and prints the menu items to the screen.

compare

System.out.println("1. menu item choice1");

with:

String[] menuitems = new String[]{"1. menu item choice1", "2. menu item choice2"};

for(String item : menuitems) System.out.println(item);

Now you can add things to the menu items array and they will be automatically printed in the for loop.

class MC {
    protected String[] menuitems = new String[]{"1. menu item choice1", "2. menu item choice2"};

    public void displayMenu() {
        for(String item : menuitems) System.out.println(item);
    }
}

Now if you subclass MC you can change menuitems. the displayMenu() will display your changes.

You may also want to consider:

  1. a function that takes a menu item string and adds it to the data structure
  2. a function that reads the user's input
  3. another data structure that maps your menu items to actual functions that implements them.

That last one could actually be the same data structure that stores the menu item. In fact, you might want to think about how you'll map the menu item string, the user's input (int), and the function that is being called.

Upvotes: 1

Related Questions