Forums Register Login

Absolute beginner question on methods

+Pie Number of slices to send: Send
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;
}

}
1
+Pie Number of slices to send: Send
Try placing getName() inside a formatted text output, like this: . Try that.
1
+Pie Number of slices to send: Send
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()
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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.

Friends help you move. Good friends help you move bodies. This tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 710 times.
Similar Threads
call second constructor?
constructor ,setter and getter methods with array of strings as a parameter
Simple Person Program
how to create a mobile application using struts2 and hibernate on eclipse
Connecting netbeans with sql database
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:19:27.