Results 1 to 4 of 4
  1. #1
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Arrays of Objects

    The program is supposed to read an input file using arrays. Please help. The professor gave us a seletal code to use as a blueprint for the program. This is as far as I've gotten. I haven't even begun the writeOut() method yet. Any and all help will be appreciated. Attaching the input file.

    Java Code:
    /******************** Payroll.java *********************
    /*
    /* 
    /* 18 March 2013
    /*
    /* This program uses the EmployeePay class to create and 
    /* array of objects.  Each element of the array represents
    /* an employees name, hours and rate.
    /*
    /* The operator is asked for the name of the output file, 
    /* the hard coded input file name is checked for its existence,
    /* if it is, it is opened along with the output file else the 
    /* program ends.
    /*
    /* Each record of the input file, consisting of name, hours and 
    /* rate if read and written into the array. A count is kept of 
    /* the number of records read from the file.
    /* The array is sorted in ascending order by name then each
    /* record is read out and displayed under column headings on the
    /* System Console along with the pay at the same time the same
    /* record is written to the output file.
    /*
    /* As each record is read from the array the total hours and
    /* total pay for all employees is accumulated.
    /*
    /* After the array has been read the total hours and total pay
    /* for all employees is displayed on the System Console followed
    /* by the average pay (totalPay / totalHours).
    /*******************************************************/
    
    import java.io.*;
    import java.util.*;
    
    
    
    
    
    
    
    
    
    
    public class Payroll
    {
    
    /**************************************************************
    /* main(String[])
    /*
    /* Called by the System upon loading into memory, sets up
    /* heading and total line labels.
    /*
    /* Calls the openFiles method.
    /* Creates a new array 20 elements of objects of the EmployeePay
    /* class.
    /* Calls the readInputData method
    /* Calls the printHeading method passing the column heading
    /* objects NAME, HOURS, RATE, TOTAL
    /* 
    /* Calls sortArray method
    /* Calls writeOutData method
    /*
    /* Displays the totalHours and totalPay for all employees on the 
    /* System Console formatted using printf
    /* Calculates average pay (totalPay / totalHours) and display on 
    /* the System Console using printf.
    /*
    /* Close the input file, and output file buffers.
    /*************************************************************/
       
      public static void main(String[] args)throws IOException
      {
        int hours;
    	 int totalHours;
    	 int totalCount;
    	 double rate;
    	 double pay;
    	 double totalPay;
    	 String name;
    
        Scanner kb = new Scanner(System.in);
    	 Scanner dataIn;
    	 
    	 openFiles();
    	 
        EmployeePay[] empArray = new EmployeePay[20];
    	 
    	 readInputData();
      }
    
    /*************************************************************
    /* writeOutData()
    /*
    /* A for loop is used to read each element position of the
    /* empArray.  
    /* The recordCount variable contains the number of records in
    /* the array.
    /*
    /* Each time through the loop, the employee name, hours and rate
    /* is read from that element of the array. The calculatePay
    /* method is called passing arguments which returns the employee
    /* pay.
    /*
    /* The employee hours and pay is added to 
    /* the totalHours and totalPay accumulators for all employees.
    /*
    /* The employee name, hours, rate, and pay is displayed on the 
    /* System Console and written to the output file being formatted
    /* with the printf.    
    /*************************************************************/
       
       public static void writeOutData()
       {
    
       }
    /**************************************************************
    /* sortArray()
    /*
    /* The empArray is sorted in ascending alphabetical order based
    /* on the employees name. 
    /* The "Bubble sort" technique is used with 2 nested for loops.
    /* The recordCount value is the number of records in the array.   
    /************************************************************/
       
       public static void sortArray()
       {
     
       }
    
    
    
    
    
    /*************************************************************
    /* readInputData()
    /*
    /* This method reads the employee record (name, hours, rate)
    /* from the input file, creates a new object in the next element /* of an array of EmployeePay object passing the name, hours and /* rate to the EmployeePay constructor.
    /*  
    /* 
    /* A loop is setup to read each record and a index pointer is
    /* incremented
    /* each time through the loop to point to the next element of
    /* the array.
    /*
    /* After all records are read the index pointer value is
    /* assigned to a 
    /* record count variable.  This value represents the number of
    /* records read and will be used in any processing the array.
    /************************************************************/
       public static void readInputData()
       {
    	  Scanner kb = new Scanner(System.in);
    	  EmployeePay[] empArray = new EmployeePay[20];
         
    	  String name = "";
    	  int hours;
    	  int recordCount = 0;
    	  int index;
    	  double rate;
    	  
    	  hours = empArray[index].getHours();
         rate = empArray[index].getRate();
    
    	  for(index = 0; index < empArray.length; index++)
    	  {
    	   
    	     empArray[index] = new EmployeePay(name, hours, rate);
    	  	  System.out.println(empArray[index]);
    	  }
    	  
    	  recordCount += index;
    
       }
    /********************************************************
    /* openFiles() throws IOException
    /*
    /* Create a keyboard object of the Scanner.
    /* Creates the File object from the hard coded name of the input
    /* file.
    /* If the file exists create a Scanner object for the input
    /* file.
    /* Creates a new keyboard object and asks the operator for the
    /* name of the output file and create a PrintWriter object using
    /* the name entered by the operator.
    /*
    /* Else if the input file name doesn't exist then notifiy the
    /* operator and exit the program  
    /*
    /* Close the keyboard buffer
    /*******************************************************/
       public static void openFiles() throws IOException
       {
    	  Scanner keyboard = new Scanner(System.in);
    	  File file = new File("EmployeeData.txt");
    	  if(file.exists())
    	  {
    	    Scanner input = new Scanner(file);
    		 Scanner kb = new Scanner(System.in);
    		 System.out.println("Enter name of output file: ");
    		 String fileName = kb.nextLine();
    		 PrintWriter pw = new PrintWriter(fileName);
    	  }
    	  else if(!file.exists())
    	  {
    	    System.out.println("Input file does not exist.");
    		 System.exit(0);
    	  }
    
       }
    
    /******************************************************
    /* calculatePay(int, double)
    /*
    /* Is passed the hours and rate for an employee and returns 
    /* the employees pay 
    /*************************************************************/
       public static double calculatePay(int h, double r)
       {
          
       }
    /******************************************************
    /* printHeading(String, String, String, String)
    /*
    /* Is passed the column headings as strings 
    /* and displays on the System Console  
    /*********************************************************/
      public static void printHeading(String n, String h, String r,
         String t)
      {
          
      } 
    } // end of class Payroll
    
    /************************ EmployeePay ***********************
    /*
    /* Class that contains employee name, hours and rate class 
    /* variables.
    /* Includes methods to return name, hours and rate.
    /* The constructor accepts name, hours and rate.
    /************************************************************/
    class EmployeePay
    {
        private String name;
        private int hours;
        private double rate;
    
       /* constructor accepts name, hours and rate and initializes
         class variables
      */
       EmployeePay ( String n, int h, double r)
      {
        name = n;
    	 hours = h;
    	 rate = r;
      }
       /* getName() method returns employee name  */
       public String getName()
       {
    	  name = n;
    	  hours = h;
    	  rate = r; 
       } 
       
    /* getHours() method returns employees hours  */
       public int getHours()
       {
    	  return hours;  
       } 
      /* getRate() method returns employees pay rate  */
       public double getRate()
       {
         return rate;
       } 
    } // end of class EmployeePay
    Attached Files Attached Files
    Last edited by pbrockway2; 04-02-2013 at 01:02 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Arrays of Objects

    I've added code tags to your post. Basically you put [code] at the start of the code and [/code] at the end. That way the code will be properly formatted and readable.

    ---

    Do you have a question? Basically the sorts of things people would like to know in order to respond are: does the code compile? if not, what are the compiler messages? if the code compiles does it do at runtime what you expect? if not, what do you expect? and what does it do?

    Since you haven't completed the assignment yet, we can't figure out what you expect to happen at the moment from the instructor's comments about what the program will do when it is finished.

  3. #3
    rgru25 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: Arrays of Objects

    Ok, thank you for clearing that up for me. I was having a problem initiating the int hours and double rate, in the readInputData() method. I'm new to this, so they way he worded it kind of confused me.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Arrays of Objects

    Hi rgru25.

    Your openFiles() and readInputData() methods would probably be better as a single method. This method would check the file exists, open it, read in the data to a global EmployeePay array and then close the file.
    You would then be able to reference the array throughout the class.

    Let us know if you require further assistance or if you have been instructed to do it another way.

    Regards.

Similar Threads

  1. Arrays for objects
    By mjfitches in forum New To Java
    Replies: 1
    Last Post: 02-23-2013, 10:20 PM
  2. arrays and/ are objects
    By willemjav in forum New To Java
    Replies: 2
    Last Post: 01-02-2013, 04:20 PM
  3. Arrays of Objects
    By superscout in forum New To Java
    Replies: 2
    Last Post: 01-15-2012, 02:16 PM
  4. How to use class objects in arrays
    By dironic88 in forum Eclipse
    Replies: 6
    Last Post: 04-06-2011, 01:34 PM
  5. Objects and Arrays
    By bannow in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 07:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •