Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Examples of class Instructor: Mainak Chaudhuri

Similar presentations


Presentation on theme: "1 Examples of class Instructor: Mainak Chaudhuri"— Presentation transcript:

1 1 Examples of class Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

2 2 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; }

3 3 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(); }

4 4 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

5 5 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];

6 6 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)); }

7 7 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”; }

8 8 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 ? }

9 9 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); }

10 10 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;

11 11 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; }

12 12 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;

13 13 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); }

14 14 Example: room // add a pet public void BringPet () { numPets++; } // get reference to a room public Room GetRoom (int roomID) { return rooms[roomID]; }

15 15 Example: room class HouseBuilder { public static void main (String a[]) { House myHouse = new House (); // repaint wall myHouse.GetRoom(0).PaintWall (“Golden”); // bring a pet myHouse.BringPet(); }

16 16 Announcements Class and lab on this Saturday Notes on classes and objects in CopyPoint Extra class on 12 th November 1400 to 1615 in L7


Download ppt "1 Examples of class Instructor: Mainak Chaudhuri"

Similar presentations


Ads by Google