• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your post is difficult to read because you didn't use the Code button.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, you could use one of java.util.Arrays.toString(...) methods.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Campbell already said, please UseCodeTags when you post source code.
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic