Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: A couple of questions.

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A couple of questions.

    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:
     public void setUnitCost(double cost)
     {
      cost = unitCost;
     }
     public double getUnitCost()
     {
      return unitCost;
     }
    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:
    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);
    	}
    }
    Where the variable guest, doesn't need to be static.

    However, writing my own code for the assignment for the week I get:
    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);
    	}
    }
    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.
    Last edited by jps; October 29th, 2012 at 08:18 PM. Reason: fixed code tag


Similar Threads

  1. A few questions
    By elatechris777 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2012, 06:33 PM
  2. Couple compile errors for dice game
    By smithmar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2012, 01:16 PM
  3. Couple Questions to have JFormDesigner Work
    By Feriscool in forum Java Theory & Questions
    Replies: 1
    Last Post: June 15th, 2011, 10:28 AM
  4. A couple of basic java assignments, need help!
    By barnabus in forum Paid Java Projects
    Replies: 1
    Last Post: August 18th, 2010, 10:18 AM
  5. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM