Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Create a phone book.

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i got a homework. I need to create a phone book that have
1. Search for the number of persons living in the specified city;
2. Search by name and surname;
3. Search by name, surname and city;
4. Sort by last name and city.
So this is my code. Don`t know what to do.
Knyga=AdressBook
Vardas=Name
Pavarde=Lastname
Numeris=Number
Miestas=City


 
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

I had a quick look at your list of seem to be requirements..

I'm wondering, why would you want to have a multiple instances of the book?

Feels like one entry in the Book (phone book) results in a new Book each time. That doesn't feel right when reading the requirement, i.e. "Search by name and surname", meaning in a book, and now you'd need to look for a specific book

The structure might not be off, but the naming is. Consider renaming to something along the lines:

PhoneBook
ContactDetails


So the PhoneBook would be a collection of ContactDetails. Makes much more sense, doesn't it?


And what's regarding the requirements, choose one and start from it as opposed to think about all of them at once.

1. Search for the number of persons living in the specified city;


So what thoughts come to your mind when reading such requirement?

Imagine you have (dummy non-compilable code) :


 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Shoudn`t it be like that? And why i`m getting errors on class Book.
Have at least 2 classes, for example option 1 would be "Book" and "...."
A constructor without parameters and a constructor with parameters
Methods for setting and scanning object properties
Scanning data from a file to an object list (ArrayList) and viewing it on the screen
Programmed search function reload
Extensive dialogue with the user
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adamas Abramovas wrote:Shoudn`t it be like that?


Be what?
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn`t mention those requirements
Have at least 2 classes, for example option 1 would be "Book" and "...."
A constructor without parameters and a constructor with parameters
Methods for setting and scanning object properties
Scanning data from a file to an object list (ArrayList) and viewing it on the screen
Programmed search function reload
Extensive dialogue with the user.
Did i atleast have a correctly direction. Or this is a bullshit?
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adamas Abramovas wrote:Shoudn`t it be like that? And why i`m getting errors on class Book.
Have at least 2 classes, for example option 1 would be "Book" and "...."
A constructor without parameters and a constructor with parameters
Methods for setting and scanning object properties
Scanning data from a file to an object list (ArrayList) and viewing it on the screen
Programmed search function reload
Extensive dialogue with the user


You seem to be too deep into technical details. Let's zoom out a bit.

Do you understand the problem at a higher level? After I tried to lean you towards the very first requirement as you said you don't know what to do, I did not get sense back from you that it actually became a bit clearer where you should start.
 
Saloon Keeper
Posts: 7381
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had a printed phone book, how would you (without a computer) count the number of people in a given city?
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:If you had a printed phone book, how would you (without a computer) count the number of people in a given city?


First of all i`d group them by city. If they are messed in list.
 
Saloon Keeper
Posts: 22628
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You seem to be too deep into technical details. Let's zoom out a bit.



It's a common failing - worrying about the technical details before you have the theory pinned down.

A good OOP program will model the real world. In the real world, you will typically only have one phone book per geographic area. Or at least only one official phone book - I used to get a second one from a private company when I lived in Orlando, FL, but the "real" phone book was the one from the telephone company. So start with that idea. Use one and only one Book to start with.

A phone book contains a collection of names and numbers. Note that I said "collection" Java has extensive support for collections of data. In particular, the abstract Java type for a collection where you have a key (name) that you want to look up a phone number (value) from, is java.util.Map. In some directories you can occasionally find multiple sorts, such as a section where you lookup a number by name and another section where you lookup a name by number. In that case, you'd have 2 Maps within the phone book. Fortunately, unlike a real book, you don't need 2 full copies of the data, since Maps hold references to the data rather than the actual data.

A TreeMap is a type of Map where you can retrieve the keys in sort order. You'd want to look at that because the keySet() returned from a generic Map doesn't guarantee that N will come after A and before Z, but TreeMap can - assuming that that's the sort order you defined for t.

I hope that gives some ideas.
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adamas Abramovas wrote:

Carey Brown wrote:If you had a printed phone book, how would you (without a computer) count the number of people in a given city?


