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:
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.
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
int[] TaxPayers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}: int[] Income = {10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 10000};
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; ?public void setUnitCost(double cost) { cost = unitCost; } public double getUnitCost() { 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.import javax.swing.*; public class Party { private int guests; public void displayGuests() { JOptionPane.showMessageDialog(null, "Guests: " + guests); } public void inputGuests() { String guestsString = new String(" "); guestsString = JOptionPane.showInputDialog(null, "Enter the number of guests at your party "); guests = Integer.parseInt(guestsString); } }
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.public class Order { private String customer; private int custNumber; private static double orderQuant, unitCost, total; public void setCustomer(String cust) { cust = customer; } public String getCustomer() { return customer; } public void setCustNumber(int number) { number = custNumber; } public int getCustNumber() { return custNumber; } public void setOrderQuant(double quantity) { quantity = orderQuant; } public double getOrderQuant() { return orderQuant; } public void setUnitCost(double cost) { cost = unitCost; } public double getUnitCost() { return unitCost; } public static void computePrice() { total = (orderQuant * unitCost); } }