【问题标题】:Creating method with a formal parameter to set a value and a method to return a string创建具有形式参数的方法以设置值和方法以返回字符串
【发布时间】:2014-09-30 04:53:31
【问题描述】:

我目前正在一个实验室工作,该实验室需要我在课本中找到的代码中添加额外的代码。我尝试在这里寻找解决问题的方法,但找不到。

我们对实验室的说明是这样的:

向 Dog 类添加另一个名为 weight 的实例变量,它是一个 int 变量。

向 Dog 类添加另一个名为 setWeight() 的方法,该方法有一个 int 形式参数,用于设置狗的体重。

向 Dog 类添加另一个名为 getSize() 的方法,该方法返回一个字符串。它将使用狗的重量如下:

如果狗小于 30 磅,则返回 Stringsmall。如果狗的体重在 30 到 60 磅之间,则返回 String medium。如果狗的体重超过 60 磅,则返回 Stringlarge

将以下代码添加到 DogDemo 类中:

Dog otto = new Dog();
otto.name = "Otto";
otto.age = 7;
otto.breed = "German Shepherd";
otto.setWeight(50);
System.out.println();
otto.writeOutput();
System.out.println("Otto is a " + otto.getSize() + " dog.");

到目前为止,我还没有弄清楚如何正确设置setWeight() 方法和getSize() 方法,我也不确定我是否正确定义了实例变量权重。到目前为止,这是我的代码。

package dogdemo;

public class Dog
{
public String name;
public String breed;
public int age;
public int weight;
public int getSize()
{
    if (weight < 30) {
        getSize = ("small");
    } else {
        if (weight >= 30 && weight <=60)
            getSize=("medium");
    } else {
            getSize=("large");
            }
}
public void writeOutput()
{
    System.out.println("Name: " + name);
    System.out.println("Breed: " + breed);
    System.out.println("Age in calendar years: " +
    age);
    System.out.println("Age in human years: " +
    getAgeInHumanYears());
    System.out.println();
}
public int getAgeInHumanYears()
{
    int humanAge = 0;
    if (age <= 2)
    {
        humanAge = age * 11;
    }
    else
    {
        humanAge = 22 + ((age-2) * 5);
    }
        return humanAge;
    }
}

这是实际输出信息的另一个类代码。

package dogdemo;

public class DogDemo
{
public static void main(String[] args)
{
    Dog balto = new Dog();
    balto.name = "Balto";
    balto.age = 8;
    balto.breed = "Siberian Husky";
    balto.writeOutput();
    Dog scooby = new Dog();
    scooby.name = "Scooby";
    scooby.age = 42;
    scooby.breed = "Great Dane";
    System.out.println(scooby.name + " is a " +
    scooby.breed + ".");
    System.out.print("He is " + scooby.age +
    " years old, or ");
    int humanYears = scooby.getAgeInHumanYears();
    System.out.println(humanYears + " in human years.");
}
}

如您所见,在公共类 Dog 中,我尝试创建一个 getSize() 方法,但并不真正知道自己在做什么。我什至没有尝试创建 setWeight() 方法,因为我完全不确定如何编写代码。为了创建实例变量权重,我将其公开,但我认为它可能必须是私有的。

提前致谢,感谢所有帮助。

【问题讨论】:

  • @KennethClark 不符合要求 - 向 Dog 类添加另一个名为 getSize() 的方法,该方法返回一个字符串。
  • 海象,我想你可以回答你自己的问题,如果你在没有任何额外帮助的情况下走到这一步。提示 - 将权重设为私有。那么你将如何设置/获取狗的重量?如果您公开体重,那么您如何防止有人输入负重或过多的体重?当您考虑这些问题并尝试将它们转换为代码时,答案就会变得清晰。
  • @DavidPostill 很抱歉你是对的,没注意,修改 size 方法使用权重返回一个字符串

标签: java string methods


【解决方案1】:

我看不出你对setWeight() 有什么问题。

这很简单:

public void setWeight(int weight)
{
    this.weight = weight;
} 

至于getSize(),您的逻辑基本正确,但您必须返回一个字符串。

public String getSize()
{
    if (weight < 30) {
       return "small";
    } else {
        if (weight >= 30 && weight <=60) {
            return "medium";
        } else {
            return "large";
        }
    }
}

