我发布了一个UML图,其中包含一个赋值,用于解码给定电阻的色带,并返回一个返回欧姆电阻的字符串及其容差 .
我在继承层次结构上有点挣扎,在教授的问题描述中,LookupTable类是一个带有抽象get()方法的抽象类 . NumberLookup和ToleranceLookup类扩展了LookupTable类,MultiplierLookup类扩展了NumberLookup类 .
我想我最难理解的是MultiplierLookup类是否应该扩展LookupTable类或NumberLookup类 .
Here is the specific problem description:
ResistorDecoder对象将是Program.java用于将颜色数组转换为人性化字符串的对象 . ResistorDecoder将需要几种不同类型的查找表,具体取决于我们是从颜色还是数值查找字符串 . LookupTable对象是一个抽象类,因为它包含一个抽象方法get() . ToleranceLookup对象采用颜色并返回一个字符串,即字符串(例如±5%) . NumberLookup对象采用颜色并且可以返回String但它也可以返回Double . 由于我们正在扩展LookupTable对象(它之前无法返回double),因此我们需要从LookupTable对象继承 . MultiplierLookup对象的工作方式与NumberLookup对象类似,但内部查找表不同 . 因此,即使我们不需要添加任何方法,我们也需要继承NumberLookup表 . 提示:你能否使用MultiplierLookup使用NumberLookup的getDouble行为来简化其责任?
到目前为止,我已尝试创建各种类/子类,如下所示:
LookupTable.java
/**
* abstract class with abstract method get()
*
*
*/
public abstract class LookupTable {
/**
*
* abstract class with abstract get method that has no implementation defined, behavior is defined in subclasses
*
* @param color
* @return
*/
abstract String get(String color);
}
NumberLookup.java
public class NumberLookup extends LookupTable {
private HashMap<String, String> numberTable = new HashMap<>();
String get(String color) { // abstract method implementation
return color;
}
public NumberLookup() {
String[] colors = { "Silver", "Gold", "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet",
"Gray", "White" };
for (String color : colors) {
for (int i = 0; i <= 9; i++) {
numberTable.put(color, (i));
}
}
// constructor
}
/**
*
* @param color
* @return the corresponding
*/
public double getDouble(String color) { // new method
return (numberTable.get(color));
}
}
ToleranceLookup.java
/**
* intakes a color and returns a String that is the tolerance String ex.( (+/-
* 5%))
*
* adds the OHM symbol and tolerance to the end of the final string to be
* printer
*/
public class ToleranceLookup extends LookupTable {
/**
*
* default empty constructor
*/
public ToleranceLookup() {
}
/**
*
* @param color band as a string
* @return the Ohm and tolerance suffix
*/
public String get(String color) {
switch (color) {
case "Black":
return "\u03A9";
case "Brown":
return ("\u03A9, \u00B1" + "1%");
case "Red":
return ("\u03A9, \u00B1" + "2%");
case "Orange":
return ("\u03A9");
case "Yellow":
return ("\u03A9, \u00B1" + "5%");
case "Green":
return ("\u03A9, \u00B1" + ".5%");
case "Blue":
return ("\u03A9, \u00B1" + ".25%");
case "Violet":
return ("\u03A9, \u00B1" + ".1%");
case "Gray":
return ("\u03A9, \u00B1" + "10%");
case "White":
return ("\u03A9");
case "Gold":
return ("\u03A9, \u00B1" + "5%");
case "Silver":
return ("\u03A9, \u00B1" + "10%");
case "None":
return ("\u03A9, \u00B1" + "20%");
default:
return null;
}
}
}
MultiplierLookup.java
/**
* inherits from the NumberLookup object but no methods need to be added.
*
* Internal lookup table is different
*
*
*/
public class MultiplierLookup extends NumberLookup {
private HashMap<String, Double> multiplier = new HashMap<>();
String get(String color) {
return color;
}
public MultiplierLookup() {// constructor
String[] colors = new String[] { "Silver", "Gold", "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue",
"Violet", "Gray", "White" };
for (String color : colors) {
int j = -2;
while (j <= 9) {
multiplier.put(color, (Double.toString(Math.pow(10, j)));
j++;
}
}
}
public Double getDouble(String color) { // returns a double value
return multiplier.get(color);
}
}
我知道到目前为止代码中存在一些错误,我认为我在很大程度上理解如何解决这些错误,但我对OOP和相互关联/依赖类很新 . 虽然我理解继承如何在基本级别上工作,但我不确定如何让我的MultiplierLookup类继承NumberLookup和LookupTable . 很抱歉,如果这个问题冗长或过于基本,但我花了很多时间浏览这个网站以获取我的所有作业,而且这里的输入非常宝贵,所以我认为这是最好的转向 .
我绝对不希望任何人为我完成这个代码,我实际上非常喜欢解决问题,但我觉得让我用一个具体的例子向我解释的概念将是有益的 . 先感谢您!
另外,我还没有完全构造ResistorDecoder类,因为它依赖于子类 . 但它主要是根据其他类的信息组成一个字符串 .