Arithmetic and IO. 25 August 2017

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

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

CS110: PROGRAMMING LANGUAGE I

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

CS111: PROGRAMMING LANGUAGE II

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Programming with Java

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CS 302: Introduction to Programming

Chapter 2: Data and Expressions

Full file at

Full file at

Basics of Java Programming

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

CEN 414 Java Programming

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

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

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

double float char In a method: final typename variablename = expression ;

LECTURE 3 C++ Basics Part 2

Chapter 2: Data and Expressions

FUNDAMENTAL DATA TYPES

Chapter 2: Data and Expressions

I. Variables and Data Type week 3

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

Program Fundamentals

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

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

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

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Basic Computation. Chapter 2

Computational Expression

Data Conversion & Scanner Class

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

CIS 110: Introduction to Computer Programming

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Week 3: Objects, Input and Processing

MODULE 02: BASIC COMPUTATION IN JAVA

Peer Instruction 1. Elementary Programming

Computer System and programming in C

Programming in Java

Introduction to Computer Science and Object-Oriented Programming

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Getting started with Java

2.5 Another Application: Adding Integers

Chapter 3: Operators, Expressions and Type Conversion

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

CMPT 125: Lecture 3 Data and Expressions

CIS133J. Working with Numbers in Java

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Chapter. Let's explore some other fundamental programming concepts

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

STUDENT LESSON A7 Simple I/O

Primitive Data Types: Intro

Data types Expressions Variables Assignment. COMP1400 Week 2

Java Reference Card. 1. Classes. 2. Methods. 3. Conditionals. 4. Operators

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Operators. Java operators are classified into three categories:

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

Java Console Input/Output The Basics. CGS 3416 Spring 2018

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

Simple Java Reference

Chapter 2 Elementary Programming

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Advanced Computer Programming

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

Section 2: Introduction to Java. Historical note

Basic Computation. Chapter 2

What did we talk about last time? Examples switch statements

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Course Outline. Introduction to java

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

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

DATA TYPES AND EXPRESSIONS

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Introduction to Java Applications

Chapter 02: Using Data

CS102: Variables and Expressions

Elementary Programming

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

COMP 110 Introduction to Programming. What did we discuss?

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

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

Section 2.2 Your First Program in Java: Printing a Line of Text

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

2: Basics of Java Programming

Transcription:

Arithmetic and IO 25 August 2017

Submissions you can submit multiple times to the homework dropbox file name: uppercase first letter, Yourlastname0829.java the system will use the last submission before the deadline mail size to confirm a file was attached

Variables variables must be declared before they can be used unlike Ada, variables in Java are not declared all at the top of a block they are declared inside the code, as late as possible, immediately where they are needed variable identifiers must begin with a lowercase letter, and use camelcase a variable may be initialized when it s declared, or not, depending on need (do not initialize when unnecessary) a constant is like a variable, except a constant must be initialized when it is declared its value can never change it is denoted by the reserved word final its identifier is in SNAKE CASE

Primitive Conversion and Casting you can convert amongst types much more easily than in Ada but much more difficult than in Python some conversions are automatic, some require a typecast lossless conversions are automatic double conversion factor = 12.34F; long accululator = 12; integer to floating is automatic, even if lossy float circumference = 9123456789123456789L; // ugh!

Primitive Conversion and Casting all other conversions either require a typecast or are illegal int quotient = 10.0 / 3.0; // illegal int quotient = (int)(10.0 / 3.0); // ok no conversion or cast is legal to or from boolean

Arithmetic the biggest deal is keeping integer and floating point arithmetic straight if all operands are integers, the result is an integer if one or two operands are floating (float or double), the result is floating there is only one division operator, / if both operands are integers, the operation is (truncating) integer division if either operand is floating, the result is floating

Increment and Decrement the most common operations in all of computing are increment-by-one and decrement-by-one Java has pre- and post-increment and -decrement operators int x = 5; x++; double d = 10.2; ++d;

Increment and Decrement what are the values of x, y, and z after this code? int x = 5; x--; int y = x--; int z = --x;

Increment and Decrement what are the values of x, y, and z after this code? int x = 5; x--; int y = x--; int z = --x; x is 2 y is 4 z is 2

Retrieve - Modify - Store the second-most common operation Awkward Newbie x = x + 5; Correct Professional Way x += 5; works for all binary arithmetic operators special trap for Ada folks: x /= 2;

Integer Division and Remainder 7/3 =

Integer Division and Remainder 7/3 = 2 7/ 3 =

Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 =

Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 = 1 7 % 3 =

Integer Division and Remainder 7/3 = 2 7/ 3 = 2 7 % 3 = 1 7 % 3 = the behavior of % is very confusing if either operand is negative different languages define it differently Java: 7 % 3 = 1 Python: 7 % 3 = 2 Ada: two different operators, mod and rem don t try to memorize know what you want, test to make sure it works

Math Computations beyond simple 4-function arithmetic, Java has the Math library it is built-in, so you don t need to import it a few examples: Math.sqrt(x) returns the square root of x Math.exp(x) returns the value e x

Console Input and Output in CS260, you will write graphical programs in CS181, all our programs use text for input and output all input will come from keyboard file all output will go to screen file

Output output is relatively simple System. }{{}}{{} out class object. print }{{} method.( }{{} foo ); argument the argument can be a primitive or object value the argument can be literal, variable, constant, or expression the print method does not add a newline the println method adds a newline at the end

Simple Formatting for spacing, tabular output, and floating values, we often want to format the output Java uses the C-style printf method with a format specifier and a list of values System.out.println("2.0 / 3.0 is: " + 2.0 / 3.0); 2.0 / 3.0 is: 0.6666666666666666 System.out.printf("2.0 / 3.0 is: %1.3f\n", 2.0 / 3.0); 2.0 / 3.0 is: 0.667

Multiple Values System.out.printf("My name is %s and I'm %d years old.\n", name, age); My name is Fred and I'm 12 years old.

Input input is a bit more complicated Java was invented to be a language for graphical programs the assumption was that most input would come from graphical widgets like this: Java has a pre-defined input object named in but it s awkward to use

Input instead, for text input, we use a helper at the top of the file we do Library Import import java.util.scanner; and then in the code we do Create Scanner Object Scanner in = new Scanner(System.in); and finally use the scanner object like this Using Scanner int age = in.nextint();

Example discuss Volume2.java from text section 2.3.2