Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default how to use get and set methods for multiple classes

    I had some issues sending and receiving data between classes, though it is fixed, it isn't using the get or set method's, referring to the book doesn't quite help here and reviewing the powerpoints is kinda befuddling since it's in reference to a rectangle(which is confusing me).. referring to a few other sources I came across a method or sending and receiving data. but appears in this fashion, "patientBMI=patientOne.calcBMI(inchHeight);", this statement is much easier for me to comprehend for some reason.

    I am not sure how to use the get and set methods when it's not pertaining to measurements.

    Code from Project7(uses HealthProfile coding)
    Java Code:
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;
    
    public class Project7
    {
    	public static void main(String[] args)
    	{
    		double birth_Month=0;
    		double birth_Day=0;
    		double birth_Year=0;
    		double inchHeight;
    		double patientAge;
    		double patientBMI;
    		double currentYear;
    		double currentMonth;
    		double currentDay;
    		
    		
    		HealthProfile patientOne= new HealthProfile();
    		
    		String patientFirstName=JOptionPane.showInputDialog("What is the patient's first name? ");
    		
    		String patientLastName=JOptionPane.showInputDialog("What is the patient's last name? ");
    		
    		String patientGender=JOptionPane.showInputDialog("What is the patient's gender?");
    		
    		String userHeight = JOptionPane.showInputDialog("Please enter patient's height in inches: ");		//Window appears prompting the user to enter his/her height into the BMI calculator.
    		inchHeight = Double.parseDouble(userHeight);		//Conversion statement from the "userHeight" String, to "inchHeight", an integer variable for use in mathematical formula.
    
    		String yearOfBirth = JOptionPane.showInputDialog("Please enter patient's year of birth: ");		//Window appears prompting the user to enter his/her weight into the BMI calculator.
    		birth_Year = Double.parseDouble(yearOfBirth);			//Conversion statement from the "userWeight" String, to "lbWeight", an integer variable for use in mathematical formula.
    
    		String monthOfBirth = JOptionPane.showInputDialog("Please enter patient's month of birth: ");		//Window appears prompting the user to enter his/her weight into the BMI calculator.
    		birth_Month = Double.parseDouble(monthOfBirth);			//Conversion statement from the "userWeight" String, to "lbWeight", an integer variable for use in mathematical formula.
    
    		String dayOfBirth = JOptionPane.showInputDialog("Please enter patient's day of birth: ");		//Window appears prompting the user to enter his/her weight into the BMI calculator.
    		birth_Day = Double.parseDouble(dayOfBirth);			//Conversion statement from the "userWeight" String, to "lbWeight", an integer variable for use in mathematical formula.
    
    		patientAge=patientOne.ageCalc(birth_Year,birth_Month,birth_Day);
    
    		patientBMI=patientOne.calcBMI(inchHeight);
    		
    		DecimalFormat medicalFormat= new DecimalFormat("#0.0");
    		
    		String patientWeight = JOptionPane.showInputDialog("Please enter patient's weight in pounds: ");		//Window appears prompting the user to enter his/her weight into the BMI calculator.
    
    		
    		System.out.println("Patient First Name: " + patientFirstName);
    		System.out.println("Patient Last Name: " + patientLastName);
    		System.out.println("Gender: " + patientGender);
    		System.out.println("Age: " + patientAge);
    		System.out.println("Weight in Pounds: " + medicalFormat.format(patientWeight));
    		System.out.println("BMI: " + medicalFormat.format(patientBMI));
    		System.out.println();
    		System.out.println("BMI VALUES");
    		System.out.println("Underweight:	less than 18.5");
    		System.out.println("Normal:	between 18.5 and 24.9");
    		System.out.println("Overweight:	between 25 and 29.9");
    		System.out.println("Obese:		30 or greater");
    
    	}
    }
    HealthProfile coding
    Java Code:
    import javax.swing.JOptionPane;
    
    public class HealthProfile
    {
    	public static void main(String[] args)
    	{
    		String patientFirstName;
    		String patientLastName;
    		boolean gender;
    		double birth_Month=0;
    		double birth_Day=0;
    		double birth_Year=0;
    		double lbWeight;
    		double inchHeight;
    		double patientAge;
    		double patientBMI;
    		
    	}
    	
    	public static double calcBMI(double num1)
    	{
    		double weight;
    		double weightPrep;
    		double heightPrep;
    		double result;
    		
    		String userWeight = JOptionPane.showInputDialog("Please enter patient's weight in pounds: ");		//Window appears prompting the user to enter his/her weight into the BMI calculator.
    		weight = Double.parseDouble(userWeight);			//Conversion statement from the "userWeight" String, to "lbWeight", an integer variable for use in mathematical formula.
    
    		
    		weightPrep = weight*703;						//Mathematical prep-work on the weight input from user for use in calculating BMI.
    		heightPrep = num1*num1;			//Mathematical prep-work on the height input from user for use in calculating BMI. 
    		result = weightPrep/heightPrep;				//Takes both prep-works from height and weight and calculates BMI.
    		
    		return result;
    	}
    	
    	public static double ageCalc(double num1, double num2, double num3)
    	{
    		double currentYear;
    		double currentMonth;
    		double currentDay;
    		double result;
    		
    		String promptYear= JOptionPane.showInputDialog("Please enter the current year in four digit format: ");
    		currentYear=Double.parseDouble(promptYear);
    		
    		String promptMonth= JOptionPane.showInputDialog("Please enter the current month in number format: ");
    		currentMonth=Double.parseDouble(promptMonth);
    		
    		String promptDay= JOptionPane.showInputDialog("Please enter the current date of the month: ");
    		currentDay=Double.parseDouble(promptDay);
    		
    		if(num3<=currentDay)
    		{
    			if(num2<=currentMonth)
    			{
    			result=currentYear-num1;
    			return result;
    			}
    		}
    		result=(currentYear-num1)-1;
    		return result;
    	}
    }
    Thanks for your time and help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: how to use get and set methods for multiple classes

    Here's a simple example.
    Java Code:
    class Test {
        public static void main(String[] args) {
            Foo f = new Foo();
            System.out.println(f.get());
            f.set(100);
            System.out.println(f.get());
        }
    }
    
    class Foo {
        private int value;
    
        public void set(int v) {
            value = v;
        }
    
        public int get() {
            return value;
        }
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: how to use get and set methods for multiple classes

    Why does HealthProfile have a main() method?
    It's not as if that method is doing anything.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: how to use get and set methods for multiple classes

    i missed the day in class and can't do much with the powerpoints he made. and the book wasn't much help either.. simply an error of mine. took it out actually after i posted this once i realized i didn't need the variable listed in HealthProfile, just didn't edit it here.

    and Junky, that still doesn't make any sense to me. I understand it a tiny bit better but i'm still confused as to how to use it in the program i've written.. could i get an example using project7's height for the BMI calculation and return the value to the patientBMI's memory, I'm sure if i get that much i can understand the rest of it and plug it in then. (sorry for being complicated! i learn better from a reverse engineering standpoint..)
    Last edited by comptechgivinsky; 11-06-2012 at 06:17 PM.

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2012, 12:04 AM
  2. Replies: 2
    Last Post: 07-13-2012, 12:04 AM
  3. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  4. Replies: 1
    Last Post: 09-09-2011, 07:57 PM
  5. Classes and Methods help
    By border9 in forum New To Java
    Replies: 5
    Last Post: 01-30-2009, 07:51 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
  •