Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng

Similar presentations


Presentation on theme: "Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng"— Presentation transcript:

1 Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

2 Overview Classes Programming in Java Bugs Arrays Collections Generics Autoboxing For loop

3 Classes (bits and bobs) Class names start with a capital letter String Label Object MyClass Methods start with a small letter println getAlignment toString myFunction

4 Classes (bits and bobs) Constants are written in all capitals Use “_” to separate words PI (defined in java.lang.Math) CENTER (defined in java.awt.Label) ACTION_EVENT (defined in java.awt.Event) Follow the convention and your code will look like Java’s inbylt code

5 Classes (bits and bobs) Visibility Keep things as local as possible! Key words private protected public default

6 Classes (bits and bobs) Key wordSame classSame packet Sub class Anywhere privateyesno protectedyes no publicyes “default”yes no Packet Class SubClass

7 Quiz List the four types of visibility modifiers in Java What is the Java convention for class and method names?

8 Classes (bits and bobs) Methods with the same name But differ signatures! Signature is : method name + parameters public int aMethod(int x) public int aMethod(int x, int y)

9 Classes (bits and bobs) Overloading constructors Call constructor in super class super(…); public Square() { this(50, 50); } Public Square(int x, int y) { m_nX = x; m_nY = y; }

10 Classes (bits and bobs) String Class String strText = “Hello World”; String methods: String concat(String str) char chatAt(int index) int length()

11 Classes (bits and bobs) Simple output Java uses class “PrintStream” System.out Normal text output System.err Error output

12 Classes (bits and bobs) PrintStream main methods print println Overloaded methods For all java types

13 Classes (bits and bobs) Static Key word Not associated with an object Access without creating an object Avoid! Breaks the object model But is sometimes needed

14 Classes (bits and bobs) Static attributes Same for all objects of a given class private static int nVar; Constants PI public static final double PI = 3.141592653589793;

15 Classes (bits and bobs) Static method Class method No need to create an object Called using class name Math.sin(double a) Math.toDegrees(double angrad) Object.equals(Object a, Object b) Util.isLocal(Stub stub)

16 Classes (bits and bobs) Static methods can’t call non static methods or attributes You have no local reference Main public static main(String[] args) Main is where the code starts Creates where you need to run your program

17 Programming in Java Sequence of steps Control structures if if … else switch while do.. while for

18 Programming in Java if true or false if (condition) { } if(bVal) if(!bVal) if(nVal == 0)

19 Programming in Java if … else if … else if … else if(bVal) … else … if(nVal == 0) … else if(nVal == 1) … else …

20 Programming in Java Switch Multiple if.. Else switch(nVal) { case 0: … case 1: … case 2: … default : … }

21 Programming in Java while do … while When you don’t know how long you have to loop Condition to get out of the loop while(!bFound) { … bFound = true; } do { … }while(!bFound)

22 Programming in Java for When you know how many times you want to loop for( ; ; ) for(i=0; i<strText.length(); i++) { }

23 Programming in Java Enhanced for loop For arrays for( : ) { … } Index is of the type of the array! int[] m_Array = {1, 2, 3}; for(int i : m_Array) { … }

24 Quiz What are the three ways to create a loop in Java? What are the two ways to make a decision in Java?

25 Bugs Program boundary Program Input Output check function

26 Bugs Use if statements to check data on the boundaries File I/O User inputs Memory allocations New What to do? Handel the error Inform user Output an error / debug message Use the debugger to finds out what happened throw (try … catch)

27 Bugs throws public void myMethod() throws Has to be caught or thrown again

28 Bugs try { … } catch ( ) { … } finally { … }

29 Quiz How do you check inputs are correct? What are the parts of a try statement?

30 Arrays nUsefullBox 25

31 Arrays nUsefullBoxes 01 2 34

32 Arrays To create a reference to an array type name[]arrayName; Int[]nArrayOfNumbers; Instantiate nArrayOfNumbers = new int[100];

33 Arrays nArrayOfNumbers[0] = 1; nArrayOfNumbers[1] = 2; nArrayOfNumbers[2] = 3; nArrayOfNumbers[i] = i + 1; private String[] strText = {“Epsilon Eridani”, “Tau Ceti”, “Alpha Ursae Majoris”};

34 Arrays Array is a data type type name[] Implemented as objects Multiple dimensions private double[][]Matrix = new double[5][5]; Arrays class Methods for manipulating arrays Search Sort

35 Arrays Array copy System arraycopy(src, 0, dest, 0, src.length); Else Element by element in a loop Multi-dimensions Call many times Passing arrays to methods private void func(String[] str); Return from a method public String[] aMethod(String[] strText)

36 Arrays Fail to instantiate an array Null pointer exception If new fails Out of memory exception Access beyond the array bounds Index out of bounds exception

37 Collections Handling a “group” of objects in one “block” Lists Queues Set

38 Collections ArrayList Class for dynamically handling arrays Add Remove toArray A type of “collection”

39 Generics A way to use the same code with differ types A generic algorithm Declare with a type parameter public class name public class Var { private T t; public void set(T t) {this.t = t} } private Var nVar;

40 Generics public class ArrayList boolean add(E e) E set(int index, E element) List subList(int fromIndex, int toIndex)

41 Autoboxing Primitive data types to/from classes Integer i = new Integer(1); Integer j = new Integer(2); Integer k = i + j; int x = i + 5; Since java 5

42 Autoboxing ArrayList myList = new ArrayList(); myList.add(new Integer(103)); ArrayList myList = new ArrayList (); myList.add(103);

43 Iterator Class for iterating around a loop Vector v = new Vector … Iteratorit = v.iterator(); … while(it.hasNext()) { … it.next(); }

44 Questions?


Download ppt "Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng"

Similar presentations


Ads by Google