1 / 15

Examples of class

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

stinsley
Download Presentation

Examples of class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Example: room // add a pet public void BringPet () { numPets++; } // get reference to a room public Room GetRoom (int roomID) { return rooms[roomID]; } }

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

More Related