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

Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Building Java Programs
03 Data types1June Data types CE : Fundamental Programming Techniques.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Fundamental Programming Structures in Java: Strings.
Understanding class definitions Looking inside classes.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
Java Basics (continued)
© 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.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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,
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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.
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Understanding class definitions
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers; Type boolean reading: , 5.6.
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.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
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.
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.
CompSci 230 S Programming Techniques
SOFTWARE AND PROGRAMMING 1
Chapter 2 Basic Computation
Formatting Output & Enumerated Types & Wrapper Classes
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture Notes – Basics (Ch1-6)
SELECTION STATEMENTS (1)
An Introduction to Java – Part I
INPUT STATEMENTS GC 201.
Understanding class definitions
CSS 161 Fundamentals of Computing Introduction to Computers & Java
An Introduction to Java – Part I, language basics
Java Variables, Types, and Math Getting Started
Java so far Week 7.
Building Java Programs
Understanding class definitions
Building Java Programs
F II 3. Classes and Objects Objectives
Java Programming Review 1
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Introduction to java Part I By Shenglan Zhang.
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 MB536: students whose family names fall in A-F Instructor: Mr Zheng Zhu LKL, tel Lab G03 Clore Centre: students whose family names fall in G-Ka Instructor: Mrs Jenny Hu SCSIS, room NG26, tel Lab 12 Gordon Sq. 43: students whose family names fall in Ke -Y Instructor: Prof. Boris Mirkin SCSIS, room 111, tel O

2 Subjects TicketMachine TextIO and Scanner class to key in data String class Math class

3 Test1 8/2/6 awareness Test1 will be carried out in MB33 during the lecture time (not lab time) from 7:30, 8/2/06 Subjects: Variable: type, declaration, initialisation Expression: arithmetic, Boolean Loop for Loop while if( )… else if( )... else Simple method

4 What we want of a Ticket Machine TicketMachine code: to model a ticket machine that issues flat-fare tickets. The functionality: - accepting fare - calculating the amount to pay back - calculating the cumulative pay -issuing tickets -informing of the price and accumulated pay Instances may have different prices

5 Coding Ticket Machine Principle: EACH function should be done with a specific variable/method Functions: - differing instances (constructor) - different pricing (var: price ) - accepting fare (var: balance ) -calculating the cumulative pay (var: total ) -calculating the money back ( diff = balance-price ) -issuing tickets (method for printing a ticket) -informing of the price and accumulated pay (methods for each)

6 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

7 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

8 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

9 Example of branching(1) Problem: calculate income tax Algorithm (Input: Income, Output: Tax): When the salary is less than 10000, there is no tax. The tax is 15% on the amount earned over up to Any money earned over are taxed at 40%, that is, they pay 6000, the tax at 50000, plus the 40% added from the earnings over

10 Example of branching(2) If()… : int Salary; int Tax=0; TextIO.putln("Input your salary "); Salary=TextIO.getInt(); //TextIO – a class to be // put into the class’ directory if ((Salary > 10000)&&(Salary<=50000)) Tax=(Salary-10000)*15/100; if (Salary>50000) Tax=(Salary-50000)*40/ ;

11 Example of branching(3) If()…else if()…else (preferable): int Salary; int Tax; if (Salary<=10000) Tax=0; else if (Salary<=50000) Tax=(Salary-10000)*15/100; else Tax=(Salary-50000)*40/ ; Q: What this would produce for Salary=15777?

12 Method for Tax calculation Method TC with input/parameter – Salary; output - Tax public float TC(int Salary) { float Tax; if (Salary<=10000) Tax=0; else if (Salary<=50000) Tax=(Salary-10000)*15/100; else Tax=(Salary-50000)*40/ ; return Tax;} Application: int mywages=15777; float mytax=TC(mywages); // would assign to mytax

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

14 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

15 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

16 Input with Scanner 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

17 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 - declare an instance of Scanner - use it for prompting the user to key in data (of a specified data type, preferably int or double or String ) with a method: nextInt() or nextDouble() or next()

18 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 (ik=0; ik<num; ik++) System.out.print(‘.’); System.out.println(); } \\end of main\\end } \\end of class\\end

19 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

20 Strings(1) Declaring a String Object String variable An object of the class String – The class String is defined in java.lang.String and is automatically imported into every program Create a String object by using the keyword new and the String constructor method String aGreeting = new String(“Hello”); or String aGreeting = “Hello”;

21 Strings(2) Comparing String Values Strings are never actually changed; instead new Strings are created and String variables hold the new addresses; A part of the Java system called the garbage collector will discard the unused strings It is impossible to make a simple comparison of Strings; thus a number of methods are: – equals() method i f s1 and s2 are declared and initialised as String: s1.equals(s2) true if s1 and s2 are exactly the same sequences of characters NEVER s1==s2 !!! This is wrong, == applies to numbers only.

22 Strings(3) Comparing String Values Try "HaHaHa ” "haHaHa"(2 differences) s1.length() number of characters in s1 charAt() method requires an integer argument which indicates the position of the character that the method returns s1.charAt(N) N-th character in s1 (starting from N=0) String ss= “Look at you!”; What is ss.charAt(3)? ss.charAt(7)? ss.charAt(17)? [In respect, ‘k’, ‘ ’, and error]

23 Strings(4) s1.substring(N,M) part of s1 in positions N, N+1,..., M-1 (positions are numbered from 0) String ss= “Look at you!”; What is ss.substring(3,7)? Concatenation Concatenation - Joining strings, can be done with symbol + “45” + “36” = “4536”

24 Class Math Math.pi  =3.14…,the ratio of the circumference to its diameter Math.abs(a) a if a >= 0, or -a if a < 0 Math.log(a) the natural logarithm (base e) of a Math.sqrt(a) square root of a Math.pow(a,b)a b, if b is integer then a b =a  a  …  a (b times) Math.random() pseudorandom number: double within interval [0.0, 1.0) (zero included, unity not) How to use it to generate a random integer between 1 and 6 (inclusive), to mimic casting a dice? double aa=Math.random(); //aa, a real number between 0 and 1 int an= 6*aa; //a real number between 0 and 6 int rand=(int) an; // whole number between 0 and 5 int randw=rand+1; // whole number betw. 1 and 6 Casting in Java: converting from higher number types to lower types int randw= (int) (6*Math.random()+1); How to generate a random integer between 10 and 20 inclusive? Answer: int rdt= (int) (11*Math.random()+10);

25 Math.random() pseudorandom number: double within interval [0.0, 1.0) (zero included, unity not) How to use it to generate a random integer between 1 and 6 (inclusive), to imitate casting a dice?

26 Casting a dice double aa=Math.random(); //aa, a real number between 0 and 1 int an= 6*aa; //a real number between 0 and 6 int rand=(int) an; // whole number between 0 and 5 int randw=rand+1; // whole number between 1 and 6 The same in one line: int randw= (int) (6*Math.random()+1);

27 Casting a dice question How to generate a random integer between 10 and 20 inclusive? Answer: int rdt= (int) (11*Math.random()+10);