通用方法和在Magic Cup中使用的实现方式

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

Generic methods and implementations used in Magic Cup

问题

已经交了,得了个零分,因为我真的不知道该怎么做,或者从课堂上讲的内容开始做,而课上讲的很少。请问有人可以教教我,或者告诉我应该去哪里学习该做什么。

任务目标如下:我们将创建一个表示通用魔法杯的数据结构。在实验中,我们应该为魔法物品创建一个通用接口,应该称为Magic,并创建一个名为Magic Cup的通用类,该类实现了Magic接口。所有魔法物品都应该能够显示物品内部的内容,清空物品,将物品放入其中,或将魔法物品与另一个物品进行比较。魔法杯应该能够容纳任何类型的物品。我们应该能够检查杯子是否为空,或者是否包含物品。想象一下街头艺术家经常做的杯子把戏。

为了测试你的工作,在一个主方法中,你应该创建一个方法,该方法实现了三个不同类型的杯子,例如:整数、字符串、布尔值,甚至是魔法杯。然后,你应该能够使用这个方法来查找填充的杯子,或者搜索所有杯子,看看是否发生了魔法。

到目前为止,我只有这些:

主方法:

import java.util.*;

interface Magic<T>{
    void add();
    void remove();
    void check();
    void compare();
    void show();
}

public class Main {
    public static void main(String[] args) {
        // ...
    }
}

杯子方法:

public class Cup<T> {
    T data;
    Cup(T data) {
        this.data = data;
    }

    void setData(T data) {
        this.data = data;
    }

    T getData() {
        return data;
    }
}

魔法杯方法:

public class magicCup<T> implements Magic<T> {
    @Override
    public void add(magicCup<T> cup) {
        // ...
    }

    @Override
    public void remove() {
        // ...
    }

    @Override
    public void check() {
        // ...
    }

    @Override
    public void compare() {
        // ...
    }

    @Override
    public void show() {
        // ...
    }
}

如果有人愿意花点时间与我坐下来,指导我该从何处开始,我会非常感激。

英文:

I have already turned this in for a 0 in my class as I just dont know what to do or how to start it past what was shown in class, Which was very little. Could someone please teach me or show me where to go for my own sake to learn what im suppose to do here.

The objective is this: In this we will create a Data Structure that represents a Generic Magic Cup. In our lab we should create a generic interface for Magic items and it should be called Magic, as well as creating a generic class called Magic Cup that implements magic. All magical items should give you the ability to show whats in the item, to empty the item, place an item inside of it, or compare the magic item to another item. A magic cup should be able to hold any item of any type. We should be able to check a cup to see if it is currently empty or if it contains an item. Imagine the cup trick that is commonly done by street artist.

To test your work, in a main method you should create a methodology that implements three cups of different type, ie. ints, strings, booleans and even magic cups. Then you should be able to use the method to see if you can find the filed cup or search through all cups to see if magic happened.

so far all I have is this:

Main Method:

import java.util.*;

interface Magic&lt;T&gt;{
    void add();
    void remove();
    void check();
    void compare();
    void show();

}
public class Main {

public static void main(String[] args) {
	
	//int cups
	Cup&lt;Integer&gt; cup = new Cup&lt;Integer&gt;(8);
	Cup&lt;Integer&gt; cup1 = new Cup&lt;Integer&gt;(32);
	Cup&lt;Integer&gt; cup2 = new Cup&lt;Integer&gt;(10);
	
	//string cups
	Cup&lt;String&gt; cup3 = new Cup&lt;String&gt;(&quot;Hello&quot;);
	Cup&lt;String&gt; cup4 = new Cup&lt;String&gt;(&quot;Test&quot;);
	Cup&lt;String&gt; cup5 = new Cup&lt;String&gt;(&quot;I am in a cup&quot;);
	
	
	//boolean cups
	Cup&lt;Boolean&gt; cup6 = new Cup&lt;Boolean&gt;(true);
	Cup&lt;Boolean&gt; cup7 = new Cup&lt;Boolean&gt;(false);
	Cup&lt;Boolean&gt; cup8 = new Cup&lt;Boolean&gt;(true);
	
	//Magic cups
	magicCup&lt;&gt; magicCup = new magicCup&lt;&gt;();
	magicCup&lt;&gt; magicCup1 = new magicCup&lt;&gt;();
	magicCup&lt;&gt; magicCup2 = new magicCup&lt;&gt;();
	
	System.out.println(cup.getData());
}

}

Cup method:

public class Cup&lt;T&gt;{
T data;
Cup(T data){
	this.data=data;
}

void setData(T data) {
	this.data = data;
}

T getData() {
	return data;
}

}

MagicCup Method:

public class magicCup<T> implements Magic<T> {

@Override
public void add(magicCup&lt;T&gt;) {
	T data;
	magicCup(T data){
		this.data=data;
	}
	
	void setData(T data) {
		this.data = data;
	}
	
	T getData() {
		return data;
	}	
}

@Override
public void remove() {
	
	
}

@Override
public void check() {
	// TODO Auto-generated method stub
	
}

@Override
public void compare() {
	// TODO Auto-generated method stub
	
}

@Override
public void show() {
	// TODO Auto-generated method stub
	
}

}

If someone would be willing to sit down with me for a little bit and walk me through what I would even do to start. I would greatly appreciate the help.

答案1

得分: 2

First,

您的Magic接口在其方法中应该使用或返回数据,如下所示:

interface Magic<T> extends Comparable<Magic<?>>{
    void add(T object);
    void remove();
    boolean check();
    void show();
}

然后,您的MagicCup的addremove方法只应设置data,如下所示:

@Override
public void add(T object) {
    if(!this.check()) { // 否则杯子已经装满
        this.data = object;
    }
}

@Override
public void remove() {
    this.data = null;
}

接下来,check方法用于检查是否有对象在内部,如下所示:

@Override
public boolean check() { // 更好的方法名可以是isEmpty()
    return this.data != null;
}

然后,show方法将返回一个表示(应该继承自ObjecttoString方法),如下所示:

@Override
public String toString() {
    return "Cup contains: " + this.data.toString();
}

@Override
public void show() {
    System.out.println(this.toString());
}

最后,compare方法将通过Comparable接口的compareTo(Object other)方法继承,与另一个您通过此链接实现的Magic对象进行比较。

英文:

First,
Your Magic interface should use or return data in their methods like

interface Magic&lt;T&gt; extends Comparable&lt;Magic&lt;?&gt;&gt;{
    void add(T object);
    void remove();
    boolean check();
    void show();
}

Then your MagicCup add and remove should only set data like

@Override
public void add(T object) {
    if(! this.check()) { // Otherwise the cup is already full
        this.data = object;
    }
}

@Override
public void remove() {
    this.data = null;
}

Then check does check if there's an object inside

@Oveeride
public boolean check() { // isEmpty() would be better
    return this.data != null;
}

Then show wil return a representation (should be toString inheritted from Object) like

@Override
public String toString() {
    return &quot;Cup contains: &quot;+ this.data.toString();
}
@Override
public void show() {
    System.out.println(this.toString());
}

Finally compare will be inherited from Comparable through compareTo(Object other) and compare data with another Magic which I let you implement with this post

huangapple
  • 本文由 发表于 2020年9月23日 21:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028595.html
匿名

发表评论

匿名网友

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

确定