• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ryan tuthill
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would i even do this?!?!

So the object has data with which to do its work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you click the link?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic