SOFTWARE AND PROGRAMMING 1 Lecture: MB33 7:30-9:00 (except 11&18.01.06) Lab: B43, MB321, MB536 6:00-7:30 (from 25.01.05) [each student must have obtained.

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.
Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
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.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
How to Create a Java program CS115 Fall George Koutsogiannakis.
Enhancing classes Visibility modifiers and encapsulation revisited
Understanding class definitions Looking inside classes 3.0.
Using Java without BlueJ Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling BlueJ projects A BlueJ.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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
1 Reflecting on the ticket machines Their behavior is inadequate in several ways: –No checks on the amounts entered. –No refunds. –No checks for a sensible.
Understanding class definitions Looking inside classes.
Introduction to Java.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
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.
POS 406 Java Technology And Beginning Java Code
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Classes CS 21a: Introduction to Computing I First Semester,
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.
 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: UCL BC57:30-9:00 (9& – from 6:00) Lab: SH 131, SH B12 6:00-7:30 (from ) (students will be assigned.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
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.
Understanding class definitions
1 COS 260 DAY 3 Tony Gauvin. 2 Agenda Questions? 1 st Mini quiz on chap1 terms and concepts –Today In BlackBoard –30 min., M/C and short answer, open.
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.
22-July-2002cse142-13B-Development © 2002 University of Washington1 Development Tools CSE 142, Summer 2002 Computer Programming 1
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.
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
SOFTWARE AND PROGRAMMING 1 Lecture 4: Ticketing machine details: Constructor, method, static/instance variables Instructor: Prof. Boris Mirkin
Objects First With Java A Practical Introduction Using BlueJ Using Java without BlueJ 1.0.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
Understanding class definitions Exploring source code 6.0.
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.
SOFTWARE AND PROGRAMMING 1
Class definitions CITS1001 week 2.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Programming without BlueJ Week 12
CompSci 230 Software Construction
Understanding class definitions
Java Programming with BlueJ
Understanding class definitions
COS 260 DAY 3 Tony Gauvin.
Understanding class definitions
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
Object Oriented Programming in java
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

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 access to Birkbeck computing] Lab MB321: students whose family names fall in A-D Instructor: Mrs Jenny Hu SCSIS, room NG26, tel Lab MB536: students whose family names fall in E-L Instructor: Mr Zheng Zhu LKL, tel Lab B43: students whose family names fall in M-Y Instructor: Prof. Boris Mirkin SCSIS, room 111, tel

SOFTWARE AND PROGRAMMING 1 WebCT/Tests/Assignments: Marie-Helene Ng SCSIS, room NG26, tel To be able to submit your assignments you must have sent your CCS username to Marie-Helene by 31 January

3 Webpages The course web page is at Please check it regularly. It will be used for announcements and assignments. Another page, at an open-to-all web-site, will function with relevant materials too:

4 Formerly Recommended Texts 1.David J. Barnes & Michael Kölling Objects First with Java: A Practical Introduction using BlueJ, Second edition, Pearson Education, 2005, ISBN The publisher supplies a helpline (team’s telephone included) in installing the related software 2. J. Farrell Java Programming, Second edition, Course Technology, Thompson, 2003, ISBN I. Pohl, C. McDowell Java by dissection, Addison-Wesley, 2000, ISBN Free: ON-LINE text by D. Eck (on my web site) and other useful URLs

5 New Recommended Text Q. Charatan, A. Kans Java in Two Semesters, Second edition, The McGrow-Hill Education,2006, ISBN [ Free: ON-LINE text by D. Eck (on my web site) and other useful URLs]

6 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 –Download BlueJ from –Run “ bluejsetup-202.exe” and follow the given instructions Software is free

7 Concepts from lecture 1 Compiler (javac.exe) and Interpreter (java.exe) Class (template) and Object (its instantiation); every Java program must be a class Variable and its type; primitive types Method (input-output operation) and its Parameters (inputs - with their types at method’s description)

8 Concepts to be learnt Arithmetic expression and precedence Boolean expression Statement (a Java instruction) Loop for Usage in classes for hello-printing and ticket-vending machine (BlueJ)

9 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, explicit in JDK or somewhat easier in BlueJ

10 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

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

12 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

13 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

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

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

16 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)

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

18 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

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

20 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

21 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

22 JDK Problem: Execute what? How does the system know which method to execute?

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

24 A simplest 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 and parameters)*/

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

26 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

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

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

29 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

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

31 Arithmetic Operations in Java * 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 or log are in class Math of Java (to be explained later)

32 Arithmetic expressions 2 * 6 / – 2 * 3 = – 6 = 2 (integers) 2 * 6.0 / (4 + 5) – 2 * 3 = 12.0/9 – 6 = – 4.67 (reals are here) 2 * 6 / 4 + (5 – 2) * 3 = 12

33 Ticket Machine (1) /* * TicketMachine models a ticket machine that issues * flat-fare tickets. */ public class TicketMachine{ private int price; private int balance; private int total; public TicketMachine(int ticketCost) //constructor { price = ticketCost; balance = 0; total = 0; } public int getPrice() { return price; } public int getBalance() { return balance; } // see next page for continuation

34 Ticket Machine (2) // TicketMachine’s continuation public void insertMoney(int amount) { if(amount > 0) balance = balance + amount; else { System.out.println("Use a positive amount: " + amount); } } public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } // continued on the next page

35 Ticket Machine (3) // TicketMachine’s end public void printTicket() { if(balance >= price) { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " pence."); System.out.println("##################"); System.out.println(); total = total + price; // Update the total balance = balance - price; // Update the balance } else { System.out.println("You must insert at least: " + (price - balance) + " more pence."); } } }//end of class

36 Questions How many methods are in TicketMachine? If there is any syntactic difference between a method and constructor? Which of the methods are accessors and which are mutators?

37 Accessor methods Accessors provide information about the state of an object. Methods have a structure consisting of a header and a body. The header defines the method’s signature. public int getPrice() The body encloses the method’s statements.

38 Accessor methods public int getPrice() { return price; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier

39 Mutator methods Have a similar method structure: header and body. Used to mutate (i.e. change) an object’s state. Achieved through changing the value of one or more fields. –Typically contain assignment statements. –Typically receive parameters.

40 Mutator methods public void insertMoney(int amount) { balance = balance + amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed

41 Printing from methods public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0; }

42 Passing data via parameters