Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: using class and method..

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using class and method..

    k48pxt.jpg

    here is instruction and here are code I wrote. it runs and produce right result but I am not so sure this is how instruction is asking me to
    write the code. could you verify and see if I did things right here? any pointers are appreciated.

    Class HealthProfile
    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 Application
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
     
    public class Project7
    {
    	public static void main(String[] args)
    	{
    		String inputString;
     
     
     
    		//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 bMonth = JOptionPane.showInputDialog("What is month of patient's birthday?");		
    		String bDay = JOptionPane.showInputDialog("What is day of patient's birthday?");
    		String bYear = JOptionPane.showInputDialog("What is year of patient's birthday?");
     
     
     
    		String fName = patient1.getFirstName(); 
    		String lName = patient1.getLastName();
    		String gender = patient1.getGender();
    		patient1.setBirthMonth(Integer.parseInt(bMonth));
    		patient1.setBirthDay(Integer.parseInt(bDay));
    	         patient1.setBirthYear(Integer.parseInt(bYear));
    		patient1.calcAge();
    		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" + "Age: " +
    		patient1.age + "\n" + "Weight in pounds: " + patient1.weight + "\n" + "BMI: " + 
    		formatter.format(patient1.BMI) + "\n" + "\n" + "BMI VALUES\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");
     
     
    	}
     
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: using class and method..

    The instructions say that all the data members (except height, weight, age and BMI) should be defined when the class is instantiated. A class is instantiated when the constructor is called. Therefor all the data members (exempt those mentioned before) should be set in the constructor.

    HINT:
    	public HealthProfile(String fName, String lName, String gender, int bDayMonth, int bDayDay, int bDayYear)

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: using class and method..

    In addition to the constructor suggestion:
    Instructions indicate that a method should calculate and return the age in years, not that it be stored as a variable - same thing with the BMI, it should be calculated and returned rather than stored
    Keep in mind that you should not store "the same data" twice. For example, you store the year, month, and day of birth, which can be used to calculate the age, so there is no reason to store age in a variable. In fact there is good reason not to do so.
    If you created a new HelthProfile and set the birth year, month, day, and age, ...what if someone realizes there was an error in the birth year, month or day when the information was typed in. With the age being calculated on the spot, it will always return the most current (supposedly accurate) information, and not the value sitting in another variable that may or may not have been updated

Similar Threads

  1. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 06:56 AM
  2. calling a method in class
    By MrBean in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2012, 12:31 AM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Using a Method from another class help?
    By jmskoda5 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 10th, 2012, 12:54 PM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM