SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from 6.00 - 7.20) Room 103.2 London Knowledge Lab 23-29 Emerald.

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 Functions II
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.
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.
LAB 10.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java. 2 Textbook David J. Barnes & Michael Kölling Objects First with Java A Practical Introduction using BlueJ Fourth edition, Pearson.
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.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
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,
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
1 CSC 222: Object-Oriented Programming Spring 2013 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 CSC 221: Computer Programming I Fall 2004 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.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
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.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Looking inside classes Conditional Statements Week 4.
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
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
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.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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
CSC 222: Object-Oriented Programming Fall 2015
Suppose we want to print out the word MISSISSIPPI in big letters.
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
Data types, Expressions and assignment, Input from User
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
Understanding class definitions
public class BankAccount{
Understanding class definitions
The for-loop and Nested loops
COS 260 DAY 3 Tony Gauvin.
CHAPTER 6 GENERAL-PURPOSE METHODS
More on Classes and Objects
Understanding class definitions
Building Java Programs
F II 3. Classes and Objects Objectives
© A+ Computer Science - Classes And Objects © A+ Computer Science -
COS 260 DAY 4 Tony Gauvin.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Building Java Programs
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Presentation transcript:

SOFTWARE AND PROGRAMMING 1 In-class open-book TEST1 on 6/02 Lab SH131: Ms Mihaela Cocea (from ) Room London Knowledge Lab Emerald Street Tel.:

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

3 Contents Ticket Machine without and with a menu or main method Method: mutator, accessor Constructor Calling a method Working over a menu with if/else and while Using TextIO/Scanner for input

4 Ticket Machine Imitates issuing flat-rate tickets Three variables needed: price – for ticket price balance – for the user’s money total – for money received from customers Three assessor methods for getting each of the variables Three mutator methods for - entering customer’s money - issuing a ticket - getting refunded

5 Blue-J 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; } public int getTotal() { return total; } // see next page for continuation

6 Blue-J Ticket Machine (2) // TicketMachine’s continuation public void insertMoney(int amount) { if(amount > 0) balance = balance + amount; else { System.out.println(“This is not positive "+ amount); } } public int refundBalance() { int amountToRefund; amountToRefund = balance; balance = 0; return amountToRefund; } // continued on the next page

7 Blue-J 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

8 Passing data via parameters

9 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?

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

11 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

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

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

14 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

15 Blue-J Ticket Machine: a review Shortcomings: No main method – cannot be used in JDK Input only with BlueJ capabilities, not with JDK Methods not ordered – refund may occur before a ticket has been issued - can be addressed by organising a dialog or menu

16 Organising a menu //--- ‘menu’ method for choosing action public static int menu() { TextIO.putln(); TextIO.putln("Please enter a number: "); TextIO.putln(" 0 - to quit "); TextIO.putln(" 1 - to get a ticket price "); TextIO.putln(" 2 - to put money and get a ticket "); TextIO.putln(" 3 - to get refunded "); TextIO.putln(" 4 - to get statistics "); int action=TextIO.getInt(); return action; }

17 Main method using menu: right? public static void main(String[ ] args){ int MeItem=1; while (MeItem!=0){ MeItem=menu();// a method if (MeItem==1){ int pp=getPrice(); // a method System.out.println("The ticket price is "+pp+" pence ");} else if (MeItem==2) { System.out.println("Please key in the money inserted, in pence"); int money.insert=TextIO.getInt(); insertMoney(money_insert); printTicket();} else if (MeItem==3) { int refund=refundBalance(); System.out.println("Please take your refund " + refund);} else {int tt=getTotal(); int bb=getBalance(); System.out.println("The total for tickets: "+tt);} }//end of while for choosing action } //end of main

18 Main method using menu: WRONG! WHY? There are some deficiencies in the program: no difference between option 4 and !(1 | 2 | 3) – but this wouldn’t make the class fail Because main method is static, but other methods and variables are not A Java Commandment : You shalt not utilise non static items in a static method! What to do? Either –Make other methods and variables static too; this works but may be in odds with flexibility considerations –Introduce an instance of the class into main method – make the constructor working – and use all methods and variables from the instance

19 Main method using menu: Right (I) public static void main(String[ ] args){ System.out.println("Please enter a ticket price "); int pi=TextIO.getInt(); TM atm=new TM(pi); int MeItem=1; while (MeItem!=0){ MeItem=menu();// a method if (MeItem==1){ int pp=atm.getPrice(); // a method System.out.println("Ticket price is "+pp);}

20 Main method using menu: Right (II) else if (MeItem==2) { System.out.println(“Key in the money inserted in pence"); int money_insert=TextIO.getInt(); atm.insertMoney(money_insert); atm.printTicket();} else if (MeItem==3) { int refund=atm.refundBalance(); System.out.println("Please take your refund " + refund);} else {int tt=atm.getTotal(); int bb=atm.getBalance(); System.out.println("The total for tickets: "+tt);} }//end of loop while for choosing action } //end of main

21 Loop for : reminder I General form: for(CounterInit; Test; CounterUpdate) { } Three parts: for – name of the loop ( ) – control of the loop { } – computations at each loop’s iteration Needs to be remembered: Process of computation is controlled in ( ) – it is only from here an exit from the loop is possible (at Test=False) Computations are performed in { }; when finished, the process goes back to ( )

22 Loop for : reminder II Task: Print integers from 0 to 10 in one line for(int ii=0; ii<=10; ii++) {System.out.print(ii + “ ”); } If one wants change the number 10 to 15? for(int ii=0; ii<=15; ii++) {System.out.print(ii + “ ”); } Or, more flexible, int lim =15; for(int ii=0; ii<=lim; ii++) {System.out.print(ii + “ ”); }

23 Loop for : reminder III Or, even more flexible, with a method int printint(int lim){ for(int ii=0; ii<=lim; ii++) {System.out.print(ii + “ ”); } } //end of method printint printint(15); //calling method at lim=15 Q: Can you find anything wrong in the method printint ? (A: (i) int output type, (ii) should put System.out.println(); in the end) Q: How to modify this if I want … printed? (A: Think, or if you don’t want to, use filtering condition ii%5==0 )

24 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

25 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

26 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