Content Analysis

profileAnEsh
OODP101_Week2.pptx

Object Oriented Design and Programming

Week 2

Kent Institute Australia Pty. Ltd.

ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051

Version 2 – 18th December 2015

1

SLIDE TITLE

Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning

2

2

Chapter 2-Variables and their manipulations

Simple input and output

3

Programming Logic and Design, Ninth Edition

Objectives

In this chapter, you will learn about:

Declaring and using variables

Data types

Performing arithmetic operations

Declaring and using constants

Object Oriented Programming JAVA basics

Simple input and output

4

Programming Logic and Design, Ninth Edition

4

Working with Variables

Variable are named memory locations

Contents can vary or differ over time

Declaration is a statement that provides a variable's:

Data type- specify the type of data it will holds

Identifier - name of variable in memory

Optionally, an initial value

5

Programming Logic and Design, Ninth Edition

5

Declaring and Using Variables

What are data types?

Data type describes:

What values can be held by the item

How the item is stored in memory

What operations can be performed on the item

All programming languages support these data types:

Numeric consists of numbers that can be used in math

String is anything not used in math

6

Programming Logic and Design, Ninth Edition

6

Understanding a Declaration’s Data Type

Numeric variable

Holds digits

Can perform mathematical operations on it

String variable

Can hold text

Letters of the alphabet

Special characters such as punctuation marks

Type-safety

Prevents assigning values of an incorrect data type

7

Programming Logic and Design, Ninth Edition

7

Data types in Java

Java has 8 primitive data types

-Int for integer numbers (26,645445)

-float for floating point numbers (26.5f,6.2f)

-double for floating point numbers (23.6547132654)

-char for characters (“a”,”b”)

-Boolean (True or False)

-long for long integers

-Short for short integers

-Byte

8

Programming Logic and Design, Ninth Edition

Data Types in Java

https://www.youtube.com/watch?v=Rilk5TayNbI

Watch this video for understanding data types in better way

9

Understanding a Declaration’s Identifier

An identifier is a variable’s name

Programmer chooses reasonable and descriptive names for variables

Programming languages have rules for creating identifiers

Most languages allow letters and digits

Some languages allow hyphens

Reserved keywords are not allowed

10

Programming Logic and Design, Ninth Edition

10

Understanding a Declaration’s Identifier (continued -1)

Variable names are case sensitive

Variable names:

Must be one word

Must start with a letter

Should have some appropriate meaning

11

Programming Logic and Design, Ninth Edition

11

Variable Naming Conventions

Camel casing

Variable names have a “hump” in the middle such as hourlyWage (Java convention)

Pascal casing

Variable names have the first letter in each word in uppercase such as HourlyWage

Hungarian notation

A form of camel casing in which the data type is part of the name such as numHourlyWage

12

Programming Logic and Design, Ninth Edition

12

Variable Naming Conventions (continued)

Snake casing

Parts of variable names are separated by underscores such as hourly_wage

Mixed case with underscores

Similar to snake casing, but new words start with a uppercase letter such as Hourly_Wage

Kebob case

Parts of variable names are separated by dashes such as hourly-wage

13

Programming Logic and Design, Ninth Edition

13

Working with Variables (continued)

14

Programming Logic and Design, Ninth Edition

14

Practice task

Can you convert declarations in previous slide using Java programming language?

Use proper data type and naming convention

15

Programming Logic and Design, Ninth Edition

Assigning Values to Variables

Assignment statement

set myAnswer = myNumber * 2

Assignment operator

Equal sign

A binary operator, meaning it requires two operands—one on each side

Always operates from right to left, which means that it has right-associativity or right-to-left associativity

The result to the left of an assignment operator is called an lvalue

16

Programming Logic and Design, Ninth Edition

16

Initializing a Variable

Initializing the variable - declare a starting value

num yourSalary = 14.55

Garbage – a variable’s unknown value

Variables must be declared before they are used in the program

17

Programming Logic and Design, Ninth Edition

17

Performing Arithmetic Operations

Standard arithmetic operators:

+ (plus sign)—addition

− (minus sign)—subtraction

* (asterisk)—multiplication

/ (slash)—division

18

Programming Logic and Design, Ninth Edition

18

Performing Arithmetic Operations (continued -1)

Rules of precedence

Also called the order of operations

Dictate the order in which operations in the same statement are carried out

Expressions within parentheses are evaluated first

All the arithmetic operators have left-to-right associativity

Multiplication and division are evaluated next

From left to right

Addition and subtraction are evaluated next

From left to right

19

Programming Logic and Design, Ninth Edition

