default value of data member.  
Author Message
Mike





PostPosted: 2007-5-29 11:01:00 Top

java-programmer, default value of data member. Hi

I have a problem about default value of data member!

+++++++ cut here++++++
class CComputer
{
private double cpu=3.0;
private double memory=1.0;

public void set(double c)
{
if(c<0)
System.out.println("Input cpu error, cpu is default value");
else
this.cpu=c;
}
public void set_memory(double m)
{
if(m<0)
System.out.println("Input memory error, memory is default
value");
else
{
memory=m;
show_memory();
}
}

public void set(double c,double m)
{
set(c);
set_memory(m);
}
void show_cpu()
{
System.out.println("cpu="+this.cpu);
}
void show_memory()
{
System.out.println("memory="+this.memory);
}
void show_all()
{
this.show_cpu();
this.show_memory();
}
}
public class bbb
{
public static void main(String args[])
{
CComputer c1=new CComputer();

c1.set(-3.5,1.5);
c1.show_all();
c1.set(-3.5,-2.0);
c1.show_all();
c1.set(-3.5);
c1.show_all();
}
}
+++++++

The output of "c1.set(-3.5,-2.0);" shows memory=1.5.
The output of " c1.set(-3.5);" also shows memory=1.5;

Why? I thought memory should be default value, i.e. 1.0.

Thank you in advance.

Mike

 
Daniel Pitts





PostPosted: 2007-5-29 12:12:00 Top

java-programmer >> default value of data member. On May 28, 8:00 pm, Mike <email***@***.com> wrote:
> Hi
>
> I have a problem about default value of data member!
>
> +++++++ cut here++++++
> class CComputer
> {
> private double cpu=3.0;
> private double memory=1.0;
>
> public void set(double c)
> {
> if(c<0)
> System.out.println("Input cpu error, cpu is default value");
> else
> this.cpu=c;
> }
> public void set_memory(double m)
> {
> if(m<0)
> System.out.println("Input memory error, memory is default
> value");
> else
> {
> memory=m;
> show_memory();
> }
> }
>
> public void set(double c,double m)
> {
> set(c);
> set_memory(m);
> }
> void show_cpu()
> {
> System.out.println("cpu="+this.cpu);
> }
> void show_memory()
> {
> System.out.println("memory="+this.memory);
> }
> void show_all()
> {
> this.show_cpu();
> this.show_memory();
> }}
>
> public class bbb
> {
> public static void main(String args[])
> {
> CComputer c1=new CComputer();
>
> c1.set(-3.5,1.5);
> c1.show_all();
> c1.set(-3.5,-2.0);
> c1.show_all();
> c1.set(-3.5);
> c1.show_all();
> }}
>
> +++++++
>
> The output of "c1.set(-3.5,-2.0);" shows memory=1.5.
> The output of " c1.set(-3.5);" also shows memory=1.5;
>
> Why? I thought memory should be default value, i.e. 1.0.
>
> Thank you in advance.
>
> Mike

You're default value only gets assigned during the construction of
your object.

your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.


 
Daniel Pitts





PostPosted: 2007-5-29 12:13:00 Top

java-programmer >> default value of data member. On May 28, 8:00 pm, Mike <email***@***.com> wrote:
> Hi
>
> I have a problem about default value of data member!
>
> +++++++ cut here++++++
> class CComputer
> {
> private double cpu=3.0;
> private double memory=1.0;
>
> public void set(double c)
> {
> if(c<0)
> System.out.println("Input cpu error, cpu is default value");
> else
> this.cpu=c;
> }
> public void set_memory(double m)
> {
> if(m<0)
> System.out.println("Input memory error, memory is default
> value");
> else
> {
> memory=m;
> show_memory();
> }
> }
>
> public void set(double c,double m)
> {
> set(c);
> set_memory(m);
> }
> void show_cpu()
> {
> System.out.println("cpu="+this.cpu);
> }
> void show_memory()
> {
> System.out.println("memory="+this.memory);
> }
> void show_all()
> {
> this.show_cpu();
> this.show_memory();
> }}
>
> public class bbb
> {
> public static void main(String args[])
> {
> CComputer c1=new CComputer();
>
> c1.set(-3.5,1.5);
> c1.show_all();
> c1.set(-3.5,-2.0);
> c1.show_all();
> c1.set(-3.5);
> c1.show_all();
> }}
>
> +++++++
>
> The output of "c1.set(-3.5,-2.0);" shows memory=1.5.
> The output of " c1.set(-3.5);" also shows memory=1.5;
>
> Why? I thought memory should be default value, i.e. 1.0.
>
> Thank you in advance.
>
> Mike

You're default value only gets assigned during the construction of
your object.

your "new CComputer()" sets it to 1.0, You're first call to set(-3.5,
1.5) sets that value to 1.5, and it isn't ever reset.