在 Java 的 driver/program 文件中使用 get.Name() 输出失败

Output failing using the get.Name() in a driver/program file in Java

我必须制作一个程序,它应该接收宠物信息的输入并以特定方式输出它。
通常这很简单,需要 10 分钟,但我们刚刚进入 OOP,我在弄清楚要在 driver 上的突变器中放入什么时遇到了一些麻烦。

Driver:

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

    }

}

import java.util.Scanner;
public class JMUnit6 {


    public static void main(String[] args) {

    JMPets myPet1 = new JMPets(); 
    JMPets myPet2 = new JMPets(); 
    JMPets mypet3 = new JMPets(); 
    Scanner stdIn = new Scanner(System.in); 
    System.out.println("Welcome to the Java Pet Tracker");
    System.out.println("Please enter the type of Pet #1:");
    String petType = stdIn.nextLine(); 

    System.out.println("Please enter the name of Pet #1:"); 
    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(); 

    System.out.println("Is "+petName+" Male?:");
    boolean isMale = stdIn.nextBoolean(); 

    myPet1.set(petType, petName, petAge, petWeight); 


    System.out.println(myPet1.getType());
    System.out.println(myPet1.getName()); 
    System.out.println(myPet1.getAge());
    System.out.println(myPet1.getWeight()); 



    }//end main
}//end class JMUnit6

我得到的唯一输出是 null null 0 0.0.

看看你的二传手,你已经有了答案。

this.petType = petType;
this.petName = petName;
this.petAge = petAge;
this.petWeight = petWeight;

您可能还缺少 isMale

这是一个让你理解构造函数和修改器的练习。您可以使用访问器测试您的实现。

本post涵盖了一些基础知识。 Java - Using Accessor and Mutator methods

The official tutorial works too, but more importantly - constructors.


关于您的评论,这里有一些准则。

For the 1st pet, use the default constructor and use proper mutator methods to set all variables

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

For the 2nd pet, use a single parameter constructor that accepts type as an argument and use proper mutator methods for all other values

(在您实现此构造函数之前不会编译)

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

For the 3rd pet, use a constructor that accepts all values as arguments

同样,和以前一样的问题,需要一个构造函数(见最后一行)。

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. 

相反,只需重新分配 name = "sally";,第二次不需要 String name