SOFTWARE AND PROGRAMMING 1 Lecture: UCL BC57:30-9:00 (9&16.01.08 – from 6:00) Lab: SH 131, SH B12 6:00-7:30 (from 23.01.08) (students will be assigned.

Slides:



Advertisements
Similar presentations
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Advertisements

SOFTWARE AND PROGRAMMING 1
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
The Web Warrior Guide to Web Design Technologies
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lecture 2: Object Oriented Programming I
Introduction to Computer Programming in C
Understanding class definitions Looking inside classes 3.0.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Using Java without BlueJ
Understanding class definitions Looking inside classes.
Introduction to Java.
Java PAL.  Contains the development kit and the runtime environment ( aka the Java Virtual Machine )  Download Link:
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
SOFTWARE AND PROGRAMMING 1 Lecture: Gor B4 7:40-9:00 (from ) Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained access.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
POS 406 Java Technology And Beginning Java Code
Introduction to Java Programming with Forte Y. Daniel Liang.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
SOFTWARE AND PROGRAMMING 1 Lecture 7/5/8: Review of concepts involved in lectures and exam Lecture 14/5/8: Review of questions in previous exam papers.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald.
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11& ) Lab: B43, MB321, MB536 6:00-7:30 (from ) [each student must have obtained.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Understanding class definitions
SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (11& – from 6:00) Lab: B43, MB321, MB536 6:00-7:30 (from ) EACH student must have obtained.
CS 4244: Internet Programming Network Programming in Java 1.0.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Chapter 1 Introduction. Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
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.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Lecture1 Instructor: Amal Hussain ALshardy. Introduce students to the basics of writing software programs including variables, types, arrays, control.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Objects First With Java A Practical Introduction Using BlueJ Using Java without BlueJ 1.0.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Using Java without BlueJ BlueJ projects A BlueJ project is stored in a directory on disk. A BlueJ package is stored in several different files.
SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from ) [each student must have obtained.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
SOFTWARE AND PROGRAMMING 1
Lecture 1b- Introduction
7 Class documentation and libraries
Java Primer 1: Types, Classes and Operators
Programming without BlueJ Week 12
Java programming lecture one
CompSci 230 Software Construction
Understanding class definitions
Understanding class definitions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

SOFTWARE AND PROGRAMMING 1 Lecture: UCL BC57:30-9:00 (9& – from 6:00) Lab: SH 131, SH B12 6:00-7:30 (from ) (students will be assigned ) EACH student must have obtained access to Birkbeck computing by – otherwise no use in the lab Instructor: Prof. Boris Mirkin SCSIS, room 111, tel Course Assistant: Lab/WebCT/Tests/Assignments: Ms Mihaela Cocea Room London Knowledge Lab Emerald Street Tel.:

2 Webpage The course web page is currently at my open web-site, with lecture notes and other relevant materials: 105

3 Texts 1.Quentin Charatan & Aaron Kans [CK] JAVA in Two Semesters, 2nd Edition, McGraw-Hill, 2006, ISBN David J. Barnes & Michael Kölling [BK] Objects First with Java: A Practical Introduction using BlueJ, 2nd edition, Pearson Education, 2005, ISBN The publisher supplies a helpline in installing related software 3. I. Pohl, C. McDowell [PM] Java by dissection, Addison-Wesley, 2000, ISBN J. Farrell [F] Java Programming, 2-4 editions, Course Technology, Thompson, , ISBN etc. 5. Free: ON-LINE text by D. Eck (on my web site); other URLs

4 Teaching Goal: Coding in Real FRAMEWORK: 1. the work of a compiler and an interpreter; 2. classes and instances; 3. variables and static variables ; 4. data and number types in Java; 5. arithmetic and Boolean expressions; 6. for/while and if…elseif…else structures; 7. processing strings; 8. elements of input/output in Java; 9. methods and constructors; 10. arrays and their usage 11. user defined types

5 Teaching Goal PRACTICE: To have developed skills in practical programming of small but real-world problems e.g. keeping transaction records, assigning seats to customers, managing a bus schedule, etc.

6 Control Final exam: 75% of mark Course-work: 25% of mark –2 open-book in-class tests, 6.02 & 5.03, –2 assignments for home work via WebCT, & Composition of CW 25% mark: T15% T27% A15% A2 8%

7 Control: Example Let marks of a student be as follows: –Exam:50 –T112 –T265 –A1100 –A20 (hasn’t submitted) The total mark will be 48 because it rounds up the result: 50* * * * *0.08= = = 47.65

8 Teaching Philosophy (extracts) Huge respect for part-time students The bulk of students to get training No student’s question is stupid Simple things to be learnt well; rather than complex things not learnt well Instructor’s small errors - a teaching device for activation of learning process Learning a language in a college differs from consuming a lunch in a diner in many ways Fast feedback on students’ issues if raised

9 Why Java Object-oriented (a program is a set of classes sending messages to each other with methods within to process inputs), which is useful for code transfer and maintenance Network-oriented (server-to-clients) Works within Internet browsers [devices viewing HTML (hyper-text markup-language) documents via HTTP (hyper-text transfer protocol) links]

10 Fundamental concepts How a Java program works Java as is and Java with BlueJ Object Class Method Parameter Variable Data type

11 High-language code to machine code translated A compiler takes a high-level-language program and translates it as a whole into an executable machine-language program. Once the translation is done, the machine-language program can be run any number of times, but of course it can only be run on one type of computer. Thus, each type of computer requires a specific translator. An interpreter is a program that runs in a loop in which it repeatedly reads one instruction from the program, decides what is necessary to carry out of that instruction, and then performs the appropriate machine-language commands to do so. This is slow but universal.

12 source file class file editor compiler (javac) virtual machine (java interpreter) Java edit-compile-interpret-execute cycle

13 Two JAVA environments Java Developer Kit JDK (currently, J2SE) (Conventional) Blue J (A public project to make JAVA coding easier) –Both available in Birkbeck

14 Conventional JDK: Editing A source code can be edited in any text editor: Notepad, emacs, PFE,... MS Word caveat: by default, Word does not save in ASCII text format Make sure to save the code before compiling! The file name: the same as that of the class, with extension: say, class NicTe{…} must be saved as file NicTe.java, case sensitive

15 Command line invocation compilation and execution of Java in JDK are done from a command line On Microsoft systems: DOS shell On Unix: Unix shell Must make sure that the commands for compiler and runtime (JVM) are in the command path.

16 Compiling with JDK Name of the JDK compiler: javac To invoke: javac compiles and all classes it depends on into an executable on JVM file.class Example: javac NicTe.java produces file NicTe.class

17 Execution “java” starts the Java virtual machine: java NicTe The named class is loaded and execution is started. Other classes are loaded as needed. Only possible if class has been compiled into a file, say, NicTe.class

18 JDK Problem: Execute what? How does the system know which of the methods to execute?

19 The main method in JDK The JDK java system always executes a method called main, it should have a certain signature: Signature _______________________ public static void main(String[ ] args) {... } To work with JDK, such a method must be present in your program!

20 A simple program /* HelloWorld.java Purpose: printing a message to the screen */ class HelloWorld { // Each program is organised as a class public static void main(String[] args) { System.out.println("Hello World!"); } } // end of class HelloWorld /* Always Three Types of Elements ONLY: -comments -class (with modifiers) -methods (with modifiers & parameters/arguments)*/

21 BlueJ coding BlueJ programs are organised in the so-called projects A BlueJ project is stored in a project- specific directory on disk Some files store the source code, some store the compiled code, some store additional BlueJ related information.

22 The BlueJ directory structure UserInterface CalcEngine Calculator project: calculator c:\bluej\calculator\ bluej.pkg bluej.pkh Calculator.java Calculator.class Calculator.ctxt UserInterface.java UserInterface.class UserInterface.ctxt CalcEngine.java CalcEngine.class CalcEngine.ctxt

23 The BlueJ file structure bluej.pkg - the package file. Contains information about classes in the package. One per package. bluej.pkh - backup of the package file. *.java - standard Java source file (text). One per class. *.class - standard Java code file. One per class *.ctxt - BlueJ context file. Contains extra information for a class. One per class.

24 Available on BBK’s network –Java JDK (which allows you to compile and execute your program) –BlueJ (Preferred editor) Installing BlueJ (for home use) –First download the Java JDK from –Then download BlueJ from –Run “ bluejsetup-202.exe” and follow the given instructions Software is free

25 Objects and classes Classes: program templates –represent all objects of a kind (example: “student”) Objects == instances –A template copy to represent a specific element of the class (“an individual student”) –Instances are created with the so-called constructors, explicitly in JDK or somewhat easier in BlueJ

26 Variables, methods and parameters classes contain data stored in the so- called variables and operations which can be invoked (they are called methods in Java) methods may have inputs (they are called parameters in Java) to get additional data needed to get them executed

27 Remarks Many instances can be created from a single class An object has attributes/variables: values stored in fields (memory locations). The class defines what fields any its object has (a template), but each object may store its own set of values (the state of the object) A variable is initialised with assigning it a value, an object – with a constructor

28 More on Method Method in Java is a named set of instructions that transforms some input into an output. This is, actually, a machine implementation of the concept of algorithm which itself is a computational analogue to the mathematical concept of function. Static method: is shared by all instances.

29 Example of a method (1) Square function y = x 2 x y The table can be used for invoking a specific value, like, 7 2 = 49 or 10 2 = 100.

30 Example of a method (2) A Java method to calculate the square function: public int sq(int x){ return x  x;} To make it work, variables are needed: int sevs=sq(7); //puts 49 into sevs int tens=sq(10); // puts 100 into tens

31 Structure of a method modifiers return-type name ( parameter-list ) { statements; return variable/expression; //if return type is not void } Modifiers: –static - method/variable that belongs to class as whole and is shared by all –public - method/variable that is accessible from anywhere –private - method/variable that is accessible from only within the class Output’s typeInputs

32 HelloWorld with a method // Hello-world program to demonstrate BlueJ class Hello{ // Method that does the work of printing public void go() { System.out.println("Hello, world"); } // main method for working outside BlueJ public static void main(String[] args) {Hello hi = new Hello(); //instance hi.go(); //method in instance hi }

33 HelloWorld : WHY Why dots in System.out.println("Hello, world"); hi.go(); ? 1)To take method from a specific class instance 2)To take a class from a set of classes: Java is organised as a hierarchically structured set of classes in individual files

34 Fields Fields store values for an object. They are also known as instance variables. Use the Inspect option to view an object’s fields. Fields define the state of an object. public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } private int price; visibility modifiertypevariable name

