Results 1 to 4 of 4
- 11-06-2012, 01:17 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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)
HealthProfile codingJava 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"); } }
Thanks for your time and help.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; } }
- 11-06-2012, 01:23 AM #2
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; } }
- 11-06-2012, 09:46 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-06-2012, 05:14 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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 05:17 PM.
Similar Threads
-
Why and where abstract methods & classes and static methods used?
By ajaysharma in forum New To JavaReplies: 1Last Post: 07-12-2012, 11:04 PM -
Why and where abstract methods & classes and static methods are used?
By ajaysharma in forum Advanced JavaReplies: 2Last Post: 07-12-2012, 11:04 PM -
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
accessing default generated private methods in Netbeans (in case of multiple classes
By lemontree45 in forum NetBeansReplies: 1Last Post: 09-09-2011, 06:57 PM -
Classes and Methods help
By border9 in forum New To JavaReplies: 5Last Post: 01-30-2009, 06:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks