2016-12-24 3 views
-5

import java.io.BufferedReader; 가져 오기 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; 
    } 
} 

}

아래 템플릿이 왜 코드가 내가 잘못된 위치에서이 공간 정보를 말하는 // 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

첫째, 매개 변수로 주소 클래스를 전달합니다.

void setAddress(Address s) 
정적 중첩 된 클래스의 주소부터

,하지만 당신은 Problem3_1 클래스 파일에서 String로 주소 선언했다. 당신이 작성하고 제대로 값을 전달하는 경우 그래서 당신은 수동으로

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

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

이 작동합니다 같은 값을 CONCAT해야합니다.

예 :

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() 외부 클래스의 방법뿐만 아니라 내부 클래스를 오버라이드 (override)가있는 경우이 원하는 출력을 인쇄해야합니다.

UPDATE

http://ideone.com/rS20uZ - 전체 코드 및 출력 여기를 확인하십시오.

+0

Problem3_1.java:84 : 비교할 수없는 유형 : java.lang.String 및 Problem3_1.Address if (p.address == a) 문자열로 선언했음을 알고 있지만 잘못했다고 생각하고 실제로 그렇게해야한다고 생각합니다. 형식의 주소 형식. – Tom

+0

'void setAddress (Address s) { \t \t // 주소 = s; \t \t \t \t this.address = s; \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 필요 : 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 : 심볼 심볼을 찾을 수 없습니다. 메소드 address (int, java.lang.String) 위치 : 클래스 Problem3_1 address = 주소 (number, street); ^ Problem3_1.java:82 : Problem3_1의 setAddress (int, java.lang.String)를 (Problem3_1.Address)에 적용 할 수 없습니다. p.setAddress (a); – Tom

+0

이제 코드를 변경하고 작동하는 netbeans에서 실행됩니다! 도움이된다면 투표를 기꺼이 받아 들여 대답으로 받아들입니다. –

관련 문제