简体   繁体   中英

Can anyone help me modify my code in Java?

Okay to make this clearer this is what i need done to my entire program. I need main to restore any calculators that are in the file "calc.bak" before presenting the user with a menu and main should save all calculators that exist in that same file (overwrite as appropriate) just before exiting. I also need to give the user the option to create a brand new calculator, name it, and add a function to it from an already existing collection (which are my Add, multiply, and divide). I also need the user to create their own function by designing and naming a combination of any pair of functions. The ouput of the first function would necessarily be input to the second function. For example, the user can create a new function called "addmult" that calls the add function (which prompts for two numbers and adds them) and establishes the sum as one of the operands for the multiply function (which would have to prompt for its second operand).

The sample out put should look like this: Welcome to the Calculator Configurator

  1. restore a calculator from a file

  2. create a calculator from scratch

  3. let me create a calculator you can use

2

OK. so you want to create one yourself.

What is the name of your calculator? Fred

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu):

Add(1)

Multiply(2)

Divide(3)

Pair of functions(4)

input (2)

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu):

Add

Multiply

Divide

Pair of functions

1 Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu): Add

Multiply

Divide

Pair of functions

4

Provide a name for this pair:

BothAddAndMult

Provide a description for this pair:

multiplies and then adds

Which function should be first?

Multiply(0)

Add(1)

0

Which function should be first?

Multiply

Add

1

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu):

Add

Multiply

Divide

Pair of functions

4

Provide a name for this pair:

MultAfter

Provide a description for this pair: multiplies last after a multiply and add

Which function should be first?

Multiply

Add

BothAddAndMult

2

Which function should be first?

Multiply

Add

BothAddAndMult

Indicate which functions from our stock you'd like to add to your calculator (enter 0 to quit this menu):

Add

Multiply

Divide

Pair of functions

0

I am a calculator named Fred

  1. quit

  2. clear memory

  3. multiply two numbers

  4. add two numbers

  5. multiplies and then adds

  6. multiplies last after a multiply and add

Can someone help me reach this output?

Here is my code so far

CalcConfig.java

import java.util.Scanner;

// program to model a configurable calculator
public class CalcConfig {
 public static void main(String [] args)
 {
   System.out.println("Welcome to the Calculator Configurator");
   Scanner kbd = new Scanner(System.in);
   CalcFunction [] funs = {
     new Add(kbd, "add two numbers"),
     new Multiply(kbd, "Multiply two numbers"),
     new Divide(kbd, "divide two numbers")};
   Calculator calc = new Calculator(funs);
   calc.go(kbd);

}
}

Calculator.java

//my Calculator class

import java.util.Scanner;

// models a configurable calcuator
public class Calculator {
   private CalcFunction [] functions;
   private double memory = 0;
   private boolean clear = true;

public Calculator(CalcFunction [] functions)
{
   this.functions = functions;
}

public void go(Scanner kbd)
{
   int choice = 0;
   do
   {
      System.out.println(this);
      choice = kbd.nextInt();
      if (choice == 0) return; // choice is to quit
      if (choice == 1)
      {
         clear = true;
         continue;
      }
      if (choice < 0 || choice >= 5)
       {
          System.out.println("error");
          continue;
       }
      if (!clear)
       {
          System.out.print("use memory [" + memory + "] (y or n)? ");
          String ans = kbd.next();
          if (ans.equals("n")) clear = true;
       }
      if (clear)
       memory = functions[choice-2].doit();
     else
       memory = functions[choice-2].doit(memory);
     clear = false;
     System.out.println(memory);
} while(choice != 0);
}

public String toString()
{
    String out = "0. quit\n1. clear memory\n";
    for (int i=0; i<functions.length; i++)
    out += (i+2) + ". " + functions[i] + "\n";
    return out;
}
}

CalcFunction.java

//my CalcFunction class
import java.util.Scanner;

// generic class to model a function in a calculator
public abstract class CalcFunction {
       private Scanner kbd;
       private String description;

public CalcFunction(Scanner kbd, String description)
{
       this.kbd = kbd;
       this.description = description;
}

public abstract double doit();
public abstract double doit(double memory);

// get a string from the user
protected String getString(String prompt)
{
     System.out.print(prompt);
     return kbd.next();
}
// get a number from the user
protected double getNum(String prompt)
{
     System.out.print(prompt);
     while(!kbd.hasNextDouble())
     {
         System.out.print("Invalid: need a number: ");
         kbd.next(); // discard invalid input
     }
     return kbd.nextDouble();
}

public String toString()
{
  return description;
}
}

Add.java

//just one of my functions(Add)
import java.util.Scanner;

// class to encapsulate adding two numbers
public class Add extends CalcFunction {
public Add(Scanner kbd, String description)
{
     super(kbd, description);
}

public double doit()
{
    double n1 = this.getNum("Enter a number: ");
    return this.doit(n1);
}

public double doit(double first)
{
    double n2 = this.getNum("Enter a second number: ");
    double answer = first + n2;
    return answer;
}
}

Please help if you can. Thanks!

Pseudo-code:

// 1 maps to add, 2 to subtract, etc

while(cur_func = scanner.read != sentinal value) {
    switch(cur_func) { 
        1:
            //toss add into the functions set
            break
        .
        .
        .
    }
}

toArray your set and build the calc

If you want the user to be able to dynamically add calculator functions to a calculator, then you will need something like this:

public void addCalcFunction(CalcFunction newCalcFunction) {
    this.functions.add(newCalcFunction);
}

...but that will require this.functions to be modified to a Collection<CalcFunction>

If you want the user to refer to these functions by name, then you'll need to map them:

private Map<String, CalcFunction> functions;

...

public void addCalcFunction(String functionName, CalcFunction newCalcFunction) {
    this.functions.put(functionName, newCalcFunction);
}

Using a Map will require you to make further changes, but I'm sure you can figure those out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM