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

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Pointers.
Written by: Dr. JJ Shepherd
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Datalogi A 1: 8/9. Book: Cay Horstmann: Big Java or Java Consepts.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Adapted from Dr. Craig Chase, The University of Texas at Austin.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Java and C++, The Difference An introduction Unit - 00.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
ArrayList, Multidimensional Arrays
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
CIT 590 Intro to Programming First lecture on Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 12: Programming Project.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Working With Objects Tonga Institute of Higher Education.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Object Oriented Software Development 4. C# data types, objects and references.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CSE 1201 Object Oriented Programming ArrayList 1.
Written by: Dr. JJ Shepherd
WELCOME To ESC101N: Fundamentals of Computing Instructor: Ajai Jain
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Computer Organization and Design Pointers, Arrays and Strings in C
Motivation and Overview
Advanced Programming Behnam Hatami Fall 2017.
Variables Title slide variables.
Java Programming Language
Java Basics Data Types in Java.
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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.

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.

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?

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.

Structure of a Java program public static void main(String[] args) Name of the method Return type (void by convention) Argument list Convention: main always receives an array of Strings. main has to be public i.e. Accessible outside of class myprog main is static so that it can be used without creating an instance of class myprog

Using String[] args class myprog { public static void main(String[] args) { for(int i=0; i<args.length; i++) { System.out.println(args[i]); } All arrays have a length variable representing the number of elements To compile and run: javac myprog.java java myprog here is some text

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.

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.

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!

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.

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

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!

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?

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!

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() func1() main() func1() func2() Can’t release memory of func1 until func2 ends

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! Data Pointer

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!

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

Forthcoming Courses C++ Programming Ten Sessions: 12 th April – 17 th May 2011 Tutor: Dr. Robert Oates Object orientation, pointers, efficient use of memory etc. Introduction to Java Programming Ten Sessions: 7 th June – 7 th July 2011 Tutor: Dr. Martin Nelson Vectors, wrapper classes, graphics, applets More info:

Course Evaluation Please fill in the course evaluation survey before you leave.