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


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

Re: hash map

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: hash map
Date 2017-04-17 12:07 -0400
Organization A noiseless patient Spider
Message-ID <od2p1h$uu7$1@dont-email.me> (permalink)
References (4 earlier) <fe8a759e-1df6-4d5d-beb8-44e49ae00e83@googlegroups.com> <od2ifk$98s$1@dont-email.me> <8a53e56c-5877-4c31-976f-5802f926f44a@googlegroups.com> <od2koi$gie$1@dont-email.me> <1bcffbc0-a529-4185-87f2-8e1d66bf319d@googlegroups.com>

Show all headers | View raw


On 4/17/2017 11:29 AM, pablo wrote:
> 
> sorry mate this is what i have so far and i am getting an error maybe more but to start with i am getting can not find symbol variable customer. i am using bluej editor
> 
> 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
> {
>     /**
>      * Constructor for objects of class Garage
>      */
>     public Garage()
>     {
>       Map<String, Customer> customers = new HashMap<>();

     This line declares a variable `customers', creates a new, empty
HashMap, and sets `customers' to point at it.  Fine, but ... how long
does the `customers' variable exist?

>     }

     Only until this point: As soon as the Garage constructor finishes,
all its variables -- including `customers' -- cease to exist.  The
HashMap that was created is now "orphaned," eligible to be collected
as garbage, because nothing in the program refers to it any more.

>     
>     public void Garage()
>     {
>        Garage Garage = new Garage();
>     }

     This method does nothing of any use whatever.

>     public void addCustomer(String key, Customer cust)
>     {
>       customer.put("123ABC",  "Andrew Smith, address 1 The High Street, area 1" );

     First problem: There never is, and never was, a variable anywhere
in your program named `customer'.  Java has absolutely no idea what you
mean when you write `customer' here; you might just as well have written
`genghisKhansGrandmother'.  Naturally, the compiler complains.

     Second problem: If `customer' did in fact refer to a Map associating
Strings with Customers, would this line be correct?  No!  A String is
not a Customer, so you can't supply a String as the value; you need to
supply a Customer.

>       customer.put("456DEF",  "Janet Jones, address 145 Main Street, area 1");
>       customer.put("789GHI",   "Christine Charter, address 50 Lake View, area 2");
>     }
>             
> }
> 
> the customer class has already been wrote

     Long years ago I had a student who approached his programming tasks
by throwing together a mish-mash of half-understood code-like source
lines, pondering the compiler's complaints, and making random change
after random change until he chanced upon something the compiler would
accept in silence.  This code would seldom then do what was intended,
so he'd keep on making herky-jerky changes until it eventually printed
what he thought might be the right answer for one test case, then he'd
hand the result in as his homework.

     I cannot recall that he ever submitted a program that would run a
second test case correctly.  And it's my strong impression that he did
not learn how to program.  He was the student and I the teacher, and
the fault for his lack of learning could lie with either side, but for
selfish reasons I decided the problem was his and not mine.  I put it
down to his tendency to scribble code before he'd really thought about
what he needed to do, in hopes that fixing his compile-time errors would
somehow guide him over the rough bits.

     The compiler is not a tutor.

     The compiler will not do your studying for you.

     The compiler will not teach you how to program.

     I strongly suggest that you start over again at Lesson One, and
resist the temptation to rush into writing code before gaining some
understanding.  It's likely that your experiences so far may help
you get more out of the material a second time around than you did
on the first attempt -- but I sincerely doubt you'll make meaningful
progress with the level of understanding you've demonstrated thus far.

     Study diligently, and good luck!

-- 
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