1 Examples of class Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Operator overloading redefine the operations of operators
1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Written by: Dr. JJ Shepherd
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
ADT Specification Example
CS1110: Computer Science I Chapter 5 Arrays. One-dimensional Arrays int [] data = new int [4]; data[0] = 1; data[1] = 3; data[2] = 5; data[3] = 7; Arrays.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
1 Library Methods and Recursion Instructor: Mainak Chaudhuri
CompSci Data Types & Operations. CompSci The Plan  Overview  Data Types  Operations  Code Samples  ChangeMaker.java  PolarCoordinate.java.
1 Methods Instructor: Mainak Chaudhuri
1 Inheritance and Subclasses Instructor: Mainak Chaudhuri
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Classes Instructor: Mainak Chaudhuri
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
1 Conditionals Instructor: Mainak Chaudhuri
1 Operators and Expressions Instructor: Mainak Chaudhuri
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
1 Announcements B7 tutorial today in CS103 –In CSE department –Ask at entry desk for direction to CS103.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
1 Examples of Recursion Instructor: Mainak Chaudhuri
CS 2430 Day 12. Agenda for today Container class example: DateList Growable container classes.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Lab 2. Prepare: 1.Create a folder named lab2 inside the workspace. 2.Download RecordDB.java and Record.java from the lab document. 3.Change the file name.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
GC211 Data structure Lecture 3 Sara Alhajjam.
Examples of class: Using System.in.read ()
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
Review Operation Bingo
null, true, and false are also reserved.
Classes & Objects: Examples
COMPUTER 2430 Object Oriented Programming and Data Structures I
Unit-2 Objects and Classes
Java Lesson 36 Mr. Kalmes.
Code Animation Examples
Recap Week 2 and 3.
Simple Classes in Java CSCI 392 Classes – Part 1.
PreAP Computer Science Review Quiz 08
Object-Oriented Programming and class Design
Object-Oriented Programming and class Design
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Chapter 5 Classes.
Instructor: Mainak Chaudhuri
Presentation transcript:

1 Examples of class Instructor: Mainak Chaudhuri

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”); // bring a pet myHouse.BringPet(); }

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