2016-12-24 56 views
-5

import java.io.BufferedReader; import java.io.InputStreamReader;如何將變量設置爲java中的傳入參數?

//填寫以下

公共類Problem3_1 {

//complete this class, called Problem3_1, with the following items: 

//1. Declare four attributes, name, age, height, and weight of types String and int-s. 
//Write a constructor for this class that initializes ONLY the name, age, and height to three incoming arguments, 
//and sets the weight to always be -1 (the latter is not an incoming argument). 
String name; 
int age; 
int height; 
int weight; 
Address address; 
public Problem3_1(String name, int age, int height) { 
    this.name = name; 
    this.age = age; 
    this.height = height; 
    weight = -1; 
} 

void setAddress(int number, String street) { 
    address = new Address(number, street); 
} 




//2. Imagine there is a class called Address that you have access to (it's below). 
//Its constructor takes an integer street number and a String street. Add an attribute called address to 
//the Problem3_1 class, and create a method called setAddress that sets the attribute to the incoming argument. 

public static class Address{ 

    int number; 
    String street; 

    public Address(int number, String street){ 
     this.number = number; 
     this.street = street; 
    } 
} 

}

模板那麼,爲什麼說代碼我在錯誤的位置這一空白信息?它的正確位置在哪裏?或者爲什麼不能「應用」?

PS:錯誤我得到:

Problem3_1.java:82: setAddress(int,java.lang.String) in Problem3_1 cannot be applied to (Problem3_1.Address) p.setAddress(a);

測試用例:

public static void main(String[] args){ 

    /*Below are tests that will check if you completed the code above correctly; if 
     your code doesn't compile, you'll need to fix those errors first. 

     DO NOT WRITE CODE BELOW THIS POINT 
    */ 

    int failed = 0; 

    Problem3_1 p = new Problem3_1("Jane", 22, 65); 

    if (p.name.compareTo("Jane") == 0 && p.age == 22 && p.height == 65 && p.weight == -1) 
     System.out.println("Test 1 passed!"); 
    else{ 
     failed ++; 
     System.out.println("Please check your code for question 1! "); 
    } 

    Address a = new Address(12, "Fairfax Dr"); 
    p.setAddress(a); 

    if (p.address == a) 
     System.out.println("Test 2 passed!"); 
    else{ 
     failed ++; 
     System.out.println("Please check your code for question 2! "); 
    }   


    if (failed > 0) 
     System.out.println(systemCall("Failed")); 
    else{ 
     System.out.println("GREAT WORK! EVERYTHING PASSED!"); 
     System.out.println(systemCall("Nice")); 
    } 

} 
+1

您忘記了setAdress函數中的地址參數 – Marko

回答

0

首先,通過地址類作爲參數。

void setAddress(Address s) 

因爲在靜態嵌套類地址,但你已宣佈地址在你的Problem3_1類文件String。所以你需要手動連接數值,如

void setAddress(Address s) { 
     //address = s; 

     address = s.number + " , " + s.street; 
    } 

這應該工作,如果你正在創建並正確傳遞值。

實施例:

Problem3_1 a = new Problem3_1("name1",20,5); 
    Problem3_1.Address a_address= new Problem3_1.Address(20,"1st street"); 
    a.setAddress(a_address); 

    System.out.println(a.address); 

這應該打印期望的輸出,如果你有覆蓋外類中的toString()方法以及內部類。

UPDATE

http://ideone.com/rS20uZ - 在這裏獲得完整的代碼和輸出。

+0

Problem3_1.java:84:無法比較的類型:java.lang.String和Problem3_1.Address if(p.address == a)我知道我把它聲明爲一個字符串,但我相信我做錯了,它實際上應該是以地址類型的形式。 – Tom

+0

'空隙setAddress(地址S){ \t \t //地址= S; \t \t \t \t this.address = S; \t}」 – zinger

+0

'空隙setAddress(地址S){\t \t \t \t this.address = S;在這兩個類 \t}」 和覆蓋toString()方法。工作正常。 測試1通過! 測試2通過! 偉大的工作!一切都過去了! 尼斯 – zinger

0

喜歡的東西:

void setAddress(Address a) { 
    this.address = a; 
} 

既然這樣,你是不是傳遞任何值setAddress ,所以setAddress沒有什麼可做的。

+0

我試過了,現在我有錯誤:Problem3_1.java:86:無法比較的類型:java.lang.String和Problem3_1.Address – Tom

+0

和Problem3_1.java:28:incompatible類型 發現:Problem3_1.Address 必需:java.lang.String – Tom

+0

聽起來像你正在傳遞一個字符串到'setAddress'而不是一個地址。你怎麼調用'setAddress'? – negacao

0
public class Problem3_1{ 

String name; 
int age; 
int height; 
int weight;  
static Address address; 

public Problem3_1(String name, int age, int height,int weight) { 
    this.name = name; 
    this.age = age; 
    this.height = height; //cm 
    this.weight = weight; //kg 
} 

void setAddress(int number,String street) { 
    address = new Address(number,street); 
} 

public static void main(String [] args){ 
    Problem3_1 problem=new Problem3_1("Tom",28,180,78); 
    problem.setAddress(4460,"new Delhi"); 
    System.out.println(address.getAddressNumber() +" number of street : "+address.getAddressString()); 

    } 


public static class Address{ 

    private int number; 
    private String street; 

    public Address(int number, String street){ 
     this.number = number; 
     this.street = street ; 
    } 
    int getAddressNumber(){ 
     return number; 
    } 
    String getAddressString(){ 
     return street; 
    } 

    } 

}

+0

希望它適用於你! –

+0

我會將測試用例添加到主代碼中,但現在我將其作爲錯誤。 Problem3_1.java:26:找不到符號 符號:方法地址(INT,java.lang.String中) 位置:類Problem3_1 地址=地址(號碼,街道); ^ Problem3_1.java:82:在Problem3_1 setAddress(INT,java.lang.String中)不能被施加到(Problem3_1.Address) p.setAddress(a)的 – Tom

+0

現在我改變了代碼並運行在netbeans中!如果它有幫助請投票並接受爲答案 –