java 试图避免违反封装

bhmjp9jg  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(170)

在Java中开始一些新的东西,我有点困难。我觉得我在正确的轨道上,只是出于某种原因,找不到一种方法来解决我的问题,而不违反封装。我有两门课,分别叫重量和项目。我已经完成了这两个,但在项目类中出现错误。其中包括'toOunces'、' ounces'和'OUNCES_IN_A_POUND'。这些在Weight类中是私有的。我已经明白我不能携带私有变量,方法等。从一个班到另一个班。我只是不知道如何在不违反封装的情况下进行此操作。下面我有重量类和项目类以及作业的详细信息。
我不找任何人做我的工作,因为我只是在寻找提示,技巧和建议。另外,请原谅我,因为我还在学习Java语言和CMIS 242的第一周。

public class Weight {

    //Private variables
    private static final int OUNCES_IN_A_POUND = 16;
    private int pounds;
    private double ounces;

    //A public parameterized constructor, which initializes the private variables.
    public Weight(int pounds, double ounces) {
        this.pounds = pounds;
        this.ounces = ounces;
        normalize();
    }

    //instance methods
    private double toOunces() {
        return (pounds * OUNCES_IN_A_POUND) + ounces;
    }

    private void normalize() {
        while (ounces >= OUNCES_IN_A_POUND) {
            pounds++;
            ounces -= OUNCES_IN_A_POUND;
        }
    }

    public boolean lessThan(Weight weight) {
        double thisOunces = toOunces();
        double otherOunces = weight.toOunces();
        return thisOunces < otherOunces;
    }

    public void addTo(Weight weight) {
        double thisOunces = toOunces();
        double otherOunces = weight.toOunces();
        double totalOunces = thisOunces + otherOunces;

        pounds = (int) (totalOunces / OUNCES_IN_A_POUND);
        ounces = totalOunces % OUNCES_IN_A_POUND;
        normalize();
    }

    public String toString() {
        return pounds + " pounds and " + String.format("%.2f", ounces) + " ounces";
    }
}

----------------------------------------------------------------------------------------------------
public class Project {
    private static Weight findMinimum(Weight weight1, Weight weight2, Weight weight3) {
        Weight minimumWeight = weight1;

        if (weight2.lessThan(minimumWeight)) {
            minimumWeight = weight2;
        }

        if (weight3.lessThan(minimumWeight)) {
            minimumWeight = weight3;
        }

        return minimumWeight;
    }

    private static Weight findMaximum(Weight weight1, Weight weight2, Weight weight3) {
        Weight maximumWeight = weight1;

        if (weight2.toOunces() > maximumWeight.toOunces()) {
            maximumWeight = weight2;
        }

        if (weight3.toOunces() > maximumWeight.toOunces()) {
            maximumWeight = weight3;
        }

        return maximumWeight;
    }

    private static Weight findAverage(Weight weight1, Weight weight2, Weight weight3) {
        double totalOunces = weight1.toOunces() + weight2.toOunces() + weight3.toOunces();
        double averageOunces = totalOunces / 3.0;

        int averagePounds = (int) (averageOunces / Weight.OUNCES_IN_A_POUND);
        double averageOuncesRemaining = averageOunces % Weight.OUNCES_IN_A_POUND;

        return new Weight(averagePounds, averageOuncesRemaining);
    }

    public static void main(String[] args) {
        //instances of the weight class
        Weight weight1 = new Weight(11, 3);
        Weight weight2 = new Weight(7, 20);
        Weight weight3 = new Weight(14, 6);

        System.out.println("Weight 1: " + weight1.toString());
        System.out.println("Weight 2: " + weight2.toString());
        System.out.println("Weight 3: " + weight3.toString());

        Weight minimumWeight = findMinimum(weight1, weight2, weight3);
        System.out.println("The minimum weight is " + minimumWeight.toString());

        Weight maximumWeight = findMaximum(weight1, weight2, weight3);
        System.out.println("The maximum weight is " + maximumWeight.toString());

        Weight averageWeight = findAverage(weight1, weight2, weight3);
        System.out.println("The average weight is " + averageWeight.toString());
    }
}

链接到PDF文件以获取作业详细信息:https://learn.umgc.edu/d2l/common/viewFile.d2lfile/Database/NzQ5OTk1NjE/Assignment1.pdf?ou=1016228

ltskdhd1

ltskdhd11#

OUNCES_IN_A_磅是私有的,应该保持私有。这就是Project类无法访问它的原因。
你走在正确的道路上:这实际上是封装的问题。你要做的是将 * 所有 * 数学运算封装在Weight类本身的公共方法中。
所以,与此相反:

double totalOunces = weight1.toOunces() + weight2.toOunces() + weight3.toOunces();
double averageOunces = totalOunces / 3.0;

int averagePounds = (int) (averageOunces / Weight.OUNCES_IN_A_POUND);
double averageOuncesRemaining = averageOunces % Weight.OUNCES_IN_A_POUND;

你想写这样的东西:

Weight sum = new Weight(weight1);
sum.addTo(weight2);
sum.addTo(weight3);

return sum.divideBy(3);

这通过让Weight类管理所有计算操作来实现封装。调用方永远不需要通过直接操作其数值属性来打破封装。
正如你所看到的,需要对Weight做一些补充:

/**
 * Makes a copy of an existing Weight instance.
 *
 * @param other non-null Weight instance to be copied
 */
public Weight(Weight other) {
    // ...
}

/**
 * Divides this Weight by the given factor.
 *
 * @param divisor amount by which this Weight will be divided
 */
public void divideBy(double divisor) {
    // ...
}

我把实现构造函数和方法的任务留给您。
最后,您可能需要考虑将addTo方法重命名为add。该方法没有向参数添加任何内容,也没有以任何方式更改参数;相反,它使用参数来添加示例自己的值。

相关问题