Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble Implementing OOP Concepts  RSS feed

 
Jaime Alnwick
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to programming and am having issues implementing OOP concepts in my model employee program. The program is working but not meeting all the program requirements. I was wondering if anyone might be able to help me revise my code to meet the below requirements:

- Provide appropriate class constructors, getter methods, setter methods, and any other methods necessary.
- Your classes don’t need to do any editing of data. The classes should not do any direct user input/output. All user input/output will be done in the main method.
- Program should prompt the user to enter data for several employees, store the data in an array of type Employee, and then display the data.

I am having trouble with constructors, utilization of the Employee class (I can't seem to get data to display from the Employee class), and entering data into an array of type Employee. Any help you're able to provide would be greatly appreciated. I definitely feel in over my head at this point.

 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor means you can build a class without having to call all the set methods on it.

eg


Could be made with:


If a Point always has x,y co-ordinates then using the constructor to specify them when you create the class produces shorter/neater code, and just 'make sense'.
Can you apply that to your classes?  For instance the Date class?
Would be awesome to be able to go new Date(year, month, day);


 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding object oriented programming, I would recommend you implement the following for all your current classes:

public String toString()

It is very useful for debugging to be able to go
System.out.println(name) as opposed to System.out.println(name.getLast());

For instance the Name class might have:


 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!  
  • First of all don't write too much code in main method. You can write a separate method to get input from user and also for output. Name them getUserInput() and printEmpInfo() or whichever you feel more expressive.
  • Declare all fields of Employee, Date, Name and Address class as private to make them more secure. So only be accessed by getter and setter methods.
  • Provide validation while taking input from user:
    • What if user enters String when asked for Enter number of employees? or intentionally enters values more than range of int x
    • Also provide validation for length of first name, last name and address etc.
  • I don't feel there is need to create separate classes for joining date and name of an employee. You can declare field like empFirstName, empLastName in Employee class. Same for date, It would be much better if you use Date-Time APIs later if you need to sort employee information by joining date then you can use comparator to sort them using these dates
  •  
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jaime Alnwick wrote: entering data into an array of type Employee.
    Yes you can create an array of type Employee in your input method of EmployeeModel class like this Or If you're acquainted with ArrayList then use it to add objects of Employee.
    After entering x number of employee's valid(for validations of name, date you can create separate methods which will return true if valid input else false) information you can assign it's reference to field of EmployeeModel class i.e. private Employee myEmployeeList[]; Use this instance variable of EmployeeModel class to print information of employees in printEmpInfo() method.
     
    Jaime Alnwick
    Greenhorn
    Posts: 16
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Stefan Evans! The tips on constructors were very helpful. I read about them but for some reason could not get how to use them until your example. The toString recommendation was also extremely helpful. I was able to implement both in my code and it cleaned up the main method a lot.

    Thank you Ganish Patil as well! All your recommendations are good ones. I haven't had the chance to work on all of them yet, but I definitely plan to (except for a few and that is only because I have to follow the requirements I was given and not necessarily go with best practises).

    Still have to work out how to enter data into an array. Might have to tackle that one tomorrow because I am still not quite getting it. We have not learned about ArrayLists yet.

    Thanks again for the help! Don't know what I would have done w/o it. :-)


     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • Yes it will take little more time to get hands on it but good for beginners to read this Java Programming Style
    Jaime Alnwick wrote:Still have to work out how to enter data into an array


  • Just add(Please read comments)

  • Start loop with id = 0 because Arrays index always starts with 0 so it is good practice if you start loop with id = 0,
    will be much easier to track the errors or output etc. and make changes like below

  • After that for printing information

  • Did you notice? If you create an object of Employee with default constructor all reference type instance variables i.e. Name, Address and Date
    are assigned with null and primitive types i.e. id , day, month and year with their respective default values.
  • Suppose you create an object like this
    rather than this in your code one line number 39
    Output:
    Employee number: 0
    Name: null, null
    Address: null, null, null null
    Date: 0/0/0

    Note: I'm not suppose to give you ready made code(This is first and last time) but since you are not getting just by explanation, I thought to make couple of changes in your code and show you so you can make further implementation as mentioned before this post. Hope you understand, If still has doubts please don't hesitate to ask   
     
    Liutauras Vilda
    Marshal
    Posts: 4650
    318
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Also keep your instance variables private and give access to them only through public getter methods.
     
    Stefan Evans
    Bartender
    Posts: 1836
    10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I must say the code looks much nicer with just those one or two little changes.
    Ganish has already given you hints about using arrays which should help you along.

    In terms of other general feedback:
    - Liutauras pointed out your class variables should be "private" by convention.  It's a good habit to get into.  You're already accessing them properly through getter/setter methods so you may as well just lock them down.  It'll probably be worth bonus points in your assignment :-)
    - Personally I don't like the one letter variable names.  I know they're just temporary, but they do make it harder to read the code. 
    - I prefer declaring an array variable as:  "Employee[] employees"  rather than "Employee employees[]"  (slightly different placement of the square brackets).  But they are equivalent ;-)
     
    Jaime Alnwick
    Greenhorn
    Posts: 16
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish, Thank you for taking the time to explain how to enter the employee information into an array. I was really struggling with meeting that requirement. Also, thanks for linking the Java Programming Style guide.

    Liutauras and Stefan, Thanks for the recommendation to keep the variables private. I implemented that change and updated the one-letter variables to more meaningful names.

    Thanks again for all the pointers!
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are most welcome
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!