Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Something i'm extremely confused on

Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Okay this is the very last question on my homework.

The others are finished with the tips and help on my last post on increasing array size...

Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee's seven-day works hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.
Su Mon Tue Wed Thu Fri Sa
Employee 0 2 4 3 4 5 8 8
Employee 1 7 3 4 3 3 4 4
Employee 2 3 3 4 3 3 2 2
Employee 3 9 3 4 7 3 4 1
Employee 4 3 5 4 3 6 3 8
Employee 5 3 4 4 6 3 4 4
Employee 6 3 7 4 8 3 8 4
Employee 7 6 3 5 9 2 7 9

I really don't know how to start this...
Should i put the hours of each employee in an array and sum it up?
And then store al the sum'd up numbers in an another array and sort it out in decreasing order?

If i manage to do that? How would i get the number like employee "5" to be at specific locations?

I'm baffled right now...

This is waht i have so far...

Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
Firstly, do you know what a two dimensional array is, effectively? It's a one dimensional array, filled with one dimensional arrays.

So --

Each row represents an employee, and each column represents their weekly hours, eh?

Employee 0 has the hours 2, 4, 3, 4, 5, 8, 8

Employee 1 has the hours 7, 3, 4, 3, 3, 4, 4

and so on....

So you're going to build an array with 8 slots (0 through 7), then populate each slot with an array of 7 (0 through 6) ints. You started off well, but the first entry in each row has to go -- it's the employee number.

Does that help?
Janeice

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Am i on the right track so far?

import java.util.*;
public class Exercise623{

public static void main(String[] args) {

int[] Employee0 = {2, 4, 3, 4, 5, 8, 8};
int[] Employee1 = {7, 3, 4, 3, 3, 4, 4};
int[] Employee2 = {3, 3, 4, 3, 3, 2, 2};
int[] Employee3 = {9, 3, 4, 7, 3, 4, 1};
int[] Employee4 = {3, 5, 4, 3, 6, 3, 8};
int[] Employee5 = {3, 4, 4, 6, 3, 4, 4};
int[] Employee6 = {3, 7, 4, 8, 3, 8, 4};
int[] Employee7 = {6, 3, 5, 9, 2, 7, 9};

int[] UnsortedHours = {SumOfHours0, SumOfHours1, SumOfHours2, SumOfHours3, SumOfHours4, SumOfHours5, SumOfHours6, SumOfHours7};
for (int i = 0; i < UnsortedHours.length; i++) {
System.out.print(UnsortedHours[i] + " ");
}
}

int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
}

}

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:

Allen Hsia wrote:Am i on the right track so far?

import java.util.*;
public class Exercise623{

public static void main(String[] args) {

int[] Employee0 = {2, 4, 3, 4, 5, 8, 8};
int[] Employee1 = {7, 3, 4, 3, 3, 4, 4};
int[] Employee2 = {3, 3, 4, 3, 3, 2, 2};
int[] Employee3 = {9, 3, 4, 7, 3, 4, 1};
int[] Employee4 = {3, 5, 4, 3, 6, 3, 8};
int[] Employee5 = {3, 4, 4, 6, 3, 4, 4};
int[] Employee6 = {3, 7, 4, 8, 3, 8, 4};
int[] Employee7 = {6, 3, 5, 9, 2, 7, 9};

int[] UnsortedHours = {SumOfHours0, SumOfHours1, SumOfHours2, SumOfHours3, SumOfHours4, SumOfHours5, SumOfHours6, SumOfHours7};
for (int i = 0; i < UnsortedHours.length; i++) {
System.out.print(UnsortedHours[i] + " ");
}
}

int total = 0;
for (int i = 0; i < array.length; i++) {
total += array[i];
}
}

}

Lol well i forgot to implement the sorting part...
But when i implement the sort, how do i put the employees next to the correct hours?

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
Use the code tag, not the quote tag for your code references.

I think you should go back to building the array the way you did in the first example....
In order to do anything to a 2d array, you typically need nested for loops.... I'm sure your text covers this.

To get the value for employee 0 on sunday, it's employeeHours[0][0].... employee 6 on wednesday? employeeHours[7][3].

Now think of the nested for loop.... iterate through x values and y values for the example: employeeHours[x][y].

I'm in the middle of a timed exam so I'll be back in a bit if you need help.
Janeice

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
Alright, so have you figured out the nested loop?

I'll give you some pseudo code now that my exam is over:

is this making sense?
Janeice

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:

This is what i have so far...? am i getting there?

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:

