Hi, I am a intro to java student and need a little bit of help starting on this new program that I have been assigned to write. Don't worry, I'm not just seeking for someone to do it for me, I just need some guidance as I proceed through it, so if anyone could please help me in the process that would be great! The reason this program is difficult for me is because it involves methods and arrays, the two fields that I'm not so well in, so some assistances would be great. Thank you in advance :)


Your program is to prompt the user to enter data for 10 employees. For each employee, the user will enter their last name (one word only), what time they started work today (first ask for the hour, then the minute), and what time they finished work today (first ask for the hour, then the minute). For example, if the person started work at 2:20PM, the user would enter 14 for the hour and 20 for the minute. Note that all times will be given as military time. You may assume that all data will be correctly entered (no validation needed), that the employee finished work after they started, and that both times entered are for the same day.

Your program is to determine how many hours each employee worked that day. It will output a nicely formatted report listing all of the employees along with the amount of time they worked (listed as hours & minutes). Also the program should output the average amount of time worked. Be sure to use good programming style. Modularization & arrays are required.


I have been taught to write the pseudocode first and then convert it into Java, but right now I'm not able to start with either. Here are the possible methods i have come up with so far:

  • read employee data
  • sum hours
  • calculate avg time
  • calculate hrs worked
  • write employee information

are those right? are there more? I really just need help laying all this out (planning) and getting started, after that it's not so bad. So, if someone could please please share some of their java skills to help me, would be appreciated so so very much, and again I'm willing to work with you as a team (promise!)! :)

