Advanced Computer Programming

Similar documents
Programming in the Small II: Control

Basics of Java Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

Datatypes, Variables, and Operations

CSC 1214: Object-Oriented Programming

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

Program Fundamentals

UNIT- 3 Introduction to C++

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

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Advanced Computer Programming

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

13 th Windsor Regional Secondary School Computer Programming Competition

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

4 Programming Fundamentals. Introduction to Programming 1 1

DATA TYPES AND EXPRESSIONS

Fundamental of Programming (C)

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

JAVA OPERATORS GENERAL

MODULE 02: BASIC COMPUTATION IN JAVA

Operators. Java operators are classified into three categories:

Reserved Words and Identifiers

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Full file at

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

A First Program - Greeting.cpp

Visual C# Instructor s Manual Table of Contents

Chapter 2 Primitive Data Types and Operations. Objectives

3. Java - Language Constructs I

Fall 2017 CISC124 9/16/2017

LECTURE 3 C++ Basics Part 2

JAVA Programming Fundamentals

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

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

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

Computational Expression

Fundamentals of Programming

CEN 414 Java Programming

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

Introduction to Java

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

CS11 Java. Fall Lecture 1

Course Outline. Introduction to java

The C++ Language. Arizona State University 1

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.

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Lecture Set 4: More About Methods and More About Operators

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

A Java program contains at least one class definition.

Introduction to Programming Using Java (98-388)

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

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

Chapter 1 Introduction to java

Programming in the Large II: Objects and Classes (Part 2)

CT 229. Java Syntax 26/09/2006 CT229

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Chapter 2: Data and Expressions

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

COMP 202 Java in one week

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 2 Elementary Programming

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

Differentiate Between Keywords and Identifiers

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

Elementary Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Programming in the Large II: Objects and Classes (Part 1)

Simple Java Reference

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

Declaration and Memory

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

Chapter 2: Introduction to C++

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

Software and Programming 1

Object-Oriented Programming

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

Identifiers and Variables

Full file at

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Chapter 3: Operators, Expressions and Type Conversion

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture Set 4: More About Methods and More About Operators

Operators and Expressions

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

Java Bytecode (binary file)

Arithmetic and IO. 25 August 2017

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Variables and Operators 2/20/01 Lecture #

Transcription:

Programming in the Small I: Names and Things (Part II) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University

Agenda Arithmetic Operations Programming Environments Programming Exercises 2

Numerical Data Types byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits 3

Number Literals int i = 34; long l = 100000; or long = 1000000l; float f = 100.2f; or float f = 100.2F; double d = 100.2d or double d = 100.2D; int octal = 035; int hex = 0xa2; 4

Constant Variables Our own declaration final datatype CONSTANTNAME = value; Examples: final double PI = 3.14159; final int SIZE = 3; Constants in Java built in classes Math.PI Its type is double Its value is 3.14... 5

Operators +,, *, /, and % Examples 5/2 =? 5.0/2 =? 2/4 =? 5%2 =? 6

Shortcut Operators Operator Example Equivalent += i += 8 i = i + 8 = i = 8 i = i 8 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 7

Increment & Decrement Operators x = 1; a = 1 + x++ =? b = 1 + ++x =? c = 1 + x =? d = 1 + x =? 8

Relational Operators A == B A!= B A < B A > B A <= B A >= B Note that A and B must have numeric types or char type. A and B cannot be String 9

Boolean Operators The boolean operator and is && The result is also a boolean value. The result is true if both of the combined values are true The boolean operator or is The result is false if either of the combined values is false The boolean operator not is a unary operator which is! 10

Short circuited The operators && and are said to be shortcircuited versions of the boolean operators. This means that the second operand of && or is not necessarily evaluated Consider the test (x!= 0) && (y/x > 1) If value of x is in fact zero, the computer will never perform the division S since when the computer evaluates (x!= 0), it finds that the result is false 11

Boolean Operators Examples int a = 2, b = 3; boolean c = (a > 2) && (++b > 3) Then a =? b =? c =? a = 2; b = 3; boolean d = (a > 2) (++b > 3) Then a =? b =? d =? 12

Conditional Operators Java has the conditional operator It s a ternary operator that is, it has three operands boolean expression? expression1 : expression2 The computer tests the value of booleanexpression If the value is true, it evaluates expression Otherwise, it evaluates expression2 13

Conditional Operators Example int n = 3; int next = (n % 2 == 0)? (n/2) : (3*n+1); Then n =?, next =? n = 2; next = (n % 2 == 0)? (n/2) : (3*n+1); Then n =?, next =? 14

