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 7 of 7

Thread: Changing Array variables from different classes

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Changing Array variables from different classes

    Hey guys/gals,

    I'm having terrible trouble with my code and I would be most grateful if somebody could point me in the right direction.
    First off let me explain my situation. I have a assignment due in Monday morning and I cant make any progress..
    The proposal from my lecturer was to create a program with a superclass called employee, a subclass called production worker that has 2 child classes, Team lead and ordinary production worker. I have to be able to input how many workers are working for the team lead and then he will get a bonus based on the "productivity" of the employees he has under him. I was told to do this using arrays so my code is minimal but also so I can add additional TL's when my program is working. I also have to declare the "bonus" as abstract in the employee class so that I can add a supervisor to the mix later on.

    I hope I have explained this so that it makes sense to somebody.. below is my code any any help would be wonderful!

    //------------------- Employee class------------------------
     
    public abstract class Employee implements Displayable
     
    {
    	private double bonus;
        private int employeeID;
        public String name;
        private int startDate;
        private double pay;
        private static int count;
     
     
        public abstract double calculateBonus();// NOTE: no body for this method
     
     
     
    	public double getBonus()
    	{
    		calculateBonus();
    		return bonus;
    	}
     
    	public void setBonus(double bonus)
    	{
    		this.bonus = bonus;
    	}
     
        ////////////////////Constructor/////////////////////////////////
     
        public Employee()
        {
            employeeID = 1 + count;        // start student IDs from 1
            count++;                    //incrementing for each new studentinStudentID;
        }
     
        public Employee(String inName)
        {
     
            employeeID = 1 + count;        // start student IDs from 1
            count++;                    //incrementing for each new studentinStudentID;
     
            name = inName;
        }
     
     
     
     
    	//----------ID---------------
     
    		public void setEmployeeID(int ID)
    		{
    			employeeID =  ID;
    		}
    		public int getEmployeeID()
    		{
    			return employeeID;
    		}
     
    		//-------------Name------------
     
     
    		public void setName(String Name)
    		{
    			name = Name;
    		}
    		public String getName()
    		{
    			return name;
    		}
     
    		//---------Start Date----
     
     
    		public void setStartDate(int DOS)
    		{
    			startDate = DOS;
    		}
    		public int getStartDate()
    		{
    			return startDate;
    		}
     
     
     
    		//------------PAY------------
    		public void setPay(double pay)
    		{
    			this.pay = pay;
    		}
     
    		public double getPay()
    		{
     
    			return pay;
    		}
     
     
    	public void display()
    	{
    		System.out.println("Name is " +name);
    		System.out.println("ID is " +employeeID );
    		System.out.println("the start of work Date " + startDate);
     
    	}
     
     
     
    }

    //------------------production worker--------------------------
     
    public abstract class ProductionWorker extends Employee
    {
     
    	public int totalPay;
    	public double hoursWorked;
    	public int unitsProduced;
    	public int rate;
     
     
    		public ProductionWorker()
    		{
    			super();
    		}
     
    		public ProductionWorker(String Name)
    		{
     
    			this.hoursWorked = hoursWorked;
    		}
     
     
    		public void sethoursWorked(double hourWorked)
    		{
    			this.hoursWorked = hoursWorked;
    		}
     
    		public double gethoursWorked()
    		{
    			return hoursWorked;
    		}
     
    		public void setUnitsProduced(int unitsProduced)
    		{
    			this.unitsProduced = unitsProduced;
    		}
     
    		public double getUnitsProduced()
    		{
    			return unitsProduced;
    		}
     
    		public void totalPay()
    		{
    			setPay(hoursWorked * rate);
    		}
     
    		public double gettotalPay()
    		{
    			return  totalPay;
    		}
     
     
    		public void display()
    		{
    			super.display();
    			System.out.println("The total income of the day: " +gettotalPay());
     
    		}
     
    		}

    //---------------------Ordinary production---------------------------------
     
     
    public abstract class OrdinaryProduction extends ProductionWorker
    {
     
    	private double productivity;
    	private double wages;
     
     
    	public OrdinaryProduction()
       	{
    		super();
        }
     
     
     
     
    	//////////////////////////////////////////////////////////////////
    	// Modifier methods - used to set values for instance variables
     
    	public void setUnitsProduced(int UP)
    	{
    	     unitsProduced = UP;
    	}
     
    	public double getUnitsProduced()
    	{
    		return unitsProduced;
    	}
     
     
    	public void setProductivity(int Productivity)
    	{
    		     productivity = Productivity;
    	}
     
    	public double getProductivity()
    	{
    		return productivity;
    	}
     
     
     
     
    	/////////////////////////////////////////////////////////////////////
    	// Accessor methods - used to retrieve values for instance variables
     
     
     
    	public void calcWages() // Sub-class implements abstract method from the ProductionWorker class
    	{
    		setWages(hoursWorked * 12);
    	}
     
    	public double getWages()
    	{
    		return wages;
    	}
     
     
    	////////////////////////////////////////////////////////////////
        // Method to determine the productivity - this is called a helper method
        // A helper method is one that is invoked within a class but is
        // not visible outside of the class
     
     
     
        private void calculateProductivity()
        {
            if( unitsProduced >=4 )
                productivity = 'A';
            else if( unitsProduced >= 3)
                productivity = 'B';
            else if( unitsProduced >= 2)
                productivity = 'C';
            else if( unitsProduced >= 1)
                productivity = 'D';
            else
                productivity = 'E';
        }
    	////////////////////////////////////////////////////////////////
    		// Method to display information
     
    	public void display()
    	{
     
    		System.out.println("Weekly wages :" + wages);
    		System.out.println("Weekly wages :" + productivity);
     
    	}
     
     
     
    }

    //----------------------Team lead----------------------
     
    public class TeamLead extends OrdinaryProduction
    {
     
    	private int unitsProduced;
    	private int productivity;
    	private int workers;
    	private double wages;
    	private OrdinaryProduction[amount];
     
    	public TeamLead()
    	{}
     
     
    	///////////////////////////////////////
    	//Set array to read in production workers
     
     
    	setOpwArray(OrdinaryProduction[] op){
     
    		//OrdinaryProductionWorkerArray = opwArray;
    }
     
     
    	public int getOpwArray(OrdinaryProduction[] op)
    	{
    		return op;
    	}
     
     
    	//////////////////////////////////////////////////////////////////
    	// Modifier methods - used to set values for instance variables
    	public void sethoursWorked(int inHoursWorked)
    	{
    		hoursWorked = inHoursWorked;
    	}
     
    	public void setWages(double inWages)
    	{
    		wages = inWages;
    	}
     
    	public void setWorkers(int inWorkers)
    	{
    		workers = inWorkers;
    	}
     
    	public void setWages(int inWages)
    	{
    		wages = inWages;
    	}
     
    	/////////////////////////////////////////////////////////////////////
    	// Accessor methods - used to retrieve values for instance variables
     
     
    	public int getHoursWorked()
    	{
    		return hoursWorked;
    	}
     
    	public int getWorkers()
    	{
    		return workers;
    	}
     
    	public int getProductivity()
    		{
    			return productivity;
    	}
     
    	public void calcBonus()
    	{
     
    		if (productivity = A)
    			bonus = 700;
    		else if( productivity = B)
    			bonus = 500;
    		else if( unitsProduced = C)
    			bonus = 200;
    		else if( unitsProduced = D)
    			bonus = 100;
    		else
    			bonus = 0;
     
    }
     
     
    	public void calcWages() // Sub-class implements abstract method from the Employee class
    	{
    		setWages(hoursWorked * 13.80);
    	}
    }

    //----------------------------Employee info-------------------------
     
    class EmployeeInformation
    	{
      public static void main (String [] args ){
     
    	Scanner input = new Scanner(System.in);
     
    	TeamLead TL1 = new TeamLead();
    	System.out.print("How many Production workers working for TL1 ?");
    	int amount = input.nextInt();
     
    	OrdinaryProduction[] opwArray = new OrdinaryProduction[amount];
    	for (int i = 0; i < opwArray.length; i++){
     
     
    		opwArray[i] = new OrdinaryProduction();
    		System.out.print("Enter name for production worker?");
    		opwArray[i].setName(input.nextLine());
    		System.out.print("Enter Start date for production worker?");
    		opwArray[i].setStartDate(input.nextInt());
    		System.out.print("Enter hours worked for production worker?");
    		opwArray[i].sethoursWorked(input.nextInt());
    		System.out.print("Enter units produced for production worker?");
    		opwArray[i].setunitsProduced(input.nextInt());
    		System.out.print("Enter productivity for production worker?");
    		opwArray[i].setproductivity(input.nextInt());
     
    	}
    }
     
    }
    Last edited by JavaPF; December 9th, 2010 at 08:57 AM. Reason: Please use [highlight=Java] code [/highlight] tags!!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Changing Array variables from different classes

    ...what's your question?

    When posting code, it should be in the form of an SSCCE (that's a link, but in other words, as short as possible). Don't forget the code tags.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Changing Array variables from different classes

    Moved to Collections and Generics - Java Programming Forums

    As Kevin says, what is the problem exactly? Please tell us where you are stuck.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Changing Array variables from different classes

    Quote Originally Posted by KevinWorkman View Post
    ...what's your question?

    When posting code, it should be in the form of an SSCCE (that's a link, but in other words, as short as possible). Don't forget the code tags.
    Oh ok, my question is what is the problem with my array code? I am missing something somewhere to read in production workers but i dont know what it is. I keep getting errors.

    tnx

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Changing Array variables from different classes

    Quote Originally Posted by smellyhole85 View Post
    Oh ok, my question is what is the problem with my array code? I am missing something somewhere to read in production workers but i dont know what it is. I keep getting errors.

    tnx
    What errors? What do you expect your code to do? What does it do instead? Be specific. Post an SSCCE that demonstrates the problems, and make sure to point out any line numbers referenced in the Exception's stack trace so that we don't have to count.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Changing Array variables from different classes

    private OrdinaryProduction[amount];

    You shouldn't have the size of the array in your declarations before the constructor.

    1.) It's not initialized yet
    2.) Even if it were, it would refer to the value at index amount, not the whole array

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Changing Array variables from different classes

    private OrdinaryProduction[] op;

    public OrdinaryProduction[] get OPArray(int amount)
    {
    op = new OrdinaryProduction[amount];
    // it's still empty at this point you know but is of size amount
    return op;
    }

Similar Threads

  1. using variables from other classes in arrays :/
    By mozyman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 11:26 PM
  2. [SOLVED] Error: Null Exception on Array of classes
    By g000we in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 09:14 AM
  3. State Variables interaction with outside classes?
    By Ace Coder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2010, 03:52 PM
  4. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM