Java help! - Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alternative49e
    New Member
    • Nov 2007
    • 5

    #1

    Java help! - Arrays

    Windows XP
    JAVA

    n00b here. I'm not very good at Java. In fact I am really struggling with it. I've spent the last 6 hours trying to get this to work. The book I have isn’t very helpful.

    I'm supposed to create a class names Taxpayer. Data fields for Taxpayer include Social Security number (use an int for the type, and do not use dashes within the Social Security number) and yearly gross income. Methods include a constructor that requires values for both data fields, and two methods that each return one of the data fields values. Write an application named UseTaxpayer that declares an array of 10 Taxpayer objects. Set each Social Security number to 999999999 and each gross income to zero. Display the 10 Taxpayer objects.
    Save the files as Taxpayer.java and UseTaxpayer.

    here is the code I have written so far. I seem to have hit a wall. I'm not even sure if I went the correct course. Any suggestions?

    thanks

    Code:
    public class Taxpayer 
    {
    	private int socNum;
    	private double yearIncome;
    	
    	public Taxpayer(int s, int i)
    	{
    		socNum = s;
    		yearIncome = i;		
    	}
    	public int getSocNum()
    	{
    		return socNum;
    	}
    	public double getYearIncome()
    	{
    		return yearIncome;
    	}
    		
    }
    Code:
    public class UseTaxpayer
    {
    	public static void main(String[] args)
    	{
    		Taxpayer[] aTaxpayer = new Taxpayer[10];
    		
    		
    		
    	
    		
    	}
    }
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Everything looks fine so far. Do you know how to use for or while loops?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by alternative49e
      Windows XP
      JAVA

      n00b here. I'm not very good at Java. In fact I am really struggling with it. I've spent the last 6 hours trying to get this to work. The book I have isn’t very helpful.

      I'm supposed to create a class names Taxpayer. Data fields for Taxpayer include Social Security number (use an int for the type, and do not use dashes within the Social Security number) and yearly gross income. Methods include a constructor that requires values for both data fields, and two methods that each return one of the data fields values. Write an application named UseTaxpayer that declares an array of 10 Taxpayer objects. Set each Social Security number to 999999999 and each gross income to zero. Display the 10 Taxpayer objects.
      Save the files as Taxpayer.java and UseTaxpayer.

      here is the code I have written so far. I seem to have hit a wall. I'm not even sure if I went the correct course. Any suggestions?

      thanks

      Code:
      public class Taxpayer 
      {
      	private int socNum;
      	private double yearIncome;
      	
      	public Taxpayer(int s, int i)
      	{
      		socNum = s;
      		yearIncome = i;		
      	}
      	public int getSocNum()
      	{
      		return socNum;
      	}
      	public double getYearIncome()
      	{
      		return yearIncome;
      	}
      		
      }
      Code:
      public class UseTaxpayer
      {
      	public static void main(String[] args)
      	{
      		Taxpayer[] aTaxpayer = new Taxpayer[10];
      		
      		
      		
      	
      		
      	}
      }
      In your TaxPayer class you declared yearIncome as double but you are trying to assign an int to it.
      Your array declaration in the UseTaxpayer class is fine.
      You now need a for loop to go through each element position in the array and assigning the new Taxpayer objects.

      Comment

      • alternative49e
        New Member
        • Nov 2007
        • 5

        #4
        ok I got it to work. thanks for the help. but now problem # 2. 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());
         
        			}
        		}
        }
        Please do not double post. Post any replies here
        Locking this thread.

        Comment

        Working...