创建一个布尔变量来查找另一种类型的旅行杯。

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

Creating a boolean to find a different kind of travel cup

问题

代码部分已翻译完成如下:

public class Cup
{
    // 实例变量
    private int volume; // 盎司
    private String color;
    private String material;

    /**
     * 默认构造函数
     */
    public Cup()
    {
        // 初始化实例变量
        volume = 8;
        color = "白色";
        material = "陶瓷";
    }
    
    public Cup(int v, String c, String m)
    {
        // 初始化实例变量
        volume = v;
        color = c;
        material = m;
    }
    
    // ...(其他方法)
}

public class LogoCup extends Cup
{
    private String logo;
    private String slogan;

    public LogoCup()
    {
        super();
        logo = "";
        slogan = "";
    }
    
    public LogoCup(int v, String c, String m, String lg, String s)
    {
        super(v, c, m);
        logo = lg;
        slogan = s;
    }

    // ...(其他方法)
}

public class TravelCup extends LogoCup
{
    public TravelCup()
    {
        super();
    }
  
    public TravelCup(int v, String c, String m, String lg, String s)
    {
        super(v, c, m, lg, s);
    }

    // ...(其他方法)
}

请注意,部分方法和注释没有在翻译中显示,因为它们与主要的代码结构和问题无关。如果您需要对特定部分进行详细的翻译,请提供具体指示。

英文:

Prompt: Many TravelCups do not have handles. To allow for this add a boolean instance variable to the TravelCup class. Change your constructors and methods to accommodate this change. Add any appropriate methods to the class this change requires. Demonstrate that changes work in your test runs.

I was able to do most of the project, but I get stuck here. I'm not sure if I'm supposed to create a new class or create a new object, but I've tried creating a new constructor and that just gives me an error. I've hit a dead-end and now I'm not sure what to do. Can anyone help?

Code:

    public class Cup
{
// instance variables 
private int volume; // in oz.
private String color;
private String material;
/**
* Default Constructor for objects of class Cup
*/
public Cup()
{
// initialise instance variables
volume = 8;
color = "white";
material = "ceramic";
}
public Cup(int v, String c, String m)
{
// initialise instance variables
volume = v;
color = c;
material = m;
}
public Cup(Cup other)
{
// initialise instance variables
volume = other.volume;
color = other.color;
material = other.material;
}
public void set(int v, String c, String m)
{
volume = v;
color = c;
material = m;
}
public String toString()
{
return "This is a " + color + " cup made of " + material
+ "\nIt holds " + volume + " oz. ";
}
/**
*
* @return    the volume
*/
public int getVolume()
{
return volume;
}
public String getColor()
{
return color;
}
public String getMaterial()
{
return material;
}
public boolean equals(Cup other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
Cup otherCup = (Cup)other;
return volume == otherCup.volume && color.equals(otherCup.color) && material.equals(otherCup.material);
}
}
}
public class LogoCup extends Cup
{
private String logo;
private String slogan;
public LogoCup()
{
super( );
logo = "";
slogan = "";
}
public LogoCup(int v, String c, String m, String lg, String s)
{
super(v, c, m );
logo = lg;
slogan = s;
}
public LogoCup(LogoCup other)
{
super(other );
logo = other.logo;
slogan = other.slogan;
}
public String toString()
{
return super.toString()
+ " Logo: " + logo + " Slogan: " + slogan;
}
public boolean equals(LogoCup other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
LogoCup otherLogoCup = (LogoCup)other;
return logo.equals(otherLogoCup.logo) && slogan.equals(otherLogoCup.slogan) && super.equals( otherLogoCup);
}
} 
public String getLogo()
{
return logo;
}
public String getSlogan()
{
return slogan;
}
}
public class TravelCup extends LogoCup
{
public TravelCup()
{
super();
}
public TravelCup(int v, String c, String m, String lg, String s)
{
super(v, c, m, lg, s );
}
public TravelCup(TravelCup other)
{
super(other );
}
public String toString()
{
return "Travel Cup! " + super.toString() + "\nEvery TravelCup has a lid!";
}
public boolean equals(Object other)
{
if(other == null)
return false;
else if( getClass() != other.getClass())
return false;
else
{
TravelCup otherTravelCup = (TravelCup)other;
return super.equals( otherTravelCup);
}
} 
public boolean equals(Object handle);
{
}
}

答案1

得分: 0

以下是翻译好的代码部分:

class TravelCup extends LogoCup {
    private boolean hasHandle = false;
    
    public TravelCup() {
        super();
    }
  
    public TravelCup(int v, String c, String m, String lg, String s) {
        this(v, c, m, lg, s, false);
    }
    
    public TravelCup(int v, String c, String m, String lg, String s, boolean h) {
        super(v, c, m, lg, s );
        this.hasHandle = h;
    }

    public TravelCup(TravelCup other) {
        super(other);
        this.hasHandle = other.hasHandle;
    }
 
    public String toString() {
        return "Travel Cup! " + super.toString() + ", hasHandle? " + hasHandle + "\nEvery TravelCup has a lid!";
    }
    
    public boolean equals(Object other)
    {
        if(other == null)
            return false;
        if (this == other)
            return true;
        if(getClass() != other.getClass())
            return false;
        TravelCup otherTravelCup = (TravelCup) other;
        return super.equals(otherTravelCup) && this.hasHandle == otherTravelCup.hasHandle;
    } 
    
    public boolean hasHandle() {
        return hasHandle; 
    }
    
    public void set(int v, String c, String m, String l, String s, boolean h)
    {
        super.set(v, c, m, l, s);
        this.hasHandle = h;
    }
}

// class LogoCup
class LogoCup extends Cup {
    // ...
    public void set(int v, String c, String m, String l, String s) {
        super.set(v, c, m);
        logo = l;
        slogan = s;
    }
}
英文:

The TravelCup may be implemented with the following updates:

  • added field boolean hasHandle, its getter and setting via constructors
  • updated constructors to use existing constructors using this(...)
  • updated toString and equals to include new field
  • (optional) added set method to set all parameters via call to super.set that needs to be added to LogoCup
class TravelCup extends LogoCup {
    private boolean hasHandle = false;
    
    public TravelCup() {
        super();
    }
  
    public TravelCup(int v, String c, String m, String lg, String s) {
        this(v, c, m, lg, s, false);
    }
    
    public TravelCup(int v, String c, String m, String lg, String s, boolean h) {
        super(v, c, m, lg, s );
        this.hasHandle = h;
    }

    public TravelCup(TravelCup other) {
        super(other);
        this.hasHandle = other.hasHandle;
    }
 
    public String toString() {
        return "Travel Cup! " + super.toString() + ", hasHandle? " + hasHandle + "\nEvery TravelCup has a lid!";
    }
    
    public boolean equals(Object other)
    {
        if(other == null)
            return false;
        if (this == other)
            return true;
        if(getClass() != other.getClass())
            return false;
        TravelCup otherTravelCup = (TravelCup) other;
        return super.equals(otherTravelCup) && this.hasHandle == otherTravelCup.hasHandle;
    } 
    
    public boolean hasHandle() {
        return hasHandle; 
    }
    
    public void set(int v, String c, String m, String l, String s, boolean h)
    {
        super.set(v, c, m, l, s);
        this.hasHandle = h;
    }
}

// class LogoCup
class LogoCup extends Cup {
// ...
    public void set(int v, String c, String m, String l, String s) {
        super.set(v, c, m);
        logo = l;
        slogan = s;
    }
}

huangapple
  • 本文由 发表于 2020年10月16日 05:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64379748.html
匿名

发表评论

匿名网友

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

确定