2016-12-24 35 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; 
    } 
} 

}

BELOWテンプレートは、なぜコードは、私が間違った場所にあるこの空の情報を持っていると言っている// COMPLETE?正しい場所はどこですか?それとも、「適用」できないのですか?

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

まず、パラメータとしてAddressクラスを渡します。静的な入れ子になったクラスの住所以来

void setAddress(Address s) 

ていますが、Problem3_1クラスファイルにStringとしてアドレスを宣言しました。したがって、手動で値を連結する必要があります

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

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

値を正しく作成して渡す場合は、これがうまくいくはずです。

例:あなたはのtoString()外側のクラスのメソッドと同様に内部クラスをオーバーライドしている場合

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

これはあなたの所望の出力を印刷する必要があります。

UPDATE

http://ideone.com/rS20uZ - フルコードと出力のために、ここで確認してください。

+0

Problem3_1.java:84:比類のない型:java.lang.StringとProblem3_1.Address if(p.address == a)私は文字列として宣言していることを知っていますが、私はそれが間違っていると信じています。 Address in typeの形式で入力します。 – Tom

+0

'void setAddress(Address s){ \t \t //アドレス= s; \t \t \t \t this.address =秒; \t} ' – zinger

+0

'void setAddress(Address 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:互換性がありませんタイプ が見つかりました:Problem3_1.Address required: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で動作します!役に立ったと投票して回答として受け入れると –

関連する問題