SOFTWARE AND PROGRAMMING 1 Advert NO TEST 1
- Slides: 29
SOFTWARE AND PROGRAMMING 1 Advert: NO TEST 1 on 7/02: TEST 1 will be 14/02 Lab: SH 131, BBK 536 6: 00 -7: 30 (from 24. 01. 07) [each student must have obtained access to Birkbeck computing, and those whose surnames start with A-L, to SCSIS computing too] Lecture: Gor B 4 7: 40 -9: 00 (from 24. 01. 07) Lab SH 131: students whose family names (surnames) begin A-L Instructor: Ms Marie-Helene Ng SCSIS, room NG 26, tel. 020 7631 6725 E-mail: marie-helene@dcs. bbk. ac. uk Lab BBK 536 : students whose family names (surnames) begin M-Y Instructor: Prof. Boris Mirkin
Webpages The course web page webct. bbk. ac. uk is used for announcements and assignments. Those who have problems with accessing the course module should contact Marie-Helene (marie-helene@dcs. bbk. ac. uk). An open-to-all web-site with lecture notes, schedule and files: www. dcs. bbk. ac. uk/~mirkin/sp 105 2
Test 1 14/2/7 awareness Open-book in-class Test 1 14/2/7 subjects: • Variable: type, declaration, initialisation • Expression • Loop for • Loop while • if( )… else if( ). . . else • Method 3
Contents • • • Leftovers (conditional operator) Ticket Machine Method; Accessor, Mutator Double loop Input data from keyboard: Text. IO, Scanner 4
Precedence table 5
Conditional operator • Used for assigning a value depending on a condition: A= Condition ? Result. If. True : Result. If. False 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; } 6
Ticket Machine (1) /* * Ticket. Machine models a ticket machine that issues * flat-fare tickets. */ public class Ticket. Machine{ private int price; private int balance; private int total; public Ticket. Machine(int ticket. Cost) //constructor { price = ticket. Cost; balance = 0; total = 0; } public int get. Price() { return price; } public int get. Balance() { return balance; } // see next page for continuation 7
Ticket Machine (2) // Ticket. Machine’s continuation public void insert. Money(int amount) { if(amount > 0) balance = balance + amount; else { System. out. println("Use a positive amount: " + amount); } } public int refund. Balance() { int amount. To. Refund; amount. To. Refund = balance; balance = 0; return amount. To. Refund; } // continued on the next page 8
Ticket Machine (3) // Ticket. Machine’s end public void print. Ticket() { if(balance >= price) { // Simulate the printing of a ticket. System. out. println("#########"); System. out. println("# The Blue. J 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 9
A comment I consider print. Ticket()method as somewhat inconsistent: printing (an accessing activity) is mixed up with changing the balance and total (mutating activities) Any suggestions? 10
Questions • How many methods are in Ticket. Machine? 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 11
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 get. Price() • The body encloses the method’s statements. 12
Accessor methods return type visibility modifier method name parameter list (empty) public int get. Price() { return price; } return statement start and end of method body (block) 13
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. 14
Mutator methods visibility modifier return type (void) method name parameter public void insert. Money(int amount) { balance = balance + amount; } field being changed assignment statement 15
Printing method public void print. Ticket() { // Simulate the printing of a ticket. System. out. println("#########"); System. out. println("# The Blue. J 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 = 0; } 16
Passing data via parameters 17
Double loop (1) class Add. Table { public static void main(String[ ] args){ int sum; for (int ite 1=1; ite 1<3; ite 1++) {//loop 1 for (int ite 2=1; ite 2<5; ite 2++) {//loop 2 sum=ite 1+ite 2; System. out. print(sum +" "); }//loop 2 System. out. println(); }//loop 1 } //method main ends } //class ends 18
Double loop (2) This prints 2 3 4 5 6 Why? A better printing ? Such as: Addition Table +1 2 3 4 12 3 4 5 23 4 5 6 19
Double loop (3) class Add. Table { public static void main (String[] args) {int sum; for (int i 1=1; i 1<3; i 1++){ System. out. print(i 1 + " ! "); for (int i 2=1; i 2<5; i 2++){ sum=i 1+i 2; System. out. print(sum +" "); } System. out. println(); } } } 20
Double loop (4): with method class Add. Table. Meth { public static void main (String[] args) { Print. Table(2, 4); }\end main public static Prin. Table(int rowsize, int columnsize){ for (int i 1=1; i 1<rowsize+1; i 1++){ System. out. print(i 1 + " ! "); for (int i 2=1; i 2<columnsize+1; i 2++){ sum=i 1+i 2; System. out. print(sum +" "); } System. out. println(); } } }\end class 21
Double loop (5) produces 1! 2 3 4 5 2! 3 4 5 6 3! 4 5 6 7 Q: How to make it look better? (See printing method in Ticket. Machine. ) Q: How to modify it to other ranges? Q: Make a MULTIPLICATION TABLE? 22
Input/Output Text. IO class Text. IO. java, added to the directory that contains your class, eases input of data from the keyboard To input an integer: int Us. Input = Text. IO. get. Int(); Computer will wait for the user to type in an integer value to Us. Input. 23
Input/Output Text. IO class (2) public class Print. Square { public static void main(String[] args) { int u. Input; // the number to be input by the user int Squared; // the user. Input, multiplied by itself System. out. print("Please type a number: "); u. Input = Text. IO. get. Int(); Squared = u. Input; //why product? System. out. print("The square is "+Squared); } // end of main() } //end of class Print. Square 24
Input/Output Text. IO class (3) Other Text. IO methods: b = Text. IO. get. Byte(); // value read is a byte i = Text. IO. get. Short(); // value read is a short j = Text. IO. get. Int(); // value read is an int k = Text. IO. get. Long(); // value read is a long x = Text. IO. get. Float(); // value read is a float y = Text. IO. get. Double(); // value read is a double a = Text. IO. get. Boolean(); // value read is a boolean c = Text. IO. get. Char(); // value read is a char w = Text. IO. get. Word(); // value read is a String s = Text. IO. getln(); // value read is a String 25
Input/Output in Java The Text. IO class contains static member methods Text. IO. put() and Text. IO. putln(), the same as System. out. print() and System. out. println(). Text. IO can only be used in a program if Text. IO is available to that program. It is not built into Java. From Java 1. 5. 0 version on, there is a similar class in Systems. in: Scanner 26
Input with Scanner class(1) From Java 1. 5. 0 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 27
Input with Scanner class (2) import java. util. * class Print. Dot{ 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. next. Int(); for (int ik=0; ik<num; ik++) System. out. print(‘. ’); System. out. println(); } \end of main } \end of class 28
Using method with Scanner import java. util. * class Print. Dot{ 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. next. Int(); 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 29
- Read the advertisement below
- Read the travel advertisement
- نمایندگی اولد اسپایس
- Instructive text structure
- Radio
- Clearasil advert
- Nspcc advert
- Communication and community development
- Quality street media studies
- Paco rabanne adverts
- Dr pepper advert
- Dairy milk advert
- Roman domus
- What makes a good radio advert
- Fairy liquid jingle
- Perbedaan linear programming dan integer programming
- Greedy vs dynamic programming
- System programming definition
- Linear vs integer programming
- Definisi integer
- Extreme programming in software engineering
- Haas macro
- System software: an introduction to systems programming
- Logo programming software
- Be next frontier software development
- Unit testing ruby
- Nocti study guide
- Chapter 3 skills and applications
- Generic and bespoke software
- Difference between student software and industrial software