Help with get and set methods.....

package Automotive;

public class OptionSet {

private String os_name;

private int os_options;

Option [] op;

public OptionSet(String name, int count)

{

os_options = count;

os_name = name;

}

public String getName(){

return s;

}

public void setName( String s)

{

os_name = s;

}

}

My question is in the method OptionSet we are passing a String "name".

When iam trying to set the name of the OptionSet should i say,

public void setName( String s)

{

os_name = s;(no error)

(or)

name = s;(name cannot be resolved)

}

Bcoz when iam trying to set

name=s;

iam getting an error "name cannot be resolved";

Also in the getName() method;

public String getName()

{

return s;(Error: s cannot be resolved)

}

Are my set and get methods right

[933 byte] By [GitaSumana] at [2007-11-27 1:31:48]
# 1

> My question is in the method OptionSet we are passing

> a String "name".

>

> When iam trying to set the name of the OptionSet

> should i say,

>

> public void setName( String s)

> {

> os_name = s;(no error)

>

> (or)

> name = s;(name cannot be resolved)

> }

I don't understand. Surely given a choice between one way that compiles

and one that doesn't, which would you choose?

> public String getName()

> {

> return s;(Error: s cannot be resolved)

> }

>

> Are my set and get methods right

I think you have a basic confusion about the scope of variables.

The names of your fields are: os_name, os_options and op.

Variables name and count are parameters to your constructor,

so they are local to it -- do not refer to them elsewhere. Variable s

is a parameter in method setName so it is local to it -- do not refer

to s elsewhere. Instead, use the fields.

DrLaszloJamfa at 2007-7-12 0:35:12 > top of Java-index,Java Essentials,Java Programming...
# 2

Ok, a couple of things.

1) name doesn't exist, so that obviously won't work.

2) The name of your accessor and mutator methods should refer to what they're accessing and mutating. so getOs_name() setOs_name(...) would actually be the proper form. However your variables are misnamed so I'd start by fixing that.

3) Package names are lower case by convention so I'd change that too.

I'd end up with something like this

package automotive; // Not exactly because there's a convention for this as well.

public class OptionSet{

private String osName ;

private int osOptionCount ;

private Option[] options ;

public OptionSet(String osName, int osOptionCount){

super() ;

this.osName = osName ;

this.osOptionCount = osOptionCount ;

}

public void setOsName(String osName){

this.osName = osName ;

}

public String getOsName(){

return osName ;

}

}

The rest I leave as an exercise for the op. Hope this helps,

PS.

puckstopper31a at 2007-7-12 0:35:12 > top of Java-index,Java Essentials,Java Programming...
# 3

It is a very lengthy question.But inorder to explain my doubt i had to explain the whole thing........

public class OptionSet {

private String os_name;

private int os_options;

Option [] options;

public OptionSet(String name, int count)

{

os_options = count;

os_name = name;

}

public Option[] getOptions()

{

return options;

}

public void setOptions(Option[] options)

{

this.options = options;

}

class Option {

String op_name;

int op_price;

public Option( String name, int price)

{

op_name= name;

op_price= price;

}

public String getName()

{

return op_name;

}

public void setName(String name)

{

op_name= name;

}

}

}

Iam confused of using Object arrays....

I have the following requirements:

Transmission -OptionSet

Options are:

1.Automatic-- $ 0.0

2.Standard-- $-815

I have an Automotive class in which i have instantiated OptionSet class.

public class Automotive implements Serializable{

OptionSet osm[] = new OptionSet[5];

osm[0] = new OptionSet(Color, 10);

osm[1] = new OptionSet(Transmission, 2);(Transmission has 2 options)

for(int i=0; i<osm.length(); i++)

{

OptionSet.Option[] xyz = osm.getOptions();

}

}

1.)When i call the method getOptions() iam not getting any of the options displayed?

2)where should i place my this piece of code:

Options op[ ] =new Options [2];

osm[1].op[0] = new Option(Automatic,0)

osm[1].op[1]=new Option(Standard,-815)

3) Not getting How to use the following constructors:

public void setOption(int i,String name,int price)

{

}

public String getOption(String name)

{

return name;

}

public int getOptionPrice(String name)

{

return ;

}

Gita.>

GitaSumana at 2007-7-12 0:35:12 > top of Java-index,Java Essentials,Java Programming...
# 4

Iam confused of using Object arrays....

I have the following requirements:

Transmission -OptionSet

Options are:

1.Automatic -- $ 0.0

2.Standard -- $-815

I have an Automotive class in which i have instantiated OptionSet class.

public class Automotive implements Serializable{

OptionSet osm[] = new OptionSet[5];

osm[0] = new OptionSet(Color, 10);

osm[1] = new OptionSet(Transmission, 2);(Transmission has 2 options)

for(int i=0; i<osm.length(); i++)

{

OptionSet.Option[] xyz = osm.getOptions();

}

}

1.)When i call the method getOptions() iam not getting any of the options displayed?

2)where should i place my this piece of code:

Options op[ ] =new Options [2];

osm[1].op[0] = new Option(Automatic,0)

osm[1].op[1]=new Option(Standard,-815)

3) Not getting How to use the following constructors:

public void setOption(int i,String name,int price)

{

}

public String getOption(String name)

{

return name;

}

public int getOptionPrice(String name)

{

return ;

}>

GitaSumana at 2007-7-12 0:35:12 > top of Java-index,Java Essentials,Java Programming...