Fundamentals of Programming Data Types & Methods

Similar documents
Fundamentals of Programming. Budditha Hettige Department of Computer Science

Fundamentals of Programming

H212 Introduction to Software Systems Honors

Object Oriented Programming. Java-Lecture 1

Programming with Java

Computational Expression

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Data Types & Variables

IT 1033: Fundamentals of Programming Data types & variables

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

AP Computer Science Unit 1. Programs

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Computational Expression

Reading Input from Text File

Input. Scanner keyboard = new Scanner(System.in); String name;

Chapter 2 ELEMENTARY PROGRAMMING

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ

Supplementary Test 1

Arrays IT 1033: Fundamentals of Programming

Object Oriented Programming. Java-Lecture 6 - Arrays

Array. Prepared By - Rifat Shahriyar

Chapter 2. Elementary Programming

CSCI 1103: File I/O, Scanner, PrintWriter

ing execution. That way, new results can be computed each time the Class The Scanner

Introduction to Java Unit 1. Using BlueJ to Write Programs

Arrays. Eng. Mohammed Abdualal

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

A+ Computer Science -

CEN 414 Java Programming

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

MIDTERM REVIEW. midterminformation.htm

Introduction to Java (All the Basic Stuff)

A+ Computer Science -

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Example: Monte Carlo Simulation 1

CMSC131. Introduction to your Introduction to Java. Why Java?

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

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

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

Introduction to Programming Using Java (98-388)

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

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

Chapter 2: Data and Expressions

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Data Conversion & Scanner Class

COMP 202 Java in one week

Chapter 2: Data and Expressions

Lecture Set 2: Starting Java

Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Lecture Set 2: Starting Java

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

COMP 202 Java in one week

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

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Array. Array Declaration:

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

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

4. If the following Java statements are executed, what will be displayed?

CSCI 1103: File I/O, Scanner, PrintWriter

COMP 202. Java in one week

Text User Interfaces. Keyboard IO plus

CS 112 Introduction to Programming

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

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

Control Statements: Part 1

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Welcome to the Primitives and Expressions Lab!

CS 112 Introduction to Programming

CS 112 Introduction to Programming

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.

CS 211: Existing Classes in the Java Library

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Introduction to Computer Programming

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

PROGRAMMING FUNDAMENTALS

Midterm Examination (MTA)

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

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

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Full file at

! Theoretical examples: ! Examples from Java: ! data type: A category of data values. " Example: integer, real number, string

Peer Instruction 1. Elementary Programming

Using Java Classes Fall 2018 Margaret Reid-Miller

Chapter. Let's explore some other fundamental programming concepts

Oct Decision Structures cont d

CS 200 Using Objects. Jim Williams, PhD

Elementary Programming

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

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Chapter 2 Elementary Programming

Transcription:

Fundamentals of Programming Data Types & Methods By Budditha Hettige

Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators 2

Exercise Create a JAVA Program to display the following output EMPLOYEE DETAILS --------------------------------------- 1. Employee NO : HIT1001 2. Name : Mr. A. B. Gamage 2. Age : 30 3. Salary : 23507.50 4. Married : Y 5. Car (Y/N) : N --------------------------------------- 3

Java Data types The eight primitive data types supported by the Java programming language 4

Limits of integer in java 5

Default values the default values for the above data type Data Type byte 0 short 0 int 0 long Default Value (for fields) 0L float 0.0f double 0.0d char String (or any object) boolean '\u0000' null false 6

Variables Declaration DataType <space> VaribaleName Example: Int count; Int studentage = 0; float GPA = 3.56 7

Input from Keyboard Use Scanner import java.util.scanner; Create a Scanner object to Scan input from command window Scanner input = new Scanner(System.in); Read value from keyboard nextint() interger nextline () String nextfloat() float nextdouble ()double Example value = input.nextint(); 8

Example Create a JAVA Program that read data from key bard and display the result as follows EMPLOYEE DETAILS --------------------------------------- 1. Employee NO : HIT1001 2. Name : Mr. A. B. Gamage 2. Age : 30 3. Salary : 23507.50 4. Married : Y 5. Car (Y/N) : N --------------------------------------- 9

Examples: Example 1 Create a java program to read 2 integer values and print the average. Example 2 Create a java program to calculate are of a room. 10

Example 1: Solution import java.util.scanner; public class FundamantalApp { public static void main(string[] args) { Scanner input = new Scanner(System.in); int value1, value2; float avg; System.out.println("Enter value1 :"); value1 = input.nextint(); System.out.println("Enter value1 :"); value2 = input.nextint(); avg = (value1 + value2)/2; System.out.println("Average is "+ avg); } } 11

Example 3: Create a java program to calculate are of the following figure c a b 12

Methods Is a group of statements that is executed when it is called from some point of the program. Types Built-in: Build-in methods are part of the compiler package, such as System.out.println( ) User-defined: User-defined methods are created by you Format Return type MethodName (Argument..) { } 13

Method Example 1 PrintMe Main printme 14

Method Example 2 PrintMe Main printme 15

Method Example 3 16

Scope of a variable 1. In a class body as class fields. Variables declared here are referred to as class-level variables. 2. As parameters of a method 3. In a method's body 4. Within a statement block, such as inside a while or for block. 17

Scope of a variable 1 2 3 4 18

Exercise Create a Java Programs to do to the following 1. Read Celsius temperature and calculate the Fahrenheit temperature Hint: Celsius to Fahrenheit: ( C 9 / 5 ) + 32 = F A Method to read data from keyboard A Method to Calculate data A Method to Print Data 19

Methods 20

Methods 21

Complete Program Main getvalue getvalue Claculate calculate printdata printdata 22

Top-Down Design Main getvalue claculate printdata 23

Example Create a JAVA Program that reads 3 integer values from keyboard and print total and average values. Hint: Draw a top down design Implement your solution 24