Assignment Operators Type of the expression on the RHS of an assignment statement must be the same as the type of the variable on the LHS But the computer may automatically convert the value computed by the expression to match the type of the variable. Bad example: if ( (a=b) == 0 ) System.out.println( Hello ); else System.out.println( Bye ); 15

Type Casting int a = 17; double x; short b; x = a; // Legal? b = a; // Legal? b = (short) a; // Legal? 16

Type Conversion of String How to convert the string "10" into the int value 10? How to convert the string "17.42e 2" into the double value 0.1742 In Java, these conversions are handled by builtin functions. Integer.parseInt( 10 ) = 10 Integer.parseInt( a ) =? 17

Sample Program 18

Precedence Rules Unary operators: ++,,!, unary and +, type cast Multiplication and division: *, /, % Addition and subtraction: +, Relational operators: <, >, <=, >= Equality and inequality: ==,!= Boolean and: && Boolean or: Conditional operator:?: Assignment operators: =, +=, =, *=, /=, %= 19

Java Packages Many times we put all Java files into one single directory But the number of files is increasing, putting all these files into the same directory would be difficult to find files Packages are nothing more than the way we organize files into different directories according to their functionality, usability category 20

Java Packages and Directories An obvious example of packaging is the JDK package from SUN (java.xxx.yyy) as shown below: 21

The Purpose of Java Packages Basically, files in one directory (or package) would have different functionality from those of another directory Files in java.io package do something related to I/O Files in java.net package give us the way to deal with the Network. 22

Packages Solve Class Name Collision Packaging also help us to avoid class name collision when we use the same class name as that of others If we have a class name called "Vector", its name would crash with the Vector class from JDK However, this never happens because JDK use java.util as a package name for the Vector class (java.util.vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.vector without fighting with anyone. 23

Programming Environments Two approaches for creating, compiling, and edit Java programs A command line environment The user types commands and the computer responds Example: javac Hello.java java Hello An integrated development environment (IDE) The user uses the keyboard and mouse to interact with a user graphical interface 24

IDEs and Eclipse In an IDE, everything you need to create, compile, and run programs is integrated into a single package, with a graphical user interface Eclipse is used by many professional programmers Eclipse is probably the most commonly used Java IDE 25

Starting Eclipse We'll be working with Eclipse to create and configure a new Java project First start up Eclipse and supply a path to a new folder which will serve as your workspace The workspace is a folder which Eclipse uses to store your source code 26

Welcome Page When Eclipse starts, you'll see the Welcome page: 27

Creating a New Project Close the Welcome page. Right click in the Navigator panel, and select New >Project: 28

Choose Project Type Choose project type as Java project Select Java Project and then Click Next 29

Assign Project Name Assign project name Fill the project name field and click Finish 30

Create a New Package Right click Project and choose package 31

Assign Package Name Type package name and then click Finish 32

Create a New Class Right click at the package and choose Class 33

Assign Class Name Type class name, chose public static void main.., and click Finish 34

Write the Code with Comments 35

Run the Program Click at the button that is circled with a black color and then choose Run As > Java Application The program output appears at the console 36

Set the Program Argument Open the run configuration 37

Set the Program Argument 38

Programming Style and Documentation Appropriate comments Naming conventions Proper indentation and spacing lines Block styles 39

Appropriate Comments Include a summary at the beginning of the program to explain about the program What the program does and its key features Special techniques it uses Program version Include the information about the programmer Your name, class section Date 40

Naming Conventions Choose meaningful and descriptive names Capitalize class name public class HelloWorld Use lowercase letters for variable and method names int a; void takeclass(string classname); Use all uppercase letters for a constant final int NUMPROVINCES = 76; 41

Proper Indentation & Spacing Indentation Make an indent codes in the same group indent in the same vertical line Have each statement on each line Spacing Use a blank line to separate a group of code 42

Programming Exercises Write a Java program that accepts the command line arguments which include the width and the height of a rectangle. Then, display its circumference and its area Sample program output: java RecComputation 2 3 The circumference of a rectangle with width = 2 and height = 3 is 10 and its area is 6 43

Group Exercise 1. Form a group of 4 people 2. Develop a Java program in package <member1>.<member2>.<member3>.<member 4> This Java Program is to multiply and divide two double values It accepts an operator and two operands. The user needs to choose which operator that he/ she will use 44

References David J. Eck, Introduction to Programming Using Java, Version 5.0, December 2006 http://math.hws.edu/javanotes/ eclipse tutorial:developing open source Java applications with java.net and Eclipse, Available at https://eclipse tutorial.dev.java.net/eclipse tutorial/part1 Patrick Bouklee, Java Package Tutorial, Available at http://www.jarticles.com/package/package_eng.html 45