By the way I only have a week :( so please please reply bck soon!:S

Hints:

Use a static variable to define the number of employees. Then create an array based on that. Example

private static int NR_EMPLOYEES = 10;
Employee[] employees = new Employee[NR_EMPLOYEES];

Obviously this Employee[] means that you will need a class called Employee that holds all of the Employee's information. So the first thing you should do is to create the Employee class, put in all of the variables you need, then provide get() and set() methods for each of those variables.

After you do the above, you can then make methods like calculateHoursWorked, etc. Your also going to need another class, the main class, where you prompt for employees and such. So in summary:

public class Employee{
//Your variables go here - stuff like name, hour arrived at work, 
//hour the employee left work, etc.

..


//Your methods such as get and set methods and other methods
//such as calculateHoursWorked() go here
}

Then your main driver class..

public class EmployeeDriver{
//Array of employees goes here, NR_EMPLOYEES goes here.

public static void main(String[] args){

for (int i =0 ; i < NR_EMPLOYEES; i++){
//call your prompt for employee method.
}

}

public void promptForEmployee(){
//This method should prompt the user for ONE employee
//then create a new Employee Object and put it in the array.
}
}

That should get you started. If you don't understand something ask but don't be surprised if I just give you a link to some resource or another. This is, after all, your assignment.

Sorry, I got caught up in exams and other assignments :( But, wow, thank you so much for taking the time out to work all that out for me. But, I'm not that far yet, actually I am required to write the pseudocode for this first. I'm a bit lost on it, here's what I have so far for the MAIN module (this is just a rough basic idea of it,, wanted to make sure it was right before i go ahead and write it):

Start
sum <-- 0
I <-- 0
DOWHILE I < 10
Write prompt message
Read data (module)
Calculate hours (module)
Write results (module)
I <-- I + 1
ENDDO
calculate average (module)
Write average (module)
Stop


is this the right idea?
thank you again!

ok i actually skipped writing the pseudocode for now, but can you please tell me how many arrays i'm going to have? is there a seperate array for each of the following:

array 1:employee last name,
array 2: time started
array 3: time finished

and would it work if i have a method for the following functions:
calculate hours worked
read employee data
calculate average
print employee data

array 1:employee last name,
array 2: time started
array 3: time finished

considering your assignment: you need 1 array, containing your employee objects. last name, time started and time finished, should be stored within that object

we haven't learned "objects" yet, therefore we can't use that in our program!

i'm slowly figured it all out, now i'm a little stuck on the part of how to calculate the amount of time worked (military time), any clues/hints/suggestions on how to come about that?

For amount of time worked, just subtract end hour - start hour and end minute - start minute. You'll need an extra two arrays to record the results. You'll also need to account for the possibility that end minute is < start minute.

Also the program should output the average amount of time worked. Be sure to use good programming style. Modularization & arrays are required.

Someone should tell your teacher that "good programming style" is impossible without using Objects for this problem. Using Objects for this problem is essential to code readability, maintainability, ease of programming, and modularity. I actually think it's funny that your teacher used the word modularity but apparently he/she doesn't know what it means.

For example: Resources about modularity in Object Oriented programming:
http://java.sun.com/docs/books/tutorial/java/concepts/object.html

What is the deadline for the assignment?

oh i'm sorry it pasted and I was able to get it solved in time I will post the result on here very soon. sorry!

Sorry I forgot to post the finish program for this problem, here is what I had submitted for this assignment in case you guys were wondering:

/*This program prompts the user to enter data for 10 employees; their last name,time they started
working, and time they finished working.It then calculates and ouputs the total amount of time each
employee worked and the average time.Written by*/

import java.util.Scanner;
public class EmployeeTime
{
	public static void main (String [] args)
	{
		// create arrays for the employees' names, hours & minutes the employee started & finished work

		String [] employeeNames = new String [10];
		int [] hourStart = new int [10];
		int [] minuteStart = new int [10];
		int [] hourFinish = new int [10];
		int [] minuteFinish = new int [10];

		//invoke the read method to read the employee data

		readEmployee(employeeNames, hourStart, minuteStart, hourFinish, minuteFinish);

		//create arrays, and invoke the method to calculate the total hours and minutes

		int[] totalHours = new int [10];
		calculateHours (hourStart, hourFinish, totalHours);

		int[] totalMinutes = new int [10];
		calculateMinutes (minuteStart, minuteFinish, totalMinutes);

		//calculate the average time worked

		double average = calcAverage (totalMinutes, totalHours);

		//display the last name and total time worked for each employee, followed by the average

		printResults(employeeNames,totalHours, totalMinutes, average);

	}

	public static void readEmployee(String[] empNames, int[] hrStart, int[] minStart, int[] hrFinish, int[] minFinish)
	{
		System.out.println("Please enter the employee's last name(without spaces), the hour & minute they\n" +
							"started work, and the hour & minute they finished work. Keep in mind the hours\n" +
							"must be 01-24 inclusive and the minutes 00-59, all based on military time.\n[e.g."+
							"2:00am --> 02:00 and 2:30pm --> 14:30].\n\nExample:\n Last Name: \t Petersen \n Start" +
							"hour: \t 05\n Start minute:\t 30\n Finish hour:\t 22\n Finish minute:  00\n");
		System.out.println("--------------------------------------------------------------------------------");

		Scanner input = new Scanner(System.in);
		for (int i = 0; i < hrStart.length; i++)
		{

			System.out.print("Last name: \t");
			empNames[i] = input.next();

			System.out.print("Start hour: \t");
			hrStart[i] = input.nextInt();

			System.out.print("Start minute: \t");
			minStart[i] = input.nextInt();

			System.out.print("Finish hour: \t");
			hrFinish[i] = input.nextInt();

			System.out.print("Finish minute:  ");
			minFinish[i] = input.nextInt();

			System.out.print("\n");
		}
	}

	public static int[] calculateHours (int[] hrStart, int[] hrFinish, int[] totalHrs)
	{
		for (int i = 0; i < hrStart.length; i++)
			totalHrs[i] = hrFinish [i] - hrStart [i];
		return totalHrs;

	}

	public static int[] calculateMinutes (int[] minStart, int[] minFinish, int[] totalMins)
	{
		for (int i=0; i < minStart.length; i++)
		{
			totalMins[i] = minFinish [i] - minStart [i];
			if (totalMins[i] < 0)
				totalMins[i] = totalMins[i] * -1;
		}
		return totalMins;
	}

	public static double calcAverage (int[] totalHrs, int[] totalMins)
	{
		int sum1 = 0;
		int sum2 = 0;
		for (int i = 0; i < totalHrs.length; i++)
		{
			sum1 += totalHrs[i];
			sum2 += totalMins[i];
		}
		double avg = ((double) sum1/(double) totalHrs.length) + ((double) sum2/(double) totalMins.length);
		return avg;
	}

	public static void printResults(String[] empNames, int[] totalHrs, int[] totalMins, double avg)
	{
		System.out.printf("%2s%30s\n" , "Employee Names", "Total Time Worked");
		for (int s = 0; s < empNames.length; s++)
			System.out.printf("%2s%15d%2s%15d%2s\n",empNames[s], totalHrs[s], "hrs", totalMins[s],"mins");
		System.out.print("\n The average amount of time worked is: " + avg + "hrs.\n");

	}
}

Thank you for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.