Reputation: 1493
So I've started dipping my hands in Java on Udacity, and recently i came across this question which I need help with. link: https://www.udacity.com/course/viewer#!/c-cs046/l-183784769/e-186954461/m-188821863
Question
Okay, I'm confused and need help.
So far I "think" we've learnt is that in java programming, we create classes, in those classes we create objects(?) and create methods to use these objects and we use variables to store these objects. Please correct me If I wrong so far...
I understand that instance variables are those variables that all methods in that class can access.
So in this case: private String name;
private String friends;
--What do these instance variable store in them, is it the your name and friends name??
Public interface are basically the method head/title(?) - In this case this was: public void addFriend(Person friend)
a1) So here for the word "addFriend", did we declare this somewhere, because how does the computer know what that even means?
b1) in the arguments for the addFriend method, what does (Person friend) mean and how did we declare/get it? the word friend in the arguments (Person friend), the word friend can't be the instance variable because the instance variable we created was "friends" not "friend", correct?
A2) for the friend.name, is the "friend" the object? B2) and I'm assuming ".name" is the instance variable? C1) to assign variables to objects do we always put the object and then .variableName?
Finally as a separate question: how do you figure out what the method for add friend method should we, I'm talking about the process/concept i.e do we look at what the object is and then figure out what to put in the method arguments???
Thanks for your help in advance and please excuse my noob-e-ness :)
Upvotes: 1
Views: 5652
Reputation: 57209
What do these instance variable store in them, is it the your name and friends name
Yes. Their type is String
so it's just expecting a string, like "Bob", or "Felicia", or "Curtis". You could even set it to "water bottle" or "screen door" or something, as long as it's a string.
But friends
is a strange name for a string since it suggests more than one friend - usually that would use a List
. But maybe you haven't learned that yet.
Public interface are basically the method head/title(?) -
Interfaces are a template for a class. Some people like the word "contract". It just tells the class "you must have these methods: blah(), blee(), and bloo()".
Your example does not use an interface, so I can't be more specific.
So here for the word "addFriend", did we declare this somewhere, because how does the computer know what that even means?
It's a method, so it's bunched in with the class when you make a new one. Later, when you instantiate person (that is, say Person Joe = new Person()
) you can call addFriend()
to give Joe a friend.
You can instantiate a bunch of Person
classes, which will each have their own addFriend
method.
in the arguments for the addFriend method, what does (Person friend) mean and how did we declare/get it?
See last answer.
the word friend in the arguments (Person friend), the word friend can't be the instance variable because the instance variable we created was "friends" not "friend", correct?
The argument friend
is local to the method. It's not visible outside of the method. So you can call it whatever you want.
I like to append a 0
to argument parameters so I know quickly that they're arguments, like this:
public void addFriend(Person friend0)
{
...do stuff with friend0...
}
for the friend.name, is the "friend" the object?
This is a little misleading This is wrong, since name
is private and can't be seen if you say, for example, Person Joe = new Person(); System.out.println(Joe.name)
.
But yes, what they mean is that friend
is an instance of Person
.
To fix it, either make name
public (not recommended) or write a getName()
method, like this:
public String getName()
{
return name;
}
This way is better because you can't accidentally change (reset) someone's name later.
and I'm assuming ".name" is the instance variable?
An "instance" is when you make a new class. Instance variables are variables that are in the class. So name
is an instance variable, but not the only one.
to assign variables to objects do we always put the object and then .variableName?
You put the type, and then the name, like int x = ...
or String address = ...
. For example, like I was saying before, if you have lots of Person
s, you tell Java something like
Person Joe = new Person();
Person Wilhelmina = new Person();
Person Winifred = new Person();
...and then you add friends and stuff to each specific instance (each time you make a new class it's called an instance).
how do you figure out what the method for add friend method should we
The handwriting scrawl on the side says to just append friend names to the friends
string. So, the method receives a friend
of type Person
, then you probably have to call getName()
to get its name as a String
, and then append it to that instance's friends
.
Upvotes: 1
Reputation: 490
Class is the blueprint of objects. When you design a class, think about the objects that will be created from that class type. Things an object knows about itself are called instance variables and things an object can do are called methods.
When you declare an instance in the class, it stores information for different object referenced by different reference variables. So its storing information for several objects.
a1. When you declare a public method in a class, you need not to declare it somewhere else to let the compiler know about the method. When a reference variable is created, it will have access to that method. So the method is a part of object.
B1. The parameter is type of "Person" and its "local" to the method. So the variable declared in class "friends" and the parameter "friend" has completely no connection. It will take a "Person type object" as a parameter.
A2+B2+C1. For "friend.name", you can only refer an object by the reference variable "friend". You know an object can have two types of things... instance variable and the methods.
Upvotes: 0
Reputation: 1
Your class is your object. You define constructors(how your object will be instantiated) within your class. You then create methods your object will use. For example, contact.getName(). Sorry I cant give a more detailed answer I am replying from my phone.
Upvotes: 0