Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36471

Re: hash map

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: hash map
Date 2017-04-17 10:54 -0400
Organization A noiseless patient Spider
Message-ID <od2koi$gie$1@dont-email.me> (permalink)
References (2 earlier) <25389b66-b30f-4076-8f8e-4bfeb91dbc6f@googlegroups.com> <od2ena$sf8$1@dont-email.me> <fe8a759e-1df6-4d5d-beb8-44e49ae00e83@googlegroups.com> <od2ifk$98s$1@dont-email.me> <8a53e56c-5877-4c31-976f-5802f926f44a@googlegroups.com>

Show all headers | View raw


On 4/17/2017 10:25 AM, pablo wrote:
> On Monday, 17 April 2017 15:15:18 UTC+1, Eric Sosman  wrote:
>> On 4/17/2017 9:53 AM, pablo wrote:
>>>
>>> import java.util.*;
>>> /**
>>>    * The class Garage is used to hold data about a collection of customers.
>>>    *
>>>    * @author M250 module team
>>>    * @version 1.0
>>>    */
>>> public class Garage
>>> {
>>>      //garage creating an empty instance of map map
>>>      private  Map<String,String> map = new HashMap<String, String>();
>>
>>       Okay: This says that each "Garage" instance owns a Map whose
>> keys are Strings and whose corresponding values are also Strings,
>> and it arranges to create such a Map (empty) each time you create
>> a Garage.  Fine so far.
>>
>>>      map.put("Andrew Smith, address 1 The High Street, area 1");
>>
>>       ... but this is, er, "less fine."  For one thing, it's not a
>> declaration but an executable statement, a call upon the Map's put()
>> method.  Executable statements can't just float around in the ether,
>> but must be inside methods or constructors (or static blocks, but
>> they are less common).
>>
>>       After you've solved that problem by finding a suitable home for
>> the executable statement, you'll discover another one: Map's put()
>> method requires two arguments, the key and corresponding value of a
>> key/value pair.  The method call you have written provides only one
>> argument, not the required two.  Perhaps you meant something like
>>
>> 	map.put("Andrew Smith", "1 The High Street, area 1");
>>
>> ... which associates the key "Andrew Smith" with the value "1 The
>> High Street, area 1".  That's just a guess, though, because we don't
>> know what relationship this map is intended to represent.
>>
>>>
>>>      
>>> }
>>>
>>
>>
>> -- 
>> esosman@comcast-dot-net.invalid
>> Thirteen hundred seventy-four days to go.
> 
> hi Eric just to put a little bit of context to this this is what i have been asked to do obviously i am struggling
> 
>   An instance of Garage is used to hold data about a collection of customers. Declare a private instance variable called customers in your Garage class, capable of referencing a map where the keys are strings and the values are Customer objects as below (the order of keys in the map is unimportant):
> Key       Value
>   "123ABC" Customer object with name "Andrew Smith", address "1 The High Street", area 1
>   "456DEF" Customer object with name "Janet Jones", address "145 Main Street", area 1
> "789GHI" Customer object with name "Christine Charter", address "50 Lake View", area 2

     Okay; that's a bit more for me to work with.  The first thing to
realize is that you need to write *two* classes: One to represent a
Garage, and the other to represent a Customer.  Let's begin with the
latter:

     A Customer has a name, an address, and perhaps an "area" -- it's
not clear to me what the problem's notation signifies.  Perhaps a
Customer has other attributes, too: How many parking spaces he rents,
how much rent is due each month, how badly the account is in arrears,
what telephone number(s) will reach him, and so on -- that's up to
you, or perhaps to the people who have set you the problem (was there,
by chance, a Customer class as the subject of an earlier exercise?).

     At any rate, you need a Customer.java file defining your Customer
class.

     Then you need Garage.java defining Garage.  That class will own a
Map relating the given keys (auto registration plate numbers, perhaps?)
to the associated Customers.  You'll have something like

	public class Garage {
	   ...
	   private Map<String, Customer> customers = new HashMap<>();
	   ...
	}

There are two things to note here, one important and one less so.  The
more important thing is that the values in this map are not Strings,
but Customer objects.  Those Customer objects in turn own Strings to
describe names and addresses and so on, but the Customer objects are
instances of your Customer class.  (The less important thing is the
use of <> on the right-hand side; the compiler already knows from the
left-hand side what should go inside the brackets, so you needn't write
it a second time -- but omitting it is just a handy abbreviation, not
something that changes the meaning in any way.)

     Okay so far?  Then how do you put Customers in a Garage?

     First, you need a Garage:

	Garage pablosGarage = new Garage();

     Next, you need some Customers:

	Customer c1 = new Customer("Andrew Smith", "1 The High Street");
	Customer c2 = new Customer("Janet Jones", "145 Main Street");

     Finally, you need to put the Customers in the Garage -- Oops!  The
Garage's map of keys to Customers is private, and can only be read and
written from inside the Garage class.  So, you need to write a method
that lets "outsiders" add things to a Garage (and probably additional
methods to retrieve what's in there, too):

	public class Garage {
	   ...
	   public void addCustomer(String key, Customer cust) {
	      customers.put(key, cust);
	   }
	   ...
	}

... and then in the "outsider" -- perhaps a Main or Driver class --
where you've set up pablosGarage and c1 and c2 and the rest, you could
write

	pablosGarage.addCustomer("123ABC", c1);
	pablosGarage.addcustomer("456DEF", c2);

... and so on to populate the Garage.

     I imagine the course will go on to have you implement additional
methods for Garage and/or Customer, making the assemblage more useful
by degrees.  Perhaps the Garage's keys will change from simple Strings
to full-fledged Automobile objects, perhaps you will be asked to write
a method to evict non-paying Customers from your Garage, perhaps ...

-- 
esosman@comcast-dot-net.invalid
Thirteen hundred seventy-four days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

hash map pablo <pa24959122@gmail.com> - 2017-04-16 11:53 -0700
  Re: hash map Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-16 19:10 +0000
  Re:hash map omegajunior <omegajunior@protonmail.com> - 2017-04-16 17:11 -0400
    Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 04:31 -0700
  Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-17 05:11 -0700
    Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 05:32 -0700
      Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-17 05:58 -0700
      Re: hash map Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-04-17 09:10 -0400
        Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 06:53 -0700
          Re: hash map Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-04-17 10:15 -0400
            Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 07:25 -0700
              Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-17 07:46 -0700
              Re: hash map Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-04-17 10:54 -0400
                Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 08:29 -0700
                Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-17 08:44 -0700
                Re: hash map Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-04-17 12:07 -0400
                Re: hash map pablo <pa24959122@gmail.com> - 2017-04-17 09:32 -0700
                Re: hash map Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-17 23:14 +0000
                Re: hash map Arne Vajhøj <arne@vajhoej.dk> - 2017-04-17 20:22 -0400
                Re: hash map Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-18 01:20 +0000
                Re: hash map Arne Vajhøj <arne@vajhoej.dk> - 2017-04-18 20:01 -0400
                Re: hash map Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-19 09:02 +0000
                Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-18 04:54 -0700
                Re: hash map Arne Vajhøj <arne@vajhoej.dk> - 2017-04-18 20:03 -0400
                Re: hash map Martin Gregorie <martin@address-in-sig.invalid> - 2017-04-19 08:55 +0000
                Re: hash map Eric Douglas <e.d.programmer@gmail.com> - 2017-04-19 04:45 -0700
                Re: hash map Arne Vajhøj <arne@vajhoej.dk> - 2017-04-18 20:05 -0400

csiph-web