• Post Reply Bookmark Topic Watch Topic
  • New Topic

stuck on constructor with parameters  RSS feed

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain how to use a constructor with parameters where the user inputs the information? I'm doing a problem where I create a Delivery class that accepts arguments for the year,
delivery number within the year, distance code (1 for short distance, 2 for long), and weight of package. The constructor is supposed to also determine the eight digit delivery number (combining the year and delivery number, like 20140054 for this year, package #54).

I know I'm not close to being done but I'm struck on the application with the constructor parameters. If I'm asking the user to input the information, does that mean I have to create a no
argument constructor so it will compile? Right now it won't compile because it's asking for the parameters but I can't put them.

This is the class:


This is the main:


And the error is:

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to create the instance before you have the information needed to construct it?
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why are you trying to create the instance before you have the information needed to construct it?


Like this?

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, have you tested it? Change the last line of code to read
System.out.println(new Delivery(year, delNum, code, weight));
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well, have you tested it? Change the last line of code to read
System.out.println(new Delivery(year, delNum, code, weight));


I tested it and it compiles and runs but when I put in 2014, 6666, 2, and 89 at the prompts it came back with:



I'm really lost at this point. I feel like I'm close but don't understand what I'm missing.
 
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don;t explicitly define a toString() method in your class, Object#toString will be called. Define your own toString() method to display the key attributes of your class as you would like.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:If you don;t explicitly define a toString() method in your class, Object#toString
will be called. Define your own toString() method to display the key attributes of your class as you would like.


I'm new to all this and have never done a toString() before. I spent a lot of time last night looking it up last night and understand it's used often and what is being generated in the
above code is a hash tag (I think). Can you explain the format for them and where they're put? Would I do it immediately after the constructor in the class, at the bottom, etc.?
Or, can you give me some websites that explain it? I looked at the Oracle page but I do much better with examples. Thanks for any advice.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:Or, can you give me some websites that explain it?

Sure!
link 1
link 2
link 3
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote: . . . what is being generated in the above code is a hash tag (I think). . . .
Another thing you can do is look for the sample chapter for Joshua Bloch's Effective Java™ (old edition: may be hard to find), which is chapter 3. That should tell you about toString, too.

I am afraid you have misread what you found on the net. You are not generating a hash tag, which is a name with the hash sign # attached. You are displaying the class name, the at sign (@) and the hash code, which is a number here shown in hexadecimal. You usually want to return something different.

Come to think of it, if you find ch 3 of Bloch, it tells you about hash codes, too. Not quite up to date, but little has changed.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good links, PP
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how do you know where to put the toString() and what to put in it? In the problem above would I put it after the public void displayFee and just copy the println statements? Does it always have to be right after the constructor? I don't get what I would put in the toString() if I have my print statements after the displayFee.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Order of methods is moot. You can define toString() anywhere a method in the class can be defined.

The method should return a string that's what you want to print out when the class is include in a print (which calls toString() implicitly).
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You put the toString() method (like any other method) anywhere within the class at "top level" (I mean inside a class and not inside anything else).
The toString() method should not use print statements. It should create and return a String object. And what should be inside this String? Well... it is you who should know it.
Read carefully whats in those links I posted before. There you can find examples how toString() method should look like and what it should do.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I did it but am unsure how to use the "fee". Do I have to make a toString() for that too? I'm actually pretty happy right this second because I've spent hours on this
and it seems to work. Thanks so much for everyone's help. This is my modified stuff:



main:



And output:



 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have been told, you can put methods anywhere in the class, but not inside other methods. There are suggestions about the order they should be put in, for example, here, and here (§1.3). Those are suggestions and it is a good idea to follow a consistent convention about everything in your code, but they are not compulsory.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate everyone's help but I don't get it still. Thanks for the advice.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I've figured everything out except one thing. When I do the if else statements I'm getting an error (at line 10) that I can't have && for 5 and 20. But, I don't know how else I would code that
since if I do >=5 in one line and <=20 in another it will always come back with everything over 5 and not move to the other values. Is there another way to do it?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the syntax of your operators.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

Of course now everything compiles but when I put in "2" for code it always results in a fee of 0.

I'm dumb.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got your code indented the way you want it to work, rather than the way it actually works. If your editor allows you to do so then you should press the key that automatically indents your code. If not...


 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You've got your code indented the way you want it to work, rather than the way it actually works. If your editor allows you to do so then you should press the key that automatically indents your code. If not...




Thank you. I got it to work about ten minutes ago. It's just frustrating how much time I've spent on one problem. Wish it sank in with me more.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, it takes a while. Right now you're trying to take in a lot of random-looking unrelated things which don't necessarily seem to have any rhyme or reason. When you're getting started every program is like a house of cards, it can fall down for a hundred trivial little reasons. Eventually it starts to make sense, but it does take practice.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the more blood sweat and tears you can see somebody put into it, the more satisfying it is when the penny finally drops and they get it right
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!