Results 1 to 19 of 19
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default BMI calculator using 2 classes.

    BMI calculator using 2 classes.-k48pxt.jpg


    ok. So there is instruction.. I started writing code but I don't get this part.

    class HealthProfile
    Java Code:
    import javax.swing.JOptionPane;
    
    public class HealthProfile
    {
    	String fName;
    	String lName;
    	String gender;
    	int bDayMonth;
    	int bDayDay;
    	int bDayYear;
    	int age;
    	double height;
    	double weight;
    	double BMI;
    	String inputString;
    	String inputString1;
    	
    	public HealthProfile()
    	{
    	}
    
    	public void setFirstName(String fN)
    	{
    		fName = fN;
    	}
    	
    	
    	public void setLastName(String lN)
    	{
    		 lName = lN;
    	}
    	
    	public void setGender(String gen)
    	{
    		 gender = gen;
    	}
    	
    	public void setBirthMonth(int bMonth)
    	{
    		 bDayMonth = bMonth;
    	}
    	
    	public void setBirthDay(int bDay)
    	{
    		 bDayDay = bDay;
    	}
    	
    	public void setBirthYear(int bYear)
    	{
    		 bDayYear = bYear;
    	}
    	
    	//get patient's first name
    	public String getFirstName()
    	{
    		return fName;
    	}
    	
    	//get patient's last name
    	public String getLastName()
    	{
    		return lName;
    	}
    	
    	//get patient's gender
    	public String getGender()
    	{
    		return gender;
    	}
    
    	
    	//get patient's month of date of birth
    	public int getBirthMonth()
    	{
    		return bDayMonth;
    	}
    	
    	//get patient's day of date of birth
    	public int getBirthDay()
    	{
    		return bDayDay;
    	}
    	
    	//get patient's year of date of birth
    	public int getBirthYear()
    	{
    		return bDayYear;
    	}
    
    	//get patient's height in inches and weight in pounds
    	public void display()
    	{
    		inputString = JOptionPane.showInputDialog("What is patient's height in inches?");
    		height = Double.parseDouble(inputString);
    		
    		inputString1 = JOptionPane.showInputDialog("What is patient's weight in pounds?");
    		weight = Double.parseDouble(inputString1);
    		
    	}
    	
    	//calculate patient's BMI
    	public void calcBMI()
    	{
    		BMI = (weight*703)/(height*height);
    	}
    	
    	//calculate patient's age
    	public void calcAge()
    	{
    		age = 2013-bDayYear;
    	}
    	
    	
    }
    Project7
    Java Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    
    
    public class Project7
    {
    	public static void main(String[] args)
    	{
    		String inputString;
    		String inputString1;
    		
    		//declare identifier for HealthProfile Objoect
    		HealthProfile patient1;
    		//create HealthProfile object by calling its constructor
    		patient1 = new HealthProfile();
    		//input patient's first name
    		patient1.setFirstName(inputString = JOptionPane.showInputDialog("What is patient's first name?"));
    		//input patient's first name
    		patient1.setLastName(inputString = JOptionPane.showInputDialog("What is patient's last name?"));
    		//input patient's gender
    		patient1.setGender(inputString = JOptionPane.showInputDialog("What is patient's gender?"));
    		//input
    		patient1.setBirthMonth(5);
    		patient1.setBirthDay(3);
    		patient1.setBirthYear(1920);
    		
    
    		
    		String fName = patient1.getFirstName(); 
    		String lName = patient1.getLastName();
    		String gender = patient1.getGender();
    		int bDayMonth = patient1.getBirthMonth();
    		int bDayDay = patient1.getBirthDay();
    		int bDayYear = patient1.getBirthYear();
    		patient1.display(); //call HealthProfile to get height and weight
    		patient1.calcBMI(); //call HealthProfile to calculate BMI
    		DecimalFormat formatter = new DecimalFormat("#0.0");
    		
    		//print out the result	
    		System.out.println("Patient First Name: " + patient1.getFirstName() + "\n" + "Patient Last Name: " 
    		+ patient1.getLastName() + "\n" + "Gender: " + patient1.getGender() + "\n" + "Weight in pounds: "
    		+ patient1.weight + "BMI: " + formatter.format(patient1.BMI) + "BMI VALUES\n" + "\n" +
    		"Underweight:	less than 18.5\n" + "Normal:	between 18.5 and 24.9\n" + 
    		"Overweight:	between 25 and 29.9\n" + "Obese:		30 or greater");
    		
    		
    	}
    
    
    }
    Here's my question. How do I make those JOptionpane inputDialogBox to accept my integer input on project7 application? I know how to do it if it was done on same class.
    I could do it like code below but obviously, I can't apply same method there. Code works now because I just put numbers there but I suppose to put dialog box that
    accepts input number there according to instruction. right? Or am I misunderstanding something here and doing this totally wrong? Help me out here. :(
    Java Code:
    inputString = JOptionPane.inputDialog("What is your age?");
    age = Integer.parseInt(inputString);
    Last edited by shin777; 07-15-2013 at 10:02 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: BMI calculator using 2 classes.

    I don't get your problem. What you propose works just fine when you add error-handling to deal with people not inputting a number.

  3. #3
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    I have to make line 23-25 on Project7 application so they accept integer number with JOptionPane. I can't get it work with old method that I've been using. How am I suppose to
    put those two lines in one parenthesis? :(
    I currently left age calculation part out on print out result because I couldn't get those line 23-25 working with JOptionPane. Program runs fine and produce the result otherwise.
    I am not so sure this is what instruction is asking me to do though. It just looks a bit weird to me.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    the JOptionPane method is JOptionPane.showInputDialog not inputDialog. The parsing way is fine, you could also add exception handling

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    Aa sorry, just saw your issue. Why don't you make a method getInt() ? that parses it or save the input to variables and pass them to your methods

  6. #6
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    yeah.. it was type.. as u said, it is showInputdialog, not inputDialog.. What my question was, How do I apply it here?

    Java Code:
    patient1.setBirthMonth(inputString = JOptionPane.showInputDialog("What is patient's month of birthday?"));
    obviously.. if i put bDayMonth = Integer.parseInt(inputString); next line.. it won't work. I am guessing I have to somehow
    put it in same parenthesis .. where and how do i put Integer.parseInt(inputString) there on above code? That's what I was asking.

  7. #7
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    It is not necessary to do that, unless you really want to for some reason.
    If you want to do that, easy way is to make a method that parses an int
    int getInt() { JOptionpane input, parse and return it }
    and then you can say
    Java Code:
    patient1.setBirthMonth(getInt());
    or save the input to variables like you have done with age in your previous post and pass the variable that stores the input to your method.

  8. #8
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    Quote Originally Posted by superhaNds View Post
    Aa sorry, just saw your issue. Why don't you make a method getInt() ? that parses it or save the input to variables and pass them to your methods
    hmm.. i am not really following. I have to make all 3 that accepts integer as value. I am pretty sure I have to make dialog box on Project7 application.. where do I make getInt()?
    on Project7 or HealthProfile?

  9. #9
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    It was a suggestion, you can make a method (in project 7 under the main) and work it like this

    Java Code:
    public static void main(String[] args) {
    	patient1.setBirthMonth(getInt("Give birth month"));
    	// the same for the others
    	
        }
        
       static int getInt(String msg) {
    	String inputString = JOptionPane.showInputDialog(msg);
    	return Integer.parseInt(inputString);
        }
    }

  10. #10
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    it doesn't seem to be working though. :(

  11. #11
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    post your updated code

  12. #12
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    Java Code:
    	public static void main(String[] args)
    	{
    		String inputString;
    		String inputString1;
    		
    		
    		//declare identifier for HealthProfile Objoect
    		HealthProfile patient1;
    		//create HealthProfile object by calling its constructor
    		patient1 = new HealthProfile();
    		//input patient's first name
    		patient1.setFirstName(inputString = JOptionPane.showInputDialog("What is patient's first name?"));
    		//input patient's first name
    		patient1.setLastName(inputString = JOptionPane.showInputDialog("What is patient's last name?"));
    		//input patient's gender
    		patient1.setGender(inputString = JOptionPane.showInputDialog("What is patient's gender?"));
    		//input
    		patient1.setBirthMonth(getInt("What month did patient born?"));
    		
    			static int getInt(String msg)
    			{
    				String inputString = JOptionPane.showInputDialog(msg);
    				return Integer.parseInt(inputString);
    			}
    		
    		patient1.setBirthDay(3);
    		patient1.setBirthYear(1920);
    		
    
    		
    		String fName = patient1.getFirstName(); 
    		String lName = patient1.getLastName();
    		String gender = patient1.getGender();
    		int bDayMonth = patient1.getBirthMonth();
    		int bDayDay = patient1.getBirthDay();
    		int bDayYear = patient1.getBirthYear();
    		patient1.display(); //call HealthProfile to get height and weight
    		patient1.calcBMI(); //call HealthProfile to calculate BMI
    		DecimalFormat formatter = new DecimalFormat("#0.0");
    		
    		//print out the result	
    		System.out.println("Patient First Name: " + patient1.getFirstName() + "\n" + "Patient Last Name: " 
    		+ patient1.getLastName() + "\n" + "Gender: " + patient1.getGender() + "\n" + "Weight in pounds: "
    		+ patient1.weight + "BMI: " + formatter.format(patient1.BMI) + "BMI VALUES\n" + "\n" +
    		"Underweight:	less than 18.5\n" + "Normal:	between 18.5 and 24.9\n" + 
    		"Overweight:	between 25 and 29.9\n" + "Obese:		30 or greater");
    		
    		
    	}
    
    
    }
    I was testing it on one line first but never got it to work. :(

  13. #13
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    You cannot define a method inside a method. This has syntax errors.
    The method you must declare static and outside the main curly brackets. No wonder it isn't working.
    Like I showed you before.

  14. #14
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    Is that the only way to do it though? I made it working but Instruction doesn't say anything about making another method on Project7 to do so. Considering how my professor is like, he would mark me 0 for attempting something that is not on instruction.
    :(
    Last edited by shin777; 07-15-2013 at 11:36 AM.

  15. #15
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    Aah, you are the guy from the other project with the teacher giving false comments.
    Well, you followed the instructions on the other project and he gave you a 50.

    Why don't you do it in the most plain and simple way. Store the input to variables and pass them.

    Java Code:
    String bMotnhString = JOptionPane.showInputDialog("Give birth month");
    patient1.setBirthMonth.(Integer.parseInt(bMotnhString));

  16. #16
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    Quote Originally Posted by superhaNds View Post
    Aah, you are the guy from the other project with the teacher giving false comments.
    Well, you followed the instructions on the other project and he gave you a 50.

    Why don't you do it in the most plain and simple way. Store the input to variables and pass them.

    Java Code:
    String bMotnhString = JOptionPane.showInputDialog("Give birth month");
    patient1.setBirthMonth.(Integer.parseInt(bMotnhString));
    yeah.. that method works too. What I am worrying about is, that would completely remove patient.getBirthMonth. since he said I have to set up both getBirthMonth and setBirthMonth on class HealthProfile, I am assume
    he would wanna see both "set and get" used on Project7 application within same method. The guy hates me, I don't think he would give me a break for not using both set and get unless I am getting instruction completely wrong.

  17. #17
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    What do you mean it would remove the get method? I don't understand. As I see it, nothing else changes in your program.

    This part is useless anyway, you don't need/use it.

    Java Code:
            String fName = patient1.getFirstName(); 
            String lName = patient1.getLastName();
            String gender = patient1.getGender();
            int bDayMonth = patient1.getBirthMonth();
            int bDayDay = patient1.getBirthDay();
            int bDayYear = patient1.getBirthYear();
    After you take the input and set the attributes to the ones given, you use the get methods and the other methods to display what you are supposed to
    according to the instructions.

  18. #18
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: BMI calculator using 2 classes.

    Sorry.. What I meant is setBirthMonth etc..

    Java Code:
    //declare identifier for HealthProfile Objoect
    		HealthProfile patient1;
    		//create HealthProfile object by calling its constructor
    		patient1 = new HealthProfile();
    		//input patient's first name
    		patient1.setFirstName(inputString = JOptionPane.showInputDialog("What is patient's first name?"));
    		//input patient's first name
    		patient1.setLastName(inputString = JOptionPane.showInputDialog("What is patient's last name?"));
    		//input patient's gender
    		patient1.setGender(inputString = JOptionPane.showInputDialog("What is patient's gender?"));
    		//input
    		String bDayMonth = JOptionPane.showInputDialog("What is month of patient's birthday?");		
    		patient1.setBirthDay(getInt1("What is day of patient's birthday?"));
    		patient1.setBirthYear(getInt2("What is year of patient's birthday?"));
    see.. all top name ones start with patient1.setFirstName etc.. but when I switch it to that, it would start with String bDayMonth = etc etc.. it removes patient1.setBirthMonth completely..
    or am I misunderstanding this instruction? It's confusing me a little.

  19. #19
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: BMI calculator using 2 classes.

    I thought you would not use another method.. But what you are saying doesn't make sense.

    Java Code:
     String bDayMonth = JOptionPane.showInputDialog("What is month of patient's birthday?");
    doing just this doesn't set any birth months, it just receives string input and stores it in a variable. You will do this in order to receive input
    and THEN you are going to use the set methods to give the value to patient1 attributes.

    Java Code:
    String bMotnhString = JOptionPane.showInputDialog("Give birth month");
    patient1.setBirthMonth(Integer.parseInt(bMotnhString));
    String bDayString = JOptionPane.showInputDialog("Give birth day");
    patient1.setBirthDay(Integer.parseInt(bDayString));
    String bYearString = JOptionPane.showInputDialog("Give birth year");
    patient1.setBirthYear(Integer.parseInt(bYearString));
    What is the problem in that? You use the set methods; it's just an extra line of code for taking the input and then passing it to the method.
    I really don't know what you are looking for.

Similar Threads

  1. Association Classes for these classes
    By kevinkhan in forum New To Java
    Replies: 9
    Last Post: 03-21-2012, 05:22 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. Use classes inside of other classes
    By BlankFile in forum New To Java
    Replies: 2
    Last Post: 02-06-2012, 01:51 PM
  4. Replies: 2
    Last Post: 03-28-2011, 04:15 PM
  5. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 08:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •