SOFTWARE AND PROGRAMMING 1 Advert : NO TEST1 on 7/02: TEST1 will be 14/02 Lab: SH131, BBK536 6:00-7:30 (from 24.01.07) [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

Understanding class definitions Looking inside classes 5.0.
Looking inside classes Fields, Constructors & Methods Week 3.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Understanding class definitions Looking inside classes 3.0.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
Classes CS 21a: Introduction to Computing I First Semester,
SOFTWARE AND PROGRAMMING 1 Revision Lecture 11/5/2011: Review of concepts involved in lectures and exam Review of questions in previous exam papers (they.
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.
SOFTWARE AND PROGRAMMING 1 Lecture 2, 2011 Instructor: Prof. Boris Mirkin DCSIS, room 744, tel Labs: from 26/1,
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
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.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: (from ) Ms Mihaela Cocea Room London Knowledge Lab Emerald.
1 Opbygning af en Java klasse Abstraktion og modularisering Object interaktion Introduktion til Eclipse Java kursus dag 2.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
Looking inside classes Conditional Statements Week 4.
SOFTWARE AND PROGRAMMING 1 Lecture 4: Ticketing machine details: Constructor, method, static/instance variables Instructor: Prof. Boris Mirkin
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Java I/O Basics MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj,
SOFTWARE AND PROGRAMMING 1 Lecture 2, 2010 Labs start : DCSIS room 131 LAB SH room B29 Lecture EACH student must have obtained.
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.
CompSci 230 S Programming Techniques
SOFTWARE AND PROGRAMMING 1
CSC 222: Object-Oriented Programming Fall 2015
Crash course in the Java Programming Language
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
Understanding class definitions
public class BankAccount{
Understanding class definitions
Java Variables, Types, and Math Getting Started
COS 260 DAY 3 Tony Gauvin.
Building Java Programs
Understanding class definitions
Building Java Programs
F II 3. Classes and Objects Objectives
COS 260 DAY 4 Tony Gauvin.
Classes CS 21a: Introduction to Computing I
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

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 access to Birkbeck computing, and those whose surnames start with A-L, to SCSIS computing too] Lecture: Gor B4 7:40-9:00 (from ) Lab SH131: students whose family names (surnames) begin A-L Instructor: Ms Marie-Helene Ng SCSIS, room NG26, tel Lab BBK536 : students whose family names (surnames) begin M-Y Instructor: Prof. Boris Mirkin

2 Webpages The course web page webct.bbk.ac.uk webct.bbk.ac.uk is used for announcements and assignments. Those who have problems with accessing the course module should contact Marie-Helene An open-to-all web-site with lecture notes, schedule and files:

3 Test1 14/2/7 awareness Open-book in-class Test1 14/2/7 subjects: Variable: type, declaration, initialisation Expression Loop for Loop while if( )… else if( )... else Method

4 Contents Leftovers (conditional operator) Ticket Machine Method; Accessor, Mutator Double loop Input data from keyboard: TextIO, Scanner

5 Precedence table

6 Conditional operator Used for assigning a value depending on a condition: A= Condition ? ResultIfTrue :ResultIfFalse Example: int b=2; int c= 7; int A=(b < c) ? b : c; %put min(b,c) into A How this can be done with if/else: int b=2; int c= 7; int A; if (b<c) {A=b;} else {A=c;}

7 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

8 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

9 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

10 A comment I consider printTicket() method as somewhat inconsistent: printing (an accessing activity) is mixed up with changing the balance and total (mutating activities) Any suggestions?

11 Questions How many methods are in TicketMachine? - five If there is any syntactic difference between a method and constructor? – two: absence of the output type, compulsory name Which of the methods are accessors and which are mutators? - Two in the beginning are accessors, three in the end are mutators

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

13 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

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

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

16 Printing method 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; }

17 Passing data via parameters

18 Double loop (1) class AddTable{ public static void main(String[ ] args){ int sum; for (int ite1=1;ite1<3; ite1++) { //loop1 for (int ite2=1;ite2<5; ite2++) { //loop2 sum=ite1+ite2; System.out.print(sum +" ");} //loop2 System.out.println();} //loop1 } //method main ends } //class ends

19 Double loop (2) This prints Why? A better printing ? Such as: Addition Table

20 Double loop (3) class AddTable{ public static void main (String[] args) {int sum; for (int i1=1;i1<3; i1++){ System.out.print(i1 + " ! "); for (int i2=1;i2<5; i2++){ sum=i1+i2; System.out.print(sum +" ");} System.out.println();}}

21 Double loop (4): with method class AddTableMeth{ public static void main (String[] args) { PrintTable(2,4); } \\end main public static PrinTable(int rowsize, int columnsize){ for (int i1=1;i1<rowsize+1; i1++){ System.out.print(i1 + " ! "); for (int i2=1;i2<columnsize+1; i2++){ sum=i1+i2; System.out.print(sum +" ");} System.out.println();} }}\\end class

22 Double loop (5) produces 1! ! ! Q: How to make it look better? (See printing method in TicketMachine.) Q: How to modify it to other ranges? Q: Make a MULTIPLICATION TABLE?

23 Input/Output TextIO class TextIO.java, added to the directory that contains your class, eases input of data from the keyboard To input an integer: int UsInput = TextIO.getInt(); Computer will wait for the user to type in an integer value to UsInput.

24 Input/Output TextIO class (2) public class PrintSquare { public static void main(String[] args) { int uInput; // the number to be input by the user int Squared; // the userInput, multiplied by itself System.out.print("Please type a number: "); uInput = TextIO.getInt(); Squared = uInput  uInput; //why product? System.out.print("The square is "+Squared); } // end of main() } //end of class PrintSquare

25 Input/Output TextIO class (3) Other TextIO methods: b = TextIO.getByte(); // value read is a byte i = TextIO.getShort(); // value read is a short j = TextIO.getInt(); // value read is an int k = TextIO.getLong(); // value read is a long x = TextIO.getFloat(); // value read is a float y = TextIO.getDouble(); // value read is a double a = TextIO.getBoolean(); // value read is a boolean c = TextIO.getChar(); // value read is a char w = TextIO.getWord(); // value read is a String s = TextIO.getln(); // value read is a String

26 Input/Output in Java The TextIO class contains static member methods TextIO.put() and TextIO.putln(), the same as System.out.print() and System.out.println(). TextIO can only be used in a program if TextIO is available to that program. It is not built into Java. From Java version on, there is a similar class in Systems.in: Scanner

27 Input with Scanner class(1) From Java version on, there is a similar class in System.in. Scanner(System.in): - import the java.util package in a line preceding the class, - then declare an instance of Scanner and - then use it for prompting the user to enter data (of a specified data type, preferably int or double ) from keyboard

28 Input with Scanner class (2) import java.util.* class PrintDot{ int num=0; public static void main(String[ ] args){ Scanner scap = new Scanner(System.in); System.out.println(“How many dots to print? “); num=scap.nextInt(); for (int ik=0; ik<num; ik++) System.out.print(‘.’); System.out.println(); } \\end of main\\end } \\end of class\\end

29 Using method with Scanner import java.util.* class PrintDot{ int number=0; public static void main(String[ ] args){ Scanner scap = new Scanner(System.in); System.out.println(“How many ampersands to print? “); number=scap.nextInt(); ppp(number); } \\end of main void ppp(nnn) { for (ik=0; ik<nnn; ik++) System.out.print(‘&’); System.out.println(); } \\end of ppp } \\end of class