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


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

Re: hash map

From Martin Gregorie <martin@address-in-sig.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: hash map
Date 2017-04-17 23:14 +0000
Organization A noiseless patient Spider
Message-ID <od3i84$i6i$1@dont-email.me> (permalink)
References (6 earlier) <8a53e56c-5877-4c31-976f-5802f926f44a@googlegroups.com> <od2koi$gie$1@dont-email.me> <1bcffbc0-a529-4185-87f2-8e1d66bf319d@googlegroups.com> <od2p1h$uu7$1@dont-email.me> <92da5574-f874-49a4-9a70-492a154f166e@googlegroups.com>

Show all headers | View raw


On Mon, 17 Apr 2017 09:32:07 -0700, pablo wrote:

> On Monday, 17 April 2017 17:07:28 UTC+1, Eric Sosman  wrote:
>> 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.
> 
> I will take that on board Eric about your old Student and i understand
> that i shouldn't rush but to be fair i am 40 years of age now and i want
> to learn this is why i am looking for a tutor aswell

If you are essentially teaching yourself to write programs for your own 
pleasure and to understand the way to do it, you can do a lot worse than 
to get a copy of "The C Programming Language" edition 2 by Brian Kernighan 
and Denis Richie, download a C compiler and work your way through the 
book, writing, compiling and running the examples in each chapter. This 
is, quite simply, the best written book on learning to program that I 
know, and as a small (270 page) paperback its not a huge investment.

If you stick with C and, after that, read and work your way through "The 
Practice of Programming by Kernighan and Pike, you'll not only know how 
the program, but how to write readable, well-structured programs and how 
to make them easy to test as well. 

At that point you'll be equipped to learn and work with different 
programming languages and have the basis for using them fro solving 
almost any programming problem.

I know this is off-topic for this newsgroup, but if I knew any book that 
covered programming in Java as well as "K&R C ed. 2" covers C I'd have 
recommended that.
 

-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

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