Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Basics of Java Programming

Declaration and Memory

Full file at

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

Getting started with Java

Chapter 2: Data and Expressions

Visual C# Instructor s Manual Table of Contents

COMP 110 Introduction to Programming. What did we discuss?

Information Science 1

3. Java - Language Constructs I

Chapter 2: Data and Expressions

CIS 110: Introduction to Computer Programming

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Chapter 2 Elementary Programming

4 Programming Fundamentals. Introduction to Programming 1 1

Full file at

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.

LECTURE 3 C++ Basics Part 2

COMP-202: Foundations of Programming

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

Chapter 2: Data and Expressions

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

JAVA OPERATORS GENERAL

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

Zheng-Liang Lu Java Programming 45 / 79

JAVA Programming Fundamentals

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Building Java Programs

Information Science 1

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

CMPT 125: Lecture 3 Data and Expressions

CS313D: ADVANCED PROGRAMMING LANGUAGE

CHAPTER 3 Expressions, Functions, Output

ARG! Language Reference Manual

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Computer System and programming in C

Building Java Programs

Chapter 3: Operators, Expressions and Type Conversion

Chapter 2: Using Data

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

CS 106 Introduction to Computer Science I

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

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

More Programming Constructs -- Introduction

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Chapter 6 Primitive types

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Chapter 2 Primitive Data Types and Operations. Objectives

Lecture Set 2: Starting Java

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

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

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Datatypes, Variables, and Operations

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

Programming with Java

Lecture Set 2: Starting Java

CS Programming I: Primitives and Expressions

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

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

Java Programming Fundamentals. Visit for more.

Section 2: Introduction to Java. Historical note

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 7 Arithmetic

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

Chapter 2 Working with Data Types and Operators

CIS133J. Working with Numbers in Java

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Object-Oriented Programming

Chapter 2: Introduction to C++

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

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CS11 Java. Fall Lecture 1

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Values, Variables, Types & Arithmetic Expressions. Agenda

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

Java Notes. 10th ICSE. Saravanan Ganesh

Outline. Data and Operations. Data Types. Integral Types

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Ex: If you use a program to record sales, you will want to remember data:

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Introduction to Computer Programming

Programming Language Basics

ECE 122 Engineering Problem Solving with Java

Exercise: Using Numbers

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Unit 3. Constants and Expressions

CHAPTER 2 Java Fundamentals

Transcription:

Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent the options to make figuring out the result easier? 67

Primitive Data Types boolean stores true (1) or false (0) values byte holds integer in one byte = 8 bits char holds a single character short, int, long hold integers of varying sizes float, double hold real numbers (floating point) Variables have 4 features: name, type, location in memory and value (once assigned) 68

Integer Representation literal values are whole numbers: 8, 23, -102 base ten values are converted to binary left-most bit is for the sign 0 means positive 1 means negative each type (byte, short, int, long) has different size in memory int is used most commonly 69

Character Representation char literals must be in single quotes: 'A', '4', '+' each character is assigned an integer code full set is Unicode System extension of original ASCII system decimal integer codes are converted to binary 'A' to 'Z' have consecutive codes (65-90) 'a' to 'z' have consecutive codes (97-122) '0' to '9' have consecutive codes (48-57) Section 3.2 in textbook 70

Real Number Representation float literals must have f: 10f, 23.342F, -102.3f double literals (more common) can be floating: 10.3, -100.23,.023345 or scientific notation: 1.03e1, -1.0023e2,.23345e-1 stored internally in two parts: 123.45 =.12345e3 12345 is mantissa converted to binary 3 is exponent converted to binary double has twice as many mantissa bits as a float, so you get more precision 71

Arithmetic Operations addition: val1 + val2 subtraction: val1 val2 multiplication: val1 * val2 division result depends on operand types: int1 / int2 gives a whole number result (div) if float or double as one operand, you get a real number result division modulus for integers: val1 % val2 gives whole number remainder after val1 / val2 72

Evaluating Expressions The data type of each binary operation result depends on the data type of the operands The "lower" number type operand is implicitly converted to the "higher" number type operand The result is the same type as the "higher" For example: int + int => int int float => float double / int => double Operator precedence (next slide) determines the order of the operations in a mixed expression 73

Operators have precedence () do inside parentheses first - negation, casting: (int), (double), (char) etc. * multiplication, / division, % mod (remainders) + addition, - subtraction = assignment Section 2.9 in textbook 74

Misc. data items and operator combinations escape sequences for special characters: \" \\ \n \t final for named constants assignment combinations: +=, -=, *=, /=, %= ++, -- in prefix or postfix mode 75

Arithmetic Activity Look at code/arithmetic.java for examples of type conversions and uses of arithmetic operators. Download the file. Use the jgrasp debugger to step through and annotate each line of code (next to the //) with the values that are being assigned. Submit your annotated arithmetic.java file on Blackboard in Classwork Submission as c02. 76

Writing Java code [Make sure your pseudocode is handy!] Start with outer structure: class, main, other methods? Declare some variables Get input, display (incorrect) output Fill in the missing pieces, one task at a time. Compile, [checkstyle] and run at each step. 77

Java program template import java.util.scanner; /** Documentation. */ public class ClassName { /** Documentation. * @param args (not used) */ public static void main(string[] args) { // executable statements } } // other method definitions can go here or above main 78

Recall: Executable statements Variable declarations Assignment statements Method calls Decision statements Loops 79

Variable Declarations For primitives and strings: type identifier; // no initialization type ident = value; // with initialization type ident1, ident2, ident3; // all same type type ident1 = value1, ident2, ident3 = value3; // ident2 was not initialized For class types (objects): ClassName varname; // no initialization ClassName varname = new ClassName(params); 80

Assignment statements A single '=' symbol is used We pronounce it "gets" Store value in varname: varname = value; Evaluate expression and store result in varname: varname = expression; Expressions can be any valid combination of operators, variables, literal values and method calls 81

Method calls Can vary depending on where and how the method is defined. If static and defined in same class: methodname(params) If static and defined in another class: ClassName.methodName(params) Math.round(value) If belongs to a class and requires an object: objectvar.methodname(params) System.out.println("hello"); mystring.charat(0) 82

Checkstyle Tool to ensure that our code conforms to a standard style (similar to the Java API code) Specific style for this course is in file http://cs.jhu.edu/~joanne/cs107/check107.xml See notes on homepage and hw2 to download, configure and use with jgrasp Advantage is it will help you learn to be better programmers and make debugging easier! 83

Checkstyle Details how to install & configure running on arithmetic.java common errors missing javadoc comment (must start with /**) file/classname must start with Capital letter { must be on same line as the block control statement indentation use jgrasp control structure diagram to fix magic numbers use final to fix read documentation linked from main course page 84

Homework Review Solutions for #1 Homework 2 start the formulas 85