• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

how to use Scanner on private variables?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
i am trying to use private variables to get the input from a file using scanner ( i hope that makes sense). can anyone help me?
+  i will put the question here:
Create a Java Application in NetBeans with the following two classes.

1. A class FoodProduct with the following private attributes:
a. Product Name
b. Price
c. ExpierationDate
d. NetWeight

The class FoodProduct should include the following methods:
e. Constructor/s
f. Accessors and mutators methods
g. Method discount(double sale) to change the price based on the sale value.

2. Another Java class called TestFoodProduct with the main() method:
a. In the main(), read from file “food.data” scores of several students. This is an example data:

Milk 10.50 19/06/2021 800
Bread 3.75 18/06/2021 300
Chicken 18.99 16/07/2021 1200

b. Create Objects for each food product

c. After creating all the objects, change the value of  expiration date of Chicken to 19/07/2021

d. Print the price of Milk using 5% discount



this is the code ::



-----------------------------------------------------------



 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a. In the main(), read from file “food.data” scores of several students. This is an example data:

Milk 10.50 19/06/2021 800
Bread 3.75 18/06/2021 300
Chicken 18.99 16/07/2021 1200


Those are some odd students in that school, and weird scores.

Seriously, you could add a method to FoodProduct that would take in a scanner and directly set the private variables, but the fact that you have mutator methods suggests that this isn't what is wanted (and would probably be a bad idea anyway).

TestFoodProduct can read the data file and use the data written to call the public FoodProduct constructor with the appropriate values.
While it is also possible to call the no-args constructor as you are doing and then set each value, that would not be as good.

They seem to want you to be looping thru the scanner input in the main function, getting the values for each member of each FoodProduct from it, and calling your constructor with those values.

Were you unsure that is what they want you to do, or just unsure how to go about doing that?

Again, directly setting the private variables without calling the setters or mutators can not be done from outside the class, but getting the values as local variables and then calling the constructor with those variables for each food item until the scanner has no more data would certainly work.
 
Saloon Keeper
Posts: 8449
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is terrible. First off variable and method names MUST begin with a lower case letter. Next, your names should be more descriptive. Don't be afraid of a little typing. And the assignment you used is not typical of setters which normally use "this.".


 
ghostlooos Om
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

This is terrible. First off variable and method names MUST begin with a lower case letter. Next, your names should be more descriptive. Don't be afraid of a little typing. And the assignment you used is not typical of setters which normally use "this.".


thank you i will fix the names but i can't use (this) since we haven't learned it yet
 
ghostlooos Om
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:

a. In the main(), read from file “food.data” scores of several students. This is an example data:

Milk 10.50 19/06/2021 800
Bread 3.75 18/06/2021 300
Chicken 18.99 16/07/2021 1200


Those are some odd students in that school, and weird scores.

Seriously, you could add a method to FoodProduct that would take in a scanner and directly set the private variables, but the fact that you have mutator methods suggests that this isn't what is wanted (and would probably be a bad idea anyway).

TestFoodProduct can read the data file and use the data written to call the public FoodProduct constructor with the appropriate values.
While it is also possible to call the no-args constructor as you are doing and then set each value, that would not be as good.

They seem to want you to be looping thru the scanner input in the main function, getting the values for each member of each FoodProduct from it, and calling your constructor with those values.

Were you unsure that is what they want you to do, or just unsure how to go about doing that?

Again, directly setting the private variables without calling the setters or mutators can not be done from outside the class, but getting the values as local variables and then calling the constructor with those variables for each food item until the scanner has no more data would certainly work.

I'm unsure on how to do it. Can you please explain more about the last point you made, getting the values as local variables
 
Carey Brown
Saloon Keeper
Posts: 8449
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 73760
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Removed from the Wiki forum since this isn't a Wiki‑type thread. Please don't use red writing since some people can't readily read coloured text; I have changed it to black.

I think you should use this.expiryDate because the whole idea of your coming to a forum is to learn something new. It is a common idiom used inside setXXX() methods and constructors and it allows your carefully‑chosen variable names to be exposed to other code.
You are neither opening nor closing your Scanner correctly. The correct thing to do is use try with resourcesLink. Remember you must close Scanners reading everything except System.in.Note:-
  • 1: It is no longer necessary to write close().
  • 2: It is probably preferable to use the modern code, Paths.get() rather than new FileReader().
  • 3: Use / rather than \\ and let the runtime convert that to \ at runtime.
  • 4: Your line is too long to be legible; the above shows how to break it into shorter lines.
  • Simply calling close() doesn't guarantee that the file will be closed, but try with resources does make such a guarantee. You didn't call close() at all.
     
    Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic