如何从这个Java问题中去除冗余(如果存在的话)?

huangapple go评论75阅读模式
英文:

How can I remove redundancy's from this java problem if there exist any?

问题

我在CS140课程中,尝试在Practice-It中解决问题。我不确定如何使这个问题的解决方案变得正确。指令是:“以下程序多次冗余地重复相同的表达式。使用适当类型的变量修改程序,以消除所有冗余表达式。”我正在学习如何使用整数,并且对此感到困惑,所以我肯定错过了一些简单的东西。我在下面以其原始形式贴出了代码,以显示我尝试解决它的方式。提前感谢您的任何帮助! 如何从这个Java问题中去除冗余(如果存在的话)?

// 这个程序计算餐费的总金额,
// 假设税为8%,小费为15%。
public class Receipt {
    public static void main(String[] args) {
        int x = 38 + 40 + 30;
        double y = 0.08;
        double z = 0.15;
        System.out.println("小计:");
        System.out.println(x);
        System.out.println("税:");
        System.out.println(x * y);
        System.out.println("小费:");
        System.out.println(x * z);
        System.out.println("总计:");
        System.out.println(x + x * y + x * z);
    }
}

原始问题如下。

// 这个程序计算餐费的总金额,
// 假设税为8%,小费为15%。
public class Receipt {
    public static void main(String[] args) {
        System.out.println("小计:");
        System.out.println(38 + 40 + 30);
        System.out.println("税:");
        System.out.println((38 + 40 + 30) * 0.08);
        System.out.println("小费:");
        System.out.println((38 + 40 + 30) * 0.15);
        System.out.println("总计:");
        System.out.println(38 + 40 + 30 +
                            (38 + 40 + 30) * 0.08 +
                            (38 + 40 + 30) * 0.15);
    }
}
英文:

I'm in CS140 and trying to solve problems in Practice-It. I'm not sure what else to do for this problem to make it correct. The instructions are, "The following program redundantly repeats the same expressions many times. Modify the program to remove all redundant expressions using variables of appropriate types." I'm just learning how to use integers and am struggling with it so I'm sure I'm missing something simple. I posted the code in its original form below how I tried to solve it. Thanks in advance for any help! 如何从这个Java问题中去除冗余(如果存在的话)?

// This program computes the total amount owed for a meal,
// assuming 8% tax and a 15% tip.
public class Receipt {
    public static void main(String[] args) {
        int x=38+40+30;
        double y=.08;
        double z=.15;
        System.out.println("Subtotal:");
        System.out.println(x);
        System.out.println("Tax:");
        System.out.println((x) * y);
        System.out.println("Tip:");
        System.out.println((x) * z);
        System.out.println("Total:");
        System.out.println((x) + (x) * y + (x) * z);
                           
                            

    }
}

The original problem is below.

// This program computes the total amount owed for a meal,
// assuming 8% tax and a 15% tip.
public class Receipt {
    public static void main(String[] args) {
        System.out.println("Subtotal:");
        System.out.println(38 + 40 + 30);
        System.out.println("Tax:");
        System.out.println((38 + 40 + 30) * .08);
        System.out.println("Tip:");
        System.out.println((38 + 40 + 30) * .15);
        System.out.println("Total:");
        System.out.println(38 + 40 + 30 +
                            (38 + 40 + 30) * .08 +
                            (38 + 40 + 30) * .15);

    }
}

答案1

得分: 1

你已经走在正确的道路上。显然,你打算为变量赋予常见的值,你已经将其命名为 xyz。然而需要注意的是,这些变量名并不是非常描述性的 — 更好的做法是从实际情况出发,将它们命名为实际代表的含义,比如 subtotal(小计)、taxMultiplier(税率系数)和 tipMultiplier(小费率系数)。

但需要注意的是,你仍然存在一些冗余。你在计算税金时重复了两次 — 一次是在打印税金时,另一次是在打印总额时。所以你应该将该值赋给一个新的双精度变量 (tax)。类似的重构也可以应用于 tiptotal

