在Java驱动程序/程序文件中使用get.Name()输出失败
我必须制作一个程序来接收宠物信息的输入并以特定的方式输出。在Java驱动程序/程序文件中使用get.Name()输出失败,java,oop,setter,Java,Oop,Setter,我必须制作一个程序来接收宠物信息的输入并以特定的方式输出。 正常情况下,这将是一个蛋糕,需要10分钟,但我们刚刚进入OOP,我有一些麻烦,弄清楚在驱动程序上的变异器中放入什么 司机: import java.util.HashSet; import java.util.Set; public class JMPets { private String petType; private String petName; private int petAge;
正常情况下,这将是一个蛋糕,需要10分钟,但我们刚刚进入OOP,我有一些麻烦,弄清楚在驱动程序上的变异器中放入什么 司机:
import java.util.HashSet;
import java.util.Set;
public class JMPets {
private String petType;
private String petName;
private int petAge;
private double petWeight;
boolean isMale;
public void setType(String petType)
{
this.petType = petType;
}
public void setName(String petName)
{
this.petName = petName;
}
public void setAge (int petAge)
{
this.petAge = petAge;
}
public void setWeight(double petWeight)
{
this.petWeight = petWeight;
}
public String getType()
{
return petType;
}
public String getName()
{
return petName;
}
public int getAge()
{
return petAge;
}
public double getWeight()
{
return petWeight;
}
public void set(String petType, String petName, int petAge,
double petWeight)
{
//WHAT DO I PUT HERE
}
}
我得到的唯一输出是null 0.0。看看您的设置程序,您已经有了答案
this.petType = petType;
this.petName = petName;
this.petAge = petAge;
this.petWeight = petWeight;
您可能还缺少
isMale
这是一个了解构造函数和变异体的练习。您可以使用访问器测试您的实现
这篇文章介绍了一些基本知识
同样有效,但更重要的是-
至于你的评论,这里有一些指导方针 对于第一个pet,使用默认构造函数并使用适当的mutator方法设置所有变量 对于第二个pet,使用接受type作为参数的单参数构造函数,并对所有其他值使用适当的mutator方法 (在实现此构造函数之前,不会编译) 对于第三个pet,使用接受所有值作为参数的构造函数 同样,和以前一样的问题,需要一个构造函数(见最后一行)
现在,如果您在一个方法中完成这一切,您将得到已经定义了变量的错误 比如说,
String name = "bob";
String name = "sally"; // <--- Error. 'name' already defined.
String name=“bob”;
String name=“sally”//注:JMPets
表示一个单一的对象-宠物。因此,您的类名最好不要是复数固定的,谢谢。您应该使用单集方法还是四个单独的、已经定义的方法?如果是后者,哪一部分会让人困惑?这是一个标题,我对这件事还不是很确定。我只是在编程入门课程的第6周(在线,讲师是afk at life)。对于第一个pet,使用默认构造函数,并使用适当的变式方法设置所有变量。•对于第二个pet,使用接受类型作为参数的单参数构造函数,并对所有其他值使用适当的mutator方法对于第三个pet,使用接受所有值作为参数的构造函数。下面的答案是正确的。不过,我们不是来教基础知识的。如果你的班级没有完成足够的工作,那么找其他资源。除了这一点,那里的说明还说要定义一个构造函数(参见链接)。您定义了一个名为set
的方法,该方法不正确。第二只宠物需要由新宠物(宠物类型)
制作,例如public void setName(String name){this.name=name;}
这是指导老师给我们提供的帮助示例,我知道您在识别自己,但这非常混乱。任何关于修复的建议都是,我尝试从main和driver重命名变量,使它们有单独的名称,并更改驱动程序中的=,但只是给了我一个未初始化变量的错误。@MichaelantonyJudge误解了。这个答案就是你在//我把什么放在这里
我还是个迷路的家伙我是个新手,就像我在网上说的,有一个老师,所以所有的东西都是自学的,我的知识有很大的漏洞我真的不知道该放什么,我把打印放在那里,重述同样的变量idk什么变异子即使这些书向我们展示了它们的例子,但没有解释它们是什么或做什么,谷歌给了我比我准备接受或理解的更深入的答案。
JMPets myPet1 = new JMPets(); // use the default constructor
System.out.println("Please enter the type of Pet #1:");
String petType = stdIn.nextLine();
myPet1.setType(petType); // use proper mutator methods to set all variables
// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods
System.out.println("Please enter the type of Pet #2:");
String petType = stdIn.nextLine();
JMPets myPet2 = new JMPets(petType); // TODO: Implement this
System.out.println("Please enter the name of Pet #2:");
String petName = stdIn.nextLine();
myPet2.setName(petName); // use proper mutator methods for all other values
// TODO: stdIn.nextLine for remainder of values. Use the 'individual' set methods
System.out.println("Please enter the type of Pet #3:");
String petType = stdIn.nextLine();
System.out.println("Please enter the name of Pet #3:");
String petName = stdIn.nextLine();
System.out.println("Please enter the age of " +petName+":");
int petAge = stdIn.nextInt();
System.out.println("Please enter the weight of "+petName+":");
double petWeight = stdIn.nextDouble();
JMPets mypet3 = new JMPets(petType, petName, petAge, petWeight);
String name = "bob";
String name = "sally"; // <--- Error. 'name' already defined.