Homework help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PLA
    New Member
    • Aug 2006
    • 44

    #16
    Originally posted by jamieexley
    A bank account is stored as a Map which maps the names of the account holders to a value representing the current balance of their account:
    Map<String,Inte ger> bank;

    Given a String variable declared like this:
    String rich = null;
    How is the loop which will store the name of the person with the most money in their account in rich written?
    Code:
    Map<String, Integer> bank = new Hashtable<String, Integer>();
    String rich = null;
    Integer richBalance = new Integer(0);
    
        bank.put("aaa", new Integer(1020));
        bank.put("bbb", new Integer(2333));
        bank.put("ccc", new Integer(1356));
    
        for (String account : bank.keySet())
        {
          if (bank.get(account) > richBalance)
          {
            rich = account;
            richBalance = bank.get(rich);
          }
        }
        System.out.println("Max is : " + rich + " (" + richBalance + ")");
    It's my last for that day!

    Comment

    • jamieexley
      New Member
      • Aug 2006
      • 9

      #17
      Just 1 more! Please
      (3rd & Final)

      Using the same bank account as in the previous question, and a list of strings:
      Map<String,Inte ger> bank;
      List<String> names;

      I need to write a loop which fills the list names with the names of all account holders whose accounts are overdrawn, i.e. the balance of their account is less than zero???

      Comment

      • PLA
        New Member
        • Aug 2006
        • 44

        #18
        Originally posted by jamieexley
        Just 1 more! Please
        (3rd & Final)

        Using the same bank account as in the previous question, and a list of strings:
        Map<String,Inte ger> bank;
        List<String> names;

        I need to write a loop which fills the list names with the names of all account holders whose accounts are overdrawn, i.e. the balance of their account is less than zero???
        Code:
            Map<String, Integer> bank = new Hashtable<String, Integer>();
            String rich = null;
            Integer richBalance = new Integer(0);
            List<String> names = new LinkedList<String>();
        
            bank.put("aaa", new Integer(1020));
            bank.put("bbb", new Integer(2333));
            bank.put("ccc", new Integer(-1356));
            bank.put("ddd", new Integer(1356));
            bank.put("eee", new Integer(-56));
        
            for (String account : bank.keySet())
            {
              if (bank.get(account) > richBalance)
              {
                rich = account;
                richBalance = bank.get(rich);
              }
              if (bank.get(account) < 0)
              {
                names.add(account);
              }
            }
            System.out.println("Max is : " + rich + " (" + richBalance + ")");
            System.out.println("Names are : " + names);
        It is easy as you can see !!!

        Comment

        • PLA
          New Member
          • Aug 2006
          • 44

          #19
          In fact it is the first time I use Generics in Java (I remembered ugly code and response time in C++ with templates) and it's quite good. Thanks for the lesson

          Comment

          • D_C
            Contributor
            • Jun 2006
            • 293

            #20
            I don't know how list is defined, but this may give you an idea of how to do it. I am assuming there are list.length() elements in the list, and list.entry(i) returns the ith element. Similarly for map. I will give you an idea for #2, and some suggestions for the others.

            Number 2
            Code:
            max = map.entry(0).getInt(); // since list is not empty.
            for(int i = 0; i < map.length(); i++)
            {
               if(max < map.entry(i).getInt())
               {
                  max = map.entry(i).getInt());
                  rich = map.entry(i).getStr());
               }
            }
            #1. Just like #2, only you don't worry about the string rich. Also it's a list instead of a map.
            #3. Change the if statement. It's condition is given to you. The code to execute should simply put the person's name into a list.

            The others seem just as trivial. Give it a go yourself, and if you have any trouble, post your code, what's wrong with it, and what you can't figure out. Then we'll look at it and see.

            Comment

            • D_C
              Contributor
              • Jun 2006
              • 293

              #21
              Java API for BufferedInputSt ream. Become friends with the Java API. I don't have Java to test this, but it should work. Also, by not catching the exception, it is returned as requested.
              Code:
              public int countChars(InputStream is) throws IOException
              {
                 int count = 0;
                 BufferedInputStream bis = new BufferedInputStream(is);
                 while(bis.read() != -1) // -1 is returned when end of input is reached.
                 {
                    count++;
                 }
                 return count;
              }

              Comment

              • PLA
                New Member
                • Aug 2006
                • 44

                #22
                Originally posted by D_C
                I don't know how list is defined, but this may give you an idea of how to do it. I am assuming there are list.length() elements in the list, and list.entry(i) returns the ith element. Similarly for map. I will give you an idea for #2, and some suggestions for the others.

                Number 2
                Code:
                max = map.entry(0).getInt(); // since list is not empty.
                for(int i = 0; i < map.length(); i++)
                {
                   if(max < map.entry(i).getInt())
                   {
                      max = map.entry(i).getInt());
                      rich = map.entry(i).getStr());
                   }
                }
                #1. Just like #2, only you don't worry about the string rich. Also it's a list instead of a map.
                #3. Change the if statement. It's condition is given to you. The code to execute should simply put the person's name into a list.

                The others seem just as trivial. Give it a go yourself, and if you have any trouble, post your code, what's wrong with it, and what you can't figure out. Then we'll look at it and see.
                An other way is to use Java 5.0 Generics and loop
                Other thread

                Comment

                • Niheel
                  Recognized Expert Moderator Top Contributor
                  • Jul 2005
                  • 2432

                  #23
                  All of jaime's questions got merged into one homework help thread.
                  niheel @ bytes

                  Comment

                  • ruth1976
                    New Member
                    • Sep 2006
                    • 1

                    #24
                    I need some help on this. I did this code but now need to be updated. I really dont understand what are the benefit. How do I do? Thank you so much for any help.
                    Ruth


                    I have the code below how do I do to update the new method:

                    int count = ((Integer)(orde rMap.get(key))) .intValue();
                    currentOrder.pu t(unknownItem, new Integer(count)) ;


                    That is the code to be updated.


                    public class ToysInventory {
                    private String[] toysOrdered = {"bear", "train", "car", "ball", "doll",
                    "ball", "train", "doll", "game", "train",
                    "bear", "doll", "train","ca r", "ball",
                    "bat", "glove", "bat", "b", "doll", "bear",
                    "ball", "doll", "bat", "car", "glove",
                    "train", "doll", "bear" };

                    private String[] toysInventoryLi st = {"ball", "bat", "bear", "car", "doll",
                    "game", "glove", "playstatio n", "train" };

                    private int InventoryCounte r[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

                    public static void main(String[] args) {//BEGIN main()

                    ToysInventory toys = new ToysInventory() ;
                    toys.countToys( );
                    System.exit(0);
                    }//END main()

                    private void countToys() {
                    int temp = 0;
                    int orderedIndex = 0;
                    int inventoryIndex = 0;

                    for (inventoryIndex = 0; inventoryIndex < toysInventoryLi st.length; inventoryIndex+ +){
                    for (orderedIndex = 0; orderedIndex < toysOrdered.len gth; orderedIndex++) {
                    if (toysOrdered[orderedIndex] == toysInventoryLi st[inventoryIndex]) {
                    temp = temp + 1;
                    }//END if
                    InventoryCounte r[inventoryIndex] = temp;
                    }
                    temp = 0; //reset counter value
                    }

                    System.out.prin tln("[order status]");
                    for (temp = 0; temp < InventoryCounte r.length; temp++){
                    // (*) before any item for which the customer has requested
                    // a quantity of 5 or more.
                    if (InventoryCount er[temp] >= 5){
                    System.out.prin t("*");
                    }
                    else {
                    System.out.prin t(" ");
                    }
                    System.out.prin tln(toysOrdered[temp] + " = " + InventoryCounte r[temp]);
                    }
                    }//END countToys()

                    }//END ToysInventory

                    Comment

                    • joej231
                      New Member
                      • Mar 2008
                      • 2

                      #25
                      for question
                      Given a list of integers and an integer variable declared like this:

                      List<Integer> list;
                      int max;

                      and assuming that some values have been added to the list, write a loop which finds the largest value in list and stores it in max.

                      i used this answer
                      for (int i : list)
                      {
                      if (i > max)
                      {
                      max = i;
                      }
                      }
                      System.out.prin tln(max);
                      }
                      and i get these errors
                      Main.java:36: illegal start of type
                      for (Integer n : list) {
                      ^
                      Main.java:48: <identifier> expected
                      }
                      ^
                      Main.java:49: 'class' or 'interface' expected
                      }
                      ^
                      Main.java:49: 'class' or 'interface' expected
                      }
                      ^
                      4 errors

                      any ideas in what im doing wrong?

                      Comment

                      • satch
                        New Member
                        • Feb 2008
                        • 23

                        #26
                        Originally posted by joej231
                        for question
                        Given a list of integers and an integer variable declared like this:

                        List<Integer> list;
                        int max;

                        and assuming that some values have been added to the list, write a loop which finds the largest value in list and stores it in max.

                        i used this answer
                        for (int i : list)
                        {
                        if (i > max)
                        {
                        max = i;
                        }
                        }
                        System.out.prin tln(max);
                        }
                        and i get these errors
                        Main.java:36: illegal start of type
                        for (Integer n : list) {
                        ^
                        Main.java:48: <identifier> expected
                        }
                        ^
                        Main.java:49: 'class' or 'interface' expected
                        }
                        ^
                        Main.java:49: 'class' or 'interface' expected
                        }
                        ^
                        4 errors

                        any ideas in what im doing wrong?
                        Hey joej231,
                        I think it would have been helpful if you had posted the complete code because without the code the errors are of no help.
                        Anyways the following code finds the max given a list of numbers(that is what you are trying to do..right?). Hope it solves your problem.
                        [ no code spoonfeeding alllowed -- mod ]

                        First of all, joej should've started his/her own thread for the problem and not hijack
                        a years old thread. Second, we don't spoonfeed source code.

                        kind regards,

                        Jos

                        Comment

                        • MonaLisaO
                          New Member
                          • Mar 2008
                          • 13

                          #27
                          Originally posted by satch
                          [ no code spoonfeeding alllowed -- mod ]

                          First of all, joej should've started his/her own thread for the problem and not hijack
                          a years old thread. Second, we don't spoonfeed source code.

                          kind regards,

                          Jos
                          I'm confused, is this the real JosAH or someone who is saying some thing that JosAH would say? Because I think also he would have said be sure to use CODE tags.

                          ~mona

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #28
                            Originally posted by MonaLisaO
                            I'm confused, is this the real JosAH or someone who is saying some thing that JosAH would say? Because I think also he would have said be sure to use CODE tags.

                            ~mona
                            I almost never say that, I simply insert those tags myself and add an 'edit' line ;-)

                            kind regards,

                            Jos ( and don't forget the CODE tags folks! ;-)

                            Comment

                            • MonaLisaO
                              New Member
                              • Mar 2008
                              • 13

                              #29
                              Originally posted by JosAH
                              I almost never say that, I simply insert those tags myself and add an 'edit' line ;-)

                              kind regards,

                              Jos ( and don't forget the CODE tags folks! ;-)
                              But apparently not in this case? lol

                              ~mona

                              Comment

                              • satch
                                New Member
                                • Feb 2008
                                • 23

                                #30
                                Originally posted by satch
                                [ no code spoonfeeding alllowed -- mod ]

                                First of all, joej should've started his/her own thread for the problem and not hijack
                                a years old thread. Second, we don't spoonfeed source code.

                                kind regards,

                                Jos
                                Point noted :)
                                I had read about not spoonfeeding source code on the forum but I felt he had written most of the logic correctly and was probably making a mistake in initialization/syntax, so I posted the code.
                                Anyways from now on I'll avoid posting code.

                                Comment

                                Working...