Title. Java Just in Time. John Latham. November 5, November 5, 2018 Java Just in Time - John Latham Page 1(0/0)

Similar documents
20 Minimum bit width 21 Minimum bit width 22 Design: pseudo code 24 Minimum bit width 25 Minimum bit width 26 Standard API: Math: pow() 27 Minimum bit

17 Statement: assignment statement: assigning an expression value 18 Age next year 19 Type: String: conversion: from int 21 Age next year 22 The full

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Basics of Java Programming

3. Java - Language Constructs I

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Chapter 6 Primitive types

Program Fundamentals

Computer Components. Software{ User Programs. Operating System. Hardware

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

26 Age history with a separate method 27 Method: void methods 28 Age history with a separate method 29 Age history with a separate method 33 Trying it

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

CMPT 125: Lecture 3 Data and Expressions

Getting started with Java

COMP-202: Foundations of Programming

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 2: Data and Expressions

CPS122 Lecture: From Python to Java

An overview of Java, Data types and variables

Chapter 2: Data and Expressions

Java Bytecode (binary file)

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS 106 Introduction to Computer Science I

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

CS11 Java. Fall Lecture 1

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

About this exam review

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

4 Programming Fundamentals. Introduction to Programming 1 1

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

4. Inputting data or messages to a function is called passing data to the function.

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

MODULE 02: BASIC COMPUTATION IN JAVA

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

1007 Imperative Programming Part II

Chapter 3: Operators, Expressions and Type Conversion

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

What did we talk about last time? Examples switch statements

Lecture Set 2: Starting Java

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Lecture Set 2: Starting Java

PROGRAMMING FUNDAMENTALS

Chapter 2: Using Data

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Visual C# Instructor s Manual Table of Contents

Program Elements -- Introduction

Key Differences Between Python and Java

Computer System and programming in C

Java Basic Datatypees

CS112 Lecture: Working with Numbers

IT 374 C# and Applications/ IT695 C# Data Structures

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

JAVA OPERATORS GENERAL

Professor Peter Cheung EEE, Imperial College

Computer Components. Software{ User Programs. Operating System. Hardware

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

1.00 Lecture 4. Promotion

Algebraic Specifications

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

Programming for Engineers Introduction to C

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2 Elementary Programming

These are notes for the third lecture; if statements and loops.

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

Information Science 1

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

1 Lexical Considerations

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Programming in C++ 4. The lexical basis of C++

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

COMP 202 Java in one week

Language Reference Manual

printf( Please enter another number: ); scanf( %d, &num2);

2 rd class Department of Programming. OOP with Java Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Full file at

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CS 142 Style Guide Grading and Details

CS102: Variables and Expressions

First Java Program - Output to the Screen

B.V. Patel Institute of BMC & IT, UTU 2014

These are reserved words of the C language. For example int, float, if, else, for, while etc.

ECE 122 Engineering Problem Solving with Java

Conditionals, Loops, and Style

Course Outline. Introduction to java

CS313D: ADVANCED PROGRAMMING LANGUAGE

A First Program - Greeting.cpp

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

A variable is a name for a location in memory A variable must be declared

Transcription:

List of Slides 1 Title 2 Chapter 9: Consolidation of concepts so far 3 Chapter aims 4 Section 2: Java concepts 5 Aim 6 Java concepts 7 Type: long 8 Type: short 9 Type: byte 10 Type: char 11 Type: char: literal 12 Variable: char variable 13 Type: char: literal: escape sequences 14 Type: float 15 Java concepts 16 Example-class.java 0-0

20 Java concepts 21 Expression: arithmetic: remainder operator 22 Java concepts 23 Section 3: Program design concepts 24 Aim 25 Program design concepts 26 Program design concepts 28 Designing the variables 29 Designing the variables 30 Designing the variables 31 Designing the variables 32 Designing the variables 33 Designing the variables 34 Designing the variables 35 Designing the variables 36 Designing the variables 37 Designing the variables 38 Designing the variables 0-1

39 Designing the variables 40 Designing the variables 41 Designing the variables 42 Designing the variables 43 Designing the algorithm 44 Designing the algorithm 45 Designing the algorithm 46 Designing the algorithm 47 Designing the algorithm 49 Designing the algorithm 50 TimesTable.java 52 Designing the algorithm 53 Concepts covered in this chapter 0-2

Title Java Just in Time John Latham November 5, 2018 November 5, 2018 Java Just in Time - John Latham Page 1(0/0)

Chapter 9 Consolidation of concepts so far November 5, 2018 Java Just in Time - John Latham Page 2(0/0)

Chapter aims Consolidate concepts covered so far. opportunity to decide what you should reread. Introduce a few more simple concepts. Look specifically at process of designing programs mostly so far done via osmosis. November 5, 2018 Java Just in Time - John Latham Page 3(0/0)

Section 2 Java concepts November 5, 2018 Java Just in Time - John Latham Page 4(0/0)

Aim AIM: To consolidate and summarize the Java concepts introduced in the preceding chapters. We also introduce some more simple concepts which were not covered before, but which compliment those that were such as the float type. November 5, 2018 Java Just in Time - John Latham Page 5(0/0)

Java concepts Read chapter to review what already has been covered. Take time also to meet new concepts some of them will be used in later chapters. Java source code lexical details... Classes, methods, types, variables and layout... November 5, 2018 Java Just in Time - John Latham Page 6(0/0)

Type: long The type int uses four bytes, 32 binary digits. Gives range 2 31 through to 2 31 1. 2 31 1 is 2147483647. The type long represents long integers using eight bytes. Gives range 2 63 through to 2 63 1. 2 63 1 is 9223372036854775807. A long literal has L on the end e.g. -15L, 2147483648L. November 5, 2018 Java Just in Time - John Latham Page 7(0/0)

Type: short The type short represents short integers using two bytes. Gives range 2 15 through to 2 15 1. 2 15 1 is 32767. Useful when have huge number of integers lying in that range uses less space. November 5, 2018 Java Just in Time - John Latham Page 8(0/0)

Type: byte The type byte represents integers using one byte. Gives range 2 7 through to 2 7 1. 2 7 1 is 127. November 5, 2018 Java Just in Time - John Latham Page 9(0/0)

Type: char Characters are represented by type char. A char variable can store a single character. November 5, 2018 Java Just in Time - John Latham Page 10(0/0)

Type: char: literal A character literal is written using single quotes. E.g. J November 5, 2018 Java Just in Time - John Latham Page 11(0/0)

Variable: char variable Can have char variables, e.g. char firstletter = J ; November 5, 2018 Java Just in Time - John Latham Page 12(0/0)

Type: char: literal: escape sequences A character literal can have escape sequences same as for string literals. E.g. char backspace = \b ; char tab = \t ; char newline = \n ; char formfeed = \f ; char carriagereturn = \r ; char doublequote = \" ; char singlequote = \ ; char backslash = \\ ; November 5, 2018 Java Just in Time - John Latham Page 13(0/0)

Type: float The type float stores real numbers uses single precision floating point representation only four bytes per number The type double uses double precision far more accurate needs eight bytes per number. A float literal ends in f or F e.g. 0.0F, -129.934F, 98.2375f November 5, 2018 Java Just in Time - John Latham Page 14(0/0)

Java concepts Typical single class program: November 5, 2018 Java Just in Time - John Latham Page 15(0/0)

Example-class.java 001: // Comments here to say what the program does. 002: // This may take several lines. 003: public class ProgramName 004: { 005: // Each class variable should have comments saying what it is used for. 006: private static int somevariable; 007: 008: // Variables can be initialized when they are declared. 009: private static double someothervariable = somevariable * 100.0; 010: 011: November 5, 2018 Java Just in Time - John Latham Page 16(0/0)

Example-class.java 012: // Each method should have comments saying what it does. 013: // If it does not return a result, we write the word void. 014: public static void somemethod(int aparameter) 015: { 016: // Local variables should also have a comment. 017: int alocalvariable; 018: 019:... Method instruction statements go here. 020: // Comments are used to help make tricky code readable. 021:... 022: } // somemethod 023: 024: November 5, 2018 Java Just in Time - John Latham Page 17(0/0)

Example-class.java 025: // Methods which are only of use in this class/program should be private. 026: // If it returns a result, we state the type in the heading. 027: private static double someothermethod(boolean parameter1, int parameter2) 028: { 029: double alocalvariable; 030:... 031: return something; 032: } // someothermethod 033: 034: November 5, 2018 Java Just in Time - John Latham Page 18(0/0)

Example-class.java 035: // The program always starts its execution at the main method. 036: // The parameters are the command line arguments of the program. 037: public static void main(string[] args) 038: { 039:... 040: } // main 041: 042: } // class ProgramName November 5, 2018 Java Just in Time - John Latham Page 19(0/0)

Java concepts Statements... Expressions... November 5, 2018 Java Just in Time - John Latham Page 20(0/0)

Expression: arithmetic: remainder operator Another arithmetic operator remainder: % also known as modulo. Given two integer operands yields remainder after dividing first by second. E.g. public static boolean iseven(int number) { return number % 2 == 0; } // iseven November 5, 2018 Java Just in Time - John Latham Page 21(0/0)

Java concepts Errors... Standard classes... November 5, 2018 Java Just in Time - John Latham Page 22(0/0)

Section 3 Program design concepts November 5, 2018 Java Just in Time - John Latham Page 23(0/0)

Aim AIM: To look more formally at the process of designing an algorithm and writing a program. In particular, we look closely at designing variables. November 5, 2018 Java Just in Time - John Latham Page 24(0/0)

Program design concepts Seen lots of example programs, done coursework by osmosis you have begun to learn skill of programming. Now we formalize this a little: how do we write a program / method? Really: how do we write an algorithm? Guidelines not a recipe: November 5, 2018 Java Just in Time - John Latham Page 25(0/0)

Program design concepts 1. Understand the problem obviously you cannot possibly get the computer to solve it otherwise. (It is amazing how many people overlook this!) 2. Ask yourself how you would solve the problem if you were not going to program a computer. If you cannot answer this then almost certainly you will fail to get the computer to do it. 3. Consider whether the way you would do it is the way the computer should do it. Often it is, because we humans are actually very good at being lazy, and finding the best way to do something when we put our minds to it. On the other hand, sometimes the way a computer would do it is different because of the nature of computers compared with us they are very quick at doing mindless things. 4. Decide how the computer should do it, i.e. what is the basic method your algorithm will use. November 5, 2018 Java Just in Time - John Latham Page 26(0/0)

Program design concepts 5. Design the variables used in the algorithm, very carefully. We shall say more about this shortly. 6. Design the algorithm itself in pseudo code. If the previous step is done properly, this one will almost do itself. 7. Finally implement the algorithm in code this should be the easiest part if the previous steps have been followed carefully. November 5, 2018 Java Just in Time - John Latham Page 27(0/0)

Designing the variables A variable is just a named location your code can stick a value there (of appropriate type) and change it from time to time. November 5, 2018 Java Just in Time - John Latham Page 28(0/0)

Designing the variables No, No, NO, NOO, NOOOOO, NOOOOOOOOOOOOO!!!!!!!! Far too computer centric view to help us design. This is exactly what is wrong with many programs. November 5, 2018 Java Just in Time - John Latham Page 29(0/0)

Designing the variables Focus on meaning of data by designing variables get this wrong and our programs will not work be badly written hard to fix essentially useless! Classic mistake: recognise variable needed of certain type then quickly choose its name never carefully consider what it means with respect to problem Result: cryptic names buggy code. November 5, 2018 Java Just in Time - John Latham Page 30(0/0)

Designing the variables Java invites this attitude: you write type of variable before name! Resist!!!! We must really think about these the other way round. November 5, 2018 Java Just in Time - John Latham Page 31(0/0)

Designing the variables Example code, written by computer centric programmer. Assume getcurrenthumidity() and PRECIPITATION THRESHOLD boolean b; if (getcurrenthumidity() > PRECIPITATION_THRESHOLD) b = true;... else b = false; if (b == true) System.out.println("Take your umbrella!"); November 5, 2018 Java Just in Time - John Latham Page 32(0/0)

Designing the variables Is that code okay? Well it does work...maybe. Recall how naughty code like if (b == true) is? Terrible! November 5, 2018 Java Just in Time - John Latham Page 33(0/0)

Designing the variables Let s choose better name for boolean variable, b what does it really mean? boolean rainislikely; if (getcurrenthumidity() > PRECIPITATION_THRESHOLD) rainislikely = true;... else rainislikely = false; if (rainislikely == true) System.out.println("Take your umbrella!"); November 5, 2018 Java Just in Time - John Latham Page 34(0/0)

Designing the variables How often do you say? Do you think it will rain equals true? November 5, 2018 Java Just in Time - John Latham Page 35(0/0)

Designing the variables Do not need if else statement. boolean rainislikely = getcurrenthumidity() > PRECIPITATION_THRESHOLD; Do not need == true. if (rainislikely) System.out.println("Take your umbrella!"); Poor design led to poor style. November 5, 2018 Java Just in Time - John Latham Page 36(0/0)

Designing the variables Example: where it leads to buggy code we wish to count items here is pseudo code. int i = 1 while there are more items get an item i++ end-while output "There were " i " items" Coffee time: Whatiswrongwiththisdesignandhowshoulditbefixed? There are two obvious ways to fix it. Which is best? November 5, 2018 Java Just in Time - John Latham Page 37(0/0)

Designing the variables Think carefully what i really is... number of items we have had so far. int itemcountsofar = 1 while there are more items get an item itemcountsofar++ end-while output "There were " itemcountsofar " items" Ah ha! First line is a lie that is where error is. November 5, 2018 Java Just in Time - John Latham Page 38(0/0)

Designing the variables Or, i is number of next item to be obtained. int nextitemnumber = 1 while there are more items get an item nextitemnumber++ end-while output "There were " nextitemnumber " items" Now last line is a lie that is where error is! November 5, 2018 Java Just in Time - John Latham Page 39(0/0)

Designing the variables i was an ambiguous name because of lack of design about what it really means was treated with different interpretation in different parts of design. Which interpretation is best? itemcountsofar or nextitemnumber? subjective but, always a count of items so far, not always a next item... November 5, 2018 Java Just in Time - John Latham Page 40(0/0)

Designing the variables Treat variables with respect design them carefully. They represent identifiable entities in the problem. If cannot fully and precisely describe meaning in one sentence then probably is a bad variable think again! That sentence will be your comment for the variable. Condense it to a few words to make variable name. Then you can identify its type! November 5, 2018 Java Just in Time - John Latham Page 41(0/0)

Designing the variables If you design variables well then algorithm code almost writes itself just ensure value of every variable reflects its meaning at all times you cannot have a bug! Well, you might make a mistake but if you do, it will be easy to spot. Plus, code is easier to read and maintain too! November 5, 2018 Java Just in Time - John Latham Page 42(0/0)

Designing the algorithm Express algorithm at high level of abstraction. Add more detail lower abstraction level. Until low enough that implementation in Java straightforward. E.g. our first example was MinimumBitWidth: get numberofvalues from command line noofbits = 0 while noofbits is too small increment noofbits output noofbits November 5, 2018 Java Just in Time - John Latham Page 43(0/0)

Designing the algorithm Then added more detail: numberofvalues = args[0] noofbits = 0 noofbits while 2 noofbits++ s.o.p noofbits < numberofvalues Close enough to Java so easy to implement. November 5, 2018 Java Just in Time - John Latham Page 44(0/0)

Designing the algorithm Next example was PiEstimation: obtain tolerance from command line set up previousestimate as value from no terms set up latestestimate as value from one term while previousestimate is not within tolerance of latestestimate previousestimate = latestestimate add next term to latestestimate end-while print out latestestimate print out the number of terms used print out the standard known value of Pi for comparison November 5, 2018 Java Just in Time - John Latham Page 45(0/0)

Designing the algorithm We lowered level of abstraction: double tolerance = args[0] double previousestimate = 0 double latestestimate = 4 int termcount = 1 while previousestimate is not within tolerance of latestestimate previousestimate = latestestimate add next term to latestestimate termcount++ end-while s.o.p latestestimate s.o.p termcount s.o.p the standard known value of Pi for comparison November 5, 2018 Java Just in Time - John Latham Page 46(0/0)

Designing the algorithm And again: double tolerance = args[0] double previousestimate = 0 double latestestimate = 4 int termcount = 1 int nextdenominator = 3 int nextnumeratorsign = -1 while Math.abs(latestEstimate - previousestimate) > tolerance previousestimate = latestestimate latestestimate += nextnumeratorsign * 4 / nextdenominator termcount++ nextnumeratorsign *= -1 nextdenominator += 2 end-while November 5, 2018 Java Just in Time - John Latham Page 47(0/0)

Designing the algorithm s.o.p latestestimate s.o.p termcount s.o.p Math.PI November 5, 2018 Java Just in Time - John Latham Page 48(0/0)

Designing the algorithm Another example was TimesTable: print the box top line print column headings print headings underline for row = 1 to 10 print a row print the box bottom line Later we implemented this using separate methods. Steps in early abstract designs often make good choice for methods: November 5, 2018 Java Just in Time - John Latham Page 49(0/0)

TimesTable.java 001: // Program to print out a neat multiplication table. 002: public class TimesTable 003: { 004: // The size of the table -- the number of rows and columns. 005: private static int tablesize = 12; 006: 007: 008: // The main method implements the top level structure of the table. 009: public static void main(string[] args) 010: { 011: // Top line. 012: printline(); 013: 014: // Column headings. 015: printcolumnheadings(); November 5, 2018 Java Just in Time - John Latham Page 50(0/0)

TimesTable.java 016: 017: // Underline headings. 018: printline(); 019: 020: // Now the rows. 021: for (int row = 1; row <= tablesize; row++) 022: printrow(row); 023: 024: // Bottom line. 025: printline(); 026: } // main... 076: } // class TimesTable November 5, 2018 Java Just in Time - John Latham Page 51(0/0)

Designing the algorithm Generalize algorithm design process: 1. Identify main steps of algorithm. 2. Express algorithm in terms of main steps. 3. Separately expand on each step using same process i.e. identify its main steps... Sometimes called top down stepwise refinement. November 5, 2018 Java Just in Time - John Latham Page 52(0/0)

Concepts covered in this chapter Each book chapter ends with a list of concepts covered in it. Each concept has with it a self-test question, and a page reference to where it was covered. Please use these to check your understanding before we start the next chapter. November 5, 2018 Java Just in Time - John Latham Page 53(0/0)