创建一个布尔值以找到不同类型的旅行杯
Creating a boolean to find a different kind of travel cup
提示:很多TravelCups没有把手。为此,向 TravelCup class 添加一个布尔实例变量。更改您的构造函数和方法以适应此更改。将任何适当的方法添加到此更改所需的 class。证明更改在您的测试运行中有效。
我能够完成大部分项目,但我被困在这里。我不确定我是应该创建一个新的 class 还是创建一个新对象,但我已经尝试创建一个新的构造函数,但它只是给了我一个错误。我已经走到了死胡同,现在我不知道该怎么办。有人可以帮忙吗?
代码:
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);
{
}
}
TravelCup
可以通过以下更新实现:
- 添加字段
boolean hasHandle
,其 getter 并通过构造函数设置
- 更新构造函数以使用现有的构造函数
this(...)
- 更新了
toString
和 equals
以包含新字段
- (可选)添加了
set
方法以通过调用 super.set
来设置所有需要添加到 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;
}
}
提示:很多TravelCups没有把手。为此,向 TravelCup class 添加一个布尔实例变量。更改您的构造函数和方法以适应此更改。将任何适当的方法添加到此更改所需的 class。证明更改在您的测试运行中有效。
我能够完成大部分项目,但我被困在这里。我不确定我是应该创建一个新的 class 还是创建一个新对象,但我已经尝试创建一个新的构造函数,但它只是给了我一个错误。我已经走到了死胡同,现在我不知道该怎么办。有人可以帮忙吗?
代码:
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);
{
}
}
TravelCup
可以通过以下更新实现:
- 添加字段
boolean hasHandle
,其 getter 并通过构造函数设置 - 更新构造函数以使用现有的构造函数
this(...)
- 更新了
toString
和equals
以包含新字段 - (可选)添加了
set
方法以通过调用super.set
来设置所有需要添加到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;
}
}