2016-12-24 40 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中!如果它有帮助请投票并接受为答案 –

相关问题