35 Assigning values Values are stored into fields (and other variables) via assignment statements: –variable = expression; –price = ticketCost; The value on the right is assigned to a variable on the left. A variable stores a single value, so any previous value is lost.

36 Variable It provides for multiple uses of the same program A variable is a name for a location in memory that can hold data. Variables are declared and/or initialised A variable declaration includes the following: –A data type that identifies the type of data that is stored in the variable –An identifier that is the variable’s name –An optional assigned initial value

37 int p; p p = 4; 4 In memory:

38

39 Scope of a variable: The range of statements that can access the variable. It stretches from the declaration point to the end of the block containing the declaration Q: WHAT is BLOCK ? (part within curly braces{…} ) Q: WHAT is DECLARATION? (type name ; 3-part command)

40 HelloWorld N times BlueJ public class HelloN { int number; \\ variable declared public void go() { System.out.println("Hello, world"); } public HelloN(int howmany) {number=howmany; } \\constr to initialise object public void prrt() \\printing number times { for(int i=1;i<=number;i++) \\loop go(); System.out.println("ok"); }}

41 Loop for for(int var=1;var<=st;var++){ do operation depending on var } Two types of parentheses: () and {} The expression in () consists of three different items: initialising a variable, variable update, and stop-condition Given a value of var, {} is executed, after which var is updated, then stop-condition checked and, if yes, {} is executed again; if no, the program proceeds further on

42 No { } in for-loop in HelloN Why? Let us add { }: where? Is there any difference between before and after “ok”?

43 Arithmetic Operators * 5  3=15 /36/9=4, 39/9=4, 39/50=0 (integers) /36.0/9=4.0, 39.0/9= , 39.0/50=0.78 (reals) %36%9=0, 39%9=3, 39%50= = 8 -5 – 3 = 2 Other operators such as Abs or exp are in class Math of Java (to be later)

44 Arithmetic Expressions 2 * 6 / – 2 * 3 = – 6 = 2 2 * 6.0 / (4 + 5) – 2 * 3 = 12.0/9 – 6 = – 4.67 (note: reals, not integers) 2 * 6 / 4 + (5 – 2) * 3 = 12 There is a natural precedence structure of arithmetic operations. If you are not sure, use parentheses!!!

45 Basic class structure public class TicketMachine { Inner part of the class omitted. } public class ClassName { Variables Constructors Methods } The outer wrapper of TicketMachine The contents of a class

46 Concepts considered Compiler & interpreter JDK/JDS and BlueJ environments Method Variable; declaration and intialisation Primitive variable types Class type Arithmetic operators and expressions Class and instance HelloWorld class in three versions