• June 6th, 2012, 05:30 AM
Yunus Einsteinium
:confused:

Stranded in this easy program. . . .

Q : Write a program that displays employ-ees and their total hours in decreasing order of the total hours

my working. . .
{I was not able to print the employee individual hours according to 'decreasing order' .How can i do that ?:confused:}
PHP Code:

``` import java.util.Scanner; public class EmployeeWeeklyHours {     public static void main(String[] args){                  System.out.println("Enter employee weekly hours: ");         int[][] employeeHours=employeeWeeklyHour();         int[] storeEmployeeTotalHours =employeeTotalHours(employeeHours);                  System.out.println("\t\tSun\tMon\tTue\tWed\tThur\tFri\tSat");         dispEmployeeWeeklyHoursFromHighest(employeeHours);     }                    public static int[][] employeeWeeklyHour(){         Scanner input = new Scanner(System.in);         int[][] employeeHours = new int[8][7];                  for(int i=0;i<employeeHours.length;i++)             for(int j=0;j<employeeHours[i].length;j++)                 employeeHours[i][j]=(int) (51 + Math.random()*50); //input.nextInt();                  return employeeHours;                  }          public static int[] employeeTotalHours(int[][] findTotalHours){         int[] storeEmployeeTotalHours = new int[findTotalHours.length];                  for(int i=0;i<findTotalHours.length;i++)             for(int j=0;j<findTotalHours[i].length;j++)                 storeEmployeeTotalHours[i] +=findTotalHours[i][j];                  return storeEmployeeTotalHours;     }          public static void dispEmployeeWeeklyHoursFromHighest(int[][] hoursPerWeek){         for(int i=0;i<hoursPerWeek.length;i++){             System.out.print("Employee "+i+" :\t");           for(int j=0;j<hoursPerWeek[i].length;j++){               System.out.print(hoursPerWeek[i][j]+"\t");           }           System.out.println();         }     } }  ```
• June 6th, 2012, 06:42 AM
Sean4u
The way most Java programmers would use to do this is to user java.util.Arrays.sort(T[], Comparator<T>) to sort the employee/hours-worked array. I think you could use it on an int[][], but it's going to be ugly. You could create a class "EmployeeTotalHours" which implements Comparator<EmployeeTotalHours> and either sort an array of such objects or use a sorted collection like TreeSet or TreeMap which automagically 'sorts' as you add objects.

If you want to stick with a pure array solution, another option would be to iterate over your array picking out the 'largest remaining' each time as many times as there are entries. You'd have to keep a couple of extra pieces of data: the 'last largest' (so on the current iteration you print out the next employee's total which is not greater than the 'last largest', and also a way of flagging employees as already-printed to allow for the situation when two employees have the same total hours (one will be printed on one iteration, the other on the next iteration).

Good luck!
• June 6th, 2012, 08:49 AM
Yunus Einsteinium
Hallo, the first paragraph i didn't understand anything maybe because i'm just novice. The idea of the array picking the largest remaining each time,and print out the corresponding employee hours struck my mind but i wasn't able to implement it...[please help me in implementation of this idea]
• June 6th, 2012, 10:26 AM
Norm
Quote:

picking the largest remaining each time,and print out the corresponding employee hours struck
Which part are you having problems with:
picking largest
print out corresponding
• June 7th, 2012, 08:59 AM
Yunus Einsteinium
sorry for the late reply,. . .picking largest each time is what i find technical. I was thinking of using for loops and the largest total in each loop i print the corresponding employee hours

• June 7th, 2012, 09:22 AM
Norm
Set a variable with a low value, in a loop compare that variable against what is in the array. When you find a larger one, put its value in the variable. At the end of the loop, the variable will have the value of the largest one.
• June 8th, 2012, 04:17 AM
azeemAkram
I am trying to authenticate a website in java applet. but it gives an error at this line :Authenticator.setDefault(new MyAuthenticator());

import java.io.InputStream;
import java.net.Authenticator;
import java.net.URL;
import javax.imageio.ImageIO;
import java.awt.*;
import java.applet.*;

public class Authenticated_Applet extends Applet {

public void init() {

}

static class MyAuthenticator extends Authenticator
{
{
// I haven't checked getRequestingScheme() here, since for NTLM
// and Negotiate, the usrname and password are all the same.
}
}

public void paint(Graphics g) {
try
{
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://192.168.1.168:8081/jpg/image.jpg");
InputStream ins = url.openConnection().getInputStream();
String str;
System.out.println(str);

//int s=2+2;

g.drawString("hello world", 50, 60 );
}
catch (Exception e)
{
System.out.println("Exception : "+ e);
}
}
}
• June 8th, 2012, 06:27 AM
Norm
Be sure to wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
• June 12th, 2012, 12:30 AM
azeemAkram