Hi everyone
I am a beginner in java,and i need your help.If i have a array like this :

int [] eda={2,5,8,9}

how to use the get and set methods for consult and change the data of this array..??
Please help me with a simple example if anyone can.Thank u
I am waiting...
Regards dhija22

Recommended Answers

All 20 Replies

can you be just a bit more specific??
what do you want? to get a single element? or to return the entire array? and how is the array stored in your object? is it an instance variable? or is it for instance a static array containing info?

Do it one step at a time

how to use the get

Define what the get method should return?
Define what arguments it needs to find and return that value.
Write the code logic to find the value to be returned.
Return that value

I want something like this:

public class student
private string Name
public static void main(String[] args)
{
student ob=new studet("eda");
System.out.println(ob.getName());
ob.setName("ben");
System.out.println(ob.getName());
}
public student (String name)
{
Name=name;
}
public String getName()
{
return name;
}
public void setName(string Name)
{
Name=name
}
}

the output that i take from this code is:
eda
ben
iwont to declarate a array Marks that take marks of this student end when chane name to change and data ,i want output like this
eda
8 9 7
ben
6 5 9
i want that this marks to be in a array and to use the get and set method to change these elements of arrays...
i think i have give a goog explain for what i want
i am waiting for u to help me
thank u
regards dhija22

Please edit your code and wrap it in code tags. Use the [code] icon above the input box.

iwont to declarate a array Marks that take marks of this student

Do you know how to define an array with the values you want it to have?

Do you know how to access the elements of an array?

This is what you need to think about when you write a get method using arrays:
Define what the get method should return?
Define what arguments it needs to find and return that value.
Write the code logic to find the value to be returned.
Return that value

yes i know how to define an array and acces an array
for example:

int[] marks1={8, 9, 7}
int[] marks2={6, 5, 9}
for (i=0;i<marks1.length;i++)
{
system.out.print(marks[i] + " ");
}
for (j=0;j<marks2.length;j++)
{
system.out.print(marks[j] + " ");
}

but to integrate with methods gets and sets i dont know
please hepl me....

This is what you need to think about when you write a get method using arrays:
Define what the get method should return?
Define what arguments it needs to find and return that value.
Write the code logic to find the value to be returned.
Return that value

Answer each of these questions.

Are these too many questions to ask at one time?

Then just answer this one for now:

Define what the get method should return?

i think that methot get should return the elements of array...marks

method get should return the elements of array

Let me rephrase that: It should return the contents of ONE ELEMENT of the array.

what argument(s) does it need to find the value to return?

:O i dont know....i am trying to write something but ......:(

package student;

public class student
{
private String Name;
private int[] Mark;
public static void main(String[] args)
{
student ob=new student("eda",8,9,6);
System.out.println(ob.getName() + ob.getMark());
ob.setName("ben");
System.out.println(ob.getName() + ob.getMark());
}
public student (String name, int[] mark)
{
Name=name;
Mark=mark;
}
public String getName()
{
return Name;
}
public void setName(String name)
{

Name=name;

}
public int[] mark getmark()
{
	int i;
	return Mark[i];
}
public void setMARK(int[]Mark)
{
	Mark[]=mark[];
}
}

what i should do ...please help me....

Where in your code is the get method we were working on designing?

What is your answer to this question:
what argument(s) does the get method need to find the value to return?

at line 29...i thing...i dont know :( whats arguments needs to find for return the value? :'(
please tell me what i should do!!:O

If the array has 20 elements in it, how would you ask the get method to return one of those elements? How would you identify which element you wanted? What kind of variable would hold the value that would tell the get method which element to return?

if the array has 20 elements on it i think i ought to use an index fot example i to acces the element of the array...

sorry ...i am a beginner in java....plasedont worry with me

p.s can u give me u msn?

thanx

i ought to use an index

Yes the get method needs the index to the array as it argument.
Now can you code just the one line for the definition of the get method?
You need to define what is returned, the name and its arguments.

Then we'll go on to how to access the value to be returned
and how to return that value.

public marks[]
getMark(int i)
{
return Mark;
}

What did you say that the get method should return?
An element of the array or the whole array?

Have you tried to compile the code you posted? What errors do you get?
public marks[]
Is marks a data type?

String, double, char are data types.
Is marks the name of a variable in your code?

i want to show all elements of array
no i don t compile that code no mark is not a data code,,,,i read this tutorial u send me i understand i should write something like this

public int getMark(int i)
{
return Marks}
public int getMark(int i)

Yes that is how you define the getMark method.

Next:
Where is the value you want to return for this method when it is called with the arg: i?
Does the variable i help you get that value?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.