• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Health Records Profile Program

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I really could use help on an assignment to code a health record program to collect info on a patient that inputs data about themselves (name, birthday, height, weight etc.) then calculates their BMI and displays all the values in the main method. The programming requires it be be in two classes with input data as private variables using constructors to pass the info around and a main method to display all the values. Anything can be put in for String variables like their name, but for a number variable like age must be within a set range, and if the answer isn't in that range, the user will be prompted on loop until it is. Two methods are public, one to read values and the other to display them, and the setter and getter methods and the BMI calculation methods are private.

I only have this so far:

import java.util.Scanner;


It's been hard to wok with constructors, and trying to work at it, could only code the beginning part of the program. Also the instructor requires students to only use Netbeans though I've only been working with BlueJay up until now. This isn't to rush for an answer from anyone, but any help with the program would very much be appreciated, since its due tonight.
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is  not the signature of a constructor, it is a method. A constructor has no return type and the name is the same as the name of the class. Example:
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What two classes do you intend on providing? I could see your HealthRecord class really being a Patient class. You'd still need a class to add and display Patients.
 
Roselyn Bordette
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the method name and the return type, and the two classes are  HealthRecordDemo, the main method, and HealthRecord, the implementation with all the class methods in it.
 
Roselyn Bordette
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, again. I've reamped the code and have looked through many of the underlying issues, but still am confused on the next steps. Here is the code so far now:



The empty parts are the ones that I'm not sure how to start, but everything else so far is finished, even though the program compiled with errors in certain areas.
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a number of problems with this.
  • The method name begins with 'set' which may lead the reader into thinking that it is a 'setter'. It is not and should be named something else, perhaps 'inputFirstName()'.
  • The method takes a 'firstName2' parameter. It should not. Everything in this method can be done with the 'firstName' field.
  • Your 'scan' variable should be a constant built with:
    public static final Scanner SCAN = new Scanner( System.in );
  •  
    Roselyn Bordette
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, the scanner part of the code has been changed, and according to assignment, there should be 8 setters for each instance variable, and each one should validate user input. That's why I thought that the object should be there then check if its correct or not within each method. If that is wrong then that code should be in the readInputs method instead, but I wouldn't know how to send each variable from that method to each method since a method can only return one value.
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • I can't for the life of me see why you'd need TWO setters for each field. Can you explain?

  • If a setter is supposed to validate the parameter passed in, are you going to throw an exception if it's not valid?
  • Please don't get user input from inside a setter method. That is not what a setter does.
  • What should 'readInputs()' do?
  •  
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
     
    Roselyn Bordette
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The code only has one setter for each variable but the setter was coded to take in inputs from the user instead, which I suppose the readInputs method should be taking care of instead. The assignment did explain that each private method did have to validate user data and reject it if it wasn't within a certain range, so that's why the inputs where being scanned in each setter method.
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suspect that you haven't gotten to the point where you can run and test what you have so far. In general, when you develop code you try at all times to have a compilable and runable program. This involves an iterative approach, implement one feature, get it to compile, test it, and only when it is successfully tested do you go on to add the next feature.

    You've got a lot of code now that hasn't been tested. Had you tested it as you went along you wouldn't have copied the same errors to many methods. Consider setting this code aside and start over. Implement all the code necessary to compile and test 'firstName' only. Make sure your tests include scenarios that test for invalid inputs, such as an empty String for 'firstName'.
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm making some assumptions here because your requirements leave room for interpretation, but something like this would be the template for all the things you'll need in order to implement the 'firstName' feature.
     
    Marshal
    Posts: 65466
    249
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Norm Radder wrote:Also posted Here: . . .

    Good point. OP: please read this. Give such notifications on every forum involved.
     
    Roselyn Bordette
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ..I honestly wasn't sure whether or not to come back to this post after working out the code with an instructor and then seeing the new replies come in and with them had figured what was wrong. But, it seemed to be that the program wasn't coded right to the point that it wasn't even using setters and getters properly and couldn't even compile. The constructor was removed, and the final printer scan wasn't needed, but everything else was worked through already, it was just confusion on encapsulation being put together correctly.  Also, using one variable at a time to test if the variables and their values passed through and the object to send data is good for debugging and did help.

    And about the 'crossposting', I was pressed for time when the topic was put up, and didn't want to wait on one source for an answer when someone else instead could give one on another website. It seemed better than repeatedly refreshing the page on one site while running out of time. I wasn't aware of it being considered bad practice to do..

    Overall, the new code works, here's the end result.


     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Very good. Nicely formatted too.

    Your 'scan' should be declared as:
    You don't want a new Scanner every time a new HealthRecord is created.
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Because this method is an instance method the passing in of height and weight is not necessary, it already has access to these member variables.
     
    Sheriff
    Posts: 6191
    164
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Roselyn Bordette wrote:And about the 'crossposting', I was pressed for time when the topic was put up, and didn't want to wait on one source for an answer when someone else instead could give one on another website. It seemed better than repeatedly refreshing the page on one site while running out of time. I wasn't aware of it being considered bad practice to do..


    At the Coderanch it's acceptable, only if you disclose the fact and post links to the other threads.  However, some sights ban the practice altogether.
     
    Sheriff
    Posts: 13729
    228
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To add to what Knute said, we encourage being transparent about crossposting because we're all volunteers here and letting us know wherever else you've posted the question helps us avoid spending time trying to help you when you've potentially already been helped elsewhere.
     
    Campbell Ritchie
    Marshal
    Posts: 65466
    249
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It also allows users on both websites to compare the advice given, but everybody would need to know about the crossposting.
     
    Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
    create, convert, edit or print DOC and DOCX in Java
    https://products.aspose.com/words/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!