Page 1 of 4 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0

    Java ArrayList help


    Hey, Just wondering if anyone could help me with a little Java I'm stuck on with my homework?

    I've done most of it I just need help with a few bits

    If possible could you PM me?

    Thanks!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Post your code and ask some specific questions about the problems you are having.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Post your code and ask some specific questions about the problems you are having.
    Hey

    I have 4 classes:

    • Borrower
      PHP Code:
      public class Borrower
      {
          private 
      String firstName;
          private 
      String lastName;
          private 
      String libraryNumber;
          private 
      int noOfBooks;
          private 
      Address address;

          
      /**
           * Constructor for objects of class Borrower.
           * The number of books should be set to 1.
           * 
           * @param firstName The Borrower's first name 
           * @param lastName The Borrower's last name
           * @param lNumber The Borrower's library number
           * @param street The Borrower's street
           * @param town The Borrower's town
           * @param postcode The Borrower's postcode
           */
          
      public Borrower(String fNameString lNameString lNumber
                          
      String streetString townString postcode)
          {
              
      firstName fName;
              
      lastName lName;
              
      libraryNumber lNumber;
              
      noOfBooks 1;        
              
      address = new Address(streettownpostcode);
          }
          
          
      /**
           * Constructor for objects of class Borrower.
           * The number of books on loan should should be set to
           * the supplied vale.
           * 
           * @param fName The Borrower's first name 
           * @param lName The Borrower's last name
           * @param lNumber The Borrower's library number
           * @param numberOfBooks The initial book borrow
           * @param street The Borrower's street
           * @param town The Borrower's town
           * @param postcode The Borrower's postcode
           */
          
      public Borrower(String fNameString lNameString lNumberint numberOfBooks
                          
      String streetString townString postcode)
          {
              
      firstName fName;
              
      lastName lName;
              
      libraryNumber lNumber;
              
      noOfBooks numberOfBooks;         
              
      address = new Address(streettownpostcode);
          }
          
          
      // accessors
          
          /**
           * Get the Borrower's first name
           * 
           * @return the Borrower's first name
           */
          
      public String getFirstName()
          {
              return 
      firstName;
          }
          
          
      /**
           * Get the Borrower's last name
           * 
           * @return the Borrower's last name
           */
          
      public String getLastName()
          {
              return 
      lastName;
          }
          
          
      /**
           * Get the Borrower's library Number
           * 
           * @return the Borrower's library number
           */
          
      public String getLibraryNumber()
          {
              return 
      libraryNumber;
          }
          
           
      /**
           * Get the number of books on loan
           * 
           * @return the number of books on loan
           */
          
      public int getNoOfBooks()
          {
              return 
      noOfBooks;
          }
          
          
      /**
           * Print out the Borrower's details to the console window
           * 
           */
          
      public void printBorrowerDetails()
          {
              
      System.out.printlnfirstName " " lastName 
                                 
      "\n" address.getFullAddress()
                                 + 
      "\nLibrary Number: " libraryNumber
                                 
      "\nNumber of loans: " noOfBooks "\n");
              
          }     
          
          
      // mutators
             
          /**
           * Increase the bumber of books on loan by 1
           * 
           */
          
      public void borrowBook()
          {
              
      noOfBooks noOfBooks 1;
              
      System.out.println("Books on loan: " noOfBooks);        
          }
          
          
      /**
           * Increase the bumber of books on loan by a given number
           * 
           * @param number of new loans to add to total
           */
          
      public void borrowBooks(int number)
          {
              
      noOfBooks noOfBooks number;
              
      System.out.println("Books on loan: " noOfBooks);        
          }
          
          
      /**
           * Return a book
           * 
           */
          
      public void returnBook ()
          {
              
      noOfBooks noOfBooks ;
              
      System.out.println("Books on loan: " noOfBooks);        
          }
          
          
      /**
           * Return the Borrower's address
           * 
           * @return the Borrower's address
           */
          
      public String getAddress()
          {
              return 
      address.getFullAddress();
          }
          
          
      /**
           * Change the Borrower's address
           * 
           * @param street the street
           * @param town the town
           * @param postcode the postcode
           */
          
      public void setAddress(String streetString townString postcode)
          {
              
      address.setFullAddress(streettownpostcode);
          }
          
          
      /**
           * Print out Borrower's address
           */
          
      public void printAddress()
          {
              
      address.printAddress();
          }
          
      // end class 
    • Address
      PHP Code:
      public class Address
      {
          private 
      String street;
          private 
      String town;
          private 
      String postcode;
          
          
      /**
           * Constructor for objects of class Address
           * 
           * @param street the street
           * @param town the town
           * @param postcode the postcode
           */
          
      public Address(String streetString townString postcode)
          {
              
      this.street street;
              
      this.town town;
              
      this.postcode postcode;        
          }
          
          
      /**
           * returns the street
           * 
           * @return the street
           */
          
      public String getStreet()
          {
              return 
      street;
          }
          
          
      /**
           * Returns the town
           * 
           * @return the town
           */
          
      public String getTown()
          {
              return 
      town;
          }
          
          
      /**
           * Returns the postcode
           * 
           * @return the postcode
           */
          
      public String getPostcode()
          {
              return 
      postcode;
          }
          
          
      /**
           * Returns the formatted address
           * one element to a line
           * 
           * @return the formatted address
           */
          
      public String getFullAddress()
          {
              
      String output "";
              
      output street "\n" town "\n" postcode;
              return 
      output;
          }
          
          
      /**
           * Set the street
           * 
           * @param Street the street
           */
          
      public void setStreet(String street)
          {
              
      this.street street;
          }
          
           
      /**
           * Set the town
           * 
           * @param town the town
           */
          
      public void setTown(String town)
          {
              
      this.town town;
          }
          
           
      /**
           * Set the postcode
           * 
           * @param postcode the postcode
           */
          
      public void setPostcode(String postcode)
          {
              
      this.postcode postcode;
          }
          
           
      /**
           * Set the full address
           * 
           * @param street the street
           * @param town the town
           * @param postcode the postcode
           */
          
      public void setFullAddress(String streetString townString postcode)
          {
              
      this.street street;
              
      this.town town;
              
      this.postcode postcode;
          }
          
          
      /** 
           * print formatted address to console window
           */
          
      public void printAddress()
          {
              
      System.out.println(getFullAddress());
          }

    • BorrowerList
      PHP Code:
      import java.util.*;


      public class 
      BorrowerList
      {
          private 
      ArrayList<Borrowerborrowers;    

          
      /**
           * Constructor for objects of class BorrowerList
           */
          
      public BorrowerList()
          {
              
      borrowers = new ArrayList<Borrower>();
          }

          public 
      void addBorrower(Borrower borrower)
          {
              
      borrowers.add(borrower);
          }

          public 
      int getNumberOfBorrowers()
          {
              return 
      borrowers.size();
          }

          
      //    public boolean getBorrower(String libraryNumber)
          //    {
          //        //lol
          //     }

          
      public void getBorrower(int borrowerEntry)
          {
              if (
      borrowerEntry 0)
              {
                  
      System.out.println("Negative entry: " borrowerEntry);
              }
              else if (
      borrowerEntry getNumberOfBorrowers())
              { 
                  
      Borrower borrower borrowers.get(borrowerEntry);
                  
      borrower.printBorrowerDetails();
              }
              else
              {
                  
      System.out.println("No such entry: " borrowerEntry);
              }
          }

          public 
      void getAllBorrowers()
          {
              for(
      Borrower borrower borrowers)
              {
                  
      borrower.printBorrowerDetails();
              }
          }

          public 
      void removeBorrower(int borrowerEntry)
          {
              if(
      borrowerEntry 0)
              {
                  
      System.out.println("Negative entry :" borrowerEntry);
              }
              else if(
      borrowerEntry getNumberOfBorrowers())
              {
                  
      borrowers.remove(borrowerEntry);
              }
              else
              {
                  
      System.out.println("No such entry :" borrowerEntry);
              }
          }

          public 
      boolean removeBorrower(String libraryNumber)
          {
              
      int index 0;
              for (
      Borrower borrowerborrowers)
              {
                  if (
      libraryNumber.equals(borrower.getLibraryNumber()))
                  {
                      
      borrowers.remove(index);
                      return 
      true;
                  }
                  
      index++;
              }
              return 
      false;
          }

          public 
      int search(String libraryNumber)
          {
              
      int index 0;
              for (
      Borrower borrower borrowers)
              {
                  if (
      libraryNumber.equals(borrower.getLibraryNumber()))
                  {
                      return 
      index;
                  }
                  else
                  {
                      
      index++;
                  }
              }
              return -
      1;
          }    

    • BorrowerTUI
      PHP Code:
      import java.util.*;

      public class 
      BorrowerTUI
      {
          private 
      BorrowerList borrowerList;
          
      Scanner myScanner;

          
      /**
           * Creates the BorrowerTUI class and the Scanner
           */
          
      public BorrowerTUI()
          {
              
      myScanner = new Scanner(System.in);
              
      borrowerList = new BorrowerList();
          }

          
      /**
           * This displays the menu on the BlueJ terminal window
           */
          
      public void menu()
          {
              
      int command = -1;

              while (
      command != 0)
              {
                  
      displayMenu();
                  
      command getCommand();
                  
      execute(command);
              }

          }

          private 
      void displayMenu()
          {
              
      System.out.println("Menu Options: ");
              
      System.out.println(" - Add a borrower................................[1]");
              
      System.out.println(" - Get the current number of borrowers...........[2]");
              
      System.out.println(" - Show the details of a certain borrower........[3]");
              
      System.out.println(" - Show all the details of all borrowers.........[4]");
              
      System.out.println(" - Remove a borrower.............................[5]");
              
      System.out.println(" - Exit the program..............................[0]");
              
      System.out.println(" ");
              
      System.out.println(" ");
          }

          private 
      int getCommand()
          {
              
      System.out.print ("Please enter a menu option number: ");
              return 
      myScanner.nextInt();
          }

          private 
      void execute(int command)
          {
              if (
      command == 1)
                  
      addBorrower();
              else
              if (
      command == 2)
                  
      getNumberOfBorrowers();
              else
              if (
      command == 3)
                  
      showBorrower();
              else
              if (
      command == 4)
                  
      showAllBorrowers();
              else
              if (
      command == 5)
                  
      removeBorrower();
              else
              if (
      command == 0)
                  
      quitCommand();
              else
                  
      unknownCommand(command);

          }

          private 
      void addBorrower()
          {
          }

          private 
      void getNumberOfBorrowers()
          {
              
      System.out.println("Current number of borrowers: " 
                  
      borrowerList.getNumberOfBorrowers() + "\n");
          }

          private 
      void showBorrower()
          {
          }

          private 
      void showAllBorrowers()
          {
          }

          private 
      void removeBorrower()
          {
          }

          private 
      void quitCommand()
          {
              
      System.out.println("Goodbye! See you later!");
          }

          private 
      void unknownCommand(int command)
          {
              
      System.out.println("The command you entered: " command 
                  
      " seems to be invalid, please enter a valid command");
          }




    Basically I've done all that, but I can't figure out the code for the following:

    PHP Code:
       private void addBorrower()
        {
           
    //Adds a borrower
        
    }

        private 
    void showBorrower()
        {
           
    //Print out borrower with chosen library number. If method is unsuccessful then a message “Could not find borrower.” should be output.
        
    }

        private 
    void showAllBorrowers()
        {
           
    //Print out all borrowers. Space should be left between each to make it easier to read.
        
    }

        private 
    void removeBorrower()
        {
           
    //Remove a borrower with the given library number. If successful output “Borrower with library [library No] removed.” If not then: “Borrower with library [library No] NOT removed.” 
    and I need to add another method to BorrowerList:

    getBorrower(libraryNumber : String) : boolean
    This method is similar to the existing getBorrower method but should receive a string representing the borrower’s library number and should print details of a borrower with the given library number to the conslole window via the printBorrowerDetails method defined in Borrower. If the number is found then it should then return true. However if the borrower is not found it should return false.

    I'm using the java scannner in input the values and such, I just really can't figure it out,

    Thanks
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Those seem like reasonable description of what the method should do. What problems do you have with coding it? I don't see where it requires getting input from a user via a Scanner method.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Those seem like reasonable description of what the method should do. What problems do you have with coding it? I don't see where it requires getting input from a user via a Scanner method.
    I just can't do it, I'm only just learning and I cannot do it, it's actually giving me a migraine

    We have to use a scanner method to add them as we're doing basic UI's D:
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    getBorrower method
    1)should receive a string representing the borrower’s library number and
    2) should print details of a borrower with the given library number to the conslole window via the printBorrowerDetails method defined in Borrower.
    3)If the number is found then it should then return true.
    4)However if the borrower is not found it should return false.

    The method should consider those 4 things
    1) get the id number
    2) look up the id number in ??? an arraylist of Borrowers
    3) if found, print it using the given method and return true
    4) if not found, return false
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    getBorrower method
    1)should receive a string representing the borrower’s library number and
    2) should print details of a borrower with the given library number to the conslole window via the printBorrowerDetails method defined in Borrower.
    3)If the number is found then it should then return true.
    4)However if the borrower is not found it should return false.

    The method should consider those 4 things
    1) get the id number
    2) look up the id number in ??? an arraylist of Borrowers
    3) if found, print it using the given method and return true
    4) if not found, return false
    Would it be?

    PHP Code:

        
    public boolean getBorrower(String librayNumber)
        {
            if (
    libraryNumber.equals(borrower.getLibraryNumber()))
            {
                
    System.out.println(getBorrowerDetails());
            }
            else 
            {
                return 
    false;
            }
        } 
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Are the Borrower objects in an arraylist?
    If so, you will need a loop to go through the list with an if statement inside that checks each Borrower it gets from the list for the desired number.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Are the Borrower objects in an arraylist?
    If so, you will need a loop to go through the list with an if statement inside that checks each Borrower it gets from the list for the desired number.
    Yeah they are, erm, I'm sorry I really, really stuck and confused

    erm

    If you gave me the rest of the code would you accept a payment? I'm just really done with it and it's making me feel sick haha
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Do it one step at a time:
    1) write a loop that goes through the arraylist and gets a Borrower from the list
    See the tutorial for how to write a for loop:http://docs.oracle.com/javase/tutori...bolts/for.html
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Do it one step at a time:
    1) write a loop that goes through the arraylist and gets a Borrower from the list
    See the tutorial for how to write a for loop:http://docs.oracle.com/javase/tutori...bolts/for.html
    PHP Code:
    public boolean getBorrower(String librayNumber)
        {
            
    int index 0;
            
    boolean found false;
            while(
    index borrowers.size() && !found)
            {
                
    String borrower borrowers.get(index);
                if(
    borrower.equals(libraryNumber))
                {
                    
    found true;
                }
                else
                    
    index++;
            }

            if(
    found)
                return 
    index;
            else
                return -
    1;
        } 
    ?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What happens when you compile that code? You should compile the code before posting it in case there are any error messages that you want to ask questions about.

    When that loop works, move to step 3 and call the method to print the Borrower object's contents.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    What happens when you compile that code? You should compile the code before posting it in case there are any error messages that you want to ask questions about.

    When that loop works, move to step 3 and call the method to print the Borrower object's contents.
    "String borrower = borrowers.get(index);"

    Error: Incompatible types D;
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What does the borrowers ArrayList contain? The get() method will return an object of that type. The compiler does not think it contains String objects.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    What does the borrowers ArrayList contain? The get() method will return an object of that type. The compiler does not think it contains String objects.
    Objects of borrower type
Page 1 of 4 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo