Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Please Help. . . .

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Please Help. . . .



    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 ?}
    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();
            }
        }



  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Please Help. . . .

    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!

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. . . .

    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]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please Help. . . .

    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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. . . .

    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

    [ please help me ]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please Help. . . .

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Yunus Einsteinium (June 7th, 2012)

  8. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. . . .

    I am trying to authenticate a website in java applet. but it gives an error at this line :Authenticator.setDefault(new MyAuthenticator());

    what is the solution, please help me


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

    public class Authenticated_Applet extends Applet {

    static final String kuser = "username"; // your account name
    static final String kpass = "password"; // your password for the account

    public void init() {


    }

    static class MyAuthenticator extends Authenticator
    {
    public PasswordAuthentication getPasswordAuthentication()
    {
    // I haven't checked getRequestingScheme() here, since for NTLM
    // and Negotiate, the usrname and password are all the same.
    System.err.println("Feeding username and password for " + getRequestingScheme());
    return (new PasswordAuthentication(kuser, kpass.toCharArray()));
    }
    }

    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();
    BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
    String str;
    while((str = reader.readLine()) != null)
    System.out.println(str);

    //int s=2+2;

    g.drawString("hello world", 50, 60 );
    }
    catch (Exception e)
    {
    System.out.println("Exception : "+ e);
    }
    }
    }

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please Help. . . .

    @azeemAkram Please start your own thread for your problem.
    Be sure to wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    My Mood
    Sad
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. . . .

    Thanks Norm... i am new to this forum.

  11. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help. . . .

    If someone could PLEASE help me. I have been trying to figure this thing out for days.
    atarsales.com
    It validates and everything works perfect so far. The only problem i'm having is the query plugin takes about 6 seconds to start sometimes and sometimes it starts right away when the page loads. I've set the timer for the banner for 6 seconds but that should only start AFTER the page loads. any thoughts or solutions on this would help...

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Please Help. . . .

    @bigeba8 Please start your own thread with your problem. Don't hijack someone else's thread.
    If you don't understand my answer, don't ignore it, ask a question.