Modifying two variables in a loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alternative49e
    New Member
    • Nov 2007
    • 5

    Modifying two variables in a loop

    I need to modify the UseTaxpayer application so each Taxpayer has a successive Social Security number from 1 top 10 and a gross income that ranges from $10,000 to $100,000, increasing by $10,000 for each successive Taxpayer. Save as UseTaxpayer2

    Here is what I have so for far from the UseTaxpayer application. Any suggestions on how to get it to increment?

    Code:
    public class Taxpayer 
    {
    	private int socNum;
    	private int yearIncome;
    	
    	public Taxpayer(int s, int i)
    	{
    		socNum = s;
    		yearIncome = i;		
    	}
    	public int getSocNum()
    	{
    		return socNum;
    	}
    	public int getYearIncome()
    	{
    		return yearIncome;
    	}
    		
    }
    and

    Code:
    public class UseTaxpayer
    {	
    	public static void main(String[] args)
    	{	
    	 Taxpayer[] aTaxpayer = new Taxpayer[10];
    	 		
           for(int i = 0; i < 10; i++)
    			aTaxpayer[i] = new Taxpayer(999999999,0);
    			for(int i = 0 ; i < 10; i++)
    			{
             	System.out.println(aTaxpayer[i].getSocNum()
    				+ " " + aTaxpayer[i].getYearIncome());
    				
    			}
    		}
    }
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    You've made a good start by putting the Taxpayer constructor into a loop. Remember that the Taxpayer constructor gets a SSAN & income input parameter, so you just need to vary these like your instructions instead of using the same values. HTH --Sam

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Have a look at

      [CODE=java] for(int i = 0; i < 10; i++) {
      // you already have a counter called i so use it
      aTaxpayer[i] = new Taxpayer(i, i + 10);
      }[/CODE]

      All you need to do is manipulate the i value at each point as required to get the values that you want.

      Comment

      Working...