2014-09-30 49 views
0

我目前正在研究一個實驗,該實驗需要我向我的教科書中找到的代碼添加其他代碼。我嘗試在這裏爲我的問題尋找解決方案,但我找不到一個。使用正式參數創建方法以設置值和返回字符串的方法

我們對實驗室的說明是這樣的:

另一個實例變量添加到狗類叫做重量,這是一個int變量。

向Dog類添加另一個方法,稱爲setWeight(),它具有一個設置狗重量的int形式參數。

向Dog類中添加另一種方法,稱爲getSize(),它返回一個String。它會使用狗的體重如下:

如果狗小於30磅,它會返回String。如果狗的體重介於30到60磅之間,則會返回String中等。如果狗超過60磅,它將返回大的String

下面的代碼添加到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; 
    } 
} 

下面是實際輸出信息的其他類代碼。如你所見,在公開課Dog中,我嘗試過創建一個getSize()方法,但並不真正知道我在做什麼。我甚至沒有嘗試創建setWeight()方法,因爲我完全不確定如何編碼。爲了創建實例變量權重,我公開了它,但我認爲它可能必須是私有的。

在此先感謝和所有幫助讚賞。

+0

@KennethClark不符合要求 - **向Dog類中添加另一種方法,稱爲getSize(),它返回一個String。** – DavidPostill 2014-09-30 05:03:19

+0

Walrus,我想你可以回答你自己的問題,如果你有這麼遠沒有任何額外的幫助。提示 - 使重量私人。你將如何設置/獲得一隻狗的重量?如果你公開體重,那麼你如何防止某人進入負面或體重過重?當你考慮這些問題並嘗試將它們轉換成代碼時,答案就會變得清晰起來。 – 2014-09-30 05:03:50

+0

@DavidPostill道歉你是正確的,沒有注意,修改大小方法返回一個字符串使用重量 – 2014-09-30 05:12:07

回答

2

我沒有看到你的問題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"; 
     } 
    } 
} 
1

它定義的很好。但是類屬性應該與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";     
     } 
    } 
} 

希望這有助於。

1

看來你沒有正確定義類的屬性試試這個

 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); 

    } 

    } 

我希望這將有助於你

1

以下工作代碼:

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. 
1

首先你必須定義Dog bean類。通常我們必須將所有即時變量定義爲私有變量,然後我們必須定義getter和setter來訪問這些變量。 然後我們必須編寫一個演示類來運行應用程序。

狗bean類

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類文件

祝您好運! !

相關問題