这将使你的代码变成如下形式:

// 这个程序计算一餐的总费用,假设税率为 8%,小费率为 15%。
public class Receipt {
    public static void main(String[] args) {
        int subtotal = 38 + 40 + 30; // 以前是 x
        double taxMultiplier = 0.08; // 以前是 y
        double tipMultiplier = 0.15; // 以前是 z
        double tax = subtotal * taxMultiplier;
        double tip = subtotal * tipMultiplier;
        double total = subtotal + tax + tip;

        System.out.println("小计:");
        System.out.println(subtotal);
        System.out.println("税金:");
        System.out.println(tax);
        System.out.println("小费:");
        System.out.println(tip);
        System.out.println("总计:");
        System.out.println(total);
    }
}

当然,你之后也可以看着这段代码说:“嗨,我只在一处使用了 taxMultipliertipMultiplier,为什么还要声明它们?”你是对的 — 你可以进一步重构,将 taxMultipliertipMultiplier 的值内联到税金和小费的计算中。

给中间值赋予具有明确含义的变量名是一种技巧,用于减少错误(例如,当你重复输入一个值时可能会出现拼写错误),还可以使情况变得更加清晰。在这段代码中,你可以清楚地看到总计是小计、税金和小费的和。原始代码中的数字混乱使这一点变得非常难以理解。此外,如果某个值发生了变化(例如小计),你只需要在一个地方进行修改,相应的更改将在代码的其他地方自动生效。

英文:

You're on the right track. Clearly you're intended to assign common values to variables, which you've done as x and y and z. Note, however, that these variable names aren't very descriptive -- a better solution would start by calling these things what they actually are ... subtotal, taxMultiplier and tipMultiplier.

But note that you still have some redundancies. You repeat your calculating for tax twice -- once when printing the tax and then again when printing the total. So you should assign that value to a new double (tax). A similar refactor can be done with tip and total.

This turns your code into something like this:

// This program computes the total amount owed for a meal,
// assuming 8% tax and a 15% tip.
public class Receipt {
    public static void main(String[] args) {
        int subtotal = 38 + 40 + 30; // formerly x
        double taxMultiplier = 0.08; // formerly y
        double tipMultiplier = 0.15; // formerly z
        double tax = subtotal * taxMultiplier;
        double tip = subtotal * tipMultiplier;
        double total = subtotal + tax + tip;

        System.out.println("Subtotal:");
        System.out.println(subtotal);
        System.out.println("Tax:");
        System.out.println(tax);
        System.out.println("Tip:");
        System.out.println(tip);
        System.out.println("Total:");
        System.out.println(total);
    }
}

Of course, you could then look at this code and say "Hey, I only use taxMultiplier and tipMultiplier once, why bother declaring them?" and you'd be right -- you could then refactor again to inline the values of taxMultiplier and tipMultiplier into the tax and tip calculations.

Assigning intermediate values to variables with sensible names is a technique which we use to minimize errors (eg you mght typo a value when repeating it) and also to make it clear what is going on. In this code, you can clearly see that total is the sum of the subtotal, tax, and tip. The number soup in the original code made it really hard to see that. Further, if a value changes (eg the subtotal), you'll only have to change it in one place and the appropriate changes will cascade throughout the rest of the code.

答案2

得分: 1

我怀疑这节课是在教你将计算与显示分开。

以下是我认为这节课想让你编写的代码。

public class Receipt {

    public static void main(String[] args) {
        int subTotal = 38 + 40 + 30;
        double tax = 0.08 * subTotal;
        double tip = 0.15 * subTotal;
        double total = subTotal + tax + tip;

        System.out.println("Subtotal:");
        System.out.println(subTotal);
        System.out.println("Tax:");
        System.out.println(tax);
        System.out.println("Tip:");
        System.out.println(tip);
        System.out.println("Total:");
        System.out.println(total);
    }

}

