1 Replies - 1081 Views - Last Post: 27 August 2012 - 05:43 PM Rate Topic: -----

#1 klin1344  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-August 12

Object oriented programming assignment help?

Posted 27 August 2012 - 05:01 PM

Hi. I am a very beginner at Java. I am taking an online course on Java and I'm stuck on an assignment. Anyone who is good with Java, can you please take a look at my code? This assignment was due yesterday but I couldn't finish it.
It is very unfinished, but I'm not sure how to continue because I'm still not familiar with the concept of object oriented programming (getters, setters, constructors, etc).
Could anyone please explain more object oriented programming to me? Also what do getters and setters do? And can you give me an example of using a constructor in the employee class? (I think that's what I'm supposed to do)
The assignment instructions are in the comments.

Sorry but the code is VERY incomplete right now. It won't even compile. I am so lost that I have no clue what I am doing anymore. Maybe I should have taken an actual class with a real teacher that could explain it...


Thank you so much. I hope somebody can help me, as I feel extremely hopeless after having spent the past 2 weeks wracking my brain on this simple assignment.

import java.util.Scanner;

/*     Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year.
Use an Employee class, a Name class, an Address class, and a Date class in your solution. Provide appropriate class constructors, getter methods, setter methods, and any other methods you think are necessary.
Your program should prompt the user to enter data for several employees and then display that data. The number of employees to store data for shall be entered from the command line.

(This is the most difficult assignment of the class. I have had only one student correctly implement it on their first try. Do not feel bad if you take multiple attempts.)

Length: No overall restriction. There should be a class and constructor for each of Employee, Name, Address, and Date. Each of those should have a constructor that initializes every instance variable within the class.

"Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year."
This translates, for the Employee class, into

Employee
{
private int number;
private Name name;
private Address address;
private Date hireDate;
}

You will need to have appropriate get/set methods in the Employee class for the data.
You must model the Employee as it is an integral part of the assignment and Object Oriented design.     */


public class project10
{
	public static void main( String[] args )
	{
		Scanner input = new Scanner( System.in );

		int numEmployees;
		System.out.println( "How many employees do you wish to enter?" );

		numEmployees = input.nextInt();
		
		Employee[] employeeArray = new Employee[numEmployees]; 
		
		for ( int i = 0; i < numEmployees; i++ ) 
		{
			Employee e1 = new Employee();	//???
			
			Name first = new Name();
			Name last = new Name();
			System.out.println( "Enter the first name of the employee" );
			e1.setFirstName( input.nextLine() );
		
			System.out.println( "Enter the last name of the employee" );
			e1.setLastName( input.nextLine() );
		}
	}
}

class Employee		
{
	private int number;
	private Name FirstName;
	private Name LastName;
	private Address address;
	private Date hireDate;
	
}

class Name
{
	private String FirstName; 
	private String LastName;

	public Name() 
	{
		FirstName = ""; 
		LastName = "";
	}
	public void setFirstName(String firstName) 
	{
		FirstName = firstName;
	}
	public void setLastName(String lastName)
	{
		LastName = lastName;
	}
	public String getFirstName() 
	{
		return FirstName;
	}
	public String getLastName()
	{
		return LastName;
	}
}

class Address
{
}

class Date
{
	private int month; 
	private int day;
	private int year;

	public Date() 
	{
		month = 0; 
		year = 0;
		day = 0;
	}
	public void setDay( int dayOfMonth ) 
	{
		day = dayOfMonth;
	}
	public void setMonth( int monthOfYear )
	{
		month = monthOfYear;
	}
	public void setYear( int whichYear )
	{
		year = whichYear;
	}
	public int getDay() 
	{
		return day;
	}
	public int getMonth() 
	{
		return month;
	}
	public int getYear() 
	{
		return year;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Object oriented programming assignment help?

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: Object oriented programming assignment help?

Posted 27 August 2012 - 05:43 PM

First of all, it is normally good practice to make a new file for each one of your classes. What getters and setters due is give access to a private variable. They are normally used to prevent access to a variable (i.e make a variable read-only). Constructors are just a special type of method to initialize the variable. They are called whenever you do something like new Object(); To make a constructor, you just leave out the return type (also constructors can never be static due to the way that they are called). Also, as a side note, it is good practice to use BufferedReader instead of Scanner do to the speed differences.
EDIT: Just realized that your code had a Name class that contains both a first name and a last name. You have 2 instances of names per employee. So either use strings for the name, or fix the name class. Also, it is a bad idea to name your class Date, due to that being the name for a java class that you are probably going to use to set that class anyways.

This post has been edited by natecat: 27 August 2012 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1