• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Another way to write this code

 
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I got this code here, and I really just need to finish this last method. The addPackaging method in my Shipper Class is going to find the total cost of everything, and I have the estimatedShipping method which already gets my shipping. So to find the total its just adding the price to the shipping, but I am not allowed to be redundant in the addPackaging method. So Because I cant just copy my if/else from estimatedShipping method...what would be another way to do that? Would it to be to similiar if I used the if/else to get the baseCost, and then a switch for the estimated shipping? any suggestions would be appreciated.

ShipperClass
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's onclear what you trying to do, where is the price supposed to com from? And if you already have a method that calculates the estimatedShipping, why would you calculate it again, simply call the estimatedshipping from the addPackagingmethod
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, all classes are below. Its been a year or so since I have done java...so it would be like this?


MainClass


PackageClass



Shipper
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry was not paying attention. it would actually be something like?

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also don't understand why you don't follow naming convention for the EstmatePackaging method, you do it for all the rest, I would rename it to getEstimatePackiging.
Then it would be
But more likely
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just following the directions. Supposed to be named estimateShipping
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So did this answer your question?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! It’s been a while since I’ve done anything with Java, but it’s slowly coming back. Thanks to everyone
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, after reading the directions again to make sure I have everything I need...I am lost on the addPackaging method. Here is what it says "The addPackageShipping method first determines the amount that it costs to ship the package to the given zone. It then adds this amount to the totalShipped variable. It should also increment the total number of packages shipped using this method."

The stuff I am confused on are:
1) the estimated method already puts that cost for each zone into estimatedPackaging variable. So I am just moving the value from one variable to another? Plus I still need to get the final cost by adding it to my getPRice, since that is what is going to return the price. So the way I am reading those instructions it looks something like:



2) Again, the addPackages does not return anything, but in main I need to output the total number of packages for each of the three shipping methods they can choose. As far as the directions go there is no getter for total packages, and all output belongs in main. So how does that work? My thought is it would be easier to create 3 variables and increment them after my if which would look like:

 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More clarification on the first problem I am confused with. I think we came up with totalShipping = pkg.getPrice + estimatedShipping; for the add packaging
Here is where I am getting confused..the addPackaging does not return anything, so I cant get that total shipping cost sitting in the variable if I cant return it??
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As with many of the posts that involve homework, I'm not sure what is "given" and cannot change, and what you can modify.  If you can modify addPackageShipping() and you want to return a value, change the method signature to return that value's type and use the return statement in your method.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other possibility is the totalShipping is declared outside of any methods and therefore is in scope in all methods.  I wouldn't recommend this, but if it is "given" then you have to work with it.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 2 of you're addPackaging method you call the EstimatedPackagingmethod, but you don't put the return value in to a variable.
You can't add the value of a variable in to a methodcall.
It's unclear to me what you trying to do for zone 2. If you have to do the same for all if's, why bougther to make them.
Based on what I see you could change it to:


It's also unclear to me why you in your main scan for a next line after taking the int.
You als have to acount for the fact that the user could put in a different value the you expect.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:The other possibility is the totalShipping is declared outside of any methods and therefore is in scope in all methods.  I wouldn't recommend this, but if it is "given" then you have to work with it.



In your earlier posts you can see that totalShipping is a private instance variable, so indeed accesible for all methods, but without a getter or a method that returns it, still not reachable from your main.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double checked, and the addPackage method is void in type. The scan.line was there because I initially had it as a string before I changed it to an int, and didnt get rid of it I guess.

Here is what I have now...could be wrong, but this is what im understanding right now. So if I set totalShipping to my estimate method that is giving me just the shipping cost returned based on the zone chosen by the user? From there I add the price the user has entered giving me the total cost for everything?

And yes knute the totalShipping has a global scope

 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If my understanding of the above is correct im still lost as how to get into that variable to output.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the moment there isn't a way. There a 4 ways to acces the variable, of which you 2 shouldn't use. You could remove the private to make it public, run all the methods and then acces it by instanceName.variable, you could make it static and access it with className.variable (but your methed are instance methods, so without making them static to and run them first you value wouldn't change), you can acces it by a getter and the one that would make most sence it to return it by the method.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I’m just gonna have to make it a get method and get a few points off, and see what his solution is.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the full asignment, maybe we can figure out what you surpose to do.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:Well I’m just gonna have to make it a get method and get a few points off, and see what his solution is.


You will lose points for using a getter?  That doesn't sound right.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the full Assignment. Hopefully it works, because attachments wont take it.

https://onlcourses.tridenttech.edu/d2l/common/viewFile.d2lfile/Database/NTA5MjA2OA/Assignment-zero.pdf?ou=108026
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should work. Sorry.

https://www.scribd.com/document/369086493/Assignment-Zero-2Assignment Instructions
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your assignment is a getTotalShipping method specified.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.........are you freaking kidding me. I’ve been stressing myself out over this, and it was sitting there the whole time. God
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this look right? In my output in Main I have those 2 if/else statements for my shipping and total. I have those 3 shipper objects in main which stand for standard, 2nd day, and express. Im just afraid he will think the code is to redundant. I did try below just to get rid of one of the if statements, but didnt work.



Here is what it looks like in main to get the shipping and totalcost
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could get rid of two if statements.



You're also using the formatter wrong. check this on how to use it.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One minor but important issue: By convention, method names in Java start with a lowercase letter. Your specifications tell you to provide an estimatedShipping method, not EstimatedShipping. It's important because people who are used to reading code that follows this convention will be a little thrown off by the capitalized name, making your code a little less readable than it should be.

As for your if statements around the shipping method, you can greatly reduce the amount of code that you write in this case by using an array of Shipper objects instead of defining three distinct variables like shipA, shipB, and shipC. Then you can define constants SHIP_STANDARD, SHIP_2ND_DAY, SHIP_EXPRESS with values that correspond to an appropriate index in the array of Shipper objects.  There is nothing that I see in your requirements that won't allow you to do this, unless of course you haven't been taught to use arrays yet.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh I see. Well now I just have to get rid of my indexoutofbounds error and I should be good. Was working before, but I guess I did something line 93 looks like where its coming from
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of scan.nextLine use scan.next
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LInes 57-59 are useless. You're calling a method that returns a value but you aren't using the value to do anything. Then right below that, you call the same method again as part of a printf call.  And you're still writing more code than you would need to if you used an array of Shipper objects instead of three distinct Shipper variables.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be sure you only get one character you could set a delimeter first.
scan.useDelimeter("") and then scan.next()
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might also help if you streamlined your program prompts and outputs. Right now, your program will do this:

Something like this would probably make more sense:
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic