CSC 1214: Object-Oriented Programming

Similar documents
3. Java - Language Constructs I

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

JAVA OPERATORS GENERAL

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Operators. Java operators are classified into three categories:

COMP 202 Java in one week

Prof. Navrati Saxena TA: Rochak Sachan

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

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

Operators and Expressions

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

Program Fundamentals

Java Programming. Atul Prakash

CSC 1107: Structured Programming

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

3. Java - Language Constructs I

SECTION II: LANGUAGE BASICS

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

Java Basic Programming Constructs

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

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

An overview of Java, Data types and variables

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Preview from Notesale.co.uk Page 9 of 108

Chapter 2. Elementary Programming

Informatics Ingeniería en Electrónica y Automática Industrial

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction to Programming Using Java (98-388)

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

Chapter 3: Operators, Expressions and Type Conversion

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

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

Operators in java Operator operands.

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

Index COPYRIGHTED MATERIAL

CT 229. Java Syntax 26/09/2006 CT229

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

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

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

DM550 Introduction to Programming part 2. Jan Baumbach.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Programming Lecture 3

Accelerating Information Technology Innovation

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

A flow chart is a graphical or symbolic representation of a process.

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

DM503 Programming B. Peter Schneider-Kamp.

Information Science 1

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

Language Fundamentals Summary

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

CMPT 125: Lecture 3 Data and Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

Java Primer 1: Types, Classes and Operators

JAVA Programming Fundamentals

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

CSC Java Programming, Fall Java Data Types and Control Constructs

The Java language has a wide variety of modifiers, including the following:

Building Java Programs. Introduction to Programming and Simple Java Programs

Lecture 02 C FUNDAMENTALS

Data Types and Variables in C language

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

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

Introduction to Programming (Java) 2/12

A variable is a name that represents a value. For

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

ECE 122 Engineering Problem Solving with Java

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

More Programming Constructs -- Introduction

Fundamentals of Programming

Advanced Computer Programming

.Net Technologies. Components of.net Framework

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

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Chapter 2: Data and Expressions

Lecture Set 4: More About Methods and More About Operators

Basics of Java Programming

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Building Java Programs

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

A Java program contains at least one class definition.

Expressions and Casting

PROGRAMMING FUNDAMENTALS

2 rd class Department of Programming. OOP with Java Programming

Fundamental of Programming (C)

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

Java: framework overview and in-the-small features

Transcription:

CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment: http://muele.mak.ac.ug office: block A, level 3, department of computer science alt. office: institute of open, distance, and elearning, room 2 The Java language Basics Kizito (Makerere University) CSC 1214 February 2018 1 / 18

Overview 1 Basic Constructs Types Variables Program Control Kizito (Makerere University) CSC 1214 February 2018 2 / 18

Types Basic Constructs Types Every variable has a type, every expression has a type, and every type is strictly defined Inbuilt types: char, int, float (32 bits), double (64 bits), boolean... User defined types: Defined using the keyword class Examples: char c = a ; short t; int i = 50, m days = new int[12]; Java is strictly typed (e.g., can t assign float to int as is possible in C) Kizito (Makerere University) CSC 1214 February 2018 3 / 18

Types Types Simple Type Groups Integers (byte, short, int, long) Floating point numbers (float, double) Characters (char) Boolean (boolean) These form the basis of all other types of data you can create Kizito (Makerere University) CSC 1214 February 2018 4 / 18

Types Types Type Conversion and Casting If two types are compatible, Java performs the conversion automatically (e.g., assign int to long) We need casting for incompatible types and narrowing conversions (target-type) expression Type promotion in expressions int d = a * b / c; (a, b, c are of type byte) double d = (int)i + (float)f; Kizito (Makerere University) CSC 1214 February 2018 5 / 18

Types String Conversion and tostring() String defines valueof() Object defines tostring() Integer defines parseint(str), tobinarystring(num), tooctalstring(num)... Example: String s = "This is a test string"; static String; System.out.println(s.toString()); System.out.println(s); System.out.print(String.valueOf(3.14)); Kizito (Makerere University) CSC 1214 February 2018 6 / 18

Variables Variables Variable is the basic unit of data storage Defined by the combination of an identifier, a type, and an optional initializer type identifier [ = value][, identifier[ = value]...]; Java is case sensitive Total, total, and TOTAL are different identifiers Examples: int a, b, c; byte x = 22, y; double d = Math.sqrt(a * a + b * b); final (const in C/C++) can t modify contents final int FILE OPEN = 1; final int FILE CLOSED = 0; Kizito (Makerere University) CSC 1214 February 2018 7 / 18

Variables Variables Instance Variables and Identifiers Sometimes we choose identifiers ourselves when writing a program (such as Welcome) Sometimes we are using another programmer s code, so we use the identifiers that he or she chose (such as println) Often we use special identifiers called reserved words that already have a predefined meaning in the language A reserved word cannot be used in any other way Variables defined within a class are called instance variables They are unique to their object Instance variables can be hidden (using this to unhide them) Instance variables declared as static are, essentially, global variables Using super to unhide instance variables Kizito (Makerere University) CSC 1214 February 2018 8 / 18

Variables Identifiers Java Reserved words abstract boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while Kizito (Makerere University) CSC 1214 February 2018 9 / 18

An operator takes one or more operands and produces a result An operator is just a function, but with a different syntax Most of Java s operators can be divided into: Arithmetic Bitwise Relational Logical Kizito (Makerere University) CSC 1214 February 2018 10 / 18

Arithmetic + addition (concatenation for strings) subtraction (also unary minus) multiplication / division % modulus ++ increment + = addition assignment = subtraction assignment = multiplication assignment / = division assignment % = modulus assignment decrement Kizito (Makerere University) CSC 1214 February 2018 11 / 18

Bitwise bitwise unary NOT & bitwise AND bitwise OR ˆ bitwise XOR (exclusive OR) >> shift right >>> shift right zero fill << shift left & = bitwise AND assignment = bitwise OR assignment ˆ= bitwise exclusive OR assignment >>= shift right assignment >>>= shift right zero fill assignment <<= shift left assignment Kizito (Makerere University) CSC 1214 February 2018 12 / 18

Relational == equal to! = not equal to > greater than < less than >= greater than or equal to <= less than or equal to Kizito (Makerere University) CSC 1214 February 2018 13 / 18

Boolean Logical & logical AND logical OR ˆ logical XOR short-circuit OR && short-circuit AND! logical unary NOT & = AND assignment = OR assignment ˆ= XOR assignment == equal to! = not equal to? : ternary if-then-else Kizito (Makerere University) CSC 1214 February 2018 14 / 18

Other = assignment () parenthesis [] square brackets. dot instanceof equals()?? Kizito (Makerere University) CSC 1214 February 2018 15 / 18

Operator Precedence () [] Highest ++! / % + >> >>> << > >= < <= ==! = & ˆ &&? : = op = Lowest Kizito (Makerere University) CSC 1214 February 2018 16 / 18

Program Control Java Program Control A program is made up of one or more classes A class contains one or more methods (functions in C) A method contains program statements. A Java application always contains a method called main (just like in C) Filename should match name of the main class Execution start in the main() method of the main class When execution of a method stops, it may return a value or not (if the return type is void) public static void main(string args[]) Kizito (Makerere University) CSC 1214 February 2018 17 / 18

Program Control First Java Program import java.util.*; public class HelloDate { public static void main(string[] args) { System.out.print("Hello, it is "); System.out.println(new Date()); } } Filename should match the name of the main class (i.e., HelloDate.java) Compiling (javac HelloDate.java) and running (java HelloDate) Integrated/Interactive Development Environments (IDEs) may provide graphical interface for compiling and running (or both at a click of a single button) Sample output: Hello, it is Mon Feb 5 10:49:03 EAT 2018 Kizito (Makerere University) CSC 1214 February 2018 18 / 18

Program Control First Java Program import java.util.*; public class HelloDate { public static void main(string[] args) { System.out.print("Hello, it is "); System.out.println(new Date()); } } Filename should match the name of the main class (i.e., HelloDate.java) Compiling (javac HelloDate.java) and running (java HelloDate) Integrated/Interactive Development Environments (IDEs) may provide graphical interface for compiling and running (or both at a click of a single button) Sample output: Hello, it is Mon Feb 5 10:49:03 EAT 2018 Kizito (Makerere University) CSC 1214 February 2018 18 / 18