Results 1 to 9 of 9
  1. #1
    zelhopper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Exclamation print/search function in bluej?

    hi,

    i have been trying to do this myself for the past month, i have the "objects first with java" book and still cannot find what i am looking for.
    i need to know what is the code for my stock class that is required to:

    search for and print all objects that match the term the user enters

    i.e - when i select this function it should pop up a message that allows me to enter a string value. it then searches through my arraylist for any objects with matching string values and prints theme

    i remember using this function before but cannot remember the code.
    i have 2 classes car and stock
    car:
    Java Code:
    /**
     * The Car class represents a car object. Information about the car is stored and can be retrieved.
     */
    public class Car
    {
        private String make;
        private String model;
        private String reg;
        private int paid;
        private int sold;
    
        /**
         * Initializing the Car class.
         */
        public Car(String theMake, String theModel, String theReg, int PricePaid, int PriceSold)
        {
            make = theMake;
            model = theModel;
            reg = theReg;
            paid = PricePaid;
            sold = PriceSold;
        }
    
        /**
         * Print details about this Car
         */
        public void print()
        {
            System.out.println("Make: " + make + " - Model: " + model + " - Reg: " + reg + " - Paid: " + paid
                                + " - Sold: " + sold + ".");
        }
    }
    stock:
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    /**
     * The Stock class is a functions like a database. It is the facility to store all the information
     * on cars that delboy has.
     */
    public class Stock
    {
        private ArrayList<Car> cars;
    
        /**
         * Construct an empty Stock database.
         */
        public Stock()
        {
            cars = new ArrayList<Car>();
        }
    
        /**
         * Add a new car to the Stock class.
         */
        public void addCar(Car theCar)
        {
            cars.add(theCar);
        }
    
        /**
         * Print a list of all stored Cars.
         */
        public void listCars()
        {
            Iterator<Car> it = cars.iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
            }
        }
    }
    the problem is that i want a search function that will print out a list of results e.g. search for "ford" and it will print out all the cars of make "ford"

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: print/search function in bluej?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    zelhopper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: print/search function in bluej?

    yes its cross posted but only because i need an answer quickly

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: print/search function in bluej?

    Quote Originally Posted by zelhopper View Post
    yes its cross posted but only because i need an answer quickly
    So it doesn't bother you if a volunteer at one of your cross-post site spends time trying to answer the question without realizing that it's already been answered elsewhere?

    As for me, I'd prefer not answering questions of folks that don't care about my time. I'd much prefer they told me up front if they were cross-posting and where.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: print/search function in bluej?

    Quote Originally Posted by zelhopper View Post
    hi,

    i have been trying to do this myself for the past month, i have the "objects first with java" book and still cannot find what i am looking for.
    i need to know what is the code for my stock class that is required to:

    search for and print all objects that match the term the user enters

    i.e - when i select this function it should pop up a message that allows me to enter a string value. it then searches through my arraylist for any objects with matching string values and prints theme

    i remember using this function before but cannot remember the code.
    i have 2 classes car and stock
    car:
    Java Code:
    /**
     * The Car class represents a car object. Information about the car is stored and can be retrieved.
     */
    public class Car
    {
        private String make;
        private String model;
        private String reg;
        private int paid;
        private int sold;
    
        /**
         * Initializing the Car class.
         */
        public Car(String theMake, String theModel, String theReg, int PricePaid, int PriceSold)
        {
            make = theMake;
            model = theModel;
            reg = theReg;
            paid = PricePaid;
            sold = PriceSold;
        }
    
        /**
         * Print details about this Car
         */
        public void print()
        {
            System.out.println("Make: " + make + " - Model: " + model + " - Reg: " + reg + " - Paid: " + paid
                                + " - Sold: " + sold + ".");
        }
    }
    stock:
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    /**
     * The Stock class is a functions like a database. It is the facility to store all the information
     * on cars that delboy has.
     */
    public class Stock
    {
        private ArrayList<Car> cars;
    
        /**
         * Construct an empty Stock database.
         */
        public Stock()
        {
            cars = new ArrayList<Car>();
        }
    
        /**
         * Add a new car to the Stock class.
         */
        public void addCar(Car theCar)
        {
            cars.add(theCar);
        }
    
        /**
         * Print a list of all stored Cars.
         */
        public void listCars()
        {
            Iterator<Car> it = cars.iterator();
            while(it.hasNext()) {
                System.out.println(it.next());
            }
        }
    }
    the problem is that i want a search function that will print out a list of results e.g. search for "ford" and it will print out all the cars of make "ford"
    So, if you truly desire our help, I have two suggestions:
    • Show links to any and all cross-posts
    • Show us your attempt to solve this. We have no idea what you're doing wrong or what your missing based on your post above.

  6. #6
    zelhopper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: print/search function in bluej?

    i would close the other post once i received a working answer and surely the is only one correct answer to my question which is clearly detailed above. i mentioned what resources i have used as well i really didnt think it was such a difficult problem.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: print/search function in bluej?

    Quote Originally Posted by zelhopper View Post
    i would close the other post once i received a working answer and surely the is only one correct answer to my question which is clearly detailed above. i mentioned what resources i have used as well i really didnt think it was such a difficult problem.
    It is quite difficult; for starters, your first comment for the Car class isn't true:

    Java Code:
    /**  * The Car class represents a car object. Information about the car is stored and can be retrieved.  */
    My question: how can any information be retrieved? Through the String representation of this class? That'd be hacking. There is absolutly nothing in your code of your classes that helps a wanted search or print functionality. We are not going to do all that work for you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    zelhopper is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: print/search function in bluej?

    i thought that by using the stock class where the arraylist is all the information about the car is stored once it has been added. does that not mean that you only need the printing searching functions in the stock class?

    and also there is a print function that prints out the details of that car.

    Java Code:
        /**
         * Print details about this Car
         */
        public void print()
        {
            System.out.println("Make: " + make + " - Model: " + model + " - Reg: " + reg + " - Paid: " + paid
                                + " - Sold: " + sold + ".");
        }
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: print/search function in bluej?

    Quote Originally Posted by zelhopper View Post
    i thought that by using the stock class where the arraylist is all the information about the car is stored once it has been added. does that not mean that you only need the printing searching functions in the stock class?

    and also there is a print function that prints out the details of that car.
    That Stock class doesn't have access to the details of a Car object either (the properties are private and there are no getXXX() methods in the Car class). Trying to obtain the values of the properties through its String representation (the print() method) is a hack.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to ad a search function
    By david.ny in forum NetBeans
    Replies: 2
    Last Post: 12-03-2011, 08:43 PM
  2. Replies: 4
    Last Post: 07-23-2009, 09:45 PM
  3. Search function for ArrayList?
    By javanoobie in forum New To Java
    Replies: 11
    Last Post: 04-17-2009, 08:38 PM
  4. How to make print function for single label?
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2009, 05:26 PM
  5. Replies: 5
    Last Post: 08-26-2008, 03:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •