Example: Tax year 2000

Similar documents
true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Methods (example: Methods1.java)

Introductory Programming Arrays, sections

(a) in assignment statements. (b) when invoking methods (with objects as parameters) (d) equality of references versus equality of objects

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

ECE 122 Engineering Problem Solving with Java

Introductory Programming Inheritance, sections

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

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

Data Conversion & Scanner Class

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

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators

Full file at

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2: Using Data

Chapter 2: Data and Expressions

JAVA Programming Fundamentals

Programming with Java

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

COMP 110 Introduction to Programming. What did we discuss?

Chapter. Let's explore some other fundamental programming concepts

Building Java Programs

Chapter 2 Elementary Programming

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Chapter 2: Basic Elements of Java

Chapter 2 ELEMENTARY PROGRAMMING

3. Java - Language Constructs I

Building Java Programs

Full file at

CS111: PROGRAMMING LANGUAGE II

CMPT 125: Lecture 3 Data and Expressions

Formatting Output & Enumerated Types & Wrapper Classes

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Visual C# Instructor s Manual Table of Contents

Chapter 2: Data and Expressions

JAVA OPERATORS GENERAL

Operators and Expressions

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

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Getting started with Java

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CEN 414 Java Programming

Chapter 2: Using Data

Values, Variables, Types & Arithmetic Expressions. Agenda

Program Fundamentals

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2. Elementary Programming

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

Chapter 6 Primitive types

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

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

Basics of Java Programming

Declaration and Memory

Values and Variables 1 / 30

Chapter 2: Data and Expressions

Building Java Programs

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CIS 110: Introduction to Computer Programming

1 Shyam sir JAVA Notes

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Section 2: Introduction to Java. Historical note

Java Primer 1: Types, Classes and Operators

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CMSC131. Introduction to your Introduction to Java. Why Java?

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

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Computational Expression

Chapter 02: Using Data

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Basic Programming Elements

Object Oriented Programming. Java-Lecture 1

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

2: Basics of Java Programming

Programming. Syntax and Semantics

More Programming Constructs -- Introduction

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

CSC 1214: Object-Oriented Programming

Important Java terminology

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

1.00 Lecture 4. Promotion

Transcription:

Introductory Programming Imperative Programming I, sections 2.0-2.9 Anne Haxthausen a IMM, DTU 1. Values and types (e.g. char, boolean, int, double) (section 2.4) 2. Variables and constants (section 2.3) 3. Statements (e.g. assignments) (section 2.3) 4. Expressions (e.g. arithmetic) (section 2.4) 5. Data conversion (section 2.4) 6. Objects and classes (e.g. String) (sections 2.0, 2.1, 2.2, 2.6, 2.7) 7. Output to screen (sections 2.1, 2.9) 8. Input from keyboard (section 2.8) a. Parts of this material are inspired by/originate from a course at ITU developed by Niels Hallenberg and Peter Sestoft on the basis of a course at KVL developed by Morten Larsen and Peter Sestoft. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-1 Example: Tax year 2000 Source: FORSTÅ Skatten..., Skatteministeriet c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-2 Example: Calculation of AMBI and special pension payment public class Tax1 { public static void main(string[] args) { int income = 120000; double ambi, pension; ambi = income * 8.0 / 100.0; pension = income * 1.0 / 100.0; } } System.out.print("AMBI: "); System.out.println(ambi); System.out.print("Special pension payment: "); System.out.println(pension); c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-3 Output from the Tax1 program AMBI: 9600.0 Special pension payment: 1200.0 c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-4

Basic concepts: values and types A (data) value can for instance be an integer (heltal): 120000 a floating point number (kommatal): 8.0 a character (tegn):! a Boolean (sandhedsværdi): false or true a character string (tegnstreng): "Bundskat: " A (data) type is a family of values and operations on these. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-5 Types in Java byte, short, int, long are types of integers:..., -2, -1, 0, 1, 2, float, double are types of floating point numbers: e.g. -32.3, 1.0, 42.456,, 4.5E6, char is the type of characters: a,..., 1,...,!,..., \n,... boolean is the type of Booleans: true, false String is a type of strings of characters: "Bundskat: ", "Peter",, String is not a primitive type, but a so-called class. More about that later. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-6 Numeric values In a computer every numeric value is represented as a binary number, i.e. combinations of bits (0 and 1). Example: the numeric value 4 is represented as the binary number 100. The various kinds of numeric values differ by the amount of memory space used to store a value of that type. E.g. the type int uses 32 bit. See figure 2.4 of the book. Literals: An integer like 17 is of type int. An integer followed by L, e.g. 14084591234L, is of type long. A floating point number like 17.2 is of type double. A floating point number followed by F, e.g. 17.2F, is of type float. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-7 Characters In a computer every character is represented as an integer, e.g. A is represented as 65. A character set is a translation from code (integers in the computer) to characters (graphics on a screen or paper). Standard character sets: ASCII, ISO Latin1, Unicode. Java uses Unicode. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-8

Exercise: what are the types of the following values? Value Type 58 true -23 "afd " 42.0 $ "42.0" "true" 7 c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-9 Basic concepts: variable, declaration and assignment A variable has a name and a location in the computer memory that can contain a value of a given type. A declaration gives a name and a type for one or more variables: double ambi, pension; The declaration also allocates space for the variables in the memory. A declaration can contain an initial value: int income = 120000; An assignment (tildelingssætning) assigns a value to a variable by storing the value in the memory location belonging to the variable: ambi = income * 8.0 / 100.0; c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-10 Basic concept: constants A constant is similar to a variable, but it has the same value all the time. Example of declaration: final double AMBI_PROCENTAGE = 8.0; Example of use: ambi = income * AMBI_PROCENTAGE / 100.0; c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-11 Basic concept: statements The state consists of the contents of the computer memory, output (on the screen), etc. A statement changes the state. Example 1: an assignment can change the value of a variable: pension = income * 1.0 / 100.0; Example 2: a print statement can write on the screen: System.out.print("Special pension payment: "); c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-12

Basic concept: expressions An expression denotes (Danish: angiver) a value. Example: income * 8.0 / 100 Kinds of expressions: arithmetic expressions (denote numeric values) Boolean expressions (denote Boolean values) string expressions (denote string values) c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-13 Arithmetic expressions Are combinations of arithmetic operators and operands. Operator Meaning Examples * Multiplication 1.5 * 60.0 24 * 60 / Division 13.0 / 2.0 13 / 2 % Remainder 13.0 % 2.0 13 % 2 + Addition 1.1 + 60.0 14 + 60 - Subtraction 1.1-60.0 134-60 Result type is int if both arguments are int, otherwise double. Read more on operator precedence and evaluation order in the book. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-14 Boolean expressions More about that in chapter 3 (next lecture). c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-15 String expressions Like you can write arithmetic expressions representing numeric values, you can write expressions representing strings of characters. An important string operator: string concatenation ( ) Example: "This is " + "course 02100" (represents the string "This is course 02100") c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-16

Types of expressions Each expression has a type. Examples: 1 2 has type int "This is " + "course 02100" has type String c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-17 Basic concept: data conversion Values of one type can (in certain cases) be converted to another type: 1. Conversions between numerical types: (a) widening : from a smaller type to a larger type (e.g. from int to double) (b) narrowing : from a larger type to a smaller type (e.g. from double to int) 2. Conversion from any type to String type In Java 1a and 2 is done automatically, but 1b can only be done explicitly by casting. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-18 Examples of implicit widening conversion double money; int dollars; //assignment conversion: dollars = 25; money = dollars; //now: money == 25.0 //illegal assignment: dollars = money; //Second argument (2) of / is converted to a double (2.0): money = money / 2; //now: money == 12.5 //Here 2 is not converted: money = 25.0; money = (int) money / 2; //now: money == 12.0 c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-19 Examples of narrowing conversion by casting money = 84.69; // casting, that throws decimals away: dollars = (int) money; //now: dollars == 84 c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-20

Example of conversion to a string int x = 7; System.out.println("the contents of x is: " + x); Second argument of + is automatically converted to the string "7", before it is concatenated with the first argument. Output: the contents of x is: 7 Generally it holds for s + v and v + s, where s is a string and v an expression of another type, that v is automatically converted to its string representation. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-21 Exercise: which values and types do the following expressions have? Expression Value of expression Type of expression 1.5 * 60.0 1.5 * 60 24 * 60 1.1 + 60-1 150.0 / 60 150 / 60 134.0 % 60 134 % 60 "02199" "x is equal to "+ "0" "x is equal to "+ 0 c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-22 Short introduction to classes and objects Informally A class represents a concept: time, appointment, car, cow, person,... An object represents a thing, an instance of a concept: a particular time, a particular car, a particular cow, a particular person,... A class has a collection of methods: those operations (functions) that can be applied to its objects. In Java A class corresponds to a type, like int, double, boolean,... An object corresponds to a value, like 17, 18.01, false,... A method corresponds to an operation, like,,... c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-23 Three steps in using classes, objects and methods 1. Define a class (incl. its methods). 2. Create objects of the class. 3. Use the methods of the objects. Some classes (e.g. String) are already defined in a class library. In that case you can skip step one. If a class has static methods, these can be used without step 2. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-24

Definition of classes Later you will learn how to define a class that contains declarations of: data (constants and variables) methods (each method is given a name and a sequence of statements, that have to be executed when the method is invoked.) For now we will only use classes from libraries. For each class there is an interface informally describing the methods of the class: name, argument types and a result type, and what happens when it is invoked. Example: Extract of interface for String int length() returns the number of characters in the string char charat(int index) returns the character at the specified index c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-25 Creations of objects An object is an instance of a class. It has data (e.g. a string) and methods as described by the class. A variable contains either a primitive value, or a reference (henvisning) to an object. Example: a variable containing a reference to an object of class String String s1 = "How do you do?"; c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-26 Example: an object of class String : String "How do you do?" s1 The variable s1 is a location in the memory containing a reference to the object containing the string c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-27 Use of methods Invocation (kald) of the methods of an object is done with the dot operator. Example: invocation of a String method s1.length(); returns the value 14 (when the object, referenced to by s1, has the value "How do you do?"). c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-28

Class libraries and packages A class library is a collection of classes. The classes are organized into various packages. The most popular standard packages are: Package Supports Classes java.lang General stuff; automatically imported Math, String, System java.io Input and output (e.g. to/from files)... java.util General aux. classes Random,... To use a class of a package, you must either qualify the class name with the package name, or use an import statement, e.g.: import java.util.*; // all classes in the package import java.util.random; // only the Random class c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-29 Output to screen The methods System.out.print System.out.println can be used to print text on the screen. The NumberFormat and DecimalFormat classes provide methods for formatting the output nicely. See section 2.9 of the book. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-30 Input from the keyboard The Keyboard class has methods for reading data from the keyboard static boolean readboolean() static byte readbyte() static char readchar() static double readdouble() static float readfloat() static int readint() static long readlong() static short readshort() static String readstring() static means that methods can be invoked via their class name, e.g. Keyboard.readBoolean() The class can be copied from the cd of the book. c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-31 Example of input (and output) import cs1.keyboard; public class Question { public static void main (String[] args) { String name; int cars; System.out.print("What is your name? "); name = Keyboard.readString(); System.out.print("How many cars do you own, " + name + "? "); cars = Keyboard.readInt(); } } if (cars>1) System.out.println(name + " owns many cars!"); c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-32

Execution of Question > java Question What is your name? Anne How many cars do you own, Anne? 1 > java Question What is your name? Henrik How many cars do you own, Henrik? 2 Henrik owns many cars! c Haxthausen and Sestoft, IMM/DTU, 9. september 2002 02100+02115+02199+02312 Introductory Programming Side 2-33