英文:
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<T>{
void add();
void remove();
void check();
void compare();
void show();
}
public class Main {
public static void main(String[] args) {
//int cups
Cup<Integer> cup = new Cup<Integer>(8);
Cup<Integer> cup1 = new Cup<Integer>(32);
Cup<Integer> cup2 = new Cup<Integer>(10);
//string cups
Cup<String> cup3 = new Cup<String>("Hello");
Cup<String> cup4 = new Cup<String>("Test");
Cup<String> cup5 = new Cup<String>("I am in a cup");
//boolean cups
Cup<Boolean> cup6 = new Cup<Boolean>(true);
Cup<Boolean> cup7 = new Cup<Boolean>(false);
Cup<Boolean> cup8 = new Cup<Boolean>(true);
//Magic cups
magicCup<> magicCup = new magicCup<>();
magicCup<> magicCup1 = new magicCup<>();
magicCup<> magicCup2 = new magicCup<>();
System.out.println(cup.getData());
}
}
Cup method:
public class Cup<T>{
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<T>) {
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的add
和remove
方法只应设置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
方法将返回一个表示(应该继承自Object
的toString
方法),如下所示:
@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<T> extends Comparable<Magic<?>>{
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 "Cup contains: "+ 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论