150 likes | 164 Views
Examples of class. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Example: complex number. public class ComplexNumber { private double real; private double img; // override default constructor public ComplexNumber () { real = 0.0; img = 0.0; }
E N D
Examples of class Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
Example: complex number public class ComplexNumber { private double real; private double img; // override default constructor public ComplexNumber () { real = 0.0; img = 0.0; } // a more meaningful constructor public ComplexNumber (double x, double y) { real = x; img = y; }
Example: complex number // compute magnitude public double Magnitude () { return (Math.sqrt(real*real + img*img)); } // compute argument public double Argument () { return (Math.atan (img/real)); } // add a complex number to it public void Add (ComplexNumber c) { real += c.GetReal(); img += c.GetImg(); }
Example: complex number // add the Get/Set methods public double GetReal () { return real; } public double GetImg () { return img; } public void SetReal (double x) { real = x; } public void SetImg (double x) { img = x; } } // end class
Example: complex number class ComplexNumberTester { public static void main (String a[]) { int n = 10, i; // construct 1+i ComplexNumber c1 = new ComplexNumber (1, 1); // construct 1-i ComplexNumber c2 = new ComplexNumber (1, -1); // add these two c1.Add(c2); // array of complex numbers ComplexNumber rootsOfUnity[] = new ComplexNumber[n];
Example: complex number for (i=0; i<n; i++) { rootsOfUnity[i] = new ComplexNumber (Math.cos (2*i*Math.PI/n), Math.sin (2*i*Math.PI/n)); } } }
Example: room public class Room { private int numDoors; private int numWindows; private int numLamps; private int numFans; private String wallColor; // Override default constructor public Room () { numDoors = 1; numWindows = 2; numLamps = 1; numFans = 1; wallColor = “White”; }
Example: room // a richer constructor public Room (int nD, int nW, int nL, int nF, String wC) { numDoors = nD; numWindows = nW; numLamps = nL; numFans = nF; wallColor = new String (wC); // what is the problem with wallColor = wC ? }
Example: room // might want to add a fan public void AddFan (int howmany) { numFan += howmany; } // might want to re-paint wall public void PaintWall (String whichcolor) { wallColor = new String (whichcolor); } }
Example: room public class House { // array of rooms private Room rooms[]; // might have some people private int numHuman; // might have some animals private int numPets; // might have a garden private boolean isThereAGarden; // might have a swimming pool private boolean isThereASwimmingPool;
Example: room // override default constructor public House () { rooms = new Room[1]; rooms[0] = new Room ();// default room numHuman = 1; numPets = 1; isThereAGarden = true; isThereASwimmingPool = false; }
Example: room // More useful constructor public House (int nD[], int nW[], int nL[], int nF[], String wC[], int nH, int nP, boolean g, boolean sp) { int numRooms = nD.length; int i; rooms = new Room[numRooms]; for (i=0; i<numRooms; i++) { rooms[i] = new Room (nD[i], nW[i], nL[i], nF[i], wC[i]); } numHuman = nH;
Example: room numPets = nP; isThereAGarden = g; isThereASwimmingPool = sp; } // might want to add a fan in some room public void AddFan (int roomID) { rooms[roomID].AddFan (1); } // paint wall of some room public void PaintWall (int roomID, String color) { rooms[roomID].PaintWall (color); }
Example: room // add a pet public void BringPet () { numPets++; } // get reference to a room public Room GetRoom (int roomID) { return rooms[roomID]; } }
Example: room class HouseBuilder { public static void main (String a[]) { House myHouse = new House (); // repaint wall myHouse.GetRoom(0).PaintWall (“Golden”); // could paint wall of room[0] as below also // myHouse.PaintWall (0, “Golden”); // bring a pet myHouse.BringPet(); } }