This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes constructor ,setter and getter methods with array of strings as a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor ,setter and getter methods with array of strings as a parameter" Watch "constructor ,setter and getter methods with array of strings as a parameter" New topic
Author

constructor ,setter and getter methods with array of strings as a parameter

Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
Hello everyone

I have created a constructor method with an array of string as one of its parameter and unable to figure out how it should be done.Same thing even for getter and setter methods.

class Student
{
private String firstName;
private String lastName;
private int studentID;
private String[] courseIDsEnrolled;

Student()
{
firstName=" ";
lastName=" ";
studentID=0;
}

Student(int studentID,String firstName,String lastName,String[] courseIdsEnrolled)// how to assign the array of string value
{
this.studentID=studentID;
this.firstName=firstName;
this.lastName=lastName;
this.courseIdsEnrolled[]=courseIdsEnrolled[];
}

concatName(String firstname,String lastName)
{
String strResult = lastName + firstName;
return strResult;
}
void setStudentID(int studentID)
{
this.studentID=studentID;
}
void setLastName(String lastName)
{
this.lastName=lastName;
}
void setFirstName(String firstName)
{
this.firstName=firstName;
}
void setCourseIDsEnrolled(String[] courseIDsEnrolled)// should take an array parameter representing courseIDsEnrolled, and set courseIDsEnrolled to the parameter)
{
this.courseIDsEnrolled[]=courseIdsEnrolled[];
}
String[] getCourseIDsEnrolled(String[] courseIDsEnrolled)// it should return the courseIDEnrolled array
{
for(int i=0;i<courseIDsEnrolled.length;i++)
{
System.out.println(courseIDsEnrolled[i]);
}
}


Can some one please let me know if what I done is correct or not?? It not correct then please let me know my mistake.

Thank you!!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

When you are referring to the array itself, rather than one of its elements, no brackets are used:

this.courseIDsEnrolled=courseIdsEnrolled;

When you're declaring an array variable, then you do use the brackets:

private String[] courseIDsEnrolled;

And of course to refer to one element of the array, the brackets and an index are needed:

System.out.println(courseIDsEnrolled[i]);

[Jess in Action][AskingGoodQuestions]
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
Thank you Ernest for your quick reply.
So
is the code for my setter method correct??

//should take an array parameter representing courseIDsEnrolled, and set courseIDsEnrolled to the parameter
void setCourseIDsEnrolled(String[] courseIDsEnrolled)
{
for(int i=0;i<courseIDsEnrolled.length;i++)
{
this.courseIDsEnrolled[i]=courseIdsEnrolled[i];
}
}

Getter Method:
//should return the courseIDsEnrolled array

String[] getCourseIDsEnrolled(String[] courseIDsEnrolled)
{
for(int i=0;i<courseIDsEnrolled.length;i++)
{
System.out.println(courseIDsEnrolled[i]);
}
}

can you please explain me..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4246
    
    7

Have you tried it out? I'm pretty sure the getter won't compile, because you never actually return anything.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Your post is difficult to read because you didn't use the Code button.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Generally,
Setters- void return type and take some parameters.
Getters- Return what ever requested, and generally no parameters.

This is though not a rule as such. But its I believe a widely used convention? (Need confirmation from other members)


Mohamed Sanaulla | My Blog
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
Hi Mohammed

Thanqq for the reply. I have written some code for the getter and setter methods but i am unable to retreive the array using getter method.

My code:
//student.java

void setCourseIDsEnrolled(String[] courseIDsEnrolled)
{
this.courseIDsEnrolled=courseIDsEnrolled;
}

String[] getCourseIDsEnrolled()
{
return courseIDsEnrolled;
}

I went and created a test.java and created the array with 2 elements and set them using the setter method.
student s= new student;

String[] courseIDsEnrolled= {"CS101","CS103"};
s.setCourseIDsEnrolled(courseIDsEnrolled);

but when I said System.out.println("getter method:"+s.getCourseIDsEnrolled()); it is giving me some garbage value.

Can someone please let me know what my mistake is??

Thank you!!

Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
hello everyone,

it is compiling fine. but is giving me the following when I run the test.java

getter method:[Ljava.lang.String;@addbf1

I really dont know what it means??
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

[quote=Divya Kotamraju]hello everyone,

it is compiling fine. but is giving me the following when I run the test.java

getter method:[Ljava.lang.String;@addbf1

I really dont know what it means??[/quote]

Its not a garbage value. Usually if the toString() method is not overridden- by default toString returns- <ClassName@hashCode> as the value when ever used in println or any other method which expects a String.

So you can loop through the array obtained from the getter method to print out the elements of the array.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Or, you could use one of java.util.Arrays.toString(...) methods.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

As Campbell already said, please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Divya Kotamraju
Greenhorn

Joined: Oct 07, 2010
Posts: 21
Hello Everyone,

Thank you all for your reply.I am new to java. I am really not knowing how to do this. Can someone let me know

"How to get individual elements of the array and concatenate with String literals and send the array to the Student constructor, not via the setter.

Thank you!!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Divya Kotamraju wrote:Hello Everyone,

Thank you all for your reply.I am new to java. I am really not knowing how to do this. Can someone let me know

"How to get individual elements of the array and concatenate with String literals and send the array to the Student constructor, not via the setter.

Thank you!!


1. Loop through the Array elements.
2. Use + operator to concatenate the elements. OR you could try using StringBuilder class for concatenating the Strings.
3. Overload the constructor like this

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Mohamed Sanaulla wrote: . . .
2. Use + operator to concatenate the elements. OR you could try using StringBuilder class for concatenating the Strings. . . .
You should use the + several times operator in one statement. If you use it repeatedly in different statements, or in a loop, the JVM cannot optimise the code and performance will be slow. In that case StringBuilder is a better option.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor ,setter and getter methods with array of strings as a parameter
 
Similar Threads
So exhausted...anyone there to help?
Java dunce is back...still can't figure this out
In struts2 delete is not Working
call second constructor?
Help with this error