任何人都可以帮我修改我的Java代码吗?

时间:2022-10-19 16:42:11

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).

好的,要清楚这一点,这就是我需要对整个程序做的事情。我需要main来恢复文件“calc.bak”中的任何计算器,然后向用户显示一个菜单,main应该在退出之前保存同一文件中存在的所有计算器(适当地覆盖)。我还需要让用户选择创建一个全新的计算器,命名它,并从现有的集合中添加一个函数(这是我的添加,乘法和除法)。我还需要用户通过设计和命名任何一对函数的组合来创建自己的函数。第一个函数的输出必然输入到第二个函数。例如,用户可以创建一个名为“addmult”的新函数,该函数调用add函数(提示输入两个数字并添加它们)并将sum建立为乘法函数的操作数之一(必须提示其为第二个操作数)。

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

示例输出应如下所示:欢迎使用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):

指出您要添加到计算器中的库存中的哪些功能(输入0退出此菜单):

Add(1)

Multiply(2)

Divide(3)

Pair of functions(4)

功能对(4)

input (2)

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

指出您要添加到计算器中的库存中的哪些功能(输入0退出此菜单):

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

1指出您要添加到计算器中的库存中的哪些功能(输入0退出此菜单):添加

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):

指出您要添加到计算器中的库存中的哪些功能(输入0退出此菜单):

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):

指出您要添加到计算器中的库存中的哪些功能(输入0退出此菜单):

Add

Multiply

Divide

Pair of functions

一对功能

0

I am a calculator named Fred

我是一个名叫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!

如果可以的话请帮忙。谢谢!

2 个解决方案

#1


0  

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

#2


0  

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>

...但是这需要将this.functions修改为Collection

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.

使用地图将需要您进行进一步的更改,但我相信您可以解决这些问题。

#1


0  

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

#2


0  

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>

...但是这需要将this.functions修改为Collection

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.

使用地图将需要您进行进一步的更改,但我相信您可以解决这些问题。