• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Help/advice with a client and class

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am stuck on a particular assignment on where we:

Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: a String representing the telephone number in 10 digits (e.g. 5168761234) with no punctuation. Include a default constructor, an overloaded constructor, the accessor and mutator methods, and the methods equals() and toString(). Your toString() method should return the telephone number with punctuation (e.g. (516) 876-4257 ). Also include a method that returns the area code (first three digits). If there are less than three digits then your area code method should return “unknown area code”. In addition, write a client class to test all the methods in your class.

I am confuse how on what I should use as my variables other than the phone.
I know my private String should be the phonnumber but what should do my double or final int?
I was going to use this as starting code for this assignment:

Class:


Client:

 
Ranch Hand
Posts: 679
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The specification makes no mention of billing so you need to get rid of everything related to that.
Start off with an empty class definition. Something like

and then go through the specification step by step to add the required features. Test everything before you go on to the next step.
 
Raina Yukoma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:The specification makes no mention of billing so you need to get rid of everything related to that.
Start off with an empty class definition. Something like

and then go through the specification step by step to add the required features. Test everything before you go on to the next step.



Thank you for the reply - that was just a sample code, here is what I started with:



The public private x2 part I am unsure what I should do for that in order to encapsulate the concept of a telephone number

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Further to what Stuart has told you, I would break it down as follows:

- Add a member variable number of type String
- Add a default constructor
- Add an accessor
- Write new tests in a TelephoneNumberTest class
- Add an overloaded constructor
- Add a mutator
- Write additional tests in TelephoneNumberTest
- Add equals method
- Write additional tests in TelephoneNumberTest
- Add toString method
- Write additional tests in TelephoneNumberTest
- Add getAreaCode method
- Write additional tests in TelephoneNumberTest

Even better than that, write the skeleton of the methods such as:

then write the tests for it and watch them fail. Then add the method logic and re-run the tests which should now pass.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the number and correctPhone variables ? You don't use them in your code so they shouldn't be there.
As I said, go through the requirements step by step and only add code/variables that are needed to fulfil the specific requirement you are working on.
 
Raina Yukoma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay
 
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
You have got some good advice there.
 
Raina Yukoma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Going to start working on most of it tonight, I took your advice and set it up something like this:


 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TelephoneNumerTest should be a separate class, not a method.
Why is your mutator method called setModel ?
 
Raina Yukoma
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got it, thanks guys. I got it to work I just need to test for it.

 
Campbell Ritchie
Marshal
Posts: 76462
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't write if (...) return true; else return false;
You can find what you should write in this style guide. You do realise that use of instanceof means an equals() method can fail if you extend the class?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getAreaCode method is wrong (reread the specification) and your toString method will throw an exception with any number of less than 6 digits.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raina Yukoma wrote:Include a default constructor, an overloaded constructor


You might also want to tell your teacher that it is not possible to have a default constructor and an overloaded constructor.
A default constructor is explicitly defined in the Java Language Specification as a constructor with no parameters that is added automagically by the compiler if you don't include any constructor of your own in the code.
If your code contains a constructor with no parameters then that is just a no argument constructor.

Of course, depending on how open they are to constructive criticism, you may not want to tell your teacher this
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic