html5-img
1 / 37

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

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

hei
Download Presentation

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

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. DT265-2 Object Oriented Software Development 2Lecture 1 : Revision Lecturer Pat Browne

  2. 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.

  3. 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

  4. 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

  5. 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."

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

  7. 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

  8. 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;

  9. 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.

  10. 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?

  11. 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”;

  12. 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.

  13. 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

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

  15. Logical Operators intwaterLevel = 20; intdangerLevel = 15; boolfloodRisk; boolinFloodPlane = true; boolstormEvent = 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

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

  17. 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.

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

  19. 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");

  20. Condition (if-else) inttestscore = 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'; }

  21. While loop class IntInApp{ public static void Main() { inti = 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(); } }

  22. 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.

  23. 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.

  24. 1 dimensional arrays // 8 elements indexed 0 to 7 int[] 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.

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

  26. Two dimensional array public 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.

  27. Functions class 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

  28. Procedures (methods) import java.util.Scanner; public class MeanApp { public static intnumSamples; // 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

  29. 2D arrays

  30. 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 are int [][] 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]

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

  32. 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);

  33. Sorting swapping elements temp = list[i]; list[i] = list[i+1]; list[i+1] = temp;

  34. 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);

  35. Bubble Sort Algorithm Elements of array list during the first iteration Elements of array list during the second iteration

  36. Bubble Sort Algorithm Elements of array list during the third iteration Elements of array list during the fourth iteration

  37. Location Quotients • See program Program5.cs and handout.

More Related