Docsity

CSE 1301 Lecture 7: Object-Oriented Programming and Software Development - Prof. Briana Mo, Exams of Computer Science

Toggle sidebar
bg1
CSE 1301
Lecture 7
Object Oriented Programming
Figures from Lewis, “C# Software Solutions ”, Addison Wesley
Briana B. Morrison
CSE 1301
7-2
Topics
• Software Development
• Identifying Classes & Objects
• Static Members
• Class Relationships
• “this”
• Parameters Revisited
• Overloading
– Methods
– Operators
• Testing
CSE 1301
7-3
The Software Life Cycle
• The overall life cycle of a program includes use and
maintenance:
• A version of the software that is made available to
user is called a release
Use
Development
Maintenance
CSE 1301
7-4
Development vs. Maintenance
Use and
Maintenance
Development
CSE 1301
7-5
The Waterfall Model
Establish
requirements
Create
design
Implement
code
Test
system
CSE 1301
7-6
An Iterative Development Process
Establish
requirements
Create
design
Implement
code
Test
system
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CSE 1301 Lecture 7: Object-Oriented Programming and Software Development - Prof. Briana Mo and more Exams Computer Science in PDF only on Docsity!

CSE 1301

Lecture 7

Object Oriented Programming

Figures from Lewis, “C# Software Solutions”, Addison Wesley

Briana B. Morrison

CSE 1301 (^) 7-

Topics

• Software Development

• Identifying Classes & Objects

• Static Members

• Class Relationships

• “this”

• Parameters Revisited

• Overloading

  • Methods
  • Operators

• Testing

CSE 1301 (^) 7-

The Software Life Cycle

• The overall life cycle of a program includes use and

maintenance:

• A version of the software that is made available to

user is called a release

Development^ Use

Maintenance

CSE 1301 (^) 7-

Development vs. Maintenance

Use and Maintenance

Development

The Waterfall Model

Establish requirements

Create design

Implement code

Test system

An Iterative Development Process

Establish requirements

Create design

Implement code

Test system

CSE 1301 (^) 7-

Identifying Classes & Objects

• Identify potential classes within the

specification

• Nouns

CSE 1301 (^) 7-

CSE 1301 (^) 7-

In Gaming…

• How do you identify the classes within a

game?

• What have you seen so far in labs and

assignments?

CSE 1301 (^) 7-

What Goes in the Class

• How do you decide what should be in

the class

– Data

– Methods

• Not easy, but must be done

Static Members

• Static methods can be invoked via the

class name

• Static variables are stored at the class

level (one copy for all instances)

The static Modifier

  • Remember that static methods (also called class methods) that can be invoked through the class name rather than through a particular object
  • For example, the methods of the Math class are static: Math.sqrt (25)
  • To write a static method, we apply the static modifier to the method definition
  • The static modifier can be applied to variables as well
  • It associates a variable or method with the class rather than with an object

CSE 1301 (^) 7-

Rational Class Design

• Let’s design a class that represents a

rational number (fraction)

• What data needs to be stored?

• What methods or operations are

needed?

• Look at the following client code:

CSE 1301 (^) 7-

Rational r1 = new Rational(6, 8); Rational r2 = new Rational(1, 3); Rational r3, r4, r5, r6, r7; Console.Out.WriteLine("First rational number: " + r1); Console.Out.WriteLine("Second rational number: " + r2); if (r1.Equals(r2)) Console.Out.WriteLine("r1 and r2 are equal."); else Console.Out.WriteLine("r1 and r2 are NOT equal."); r3 = r1.Reciprocal(); Console.Out.WriteLine("The reciprocal of r1 is: " + r3); r4 = r1.Add(r2); r5 = r1.Subtract(r2); r6 = r1.Multiply(r2); r7 = r1.Divide(r2); Console.Out.WriteLine("r1 + r2: " + r4); Console.Out.WriteLine("r1 - r2: " + r5); Console.Out.WriteLine("r1 * r2: " + r6); Console.Out.WriteLine("r1 / r2: " + r7); Console.In.ReadLine(); // Wait for enter key

CSE 1301 (^) 7-21 CSE 1301 (^) 7-

UML Design

Rational

Constructor

public Rational (int numer, int denom)

Reciprocal

public Rational Reciprocal ( )

CSE 1301 (^) 7-

ToString

public override string ToString ( )

CSE 1301 (^) 7-

Reduce

private void Reduce ( ) { if (numerator != 0) { int common = Gcd(Math.Abs(numerator), denominator);

numerator = numerator / common; denominator = denominator / common; } }

CSE 1301 (^) 7-

Gcd

private int Gcd (int num1, int num2)

while (num1 != num2)

if (num1 > num2)

num1 = num1 - num2;

else

num2 = num2 - num1;

return num1;

CSE 1301 (^) 7-

Add

public Rational Add (Rational op2)

Subtract

public Rational Subtract (Rational op2)

Multiply

public Rational Multiply (Rational op2)

CSE 1301 (^) 7-

The this reference

• The this reference can also be used to

distinguish the parameters of a constructor

from the corresponding instance variables

with the same names

public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; }

CSE 1301 (^) 7-

Assignment Revisited

• The act of assignment takes a copy of a

value and stores it in a variable

• For primitive types:

num2 = num1;

Before

num

5

num

12

After

num

5

num

5

CSE 1301 (^) 7-

Reference Assignment

• For object references, assignment copies

the memory location:

bishop2 = bishop1;

Before

bishop1 bishop

After

bishop1 bishop

CSE 1301 (^) 7-

Aliases

• Two or more references that refer to the same

object are called aliases of each other

• One object (and its data) can be accessed

using different reference variables

• Aliases can be useful, but should be managed

carefully

• Changing the object’s state (its variables)

through one reference changes it for all of its

aliases

Parameters

There are three main types of parameters:

  1. Value - passes a copy (value) of the variable to the method. This is the default.
  2. Reference - passes a reference to the actual variable. Marked with "ref", use this when you want to pass a value in and have any change to that value be persistent when the method is complete
  3. Out - passes a reference to the actual variable. Marked with "out", use this when you want the method to generate a value and place it for later use in the actual variable (persists when the method is complete)

using System; namespace ParameterTester { public class Num { private int value; // Sets up the new Num object, storing an initial value. public Num (int update) { value = update; } // Sets the stored value to the newly specified value. public void SetValue (int update) { value = update; }

// Returns the stored integer value as a string. public override string ToString ( ) { return value + ""; } } }

CSE 1301 (^) 7-

using System; namespace ParameterTester { class ParameterPassing { static void Main(string[] args) { ParameterModifier modifier = new ParameterModifier(); int a1 = 111; Num a2 = new Num(222); Num a3 = new Num(333); Console.Out.WriteLine("Before calling changeValues:"); Console.Out.WriteLine("a1\ta2\ta3"); Console.Out.WriteLine(a1 + "\t" + a2 + "\t" + a3 + "\n"); modifier.ChangeValues(a1, a2, a3); Console.Out.WriteLine("After calling changeValues:"); Console.Out.WriteLine("a1\ta2\ta3"); Console.Out.WriteLine(a1 + "\t" + a2 + "\t" + a3 + "\n"); Console.In.ReadLine(); // Wait for enter key } } } (^) CSE 1301 (^) 7-

using System;

namespace ParameterTester { public class ParameterModifier {

public void ChangeValues(int f1, Num f2, Num f3) { Console.Out.WriteLine("Before changing the values:"); Console.Out.WriteLine("f1\tf2\tf3"); Console.Out.WriteLine(f1 + "\t" + f2 + "\t" + f3 + "\n");

f1 = 999; f2.SetValue(888); f3 = new Num(777); Console.Out.WriteLine("After changing the values:"); Console.Out.WriteLine("f1\tf2\tf3"); Console.Out.WriteLine(f1 + "\t" + f2 + "\t" + f3 + "\n"); Console.In.ReadLine(); } } }

CSE 1301 (^) 7-45 CSE 1301 (^) 7-

Example 1

static void Main() { int a = 42; Console.WriteLine (a); B (a); Console.WriteLine (a); } static void B (int x) { x += 9; Console.WriteLine (x); }

Example 2

static void Main() { int a = 42; Console.WriteLine (a); B (ref a); Console.WriteLine (a); } static void B (ref int x) { x += 9; Console.WriteLine (x); }

Overloading Operators

• In C#, not only can methods be

overloaded, operators can be

overloaded as well.