你还可以通过创建一个打印方法来进一步减少代码量。

public class Receipt {

    public static void main(String[] args) {
        int subTotal = 38 + 40 + 30;
        double tax = 0.08 * subTotal;
        double tip = 0.15 * subTotal;
        double total = subTotal + tax + tip;

        printAmount("Subtotal:", subTotal);
        printAmount("Tax:", tax);
        printAmount("Tip:", tip);
        printAmount("Total:", total);
    }

    private static void printAmount(String label, double value) {
        System.out.println(label);
        System.out.println(value);
    }

}

最后,你可以将文本和值打印在同一行,并用美元符号格式化值,但这已经超出了你当前的课程内容。

英文:

I suspect that the lesson is teaching you to separate your calculations from your display.

Here's what I think the lesson wanted you to code.

public class Receipt {

	public static void main(String[] args) {
		int subTotal = 38 + 40 + 30;
		double tax = .08 * subTotal;
		double tip = .15 * subTotal;
		double total = subTotal + tax + tip;

		System.out.println("Subtotal:");
		System.out.println(subTotal);
		System.out.println("Tax:");
		System.out.println(tax);
		System.out.println("Tip:");
		System.out.println(tip);
		System.out.println("Total:");
		System.out.println(total);
	}

}

You can further reduce the size of the code by creating a print method to print the values.

public class Receipt {

	public static void main(String[] args) {
		int subTotal = 38 + 40 + 30;
		double tax = .08 * subTotal;
		double tip = .15 * subTotal;
		double total = subTotal + tax + tip;

		printAmount("Subtotal:", subTotal);
		printAmount("Tax:", tax);
		printAmount("Tip:", tip);
		printAmount("Total:", total);
	}

	private static void printAmount(String label, double value) {
		System.out.println(label);
		System.out.println(value);
	}

}

Finally, it's possible to print the text and value on the same line with the value formatted with a dollar sign, but that's going way beyond your lesson for now.

答案3

得分: 0

public class Receipt {

public static void main(String[] args) {
    int firstMealPrice = 38;
    int secondMealPrice = 40;
    int thirdMealPrice = 30;

    double taxMultiplier = 0.08;
    double tipMultiplier = 0.15;

    int mealSubtotalPrice = firstMealPrice + secondMealPrice + thirdMealPrice;
    double mealTax = mealSubtotalPrice * taxMultiplier;
    double mealTip = mealSubtotalPrice * tipMultiplier;

    double mealTotalPrice = mealSubtotalPrice +
        mealTax +
        mealTip;

    print("Subtotal:");
    print(mealSubtotalPrice);
    print("Tax:");
    print(mealTax);
    print("Tip:");
    print(mealTip);
    print("Total:");
    print(mealTotalPrice);

}

private static void print(Object obj) {
    System.out.println(obj);
}

}

英文:

My version:

public class Receipt {

  public static void main(String[] args) {
    int firstMealPrice = 38;
    int secondMealPrice = 40;
    int thirdMealPrice = 30;

    double taxMultiplier = 0.08;
    double tipMultiplier = 0.15;

    int mealSubtotalPrice = firstMealPrice + secondMealPrice + thirdMealPrice;
    double mealTax = mealSubtotalPrice * taxMultiplier;
    double mealTip = mealSubtotalPrice * tipMultiplier;

    double mealTotalPrice = mealSubtotalPrice +
        mealTax +
        mealTip;

    print("Subtotal:");
    print(mealSubtotalPrice);
    print("Tax:");
    print(mealTax);
    print("Tip:");
    print(mealTip);
    print("Total:");
    print(mealTotalPrice);

  }

  private static void print(Object obj) {
    System.out.println(obj);
  }
}

huangapple
  • 本文由 发表于 2020年10月12日 01:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64307147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定