First of all i`d group them by city. If they are messed in list.


Why would you want to group all the people by the city, if you need to know a number of people just in one particular city? i.e. as a one off operation.
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually don`t undersand. I thinking to deep this causes a lot of problems. I`m trying to search in youtube/forums some solutions but there like a lot of stuff that i didn`t learned.
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adamas Abramovas wrote:I`m trying to search in youtube/forums some solutions


Not sure what you exactly mean by solutions, but if you mean already baked off, then my friendly advice is - don't. You just simply block yourself from thinking and you'll be in frustrating searches all the time without a guarantee to succeed.
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I`m not baked off i just don`t know how to start. Like this is what i got atm:
/*
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Adamas Abramovas wrote:

Carey Brown wrote:If you had a printed phone book, how would you (without a computer) count the number of people in a given city?


First of all i`d group them by city. If they are messed in list.


Why would you want to group all the people by the city, if you need to know a number of people just in one particular city? i.e. as a one off operation.


Just to be clear. I'm not saying this is plainly wrong, all I'm saying is, so you'd think about alternatives, likely simple and dumb.

As been asked, how you'd search if that was a paper phone book where grouping is not an option? The simplest thing which comes to my mind is - just go page by page, contact by contact and whenever I see a person who lives in the city X, I just add +1 to the count. Now, depending on what the requirements are, you could consider your approach too.

 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one comment before I look at it more closely:


I presume that's the phone number. Don't try to represent phone number as an actual mathematical number. Keep it as a string as you won't need to do any arithmetics with it anyway.
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Search for the number of persons living in the specified city;


It is possible that you confusing requirement. Requirement asks how many people are in particular city. Not their number (which is phone, what you seem understood as).

Otherwise I could not understand what's your intention with this line of code:


In particular line 11 from this snippet.
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to do it with example. Which code is
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or it could be well that I misunderstand that. From the English stand point, what is more likely?
 
Liutauras Vilda
Marshal
Posts: 7778
536
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, but I'm confused with the code you posting. Every time seem to be significantly different.

Can we narrow down the problem so it is easier to discuss and follow? Or maybe ask concrete questions about the problem/code before it gets messy.
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. First of all i need make a 2 classses.  constructor with parameters and without.
Then methods to change parameters with set and get.
Data scan from arraylist
Program search reset.

1st. requirement. In a specified city living persons amount search.
2. search by name and lastname.
3. search by name lastname and city
4. Group by lastname and city
 
Tim Holloway
Saloon Keeper
Posts: 22628
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about code.

It's too early for code. Post ideas.

Don't talk about classes and methods, talk about book(s), telephone entries, search types and stuff like that. Talk about how you plan to arrange them (NOT in java terms, in English terms. Well, Russian's OK, but before you post it, please translate to English, since that's the language of the Ranch.

At its heart, programming is a set of stages of taking abstract ideas and repeatedly refining them until you have compilable code. A common intermediate step is "pseudo code", which is English (Russian) description of how the program works like it was in Java, but not limited to the precise structure and rule of Java. The way you'd tell an unskilled human to do the job.

Trying to go straight from the concept to somethine executable without passing those intermediate steps will make your task much harder and the resulting software will be of poorer quality.
 
Adamas Abramovas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:Forget about code.

It's too early for code. Post ideas.

Don't talk about classes and methods, talk about book(s), telephone entries, search types and stuff like that. Talk about how you plan to arrange them (NOT in java terms, in English terms. Well, Russian's OK, but before you post it, please translate to English, since that's the language of the Ranch.

At its heart, programming is a set of stages of taking abstract ideas and repeatedly refining them until you have compilable code. A common intermediate step is "pseudo code", which is English (Russian) description of how the program works like it was in Java, but not limited to the precise structure and rule of Java. The way you'd tell an unskilled human to do the job.

Trying to go straight from the concept to somethine executable without passing those intermediate steps will make your task much harder and the resulting software will be of poorer quality.


Okay First of all i`d make a phone book. Write name/lastname/number/city. Since i`ve got some peoples in my adressbook. i`d count how many peoples do i got and what are theirs name/last names where are they from. Then i`d group them by cities by names lastnames. This is how i think
 
Tim Holloway
Saloon Keeper
Posts: 22628
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out CRC cards: http://agilemodeling.com/artifacts/crcModel.htm

This is a popular way of formalizing basic requirements. There's a similar sort of diagram in the Unified Modeling Language (UML) as well, but UML can be a bit excessive for getting started.
 
Marshal
Posts: 70594
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pease spread your code out a bit. Put empty lines before each constructor or method. And a space before {.
Why have you written a no‑arguments constructor (line 15→line 19)? I don't think you want to have Student objects with empty Strings for names.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic