954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Manipulate Constructor method/get Method into a for loop, help!!

The code below is the file for the constructor method and the get methods

//Date: 3/18/2010

public class Taxpayer 
{
	private int socSec;
	private double yearlyGross;
	
	public Taxpayer(int taxPayer, double income) 
	{
		socSec = taxPayer;
		yearlyGross = income;
	}
	public int getSocSec()
	{
		return socSec;
	}
	public double getYearlyGross()
	{
		return yearlyGross;
	}

}

Next is the code for the for loop printing of the constructor and get method in another file

//Date: 3/18/2010

public class UseTaxpayer 
{
	public static void main(String[] args) 
	{
	Taxpayer[] somePayer = new Taxpayer[10];
	int x;
		
	for (x = 0; x < 10; ++x)
	{
	somePayer[x] = new Taxpayer(999999999, 0.0);
	System.out.println("Social Security #: " + somePayer[x].getSocSec() + ", and yearly gross income: $" + somePayer[x].getYearlyGross());
		}
	}

}

As you can see if you run it, it prints out to
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0

which is what I want it to do, my problem is taking it and manipulating it to read
Social Security #: 111111111, and yearly gross income: $10000.0
Social Security #: 222222222, and yearly gross income: $20000.0
Social Security #: 333333333, and yearly gross income: $30000.0
Social Security #: 444444444, and yearly gross income: $40000.0
Social Security #: 555555555, and yearly gross income: $50000.0
Social Security #: 666666666, and yearly gross income: $60000.0

all the way up to 999999999 and income of $100,000.
I spent 2 hours trying to do it with what I learned from the book and I can't figure it out. Someone help?

musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

You are entering a taxpayer with the same parameters within your for loop. Depending on what parameters you start with you are going to need to increment or decrement, for your current example it you would need 2 variables. One would increment by 10,000 each time and you would add it to your income parameter each loop. For the taxpayer, you could set a variable equal to 111111111 and then increment it times 2 upon each loop.

So a general overview of what i just tried to say. create 2 variables that will be your paremeters for new taxPayer and then increment/decrement as needed for it to match the criteria you want it to output in the end.

That explanation may not be the best, but maybe it can atleast give you an idea.

LReynolds
Newbie Poster
15 posts since Mar 2010
Reputation Points: 10
Solved Threads: 4
 

thank you sooo much. That's all I needed to do, duh, haha. That was so easy. I like this website, very helpful people. :-)

musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Quick question: I was able to do everything that I wanted done, but now my only question is, how do I do the whole for loop, but change just ONE of them...as you see below, all social security numbers go from 1 to 9 but the 10th one does 111....0 which is 1 too many for a social security number, how do I change just the 10th one to say: 101010101????

Social Security #: 111111111, and yearly gross income: $10,000.00
Social Security #: 222222222, and yearly gross income: $20,000.00
Social Security #: 333333333, and yearly gross income: $30,000.00
Social Security #: 444444444, and yearly gross income: $40,000.00
Social Security #: 555555555, and yearly gross income: $50,000.00
Social Security #: 666666666, and yearly gross income: $60,000.00
Social Security #: 777777777, and yearly gross income: $70,000.00
Social Security #: 888888888, and yearly gross income: $80,000.00
Social Security #: 999999999, and yearly gross income: $90,000.00
Social Security #: 1111111110, and yearly gross income: $100,000.00

is it a if() statement?? help.

musikluver4
Junior Poster in Training
81 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

i think you have not extended the taxpayer class from other main method class and also the both classes shud be in same package
The code below is the file for the constructor method and the get methods

//Date: 3/18/2010

public class Taxpayer 
{
	private int socSec;
	private double yearlyGross;
	
	public Taxpayer(int taxPayer, double income) 
	{
		socSec = taxPayer;
		yearlyGross = income;
	}
	public int getSocSec()
	{
		return socSec;
	}
	public double getYearlyGross()
	{
		return yearlyGross;
	}

}

Next is the code for the for loop printing of the constructor and get method in another file

//Date: 3/18/2010

public class UseTaxpayer extends Taxpayer 
{
	public static void main(String[] args) 
	{
	Taxpayer[] somePayer = new Taxpayer[10];
	int x;
		
	for (x = 0; x < 10; ++x)
	{
	somePayer[x] = new Taxpayer(999999999, 0.0);
	System.out.println("Social Security #: " + somePayer[x].getSocSec() + ", and yearly gross income: $" + somePayer[x].getYearlyGross());
		}
	}

}

As you can see if you run it, it prints out to
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0
Social Security #: 999999999, and yearly gross income: $0.0

which is what I want it to do, my problem is taking it and manipulating it to read
Social Security #: 111111111, and yearly gross income: $10000.0
Social Security #: 222222222, and yearly gross income: $20000.0
Social Security #: 333333333, and yearly gross income: $30000.0
Social Security #: 444444444, and yearly gross income: $40000.0
Social Security #: 555555555, and yearly gross income: $50000.0
Social Security #: 666666666, and yearly gross income: $60000.0

all the way up to 999999999 and income of $100,000.
I spent 2 hours trying to do it with what I learned from the book and I can't figure it out. Someone help?

Ron2794
Newbie Poster
16 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 
i think you have not extended the taxpayer class from other main method class and also the both classes shud be in same package


You couldn't be more wrong. Please don't give wrong advices if you don't know what you are talking about.

Do all Social-Security-Numbers(SSN) need to be like this: (111...111, 222....222, ...) ?
Since you can have 999999999 different combinations, why do you want to limit yourself by using only those, when you can do this:

for (x = 0; x < 10; ++x)
	{
	somePayer[x] = new Taxpayer((x+1), 0.0);

}

The above will print:
1
2
3
4
....

If you want them to be:
000000001
000000002
000000003
000000004
Then don't use an int variable, use a String. Ints are used for calculations and you are not going to do any addittions with the SSN.
So this is my suggestion:

//Date: 3/18/2010

public class Taxpayer 
{
	private String socSec;
	private double yearlyGross;
	
	public Taxpayer(int taxPayer, double income) 
	{
		convertToString(taxPayer);
		yearlyGross = income;
	}
	public String getSocSec()
	{
		return socSec;
	}
	public double getYearlyGross()
	{
		return yearlyGross;
	}

       private convertToString(int i) {
           socSec = String.valueOf(i);

           while (socSec.length()<9) {
                   socSec = "0"+socSec;
           }
       }
}


In that wat when you call the constructor with an int: 1, then it will become the Stirng "0000000001"

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: