Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with an assignment  RSS feed

 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get my child classes constructors to pass an array of info to my parent classes and I am bit stumped on it.

Still a work in progress, any help is greatly appreciated. I am a bit confused on how to try and save more than one piece of information to a single
index of an array.


 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code, as is, won't compile. I suggest you start there.

Your child classes can invoke the parent's constructor like this.
I'm not sure why your parent constructor wouldn't take a rate parameter as well. The child constructors can just pass in hard coded values to the parent constructor for rate.
I don't quite understand what array you are trying to pass from child to parent. This is not appearing in the code you've posted.
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array is supposed to contain an address and subscription type, I am still building it.
I am trying to figure out how put two arguments into one index(address, contract).
Thank you for the help with constructors I am changing a lot of the code!
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest making a helper class, e.g. "AddressContract", that you can use to create an index array from. Once you have this array, how do you intend on using it?
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array is going to hold info for each subscriber and will need to be checked against the other subs to make sure that none of the addresses overlap.
The constructor from newspaper says that it's undefined, and I must invoke another constructor
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the constructor part now, I just needed a blank one.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Landon Simmons wrote:I got the constructor part now, I just needed a blank one.

I'm not sure you do; and I'm particularly not convinced that you need to use setters to solve it.

It seems to me that you're missing at least two classes here: Address and Contract, because they really aren't just Strings (you can read more about that here).

The nice thing about putting them in classes is that even if they are initially just Strings, you can add to them later (eg, for an Address, separating out the postcode), and that you can have more than one subscriber at the same Address.

Another stylistic point: NewspaperSubscriber is a bit of a mouthful. What about just Subscriber? Quite apart from saving you from CTS, having a more general name allows you more scope for development - for example: you could add a type, which is the "thing they subscribe to", allowing you create Subscriber<Newspaper> or Subscriber<BookClub> objects.

Probably not worth changing now; but maybe something to think about.

HIH

Winston
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Landon Simmons,

Apart from a lot other advises, I have couple from my side.

1. Comment "//parent", "//child". Better probably not to use such terminology. Better change parent to base class or super class (that is why you call constructor "super(...)", not "parent(...)". Same as accessing other super class members.
2. Variables as a, c and r. All such variables just increase amount of cipher text in your code, which makes your code way more difficult to read. That should be changed at least to, i.e.:
3. Indentation not good enough. You should be leaving an empty line in between successive methods. i.e.:
But not as below:
4. Method toString(). Use annotation @Override, which tells the compiler that you are intended to override a method in the superclass. In case there is no such (most likely because of typo), you'd get compiler error.
5. Method setRate. There are some values in two separate classes. In one you use 4.5, in another 2. That is something not good too. Consider passing that value as an argument, and the value itself as a self describing variable. Most likely a constant. silly example:
6. This is something very unconventional too. For the loop control, usually are used i, j and k variables. I'd suggest to stick to the standard idiom rather than improvise. Variable newSub doesn't reveal its intention, some kind of poorly chosen. Better longer name, but self explaining rather then cryptic one (new subscriber? news subscriber?) 7. Where that number 6 come from? Why number 6, why not 8 or 44?
It seems I end up with more than couple of insights. I am sorry if I made more work for you
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cleaned up my code a bit check it out! Thank you for all your advice!

/
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will always be true, and you have this in two places in your code.

 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought because I was passing the two different objects that the equals method would check two different addresses?
I am new to java and appreciate any advice.
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you need to change this

to

or, better yet

Remember that variables should begin with a lower case letter.

I think you need a class to manage a List<NewspaperSubscriber>. You could have this class use an array instead but that takes more work to manage how many array elements actually hold a subscriber. I would have called this class "Subscribers" but you've already used that name. Perhaps SubscriberManager. In this new class you have a method to add a new subscriber and the first thing it does is go through your List (or array) of subscribers to see if it is a duplicate.
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not understanding how to pass the seperate addresses to the boolean statement.
 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because your equals method is a non-static method of a class, you always have implicit access to the class's fields. You could write this.address but using "this" is redundant in most cases. However, when you use "address" there is only one unless you qualify it. In your case you need to compare two addresses, one belongs to the object you are operating on (i.e. this) and the other comes from the object you are passing in as a method parameter (i.e. secondSub). So, in order to get the SECOND address you need to qualify it by putting the name of the object reference (secondSub), a period, and then the class's field name.

Perhaps this would help https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Not only is that always equal, even if you changed it to:

it will still be always equal, as you have just set the attribute 'addresss' to the parameter 'address', so they are the same.

What exactly are you checking for here?
This is not the place to be checking for duplicate addresses as the NewspaperSubscriber only knows about a single address, and that is the address supplied in this constructor.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Landon Simmons, if I were you, I'd step back a little bit and would comment out mostly of the code in all classes by leaving only constructor and method toString in one of the chosen class.
1. THEN, in the class where main method is, would instantiate one of the class and see with toString method if all fields were initialized as expected.
2. After you make sure constructor works as expected and all instance fields gets their desired values, would add 1 setter method AND would use the same technique: [1] Create an instance, [2] use toString, [3] set to different value, [4] check with toString if result is desired.
3. Add 1 get method, then take similar actions in order to be sure method is working as expected.
4. Look back again, think, maybe you missed something, consider if you need any checks prior setting value, in case "yes", go back to step 2 and repeat until this step.
5. Then would need to do the same with each of the method in this class, later exactly the same with another class.

That would give you a confidence about the correctness or opposite about your classes. That is called unit testing. Of course there are other techniques to do that in the right way, but at the moment, the way I mentioned earlier would be sufficient in my opinion.
 
Landon Simmons
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you I am going to try that, didn't know about that and I can see where the code went wrong.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Also you got your main method very long. Check this FAQ written by Winston (not Churchill, but influential too), so you'll see the reasoning in details there.
  • After you read that, question is what to do with all that code which is laying down currently in main? Decompose into smaller methods, i'm sure you can. One of them could sound getUserInput("message"), another maybe defineSubscriberType maybe different name, but something what would reveal your intension.
  • I see you still using x for loop control. This is something unconventional too (I think I mentioned that earlier), you can check coding convention here (very old document, but still widely applied).
  • Why you got there few empty else blocks? If not needed, remove them. You should not keep the code which is not going to be used.
  • check below, and think which is clearer for your
  • It seems everything are tinny details, but rather important.
     
    Landon Simmons
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the x and y is the form my book / teacher uses. I was pretty shocked at all the code that I didn't need in the super class alone

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!