• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

[JAVA] Constructors!

Kush

High On Life!
#1
hey guys im in ap computer science (java) and i am not undrestanding how to define contructors or how to use them/send data back and forth from them. i undrestand its like a function in visual basic, but does anyone know how to guide me through it, the teacher said its really important for the whole year, and i am not getting anything i have attached one java file along with its contructor
 

Attachments

#2
well you basically dont send data back from them at all, you can pass information in in the form of parameters

Code:
class Member {
    private String name;
    private String location;
    private int age;

    // Constructors, they have no return type, and must be marked "public"
    // empty constructor, no arguments/parameters, so it fills the fields with default values
    public Member() {
        name = "John Doe";
        location = "Utopia";
        age = 31;
    }

    // constructor which creates specialized versions of the class
    public Member(final String pName, final String pLocation, final int pAge) {
        name = pName;
        location = pLocation;
        age = pAge;
    }
}
you could then do the following
Code:
Member Anonymous = new Member();  // creates John Doe, age 31 in Utopia
Member Geffy = new Member("Geffy", "United Kingdom", 20);  // creates Geffy, age 20 in United Kingdom
Now for the more specific version for your code, mainly in the GCF.java

Code:
public class Gcf{
	
	private double nr1;
	private double nr2;
	private double gcf;
	private double temp;
	
	// you need the () to show this as a function/constructor
	public Gcf{
		this(0,0,0);   // this is effectively trying to call a constructor with three parameters, you have not defined a constructor which does this
	}

        // dont know what you are trying to do with this one
	public Gcf(Gcf g){
		this(g,nr1,nr2,gcf);
	}
}
If you can tell me how you are wanting to use this more specifically, and also indicate which of your parameters are supposed to be what in your first constructor. Also the second constructor, I think you are trying to create a cloning constructor in which case you would want to do it more like this
Code:
public Gcf(Gcf g) {
    nr1 = g.nr1;
    nr2 = g.nr2;
    gcf = g.gcf;
}
This uses the Gcf Object "g" and copies the contents of its fields into the newly constructed Gcf Object, thereby creating a clone of the Gcf Object "g"
 

Kush

High On Life!
#3
i was trying to get the greatest common factor of number 1 and number 2, btw do u use jcreator? if u dont want compiler do u use?
 
#4
For the compiler, I install the Sun JDK, current version is 1.4.2_05 I think

Then you add the path to the jdk binaries to your Environment Path
this is in Start->Control Panel->System then click the Advanced tab, then the Environment Variables button near the bottom, find the Path entry for the system variables and edit that, add ;C:\j2sdk1.4.2_05\bin to the end of the line (the actual C:\.... part may change depending on your JDK version.
Once that is done you can then use the javac and java commands on the command line to compile and execute your java code.

BTW I typically use SciTE for writing Java code.

OK as you are trying to get the Greatest Common factor, then you would want to have something like these for your constructors
Code:
public Gcf() {
    this(0,0);
}
public Gcf(final double pNr1, final double pNr2) {
    nr1 = pNr1;
    nr2 = pNr2;
    temp = 0.0;
}
That should get things roughly the way you want them
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies