37
DT265-2 Object Oriented Software Development 2 Lecture 1 : Revision Lecturer Pat Browne

DT265-2 Object Oriented Software Development 2 Lecture 1 : Revision

  • Upload
    hei

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

DT265-2 Object Oriented Software Development 2 Lecture 1 : Revision. Lecturer Pat Browne. Syllabus. Revision Advanced OO programming Polymorphism, Classes and Interfaces Nested classes Anonymous functions Attributes/annotations etc . Generics Concurrency Threading and concurrency - PowerPoint PPT Presentation

Citation preview

Page 1: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

DT265-2 Object Oriented Software Development 2

Lecture 1 : Revision

Lecturer Pat Browne

Page 2: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Syllabus• Revision• Advanced OO programming

– Polymorphism, Classes and Interfaces– Nested classes– Anonymous functions– Attributes/annotations etc.– Generics

• Concurrency – Threading and concurrency– Thread synchronization– Parallel extensions and platforms

• Data Structures & Algorithms – Implement complex data structures and algorithms in an OO programming

language e.g. stacks queues, trees, heaps etc.

Page 3: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

What is a Programming Language?

• A PL helps people to express ideas in a form that can be understood by a computer.

Figure from: Java Programming for Spatial Sciences by Jo Wood

Page 4: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Writing, compiling, running

• We will use VisualStudio for writing, compiling and running programs

• Start VisualStudio for C#• Make a new project called Lab1• All the project files will be stored• ..\Visual Studio 11\Projects

• If using laptop• C:\Users\YourName\Documents\Visual Studio 11\Projects

Page 5: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Writing, compiling, running

• Run Program1• Alter Program1 to print your name.• In order for a computer to "execute" or "run" a program, the program must be

translated into the ones and zeros that the computer "understands."

Page 6: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Saving and renaming programs

• Save Program1.cs• To move to next program• Save as Program2.cs• Include current • Exclude other programs

Page 7: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Variables

• A variable has name, type, value, address size, and scope. The C# programming language is a strongly typed language, which means that every variable and every expression has a type that is known at compile time (leaving aside dynamic typing for the moment Lab1-Program6). Types limit the values that a variable can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong typing helps detect errors at compile time.

• http://msdn.microsoft.com/en-us/library/orm-9780596521066-01-03.aspx• http://en.wikibooks.org/wiki/C_Sharp_Programming/Variables

Page 8: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Variables

• The purpose of variable is to hold a value. We can say x holds the value 4 as follows

• int x = 4;• Every variable in C# must have a type.• We can change the value of x during the

program, as follows:• x = 3;

Page 9: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Variables

• Variables are somewhat like variables in maths, but there are differences.

• int x,y,z;• In the following is similar to maths.• x = y + z;• But the next line is not true in maths.• x = x + 3;• These are assignments where the variable on the LHS

is made to store the expression on RHS.

Page 10: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Variables• Here is a program with a variable, that is changed during the program.

class Program2 static void Main(string[] args) { int x = 4; Console.WriteLine("x is " + x); // What Happens if we remove the comment below? // x ="hello"; x = 5; Console.WriteLine("x is " + x); Console.WriteLine("x + x " + (x + x)); Console.WriteLine("Press Any"); Console.ReadKey();} }}

Run and explain Program.Remove comment, what does VisualStudio tell you?

Page 11: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Variable with wrong type• For the moment we will insist that x can only be of

type int. So the following is not allowed:• x = “hello”;

Page 12: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Java Primitive Data Types• The type of value that a variable can hold is called data type. When we

declare a variable we need to specify the type of value it will hold along with the name of the variable. This tells the compiler that the particular variable will hold certain amount of memory to store values. For example, in the code above x is of type int and takes 32 bits to hold the integer value.

Page 13: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Java Primitive Data Types• Variable declaration and initialization• int a; // This is a declaration• int a = 0; // This is an declaration and initialization• It is possible to declare a variable without giving it a value. • // represents the start of a comment

Page 14: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Operators

• http://en.wikibooks.org/wiki/C_Sharp_Programming/Operators

Page 15: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Logical Operators int waterLevel = 20; int dangerLevel = 15; bool floodRisk; bool inFloodPlane = true; bool stormEvent = true; floodRisk = (inFloodPlane || stormEvent) && (waterLevel >= dangerLevel);

Console.WriteLine(“Current flood risk "+ floodRisk );

Add code to the existing program to prompt the user for a new water level and print out the new flood risk

Page 16: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sequence, iteration, and condition

• Sequence, iteration, and condition control the flow of a program.

Page 17: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sequence, Iteration, and condition

• We have already seen sequence. We say how one program statement could follow another. However, we can also have sequences of conditions or iterations.

Page 18: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Iteration class Program0{ static void Main(string[] args){ for(int i=1; i<11; i++){ Console.WriteLine("Count is: " + i); }

Console.WriteLine("Press Any"); Console.ReadKey(); }

}

Page 19: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Condition (if) // Condition int value1 = 1; int value2 = 2; if(value1 == value2) Console.WriteLine("value1 == value2"); if(value1 != value2) Console.WriteLine("value1 != value2"); if(value1 > value2) Console.WriteLine("value1 > value2"); if(value1 < value2) Console.WriteLine("value1 < value2"); if(value1 <= value2) Console.WriteLine("value1 <= value2");

Page 20: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Condition (if-else)

int testscore = 76; char grade;

if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; }

Page 21: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

While loopclass IntInApp{

public static void Main() {

int i = 1;int sum = 0;Console.Write("While loop:");

while (i != 0) {Console.Write("Please input numbers 0 to exit:");i = int.Parse(Console.ReadLine());sum = sum + i;Console.WriteLine("Count = "+ sum);}Console.WriteLine("Total = " + sum); Console.WriteLine("Press Any"); Console.ReadKey();} }

Page 22: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Distance public class DistanceApp {static void Main(string[] args) {int x1, y1, x2, y2;double distance;

Console.Write("Enter the x coordinate for point 1: ");x1 = int.Parse(Console.ReadLine());Console.Write("Enter the y coordinate for point 1: ");y1 = int.Parse(Console.ReadLine());Console.Write("Enter the x coordinate for point 2: ");x2 = int.Parse(Console.ReadLine());Console.Write("Enter the y coordinate for point 2: ");y2 = int.Parse(Console.ReadLine());

distance = Math.Sqrt( Math.Pow((x2 - x1),2) + Math.Pow((y2 - y1),2));

}}

Change to floating point input using nextDouble and change coord to double.

Page 23: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

One-Dimensional arrays

• An array is an ordered collection, or numbered list, of values. All of the values in an array must be of the same type. The type of the array is the type of the values it holds, followed by the characters []. An index is used to refer to individual values in the array. If we have N values, we think of them as being numbered from 0 to N-1. Each cell of the array called

num contains a value.

Page 24: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

1 dimensional arrays // 8 elements indexed 0 to 7int[] a = new int[] { 17, 3, 60, 128, 11, 11, 2, 8 };for (int i = 0; i < 8; i++) Console.Write(a[i] + " ");• Make the above program omit the last element.• Make the above program omit the first element.• If we have N values, we think of them as being numbered

from 0 to N-1.

Page 25: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Two dimensional array

Above Java like, use [,] as per labs. Each cell of the array contains a value

Page 26: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Two dimensional arraypublic class ArrayApp2 { //... Define named constants to centralize definitions static final int ROWS = 2; static final int COLS = 4;

public static void main(String[] args) { int[][] a2 = new int[ROWS][COLS]; string output = ""; // Accumulate text here //Print array in rectangular form using nested for loops. for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { output += " " + a2[row][col];} output += "\n";} // display output • Convert from Java [][] to [,]• Assign the row value to each element• Assign the column value to each element.

Page 27: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Functionsclass FactApp { static public void main (String [ ] args) {

int a = Integer.parseInt(args[0]); int f = factorial (a); System.out.println("Factorial of " + a + " is " + f); }

static public int factorial (int n ) { int c = n - 1; int r; if (c > 0) r = n * factorial(c); else r = 1; return r; }}

• The factorial of a number N, denoted by N!, is the product of all positive numbers less than or equal to N.

• Convert from Java• Takes input from command line. Change to take input from prompt.• Make a function to increment an integer• Make a function to add two integers

Page 28: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Procedures (methods) import java.util.Scanner;public class MeanApp {public static int numSamples; // Number of samples to be computed.public static float total=0.0f, // Total of all sample values. mean =0.0f; // Mean of the sample values. // Asks user for number of samples and gathers input from the keyboard. public static void gatherInput() { // Create a new KeyboardInput object for input. Scanner keyIn = new Scanner (System.in); int count; // Counts through samples. System.out.print("How many samples? :"); numSamples = keyIn.nextInt(); for (count=1; count <= numSamples; count++) {System.out.print("Sample number " + count + " :"); total += keyIn.nextFloat(); } // Do the mean calculation. mean = total / numSamples;} // Displays the results of sample mean calculations. public static void displayResults() { System.out.println("The mean of the " + numSamples + " samples is " + mean); }

public static void main(String[] args){gatherInput();

displayResults();}}// Convert from Java

Page 29: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

2D arrays

Page 30: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

CSV2DArrayApp

• This program reads a comma separated file into a two dimensional array.

• Copy the program and data files, called male.csv and female.csv to your folder.

• The dimensions of the array areint [][] numbers = new int[13][3];int [,] numbers = new int[13,3];• Change these to [20,20] and then to [12,3], and then

to [13,2]

Page 31: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sorting

• public static void Sort()• {• int list[] = {10,7,19,5,16};• sortList(list);• }

Page 32: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sorting

do { displayList(list); listChanged = false;

for (int i=0; i<list.length-1; i++) { if (list[i] > list[i+1]) {temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; listChanged = true;}} } while (listChanged == true);

Page 33: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sorting swapping elements

temp = list[i]; list[i] = list[i+1]; list[i+1] = temp;

Page 34: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Sorting

do { displayList(list); listChanged = false;

for (int i=0; i<list.length-1; i++) { if (list[i] > list[i+1]) {temp = list[i]; list[i] = list[i+1]; list[i+1] = temp; listChanged = true;}} } while (listChanged == true);

Page 35: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Bubble Sort Algorithm

Elements of array list during the first iteration

Elements of array list during the second iteration

Page 36: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Bubble Sort Algorithm

Elements of array list during the third iteration

Elements of array list during the fourth iteration

Page 37: DT265-2  Object Oriented Software Development 2 Lecture 1 : Revision

Location Quotients

• See program Program5.cs and handout.