Java Classes & Primitive Types

Similar documents
Java Classes & Primitive Types

Java Inheritance. Classes implement the concept of ADT:

Getting started with Java

Java Basic Datatypees

Fall 2017 CISC124 9/16/2017

Computational Expression

Introduction to Programming Using Java (98-388)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

An introduction to Java II

Array. Prepared By - Rifat Shahriyar

Chapter 2 Elementary Programming

Java Arrays & Vectors

Java Identifiers, Data Types & Variables

CHAPTER 7 OBJECTS AND CLASSES

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Java Notes. 10th ICSE. Saravanan Ganesh

Zheng-Liang Lu Java Programming 45 / 79

3. Java - Language Constructs I

CS260 Intro to Java & Android 03.Java Language Basics

CHAPTER 7 OBJECTS AND CLASSES

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

COMP6700/2140 Data and Types

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Programming. Atul Prakash

Full file at

Basics of Java Programming

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

VARIABLES AND TYPES CITS1001

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.

Datatypes, Variables, and Operations

Getting started with Java

JAVA Programming Fundamentals

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

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

CS121/IS223. Object Reference Variables. Dr Olly Gotel

PROGRAMMING FUNDAMENTALS

Lecture 2 Tao Wang 1

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

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

C# MOCK TEST C# MOCK TEST I

Assumptions. History

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

CSc Introduction to Computing

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

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

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

9 Working with the Java Class Library

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Software Practice 1 Basic Grammar

CLASSES AND OBJECTS. Fundamentals of Computer Science I

1B1b Classes in Java Part I

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

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CMPT 125: Lecture 3 Data and Expressions

Program Fundamentals

Data Types, Literals, Operators

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

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Programming Languages and Techniques (CIS120)

Visual C# Instructor s Manual Table of Contents

Binghamton University. CS-140 Fall Data Types in Java

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS149: Elements of Computer Science. Fundamental C++ objects

Chapter 2: Using Data

Understand Computer Storage and Data Types

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Programming. Syntax and Semantics

Programming Language Concepts: Lecture 2

Chapter 2: Using Data

Key Differences Between Python and Java

Tokens, Expressions and Control Structures

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

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

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

COE318 Lecture Notes Week 5 (Oct 3, 2011)

VARIABLES AND CONSTANTS

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Creating an object Instance variables

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

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

4 Programming Fundamentals. Introduction to Programming 1 1

The C++ Language. Arizona State University 1

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

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

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

Object-Oriented Programming Concepts

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

LECTURE 02 INTRODUCTION TO C++

Java Object Oriented Design. CSC207 Fall 2014

Lexical Structure (Chapter 3, JLS)

Chapter 02: Using Data

Java Programming Fundamentals. Visit for more.

CS2141 Software Development using C/C++ C++ Basics

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

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Transcription:

Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.) q Class types (e.g., String, Date, Vector, Ponto, etc.) q Automatic initialisation (primitive type variables are initialised to 0; class type variables are initialised to null) n Methods Ponto( ) getx( ) gety( ) setx( ) sety( ) drawponto( ) q Constructors (special methods with same name of class and no return value) q Behaviour of classes (receive list of pass-by-value parameters and return given type value) Rui Moreira 2 1

