• Post Reply Bookmark Topic Watch Topic
  • New Topic

Absolute beginner question on methods  RSS feed

 
Aaron Rose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am an IT major and am required to take Java. I have a very basic understanding of C++ as I took a course over 5 years ago and do not remember much. Programming has always been tough for me to understand and I am having trouble completing the first homework for this course. I don't expect or really want to be given the answer to such a simple problem. I hope that this site will be able to help me for this semester and the future as I embark on a journey to be able to truthfully say I can write basic java programs. Of the program I am going to post, I get the gist of what everything in the code is however I do not know where or what I should type in the student.java file in order to return student information when the program is run. I guess what I am asking is, where do I need to create this method and what is REQUIRED to connect these two classes and display the data I want. Any tips would be greatly appreciated to a novice such as me.

This problem should be very simple but here is where I am stuck. I don't know how to create a method inside of student.java that returns

Using the classes below (app and student) create a solution for returning the complete student information using a method in the class student.


public class app
{
public static void main(String args[])
{
String initName = "John";
String lastName = "Smith";
int age = 22;
student st = new student(initName,lastName,age);
}
}



class student
{
String firstName;
String lastName;
int age;

student(String informedFirstName, String informedLastName, int informedAge)
{
firstName = informedFirstName;
lastName = informedLastName;
age = informedAge;
}

String getName()
{
return firstName +" "+lastName;
}

}
 
Zeke Hans
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try placing getName() inside a formatted text output, like this: . Try that.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Aaron, welcome to the Ranch!
It looks like you created the method okay, nothing jumps off as being wrong to me. So now all you have to do is call it. Zeuss showed how you would do it from inside the student class. If you wanted to call it from the main method, you need to call it on an instance of student. You already have an instance, the st variable, so you could do st.getName()
 
Aaron Rose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the fast responses! I hope to be able to call this place my new home for Java.

I don't think I understood what it meant to "call" a method. Thus I have wrote something that should call the method in java.app: System.out.println("student name is" + st.getName());

Now what I am not sure of is, where in a class can a method be called? Or rather, where would be the best place to insert that line to get it to display the student information.

Thanks in advance for the help.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sidenote: it is customary to capitalize the class names. Look at the API. So I'd better call it Student.

Another sidenote: in German the nouns (and even other words if used as nouns) are capitalized.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!