• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reference data type  RSS feed

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm a beginner in Java and we just started learning reference data types.
I have this lab that I need to do (I know it's really easy, but I'm still confused). Everything has been done in BlueJ.
The VehiclePurchase class has the following fields:   private Customer customer   private PurchaseDate  purchaseDate  private Vehicle vehiclePurchased  private boolean servicePackage  public static final double SERVICE_FEE = 500.00 

The single VehiclePurchase constructor looks like this:  public VehiclePurchase(Customer renter, PurchaseDate  purchaseDate, Vehicle vehiclePurchased, boolean servicePackage) 
Provide accessor methods for each field in the class. Note that the accessors for customer, purchaseDate and vehiclePurchased will return the relevant object types. There should be NO mutator methods for customer, purchaseDate or vehiclePurchased.Provide a mutator method for servicePackage.

Provide the method calculatePurchasePrice(double purchasePrice)that first uses the existing Vehicle method to validate the parameter. If the servicePackage field is true then the SERVICE_FEE is added to the parameter and the Vehicle sellingPrice is reset to the new value. 

What I did so far is :


and I'm stuck here.
I need also create object with main method.

Thank you!!


[HENRY: Fixed code tags]
 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Provide the method calculatePurchasePrice(double purchasePrice)that first uses the existing Vehicle method to validate the parameter. If the servicePackage field is true then the SERVICE_FEE is added to the parameter and the Vehicle sellingPrice is reset to the new value. 
Very First thing is:please use Code Tags.
you need to provide a calculatePurchasePrice(double purchasePrice) in your vehiclePurchase class.now instructor has provided you some policy(stated above in your quote) that how you really should write this method.your method has one parameter-"purchasePrice",he needs you to provide some method like validatePurchasePrice(double purchasePrice) in your vehical class which checks for the mentioned purchase price is true or not and should return true and false OR -1 or 1 respectively as you like.after then he needs you to calculate the purchase price as
--------->"purchasePrice=servicePackage?purchasePrice+SERVICE_FEE:purchasePrice"----this is a ternary operator,a kind of conditional operator.
Actually your problem is lacking various things like what should be the design and members in other classes--Customer,PurchaseDate..etc.
In the calculatePurchasePrice method what it is supposed to do if the parameter is invalid...etc if you have some extra info,please share those one also.ask your instructor about these classes(design and members) and situations.may be she will tell you to design these classes on your own,in that case it is appropriate to discuss them here.

hope it helps!

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to CodeRanch...
 
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, if the only thing Henry did to your post was to surround the code with code tags, then you are a very rare bird. We don't see many beginners around here who so meticulously align their code properly and neatly. Kudos to you for being nice and neat with your indentation and formatting.

Next, kudos also for being able to follow directions to a T. What you've written so far is about as close to perfect as can be expected from a beginner. The only thing I'd point out is that when it comes to boolean fields, accessors, aka getters, can have other prefixes like "is" instead of "get" to make their logical nature more apparent and so that code that uses it becomes more grammatically correct. Other prefixes you can use for methods that return a boolean are "has" and "can". You can even make negation more readable by using names like "isNotEmpty()" or "isNotOver()". 

In this case, I think "has" makes more sense as a prefix for a boolean servicePackage field. Just go with "get" for now but try to discuss the alternative with your professor and see if using it will be acceptable for your submission.

See how choosing good names can make your code much easier to read and understand?

Lastly, you haven't shown any code that does this but it's a common rookie mistake: don't compare booleans with true or false. These are redundant and demonstrate poor code style:
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome again to the Ranch!

As for where you are stuck, what is your understanding of what you are asked to do with the calculatePurchasePrice method? I didn't see any mention in the requirements you shared about a return value. What exactly does "the Vehicle sellingPrice is reset to the new value" mean? Does that imply that there should be a sellingPrice field in your class? Or should the calculatePurchasePrice have a local variable called sellingPrice that it calculates and returns as the result? To know how to properly write this method, you need to be crystal clear about what it's required to do. Or maybe the Vehicle object has a setSellingPrice() mutator that you need to call. You could make an educated guess but then we wouldn't be able to give you any definitive opinions about the correctness of what you did.

About that last possibility of the Vehicle object having a setSellingPrice() mutator, what's to keep anyone from calling calculatePurchasePrice() multiple times and keep jacking up the vehicle's sellingPrice? That is, the first time you call it, you could jack up the price by 500.00. What's to prevent someone from calling it 15 more times and just keep jacking up the sellingPrice? I know that's probably beyond the scope of this exercise but it's something to consider if you want to get an idea of the kind of things you'll have to deal with when you're writing real-world programs.

If you can get some answers to these questions, then you will have a better idea of how to write the code for that method.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may seem a little nitpicky but you're already doing so well you might as well add some "polish" to what you have.

You wrote:

That is fine but it would be even better with a little bit more spacing to give your words and operators some breathing room. Compare that with this more spread-out version:

Don't skimp on whitespace (spaces, blank lines, etc.). Judicious use of whitespace can make your code more readable. The computer doesn't care about whitespaces and it will take the following code without any complaints:

To the computer, these instructions are exactly the same as the other set that has been formatted so that it's easy for *people* to read it.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One last nitpick before I stop (for now) and wait for you to respond about those requirements clarifications.

When you have a long list of parameters like that in your constructor, you can make the code span multiple lines. Just indent the subsequent lines a couple of levels to make it clear that they are a continuation of the previous line. Also, I've modified the JavaDoc comments to conform to  the officially recommended style for these types of comments. There are more details you can put into the JavaDoc comments but I'll leave those out in this example.

Notice that the last two parameters have been wrapped to the next line but indented an extra level so that they don't align with the code in the body of the constructor. Your style for placement of braces makes for a natural separation between the method header and the method body but other common styles (like the one that I use) would need a blank line to help visually delineate the parts from each other.

You can read more about various indentation styles here: https://en.wikipedia.org/wiki/Indent_style
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . You can read more about various indentation styles here: https://en.wikipedia.org/wiki/Indent_style
. . . If you have a week to spare

And welcome again.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote:. . . You can read more about various indentation styles here: https://en.wikipedia.org/wiki/Indent_style
. . . If you have a week to spare

Yeah, there are many of them and people will usually fall in one camp and stay there. For Java, the main camps are K&R and variants, Allman, and Horstmann. It doesn't really matter much which one you chose as long as you stay consistent in the same program or code base. Outside of having no style at all, there are few things more annoying than seeing mixed indentation styles in the same codebase.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . . It doesn't really matter much which one you chose . . . .
It might, if you work somewhere that has a style guide mandating a particular indentation style. I remember a long time ago somebody posting instructions for an assessment which included strict details about indentation, even saying three spaces per level. But I agree, if you aren't mandated a specific style you must be consistent with yourself.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote:Or maybe the Vehicle object has a setSellingPrice() mutator that you need to call. You could make an educated guess but then we wouldn't be able to give you any definitive opinions about the correctness of what you did.

About that last possibility of the Vehicle object having a setSellingPrice() mutator, what's to keep anyone from calling calculatePurchasePrice() multiple times and keep jacking up the vehicle's sellingPrice? That is, the first time you call it, you could jack up the price by 500.00. What's to prevent someone from calling it 15 more times and just keep jacking up the sellingPrice?

The more I think about that, the more I'm convinced that was the intent of the instructions. Regarding the matter of preventing multiple calls to calculatePurchasePrice() from inadvertently making the vehicle's selling price skyrocket, here's what I would do: 

Have a private boolean sellingPriceAdjusted field in the VehiclePurchase class. This should be set to false in the constructor. Actually, if you just declare it as a field, it will be set to false by default upon instantiation but it's better at this point that you set its initial value explicitly in the constructor. That way, your intent will be clear. When the calculatePurchasePrice() is called, I would check sellingPriceAdjusted. If it's still false, then you go ahead and adjust the price and then set the sellingPriceAdjusted field to true. In any subsequent calls to calculatePurchasePrice(), the initial check will detect that sellingPriceAdjusted is true and it will just exit from the method without doing anything. This kind of check at the start of a method is called a "Guard Clause".
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

Thank you for you welcoming and all your replies. They are much appreciated.
Sorry yes, I didn't give you, guys, much info.
I have 4 classes (Customer, PurchaseDate,Vehicle and VehiclePurchase).
I'm working on VehiclePurchase right now, it's the only class that has reference data type and invoke methods from other classes.
Method calculatePurachasePrice invoke method in existing Vehicle class  - validate sellingPrice and then if service is true, it's added to sellingPrice and assign to sellingPrice field
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's what I have been trying to do:

 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the last part of my first reply to you, the part about not making a rookie mistake which you apparently made right on cue.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't assign a value to a method call (line 4). That's not how it works. You have to pass the value to the method as a parameter. That means the whole expression on the right side of the assignment operation needs to inside the parentheses that comes after the method name on the left side.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu,

