Its only called a no-argument constructor if it is a constructor with no arguments. What you posted is a no-argument method. And the purpose of a no-argument method can be almost anything. For example, lets say you wanted to print out a menu to the user.

public void printMenu(){

System.out.println("Type 1 for Whatever");
System.out.println("Type 2 for Whatever Else");
}


Obviously, no-argument methods aren't only for printing menus, they can be used for lots of different things, but that's one example.

Oh I see, then what exactly is a no argument constructor?

A constructor is a special construct of the Java programming language which can be loosely thought of as a method with the same name as that of the class for which the constructor is meant with *no* return type and which will *always* be invoked when an instance creation is requested.

A no-arg constructor is a constructor which doesn't take any arguments.

public class Person {
  private String name;

  // no-arg constructor
  public Person() {
    this.name = "default";
  }

  // a constructor with one argument, name
  public Person(String name) {
    this.name = name;
  }
}

In case you fail to provide a constructor, a `default' constructor is automatically provided to you which *always* will be a no-arg constructor.

I would recommend you first need to read the basic Java tutorial hosted on Sun and those posted in the stickies at the top of this forum since all these questions are already answered there. If you really want to learn, your questions should *not* be of the form "What is XXX?" rather "Given that XXX is YYY, I really don't how ZZZ fits the picture".

Thanks for all the help, so after about 30 minutes of starting the code:

public class Complex{
	
	private double real;
	private double imag;
	
	public Complex(){
		this.real=0;
		this.imag=0;
	}//Complex
	
	public Complex(double real, double imag) {
		this.real = real;
		this.imag = imag;
	}
	//Complex
	public void setReal(double real) {
		this.real = real;
	}//setReal
	
	public void setImag(double imag) {
		this.imag = imag;
	}//setImag
	
	public Complex add(Complex num) {
		Complex total = new Complex();
		total.setReal(this.real + real);
		total.setImag(this.imag + imag);
		return total;
	}//add
	
	public Complex subtract(Complex num) {
		Complex total = new Complex();
		total.setReal(this.real + real);
		total.setImag(this.imag + imag);
		return total;
	}//subtract
	
}//Complex Constructor

When ran:

Complex@19821f
Complex@addbf1
Complex@42e816
Complex@9304b1
num3 = Complex@190d11
Complex@a90653

Don't need you to give me where the error is, but a little help appreciated, sick right now and about to go eat dinner with family, I'll relook it later.

javaAddict wrote in #7 post http://www.daniweb.com/forums/post764802-7.html about method toString() .
Complex-class extends by default Object-class.
Look in javadoc - class java.lang.Object for this method.
If this method is absent in Complex-class , the default method toString() of Object-class is invoked.

Alright, how does this look?

public class Complex{
	
	private double real;
	private double imag;
	
	public Complex(){
		this.real=0;
		this.imag=0;
	}//Complex
	
	public Complex(double real, double imag) {
		this.real = real;
		this.imag = imag;
	}
	//Complex
	public void setReal(double real) {
		this.real = real;
	}//setReal
	
	public void setImag(double imag) {
		this.imag = imag;
	}//setImag
	
	public double getReal() {
 		return this.real;
	}
  
	public double getImag() {
		return this.imag;
	}
	
	public Complex add(Complex num) {
		Complex total = new Complex();
		total.setReal(this.real + num.getReal());
		total.setImag(this.imag + num.getImag());
		return total;
	}//add
	
	public Complex subtract(Complex num) {
		Complex total = new Complex();
		total.setReal(this.real - num.getReal());
		total.setImag(this.imag - num.getImag());
		return total;
	}//subtract
	
	public String toString(){
		if (real == 0) {
			return ( imag +"i" );
		}//if
	 	else if (imag == 0 ) {
			return String.valueOf ( real );
		}//else if
		else if (imag == 0 && real == 0 ){
			return ( "0" );
		}//else if
		else if ( imag < 0 ) {
			return (real +"" +imag  +"i");
		}//else if
		else	
			return (real +"+" +imag +"i");	
	}//toString
	
}//Complex Constructor

The print out is all right, just wondering if I have any conventional errors, as I'm new to how you name, etc constructors.

Thanks for all the help everyone!

Just want to comment that you're using this. correctly now. Except in your no argument constructor, "this." isn't necessary at all (although it doesn't hurt, either).

And it looks fine to me. Also, remember to mark solved when you're done asking qns

Will do, and Thanks so much everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.