File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Divya Kotamraju
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
 
Divya Kotamraju
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it out? I'm pretty sure the getter won't compile, because you never actually return anything.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your post is difficult to read because you didn't use the Code button.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Divya Kotamraju
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 227
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, you could use one of java.util.Arrays.toString(...) methods.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell already said, please UseCodeTags when you post source code.
 
Divya Kotamraju
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic