2014-11-17 2 views
0

안녕하십니까. 링크 된 목록에서 연도의 범위를 결정하려고하는데 도움이 필요합니다. 이것을 달성하기 위해 나는 subscYears 링크 된 목록을 보유하고있는 컨테이너 클래스에 minYear 및 maxYear라는 2 개의 변수를 가지고 있습니다. SubscriptionYear는 해당 연도의 연도 및 셀룰러 데이터를 읽습니다. 클래스 국가는 국가 이름을 저장하고 각 국가의 연도 및 셀룰러 데이터를 저장하는 linked list subscriptionYear의 컨테이너입니다.링크 된 목록의 연도 범위 결정

minYear는 9999로 설정되고 maxYear는 0으로 설정됩니다. 목록에 구독을 추가 할 때마다 minYear 및 maxYear를 업데이트합니다. 그리고 "minYear"와 "maxYear"를 사용하여 요청한 가입 기간이 유효한지 확인합니다. 요청한 구독이 유효한지 확인하기 위해 minYear 및 maxYear를 어떻게 구현합니까?

내 수업 Subscriptionyear는 해당 연도의 연도 및 셀룰러 데이터를 이중으로 저장합니다.

//stores the year and statistical subscription data for that year 
public class SubscriptionYear { 

private int year; 
private double subscriptions; 
SubscriptionYear next; 

public SubscriptionYear(int year,double subscriptions) 
{ 
    setYear(year); 
    setSubscription(subscriptions); 
    this.next = null; 
} 
public void setYear(int Year) 
{ 
    this.year= Year; 
} 
public void setSubscription(double value) 
{ 
    this.subscriptions = value; 
} 
public int getYear() 
{ 
    return year; 
} 
public double getSubscription() 
{ 
    return subscriptions; 
} 
public String toString() 
{ 
    return "Number of Subscriptions: "+subscriptions; 
} 
public void setNode(SubscriptionYear next) 
{ 
    this.next = next; 
} 
public SubscriptionYear getNext() 
{ 
    return this.next; 
} 
} 

수준의 나라 : 내 클래스 국가 국가의 이름을 저장하고 해당 국가 연도와 통계 정보를 포함 susbcriptionYear의 연결리스트에 대한 컨테이너 역할을합니다. 목록을 만들고 성공적으로 목록을 인쇄 할 수 있었지만 위의 해결 된 문제를 해결할 수 없습니다. Country 객체가 minYear=9999maxYear=0와 함께, 당신은 비교 및 ​​검증을 수행 할 Country.addSubscription() 방법을 업데이트 할 수 있습니다 인스턴스화해야하는 과제에 제약이 있기 때문에 통계 데이터 1960 년에서 2012 년

public class Country { 

private String countryNames; 
private SubscriptionYear subscriptions; 
private int minYear; 
private int maxYear; 

public Country(String country) 
{ 
    this.countryNames = country; 
    this.subscriptions = null; 
    this.maxYear = 0; 
    this.minYear = 9999; 
    } 

//adds the subscription and updates the minYear and maxYear 
//Dont think i set up the minYear and maxYear correctly. 
public void addSubscriptionYear(int year, double subscription) 
{ 
    SubscriptionYear newNode = new SubscriptionYear(year, subscription); 
    if(this.isEmpty()) 
    { 
     newNode.setNode(subscriptions); 
     subscriptions = newNode; 
     if(newNode.getYear()==1960) 
     { 
      this.minYear++; 
     } 
    } 
    else{ 
     SubscriptionYear current = subscriptions; 
     while(current.getNext()!=null) 
     { 
      if(current.getYear()==1960) 
      { 
       this.minYear++; 
      } 
      else if(current.getYear()==2012) 
      { 
       this.maxYear++ 
      } 
     current = current.getNext(); 
     } 
     current.setNode(newNode); 
    } 
} 

//overrides the toString method and prints out the countries. 
public String toString() 
{ 
    String result=""; 
    result += "\n"+this.countryNames; 
    SubscriptionYear current = subscriptions; 
    while(current!=null) 
    { 
     result+="\t"+current.getSubscription(); 
     current = current.getNext();   
    } 
    return result; 
} 
//returns countryName 
    public String getName() 
{ 
    return this.countryNames; 
} 
public boolean isEmpty() 
{ 
    return (subscriptions == null); 
} 
} 
+0

가입 기간 중 가장 이른 연도와 가장 최근 연도를 찾으십니까? 또는 1960 년과 2012 년에 구독이 몇 개인 지 계산하려고합니까? 또한이 컴파일 오류가 발생 했습니까? –

+0

@DavidWallace와 같은 질문을 할 것입니다. 또한 호기심을 위해서 왜 LinkedList 클래스를 사용하여 몇 년 동안의 목록을 유지하고 있지 않습니까? – working

+0

@RJadhav 이것은 교수의 프로젝트였습니다. 우리는 그녀의 지침을 따라야했습니다. 그녀는 API –

답변

1

에 시작한다.

그래서이 같은 작업을해야합니다 :

public void addSubscriptionYear(int year, double subscription) { 
    // check if this new node's year is within the valid range 
    if(year > 2012 || year < 1960) 
    throw new IllegalArugmentException("New node's year is not within the 1960 and 2012 range"); // or however else you want to handle this 

    // check if this new node has a year earlier than this.minYear 
    this.minYear = this.minYear > year ? year : this.minYear; 

    // check if this new node has a year late than this.maxYear 
    this.maxYear = this.maxYear < year ? year : this.maxYear; 

    SubscriptionYear newNode = new SubscriptionYear(year, subscription); 
    if(this.isEmpty()) { 
    newNode.setNode(subscriptions); 
    subscriptions = newNode; 
    } 
    else{ 
    SubscriptionYear current = subscriptions; 
    while(current.getNext()!=null)   
     current = current.getNext(); 
    current.setNode(newNode); 
    } 
} 

새로운 노드가 추가 될 때마다, 우리는 새해가 this.maxYear보다 작 this.minYear 이상 여부를 확인한다. 그럴 경우 this.minYearthis.maxYear을 적절하게 조정합니다.

+0

심각하게? 초보자에게 삼항 연산자를 사용하도록 가르치고 있습니까? –

+0

@DavidWallace 나쁜 것입니까? 문제가 무엇인지 정확히 알지 못합니다. 나는 일반적으로 더 작고 더 짧은 방법으로 코드를 리팩토링 할 것입니다. 그러나 FWIW, 나는 OP를 더 혼동하고 싶지 않다. –

+0

대부분의 초보자는 삼항 연산자를 혼란스럽게 생각합니다. 그래서 내 대답에 간단한 'if'문을 사용했습니다. –

0

addSubscriptionYear 메쏘드의 어딘가에 다음과 같은 것이 필요할 것입니다. 당신이 이미 발견 한 초기보다 이전 해와 구독을 발견 할 경우

if (year < minYear) { 
    minYear = year; 
} 

if (year > maxYear) { 
    maxYear = year; 
} 

그런 식으로, 당신은 minYear을 조정하고; 이미 발견 한 최신 구독보다 늦은 연도 구독을 발견하면 maxYear을 조정 중입니다.

프로그램에 몇 가지 오류가 있습니다. 하지만 계속 일하면 계속 찾아서 제거 할 것입니다.