0

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.

0

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".

0

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.

0

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!

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.