Examples of class Instructor Mainak Chaudhuri mainakccse iitk

  • Slides: 16
Download presentation
Examples of class Instructor: Mainak Chaudhuri mainakc@cse. iitk. ac. in 1

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

Example: complex number public class Complex. Number { private double real; private double img;

Example: complex number public class Complex. Number { private double real; private double img; // override default constructor public Complex. Number () { real = 0. 0; img = 0. 0; } // a more meaningful constructor public Complex. Number (double x, double y) { real = x; img = y; } 2

Example: complex number // compute magnitude public double Magnitude () { return (Math. sqrt(real*real

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 (Complex. Number c) { real += c. Get. Real(); img += c. Get. Img(); } 3

Example: complex number // add the Get/Set methods public double Get. Real () {

Example: complex number // add the Get/Set methods public double Get. Real () { return real; } public double Get. Img () { return img; } public void Set. Real (double x) { real = x; } public void Set. Img (double x) { img = x; } } // end class 4

Example: complex number class Complex. Number. Tester { public static void main (String a[])

Example: complex number class Complex. Number. Tester { public static void main (String a[]) { int n = 10, i; // construct 1+i Complex. Number c 1 = new Complex. Number (1, 1); // construct 1 -i Complex. Number c 2 = new Complex. Number (1, -1); // add these two c 1. Add(c 2); // array of complex numbers Complex. Number roots. Of. Unity[] = new 5 Complex. Number[n];

Example: complex number for (i=0; i<n; i++) { roots. Of. Unity[i] = new Complex.

Example: complex number for (i=0; i<n; i++) { roots. Of. Unity[i] = new Complex. Number (Math. cos (2*i*Math. PI/n), Math. sin (2*i*Math. PI/n)); } } } 6

Example: room public class Room { private int num. Doors; private int num. Windows;

Example: room public class Room { private int num. Doors; private int num. Windows; private int num. Lamps; private int num. Fans; private String wall. Color; // Override default constructor public Room () { num. Doors = 1; num. Windows = 2; num. Lamps = 1; num. Fans = 1; wall. Color = “White”; } 7

Example: room // a richer constructor public Room (int n. D, int n. W,

Example: room // a richer constructor public Room (int n. D, int n. W, int n. L, int n. F, String w. C) { num. Doors = n. D; num. Windows = n. W; num. Lamps = n. L; num. Fans = n. F; wall. Color = new String (w. C); // what is the problem with wall. Color = w. C ? 8 }

Example: room // might want to add a fan public void Add. Fan (int

Example: room // might want to add a fan public void Add. Fan (int howmany) { num. Fan += howmany; } // might want to re-paint wall public void Paint. Wall (String whichcolor) { wall. Color = new String (whichcolor); } } 9

Example: room public class House { // array of rooms private Room rooms[]; //

Example: room public class House { // array of rooms private Room rooms[]; // might have some people private int num. Human; // might have some animals private int num. Pets; // might have a garden private boolean is. There. AGarden; // might have a swimming pool private boolean is. There. ASwimming. Pool; 10

Example: room // override default constructor public House () { rooms = new Room[1];

Example: room // override default constructor public House () { rooms = new Room[1]; rooms[0] = new Room (); // default room num. Human = 1; num. Pets = 1; is. There. AGarden = true; is. There. ASwimming. Pool = false; } 11

Example: room // More useful constructor public House (int n. D[], int n. W[],

Example: room // More useful constructor public House (int n. D[], int n. W[], int n. L[], int n. F[], String w. C[], int n. H, int n. P, boolean g, boolean sp) { int num. Rooms = n. D. length; int i; rooms = new Room[num. Rooms]; for (i=0; i<num. Rooms; i++) { rooms[i] = new Room (n. D[i], n. W[i], n. L[i], n. F[i], w. C[i]); } 12 num. Human = n. H;

Example: room num. Pets = n. P; is. There. AGarden = g; is. There.

Example: room num. Pets = n. P; is. There. AGarden = g; is. There. ASwimming. Pool = sp; } // might want to add a fan in some room public void Add. Fan (int room. ID) { rooms[room. ID]. Add. Fan (1); } // paint wall of some room public void Paint. Wall (int room. ID, String color) { rooms[room. ID]. Paint. Wall (color); 13 }

Example: room // add a pet public void Bring. Pet () { num. Pets++;

Example: room // add a pet public void Bring. Pet () { num. Pets++; } // get reference to a room public Room Get. Room (int room. ID) { return rooms[room. ID]; } } 14

Example: room class House. Builder { public static void main (String a[]) { House

Example: room class House. Builder { public static void main (String a[]) { House my. House = new House (); // repaint wall my. House. Get. Room(0). Paint. Wall (“Golden”); // bring a pet my. House. Bring. Pet(); } } 15

Announcements • Class and lab on this Saturday • Notes on classes and objects

Announcements • Class and lab on this Saturday • Notes on classes and objects in Copy. Point • Extra class on 12 th November 1400 to 1615 in L 7 16