aspose file tools*
The moose likes Beginning Java and the fly likes A couple of questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A couple of questions" Watch "A couple of questions" New topic
Author

A couple of questions

Sean Heise
Greenhorn

Joined: Oct 30, 2012
Posts: 22
First off, I had an assignment last week, and was curious if I was reading to far into the assignment or assuming what needed to happen.
The assignment was:
[highlight]
The purpose of this assignment is to use arrays and loops. It is based on a problem from Java Programming by Joyce Farrell.

Create a class called Taxpayer. This will be a class from which objects will be instantiated (i.e. no main method). Also create a UseTaxpayer class. Include a main method in this class. The UseTaxpayer class will be the class where you instantiate 10 Taxpayer objects.
The Taxpayer class will have two private variables - a long for the social security number (SSN) and a double for the annual income. Create a Taxpayer constructor that accepts arguments for the SSN and the income. Also create get/set methods for both class variables.
In the UseTaxpayer class, create an array of 10 Taxpayer objects. Instantiate the 10 Taxpayer objects using the constructor that requires values for SSN and income. Use 1 to 10 for the SSN values and 10000 (10,000) to 100000 (100,000) for the income respectively. That is, the first taxpayer will have a SSN of 1 and an income of 10000. The tenth taxpayer will have a SSN of 10 and an income of 100000. You must use a looping statement to call the constructor for the Taxpayer objects.
Print the values from your Taxpayer array objects in the UseTaxpayer class. (Sample output). You must print the values from the array in a separate method (separate from main). You must pass the Taxpayer array as an argument to this method.
Use the length instance variable for all loop control conditions.
The UseTaxpayer class cannot have any "class-wide" variables except symbolic constants. This means all your variables in the UseTaxpayer class must be defined within a method.
This project is worth a maximum of 30 points if received by the deadline. Check the class syllabus for the penalties for submitting an assignment late. Check the online calendar for the exact due date if you are in an online section of this class. Check the class syllabus for the exact due date if you are in an on-campus section of this class.
[/highlight]

The output should read like this:
SSN = 1 Income = $10000.00
SSN = 2 Income = $20000.00
SSN = 3 Income = $30000.00
SSN = 4 Income = $40000.00
SSN = 5 Income = $50000.00
SSN = 6 Income = $60000.00
SSN = 7 Income = $70000.00
SSN = 8 Income = $80000.00
SSN = 9 Income = $90000.00
SSN = 10 Income = $100000.00

which mine does as above.
However, as the assignment reads, it seems like I should be having a constructor in the first class as I do above, however instead of initializing the array like I have, It should be an array like


However if I did it like this, I'd be asking for the print out to be printing Income[x] etc, (whatever is in placeholder 0, 1, 2 etc etc) which would, I feel, negate the purpose of having a constructor in one class, and an array in the next?

Is this a correct assumption and I've written my code incorrectly? I understand there's different ways of writing code for things, but I don't feel like I'm doing what the assignment asks however I feel the assignment makes no sense the way it's written?
------------------------------------------------------------------------------------------------
Secondly, using the get/set methods I was wondering if when you use the get/set methods, such as:

Is it more proper in the get to do return cost; or return unitCost; ?

------------------------------------------------------------------------------------------------
Thirdly, when using variables, I was wondering why some variables must be static and others not. For example, I was doing the tutorial in my text book and they had this:

Where the variable guest, doesn't need to be static.

However, writing my own code for the assignment for the week I get:

Where it's telling me that "orderQuant", "orderCost", and "total" must be static, no if's and or buts about it. Is there a reason this is so? The assignment deals with inheritance, dunno if that's important or not, but thought I'd throw it in there. Anyway I know it's a lot, but thanks in advance.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Sean Heise wrote:First off...

Sean.

I've shortened those enormous <hr> lines of yours because they screw up the window formatting and make your thread hard to read. For future reference, the same is also true of very long lines inside code tags (not a problem in this case).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2628
    
    9

Hello first off, the instructions are pretty straight telling you need to do. The thing I see that is different from you is the array.

Sean Heise wrote:
In the UseTaxpayer class, create an array of 10 Taxpayer objects. Instantiate the 10 Taxpayer objects using the constructor that requires values for SSN and income. Use 1 to 10 for the SSN values and 10000 (10,000) to 100000 (100,000) for the income respectively. That is, the first taxpayer will have a SSN of 1 and an income of 10000. The tenth taxpayer will have a SSN of 10 and an income of 100000. You must use a looping statement to call the constructor for the Taxpayer objects.



Instead of have 2 arrays it really wants an array of Taxpayers


Sean Heise wrote:
Print the values from your Taxpayer array objects in the UseTaxpayer class. (Sample output). You must print the values from the array in a separate method (separate from main). You must pass the Taxpayer array as an argument to this method.
Use the length instance variable for all loop control conditions.


This is simply loop the array using for loop and using the getXXX methods from the TaxPayers to get the SSN and income.

Sean Heise wrote:
The UseTaxpayer class cannot have any "class-wide" variables except symbolic constants. This means all your variables in the UseTaxpayer class must be defined within a method.


The TaxPayer class doesn't need to have "class-wide" (static) variables, just normal instance variables.

Also I notice you setXXX methods are sometimes mixed up with what variables to assign to.



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

K. Tsang wrote:Hello first off...

K, Tsang, I'll say to you what I said to Sean:
Please don't put very long lines inside code tags.
They screw up the window formatting and make threads hard to read.
I've broken yours up this time.

Please re-read the UseCodeTags page thoroughly.

Winston
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2628
    
    9

Sorry Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

K. Tsang wrote:Sorry Winston

No probs.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A couple of questions