Example: Ponto class /** Ponto Class Declaration: represents a generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** Default Constructor: automatically provided by Java language */ //public Ponto() { /* A constructor is a public method with the same name as the class and with no return type; a constructor method is used to build/create objects of the class type, e.g., Ponto p = new Ponto(); If no other constructor is provided we do not need to implement the default constructor (that is why it is commented!!) */ Rui Moreira 3 Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** We may specify other specific constructors, for example, to explicitly initialize the attributes of our objects: Ponto p = new Ponto(2, 4); */ public Ponto(int a, int b) { x = a; y = b; /** If the previous constructor is provided Java no longer provides the default constructor; therefore, if we want to be able to use the default constructor we also need to implement it as follows... */ public Ponto() { x = 0; y = 0; Rui Moreira 4 2

Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Attributes: by default these attribute fields are public */ int x = 0; int y = 0; /** Specific constructors: explicitly initialize object attributes */ public Ponto(int a, int b) { x = a; y = b; /** Default Constructor: explicit implementation because other constructors exist */ public Ponto() { x = 0; y = 0; Rui Moreira 5 Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Encapsulation: hide implementation with private attributes; provide access to attributes through well defined public method interfaces */ private int x = 0; private int y = 0; /** Get attribute value */ public int getx() { return x; /** Set attribute value */ public void setx(int a) { x = a; // Do the same for the y attribute Rui Moreira 6 3

Example: Ponto class /** Ponto Class Declaration: represents generic point in space */ /** Encapsulated attributes behind public interface methods */ private int x = 0; private int y = 0; /** Constructors */ public Ponto(int a, int b) { x = a; y = b; public Ponto() { x = 0; y = 0; /** Get attribute values */ public int getx() { return x; public int gety() { return y; /** Set attribute values */ public void setx(int a) { x = a; public void sety(int b) { y = b; /** Other behaviour.../ public void drawponto() { /*... */ Rui Moreira 7 Identifiers & Name Conventions n Identifiers (for classes, interfaces, methods and attributes) q Are case-sensitive; q No size limit (number of characters); q Must start with: letter, underscore or $; q May contain digits/numbers after first character; n Classes & Interfaces q Usually first letter of each word is uppercase, e.g., OneClasse, OneInterface n Methods & Attributes q Usually first word is entirely smallcase then first letter of remainer words is uppercase, e.g., onemethod(), oneattribute Rui Moreira 8 4

Attributes: primitve types n Logical q boolean (true or false); n Textual q char (unsigned 16 bit number - unicode character); q String (final Class - sequence of unicode chars); n Integer q byte (8 bits); short (16 bits); q int (32 bits); long (64 bits); n Floating Point q float (32 bits floating-point) q double (64 bits floating-point) Rui Moreira 9 Attributes: primitve types particularities n Casts not allowed between booleans <-> intetegers; n String is a class but presents also behaviour of primitive types: q String s = Hello World! ; // e.g., assigned a literal sequence of chars n Integer literals are signed (e.g., byte b; // -128 < b < 127); n Integers are int by default except when followed by L (long); n Integers may be represented in the following formats: q int d = 10; // decimal format q int o = 077; // octal format q int h = 0xA; // hexadecimal format n Floating Point literals may use. or E: q float f1 = 3.14f; // float number followed by letter f or F q float f2 = 6.04E23; // float number representing 6.04x10 23 q double d = 23.6E+235D; // double number followed by d or D Rui Moreira 10 5

Some declarations n boolean truth = false; n char a = a, c = C ; n char r= \n, t= \t ; // Usage of escape char n char u = \u0000 ; // Represents null char n String str = new String( string example ); n byte b = 128; n short s = 512; n int i = 1024; n long l = 2L; n float f = 2.78F n double d = 8.16; Rui Moreira 11 Memory allocation n When declaring variable/attributes of primitve types the memory space for the variables is automatically allocated; n When declaring variable/attributes of class types (e.g., String str) the memory is allocated for the reference (variable) and not for the object! n Before we can use a reference variable we must set it to point to a given object! Rui Moreira 12 6

Creating Objects puclic class DateApp { public static void main(string args[]){ // Declare reference variable Date today; // Create object today = new Date(12, 2, 2005); class Date { int day=0, month, year; public Date(int d, int m, int y){ day=d; month=m; year=y; Calling constructor: Creating objects in 3 Steps: Allocate memory for the reference; Allocate memory for the object; Set reference pointing to object. 1 st allocates memory for the specific object; 2 nd initialises attributes by default (month=0, year=0) and sets explicit attribute values (day=0); 3 rd runs constructor code (inside constructor method) Rui Moreira 13 Creating Objects // Declare reference variable - today Date today; today variable day 12 // today references new Date object today = new Date(12, 2, 2005); month 2 year 2005 Date object Rui Moreira 14 7

Default Initialisation of Attributes n Java automatically initialises attributes (class member variables): q Primitive types: n boolean attribute variables false n char attribute variables \u0000 n integer attribute variables - 0 n floating point attribute variables 0.0f or 0.0d q Class types: n reference attribute variables null String s; // s is set to null, meaning it points to no object Rui Moreira 15 Local Variables n Default initialisation is done only for attribute variables (class member variables); n Local variables (and parameters) are stored in the STACK which means these are NOT automatically initialised, i.e., we may NOT use local variables without explicit initialisation; n The Java compiler generates a compile-time error if we try to use a non-initialised local variable. Rui Moreira 16 8

Exercises implement classes: n Implement Class Rectangulo n Implement Class Triangulo n Implement Class Circulo n Implement Methods: Rectangulo upleftponto : Ponto = null downrightponto : Ponto = null Rectangulo( ) getupleftponto( ) getdownrightponto( ) setupleftponto( ) setdownrightponto( ) isinside( ) area( ) tostring( ) Triangulo upponto : Ponto = null downponto : Ponto = null middleponto : Ponto = null getupponto( ) getdownponto( ) getmiddleponto( ) setupponto( ) setdownponto( ) setmiddleponto( ) isinside( ) area( ) tostring( ) q public void move(int dx, int dy) - changes coordinates of the given amount q public boolean isinside(ponto p) - checks if p is inside the geometric figure q public float area() - returns the area of the geometric figure q public String tostring() - returns a string describing the geometric figure, e.g., for the Rectangulo with points (1,2) and (3,7) it returns Rectangle@[(1,2);(3,7)] Circulo centerponto : Ponto = null diameter : int = 0 getcenterponto( ) getdiameter( ) setcenterponto( ) setdiameter( ) isinside( ) area( ) tostring( ) Rui Moreira 17 Instantiation example // Declaring and creating 2 points Ponto p1 = new Ponto(1, 2); Ponto p2 = new Ponto(4, 6); Rectangulo upleftponto : Ponto = null downrightponto : Ponto = null Rectangulo( ) getupleftponto( ) getdownrightponto( ) setupleftponto( ) setdownrightponto( ) isinside( ) area( ) tostring( ) // Declaring and creating 2 rectangles Rectangulo r1 = new Rectangle(p1, p2); Rectangulo r2 = new Rectangle(new Ponto(5, 8), new Ponto(10, 12)); reference p1 reference p2 reference r1 object Ponto x 1 y 2 object Ponto x 4 y 6 object Rectangulo upleftponto downleftponto object Ponto x 5 y 8 object Ponto x 10 y 12 object Rectangulo upleftponto downleftponto reference r2 Rui Moreira 18 9