It is how we were tought in class, I'm pretty sure that isn't a main mistake that doesn't let me run the code.

Thank you though!
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally, if you declare a method to have a void return type, you can't contradict that by writing what you did on line 6, a statement that tries to return a value. At most, you can put just return without anything after it as your last statement in the method.  You could also just not put any return statement at all and let execution just naturally "fall out" of the method when all the statements in it have been executed.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still doesn't work "method checkSellingPrice in class Vehicle cannot be applied to given types"
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, by ”it's how we were taught” are you referring to comparing a boolean with true or false? Or trying to assign a value to a method call. The former is just sad because they would be teaching you bad form. The latter would be atrocious because that's just plain wrong.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how we were taught is about comparing boolean with true or false.
Thank you
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:Still doesn't work "method checkSellingPrice in class Vehicle cannot be applied to given types"

Please show the code you tried that still doesn't work.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The complaint is about that class being inconsistent with the Vehicle class. So, to resolve the issue, you need to show use the Vehicle class too.

Henry
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Think about what the compiler error message is saying. Then look at your code.  Does it make sense to try to add a number and a boolean? How do you add "true" to $5321.00? What value are you supposed to get, *really* $5321.00? And what about adding "false" and $5321.00, are you supposed to get *not really* $5321.00? There's no such thing.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think logically: if the purchase has a service package included, then what are you supposed to add to the vehicle purchase price? Go back over what you wrote already. It's right there, kind of SHOUTING at you.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got that I was supposed to do this:




but mistake is still there
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a problem with invoking method checkSellingPrice and I don't understand what it is.
I don't think put in () is right.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Vehicle class checkSellingPrice() method takes a single double parameter. Your VehiclePurchased class is using that method with no parameters. This mismatch is what the compiler is complaining about.

Henry
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out, but what should be in there?
Not number for sure
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and it's also saying "void" type isn't allowed here
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, you have to think logically and understand what your code is doing.

You have this in Vehicle:

Do you understand what that's doing? It's declared as a void method, therefore it will return *nothing*, not even 0 or null. 

You can't add void to anything, which is what you're trying to do with this:

Nor can you call a method that expects a number and try to give it void, like what you're trying to do with this:

Again, stop and think logically.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used void return type because I'm trying to set value here, I assumed it will be same as in any set method
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote:

Were you taught to do this _sellingPrice thing, with the underscore in front of the name, too?  Again, I find that sad. This is not even acceptable style in many places. This is the way most places would have you write that:

When you have a parameter that has the same name as a field, you use the this keyword to differentiate between references to the field and references to the parameter.
 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now we are using this. , but before as we are very very beginners I used _sellingPrice, thanks though for letting me know
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:I used void return type because I'm trying to set value here, I assumed it will be same as in any set method

I'm not telling you *that* is wrong. Mutator methods *are* usually declared as void because they do something and there is no expectation for them to give anything back.  It's like you telling somebody to do something with the understanding that they will just go do it without giving you back anything. Like if you ask someone to write down a number, you don't expect them to come back and tell you "5!".  That's the "conversation" that a setXXX() method represents.

My point about it being void is that void is not a value that you can try to add to something or pass to something as a parameter value.  If a method expects a number, you can't give it void. If you are trying to add something to a number, you can't try to add void to it. Therefore, things like the examples below are not allowed:

 
Kristina Hawkins
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thank you.

Can you, guys, explain to me please how exactly I need to fix the code, because I'm completely lost here.

Thanks,
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kristina Hawkins wrote:
Can you, guys, explain to me please how exactly I need to fix the code, because I'm completely lost here.

I think we already did the best we could without doing your work for you. Go back and read over the replies you have so far. Maybe it will come to you if you try again.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, one more hint. You have this:

The compiler is complaining that you're not passing anything to the checkSellingPrice method when you try to call it. This code says that it needs a number to do whatever it does. That means you need to call it this way:

What makes sense here? What number should you be passing to the method when you make this call? How do you get (← hint!) that number?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!