【讨论】:

    【解决方案2】:

    定义很好。但是类属性应该像这样带有private修饰符。

    这是它的样子,

    public class Dog{
        private String name;
        private String breed;
        private int age;
        private int weight;
    
        public void setWeight(int weight){ // parameter name is same as the attribute name
            this.weight = weight; //Using this keyword we address the attribute.
        }
    
        public String getSize(){
            if (weight < 30) {
                return "small";
            } else if (weight  >= 30 && weight <=60)
                return "medium";            
            } else {
                return "large";                
            }
        }
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      似乎你没有正确定义你的类的属性试试这个

            public class Dog
          {
          public static String name;
          public static String breed;
          public static int age;
          public static int weight;
          public static String size;
      
      
          public static String getName() {
              return name;
          }
          public static void setName(String name) {
              Dog.name = name;
          }
          public static String getBreed() {
              return breed;
          }
          public static void setBreed(String breed) {
              Dog.breed = breed;
          }
          public static int getAge() {
              return age;
          }
          public static void setAge(int age) {
              Sample3.age = age;
          }
          public static int getWeight() {
              return weight;
          }
          public static void setWeight(int weight) {
              Sample3.weight = weight;
          }
          public static String getSize() {
              return size;
          }
          public static void setSize(String size) {
              Dog.size = size;
          }
      
      public static void main(String[] args){
      setName("Browny");
      setBreed("BlaBla");
      setAge(12);
      setWeight(32);
      writeOutput();
      }
          public static void writeOutput()
          {
      if(getWeight()<30){
      setSize("small");
      }
      if(getWeight()>30 && getWeight()<60){
      setSize("medium");
      }
      if(getWeight()>60){
      setSize("Large");
      }
              System.out.println("Name: " + name);
              System.out.println("Breed: " + breed);
               System.out.println("Age:: " + age);
             System.out.println("Weight: " + weight);
             System.out.println("Size: " + size);
      
          }
      
          }
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        以下工作代码:

        public class Dog{
            public String name;
            public String breed;
            public int age;
            public int weight; //Good way replace access to private and use getter and setter.
            /**
             * It setter for weight variable.
             */
            public void setWeight(int weight){
                this.weight = weight;
            }
            /**
            * It getter for weight variable. I would replace to getWeight().
            * Furthermore your sample contain error because return type is int
            */
            public String getSize() {
                if (weight < 30) return "small";
                else {
                    if (weight >= 30 && weight <=60) {
                        return "medium";
                    } else {
                        return "large";
                    }
                }
            }
            /**
            * Note, I add output of weight. Good way is rename this method to toString()
            * Read it http://www.tutorialspoint.com/java/number_tostring.htm
            */
            public void writeOutput() {
                System.out.println("Name: " + name);
                System.out.println("Breed: " + breed);
                System.out.println("Age in calendar years: " + age);
                System.out.println("Age in human years: " + getAgeInHumanYears());
                System.out.println("Weight: " + getSize());
                System.out.println();
            }
            public int getAgeInHumanYears() {
                int humanAge = 0;
                if (age <= 2) humanAge = age * 11;
                else humanAge = 22 + ((age-2) * 5);
                return humanAge;
            }
            public static void main(String[] args){
                Dog otto = new Dog();
                otto.name = "Otto";
                otto.age = 7;
                otto.breed = "German Shepherd";
                otto.setWeight(50);
                System.out.println();
                otto.writeOutput();
                System.out.println("Otto is a " + otto.getSize() + " dog.");
                Dog balto = new Dog();
                balto.name = "Balto";
                balto.age = 8;
                balto.breed = "Siberian Husky";
                balto.setWeight(20);
                balto.writeOutput();
                Dog scooby = new Dog();
                scooby.name = "Scooby";
                scooby.age = 42;
                scooby.breed = "Great Dane";
                scooby.setWeight(70);
                scooby.writeOutput();
                System.out.println(scooby.name + " is a " + scooby.breed + ".");
                System.out.print("He is " + scooby.age + " years old, or ");
                int humanYears = scooby.getAgeInHumanYears();
                System.out.println(humanYears + " in human years.");
            }
        }
        

        测试一下:

        ~/tmp $ javac Dog.java 
        ~/tmp $ java Dog
        Name: Otto
        Breed: German Shepherd
        Age in calendar years: 7
        Age in human years: 47
        Weight: medium
        
        Name: Balto
        Breed: Siberian Husky
        Age in calendar years: 8
        Age in human years: 52
        Weight: small
        
        Name: Scooby
        Breed: Great Dane
        Age in calendar years: 42
        Age in human years: 222
        Weight: large
        
        Scooby is a Great Dane.
        He is 42 years old, or 222 in human years.
        

        【讨论】:

          【解决方案5】:

          首先您必须定义 Dog bean 类。通常我们必须将所有即时变量定义为私有变量,然后我们必须定义 getter 和 setter 来访问这些变量。 然后我们必须编写一个演示类来运行应用程序。

          狗豆类

          public class Dog {
          
            private String name;
            private String breed;
            private int age;
            private int weight;
          
            public Dog() {
            }
          
            public Dog(String name, String breed, int age, int weight) {
                this.name = name;
                this.breed = breed;
                this.age = age;
                this.weight = weight;
            }
          
            public String getName() {
                return name;
            }
          
            public void setName(String name) {
                this.name = name;
            }
          
            public String getBreed() {
                return breed;
            }
          
            public void setBreed(String breed) {
                this.breed = breed;
            }
          
            public int getAge() {
                return age;
            }
          
            public void setAge(int age) {
                this.age = age;
            }
          
            public int getWeight() {
                return weight;
            }
          
            public void setWeight(int weight) {
                this.weight = weight;
            }
          
            public String getSize() {
                if (weight < 30) return "small";
                else if (weight >= 30 && weight < 60) return "medium";
                else return "large";
            }
          
            public int getAgeInHumanYears() {
                int humanAge = 0;
                if (age <= 2) humanAge = getAge() * 11;
                else humanAge = 22 + ((getAge() - 2) * 5);
                return humanAge;
            }
          
            public void writeOutput(){
                System.out.println("Name: " +getName());
                System.out.println("Breed: " + getBreed());
                System.out.println("Age:: " + getAge());
                System.out.println("Weight: " + getAge());
                System.out.println("Size: " + getSize());
                System.out.println("Age in human age : "+ getAgeInHumanYears());
            }
          

          }

          狗示范课

          public class DogDemo {
          
            public static void main(String[] args) {
                Dog dog = new Dog("Jason", "Great Dane", 12, 65);
                dog.writeOutput();
            }
          

          }

          如果您使用 IDE 运行此应用程序,只需编译并运行此应用程序。如果您使用命令提示符来运行此应用程序,请创建单独的两个 java 文件并在类名中命名这两个文件并将上述代码添加到这些文件中并使用 javac 编译这两个文件并使用 java 运行 DogDemo 类文件

          祝你好运!!!!

          【讨论】:

            猜你喜欢
            • 2012-08-02
            • 2021-04-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-27
            • 1970-01-01
            • 1970-01-01
            • 2018-12-16
            相关资源
            最近更新 更多