Allen Hsia wrote:

This is what i have so far...? am i getting there?

YES! first two sections done, right?

What EXACTLY does the assignment spec say about the sorting?

Janeice

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Basically we're supposed to have this as a result

Employee 7 : 41
Employee 6 : 37
Employee 0 : 34
Employee 4 : 32
Employee 3 : 31
Employee 1 : 28
Employee 5 : 28
Employee 2 : 20

We're supposed to sort the hours in descending order with the employees in the correct spot.

In the textbook I can only sort it in ascending order but it never mentions to sort it in descending order...
all we were given is this

java.util.Arrays.sort(totalHours); //totalHours being the array of the unsorted hours.

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
I'm sorry for being really noobish.

I'm having a lot of stress balancing my highschool senior year homework with my 1 sememster online Java class.
I've been working all day on Java...

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
OK. I don't know if this is right, but here goes.

so we need to sort and be able to find the old position...

I dunno if this is right or will work.... but here's a shot.

I have no idea if it will work, but using the old array and finding the old location could be a step in the right direction..........
Hopefully someone else is following this thread.

Janeice

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
I'm going to bed.... but do you see what we did? We went through step by step, broke the application down and (I hope) you compiled and ran after each addition.....

All you need to do to finish this is talk each step of what LOGICALLY needs to be done out, write it down, code, compile and test.

We got pretty far, but it's late here, and I have a long day ahead of me tomorrow. I think what we have is sloppy but functional so far, and I hope someone else comes along and helps you the rest of the way.

Janeice

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Thank you

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
I understand the general concept of coding.

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:

This is what i have so far. I'm getting tired. and on my compiler it says that

has an error called "incompatible type - found int[] but expect int." Both of them have a bracket so... i don't know what's wrong

Marshal
Posts: 79013
375
• Number of slices to send:
Optional 'thank-you' note:
You are passing "totalHours" in the []. You are only supposed to put an int inside the [], not an array.
Do you want totalHours.length (= creates an array of the same size)?
Or add the elements of totalHours together and pass the total (= array size of how many hours were worked)?
Or something different?

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
Campbell --

is there a better way of finding the old index of a number without making 3 arrays? He needs to sum up the total, sort the totals (descending or I suppose ascending and output via a backwards loop), and match up the totals with the original slots (the employee numbers).

My way seemed sloppy last night.... and I didn't know how to find the original index.....
Janeice

Campbell Ritchie
Marshal
Posts: 79013
375
• Number of slices to send:
Optional 'thank-you' note:
This looks vaguely familiar; I could have sworn I told somebody to create an Employee object which incorporates the hours worked array.

That is, of course dependent on people learning object-oriented programming rather than procedural programming

I have searched and found this thread from somebody who obviously has the same homework. Allen Hsia: please use thread titles which tell us what th thread is about. And don't do what I did when I was new, spending a whole hour trying to explain how to use sin and cos to draw a clockface before realising I would be 6 feet from the poster the following Thursday

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:

Campbell Ritchie wrote:This looks vaguely familiar; I could have sworn I told somebody to create an Employee object which incorporates the hours worked array.

That's a GREAT idea! I need to start thinking more OO.

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Okay Should i post on the other thread since someone else already is doing the same problem i'm doing?

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:

I can't really see the error in this line

After the first parenthesis i'm supposed to put the array i want to reverseOrder...
I did that. I copied the array from totalHours to sorted in order to reverse the order...

But it says cannot find symbol - method sort(int[], java.util.Comparator<java.lang.Object>)

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Yay! I got it to descend!

NOw i just don't know how to put the employees in the right spot.

Allen Hsia
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:

Janeice DelVecchio wrote:

Campbell Ritchie wrote:This looks vaguely familiar; I could have sworn I told somebody to create an Employee object which incorporates the hours worked array.

That's a GREAT idea! I need to start thinking more OO.

Um we haven't learned objects yet...

Janeice DelVecchio
Bartender
Posts: 1849
15
• Number of slices to send:
Optional 'thank-you' note:
Look at the API for Arrays -- they have a binarySearch that could be useful.... I haven't used it so I'm not sure.

If it searches the array for a value and returns the spot in the array that holds the value, that may be the answer. Just search the unsorted totals array for the value. The address is the same as the employee number :-)

Campbell Ritchie
Marshal
Posts: 79013
375
• Number of slices to send:
Optional 'thank-you' note:
You can't run a binary search on unsorted arrays . . . not if you want a correct answer, that is.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.