19

Performing Arithmetic Operations (continued -2)

20

Programming Logic and Design, Ninth Edition

20

Constants

There are two types of constants

Numeric constant (or literal numeric constant)

Contains numbers only

Number does not change

String constant (or literal string constant)

Also known as Alphanumeric values

Can contain both alphabetic characters and numbers

Strings are enclosed in quotation marks

21

Programming Logic and Design, Ninth Edition

21

Constants in Java

Word “final” will make any variable constant in Java

For Example:

-final int weight=54;

No change of value in program at any point

Can use same value many times

22

Programming Logic and Design, Ninth Edition

Let’s start learning about input and output in JAVA

23

Programming Logic and Design, Ninth Edition

Simple input and output

Before we start learning input and output using Java programming language

Little bit about Object Oriented Programming and its basic requirement

Further detailed concepts will be discussed later in this unit

24

Programming Logic and Design, Ninth Edition

Introduction to Object Oriented Programming JAVA

Java is Object Oriented Programming Language

It uses object and classes

Objects

-Can be tangible or intangible

-Have unique identity, properties and behavior

For example- A Student object

-Properties or Attributes:- Name , Date of Birth

-Behavior:- set name, find age

25

Programming Logic and Design, Ninth Edition

Introduction to Object Oriented Programming JAVA

Objects of same type are defined using common class.

Objects are instance of Classes.

Each one of you is instance of class Student .

So, classes are the recipe of making objects.

We can create Student 1, Student 2 and so on….

26

Programming Logic and Design, Ninth Edition

Introduction to Object Oriented Programming JAVA

You can build as many classes as you want in program

For example:-class for student, staff, movie etc.

Think about these classes, properties they can have and operations they can have.

To make some tasks easy, Java has pre-defined classes that we can use in our program

27

Programming Logic and Design, Ninth Edition

Introduction to Object Oriented Programming JAVA

Have a look on following link and browse Scanner Class

https://docs.oracle.com/javase/9/docs/api/overview-summary.html

Scanner Class- predefined class that we can use for input

More about classes and creation of their objects will be discussed later in unit.

For now, learn simple scanner class and its usage

28

Programming Logic and Design, Ninth Edition

Simple Input

29

Programming Logic and Design, Ninth Edition

Analyses of same example:

Input is value that program expect from its user

It can be entered via keyboard, mouse etc.

myNumber is the input in this case

Simple Input

For simple input, just follow steps:

- Import java.util.Scanner in your program

Create Scanner Class Object

Scanner sc= new Scanner (System.in);

30

Programming Logic and Design, Ninth Edition

Class Name

Name of object

Use this word to create new object

Class Name

Take input from system

Simple Input

For taking mynumber as input from user:-

int mynumber=sc.nextInt();

31

Programming Logic and Design, Ninth Edition

Data type of variable

Name of variable

Assignment operator

Object name that you created in previous line

Method of scanner class to take integer as input

Simple input

Some commonly used methods of Scanner Class:

32

Programming Logic and Design, Ninth Edition

32

Simple Output

33

Programming Logic and Design, Ninth Edition

Analyses of same example:

Output is value that program return to user

It is displayed on screen that is console

myanswer is the output in this case

Simple output

For simple output, just follow steps:

System.out.println(myanswer);

or

System.out.print(myanswer);

First line will display your answer and will go to next line on console

Second line will display your answer and will be on same line

Try to practice both on your systems

34

Programming Logic and Design, Ninth Edition

Simple output

Have a look on these following lines of code and guess the output:

System.out.println(“Java is interesting”);

int myNumber=98;

System.out.print(“myNumber”);

System.out.println(myNumber);

System.out.println(“myNumber is”+myNumber):

System.out.println(“Java is interesting and “+“myNumber is”+myNumber):

35

Programming Logic and Design, Ninth Edition

Note to lecturers:

Explain the concept of concatenation of strings while explaining these outputs

35

Simple Output

String myName=“Johnie”;

System.out.println(myName);

System.out.println(“My Name is”+myName+”\n”);

System.out.println(“My Name is”+”\t”+”MyName”);

36

Programming Logic and Design, Ninth Edition

36

Java Program

37

Programming Logic and Design, Ninth Edition

37

Summary

Variables are named memory locations.

Declaration include data type, identifier and optional initial value.

Object Oriented programming works with objects

Object can anything tangible or intangible

Classes are recipe of creating objects.

Java has predefined classes to use.

Scanner is one of the class for input.

System is the class to display output.

38

Programming Logic and Design, Ninth Edition

38

kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051

39

39