[JAVA] Constructors!

Kush

High On Life!
Joined
13 Jan 2002
Messages
4,590
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

  • GCF.rar
    685 bytes · Views: 72
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"
 
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?
 
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
 
1.5.x is the latest Geffy, school here is behind the time as well.
 
1.5 isnt really what i would class as being in circulation
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,623
Latest member
AndersonLo
Back