programming for beginners martin nelson elizabeth fitzgerald lecture 15: more-advanced concepts

21
Programming for Programming for Beginners Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Upload: margaret-hunt

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Programming for Programming for BeginnersBeginners

Martin Nelson

Elizabeth FitzGerald

Lecture 15: More-Advanced Concepts

Page 2: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Session 15 – aims and objectives

Review of the course to date.

Brief introduction to some more advanced topics.

Where to go next?

Chance to wrap up anything that’s not been clear so far.

Page 3: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Recap of the course so far! We now know LOADS about Java!

Most of the Java concepts we’ve talked about transfer directly to other languages.

We’ve seen different types of data: int, double, String, boolean, char, char[], objects,...

We know how to design and structure codes Pen & paper, pseudocode, testing, successive refinement.

We now know the difference between procedural and object oriented programming.

We’ve seen a bit of procedural C++ and some scripting languages.

Page 4: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Structure of a Java program We’ve seen this many times now...

class myprog

{

public static void main(String[] args)

{

System.out.println(“...”);

}

}

But what does it all mean?

Page 5: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Structure of a Java program

class myprog

{

public static void main(String[] args)

{

System.out.println(“...”);

}

}

Every Java program is a class.

Here is the class’ name.

Every Java application must have a method called main,which is run as soon as the application is started.

Page 6: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Structure of a Java program

public static void main(String[] args)

Name of the methodReturn type

(void by convention)

Argument list

Convention: main always receivesan array of Strings.

main has to be publici.e. Accessible outside

of class myprog

main is static so thatit can be used withoutcreating an instance of

class myprog

Page 7: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Using String[] argsclass myprog

{

public static void main(String[] args)

{

for(int i=0; i<args.length; i++)

{

System.out.println(args[i]);

}

}

}

All arrays have a lengthvariable representing thenumber of elements

To compile and run:

javac myprog.java

java myprog here is some text

Page 8: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Using String[] args What if you want to pass in something other than

Strings? main() can only receive Strings!

Need some way of converting Strings to, say, integers...

Can do this using Wrapper classes.

Page 9: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Wrapper classes We have seen that classes (e.g. Strings) are really

useful as they have loads of associated methods which we can use.

Primative data types (e.g. int, char, double, etc.) don’t have such methods.

However, wrapper classes can be used to `wrap’ a primative variable into an object.

They provide more methods & greater flexibility.

Page 10: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Wrapper classes int can be wrapped as an Integer object. double can be wrapped as a Double object. char can be wrapped as a Character object.

Include methods to convert the numeric value of a String into an Integer, and then convert an Integer back to an int.

It is your responsibility to check that this conversion is sensible.

For more info, see the Core Class documentation, or come to the next Java course!

Page 11: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Arrays vs. Vectors Arrays can store a number of variables of the

same type in a logical manner.

We must know the maximum number of variables to store in advance!

What if we don’t know the maximum number of variables? Could just declare a HUGE array, but this wastes memory

and risks causing a bug.

Instead of arrays, we can use Vectors.

Vectors can increase/decrease in size dynamically.

Page 12: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Vectors Import the java.util.Vector class:

import java.util.Vector;

Instantiate a new Vector:Vector numbers = new Vector();

Add a new element:numbers.addElement(3);

Get an element (the 0th element in this case):numbers.elementAt(0)

Delete an element (the 7th element in this case):numbers.remove(7);

See the Core Class documentation for more details.

Page 13: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Other uses of Java Graphical programming

External libraries java.awt and java.swing can be used to create graphical programs.

The JVM makes program design independent of the operating system.

Min/Max/Close buttons are automatic, for example!

Applets Small applications which are distributed over the web.

Byte code is held remotely on a server.

A JVM is built in to most modern web browsers.

Must have a java enabled browser to work!

Page 14: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Memory Issues When designing code, we should always try to

minimise the amount of memory used.

Not as much of a problem these days as memory is more abundant.

For long running, high intensity jobs, memory management is more important. Numerical simulations and computations, Matlab codes...

Things to think about... Am I storing data that I don’t need?

Am I looping un-necessarily? (Very expensive)

What do I do with data after I am finished with it?

Page 15: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Memory Issues

In general, the computer’s memory is divided into three

segments:

The Code Segment Where the compiled code itself is stored.

The Stack Segment Used for variables which are created as functions are

started.

The Heap Segment Dynamic memory allocation.

This is a bit more complicated in Java!

Page 16: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

The Stack The stack segment is used to store variables which

are created as a function runs.

When a new function starts, a new block of memory is reserved for that function’s data.

Minimal control of how much memory is allocated.

Memory is cleared in a last in – first out manner.

main() main() main() main()

func1()

main()

func1() func1()

func2()

Can’t release memory of func1 until func2 ends

Page 17: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

The Heap Memory is allocated dynamically.

Slower than the stack, as memory allocation is more complicated.

Much more memory can be made available, and memory which is no longer needed can be released straight away.

Heap data is accessed via its memory address using pointers.

Heap data survives afterthe function which created it ends... until you delete it!

DataData

DataData

Data

DataData

Pointer

Page 18: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Pointers & Dynamic Memory Pointers are used a lot in languages such as C++.

Some people find pointers very complicated.

When using dynamic memory, you must always explicitly delete data when it is finished with, or the memory cannot be re-used.

Failure to empty memory is known as a memory leak. They will often cause your program to crash as memory runs out!

Page 19: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Pointers & Dynamic Memory We haven’t seen pointers in Java. Why?

Java doesn’t have them!

Or, more accurately, everything in java is a pointer, so you never have to worry about them.

In java, everything is stored on the heap.

Big advantage of Java: Memory management is automatic. Don’t have to worry about deleting variables when they

are finished with. Not the case with C++.

To find out more about pointers and dynamic memory allocation, sign up for the forthcoming C++ programming course...

Page 20: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Forthcoming Courses C++ Programming

Ten Sessions: 12th April – 17th May 2011

Tutor: Dr. Robert Oates ([email protected])

Object orientation, pointers, efficient use of memory etc.

Introduction to Java Programming

Ten Sessions: 7th June – 7th July 2011

Tutor: Dr. Martin Nelson ([email protected])

Vectors, wrapper classes, graphics, applets

More info: www.nottingham.ac.uk/csc

Page 21: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts

Course Evaluation

Please fill in the course evaluation survey before you leave.