aspose file tools*
The moose likes Beginning Java and the fly likes I dont get the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I dont get the "get and set" method for my homework" Watch "I dont get the "get and set" method for my homework" New topic
Author

I dont get the "get and set" method for my homework

ryan tuthill
Greenhorn

Joined: Aug 31, 2009
Posts: 3
Hello, I am on my third chapter of java and I am utterly lost. I am supposed to make an app and class that uses get and set as well as constructors. The user of the program is supposed to enter first name, last name,ect This data passes to my heartrates class. I don't understand how or why you go from inputing strings and int to passing that data to the object class. java is really annoying the out of me! Here's the code of my java application that calls a class. How do i take inputed information and send it to the object classes methods? (Why would i even do this?!?!)

import java.util.Scanner;

//I can make it work


public class GettingMad
{
//Main method begins execution of Java application
public static void main(String[] args)
{

// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );

String first; // first name
System.out.print("Please enter first name: ");
first = input.nextLine(); //read input from user

String last; // last name
System.out.print("Please enter last name: ");
last = input.nextLine(); //read input from user

int month; //month of birth
System.out.print("Please enter your month of birth: ");
month = input.nextInt();//read input from user

int day; //day of birth
System.out.print("Please enter your day of birth: ");
day = input.nextInt();//read input from user

int year; //year of birth
System.out.print("Please enter your month of birth: ");
year = input.nextInt();//read input from user

age = (year - 2009);//calculate users age in years

max = (220 - age);//calculate max heart rate




HeartRates myHeartRates = new HeartRates(); //create a HeartRates object

//call myHeartRate's displayMessage method
myHeartRates.displayMessage();

}//end main method

}//end class GettingMad
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags. Unformatted code/config/etc. is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why would i even do this?!?!

So the object has data with which to do its work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39386
    
  28
You are doing it the wrong way round. Write the HeartRate class first. Give it fields and a constructor and get and set methods. You will probably find examples in your book, or in the Java™ Tutorials; there are sections about methods, constructors, "passing information" etc.

Are you really making a HeartRate class with a name? I have a heart rate, but have never thought to give it a name. I have a name myself however. So a real-life application would probably have a Person class and that has name, heart rate, age, etc.

And welcome to JavaRanch
ryan tuthill
Greenhorn

Joined: Aug 31, 2009
Posts: 3
Thanks for your quick replies. I am a total noob to object programing and have to reinvent the way I think.

TAGS; Are you talking about HTML tags?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you click the link?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I dont get the "get and set" method for my homework