Programming with Java

Similar documents
Programming with Java

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Basic Programming Language Syntax

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

printf( Please enter another number: ); scanf( %d, &num2);

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

DEPARTMENT OF MATHS, MJ COLLEGE

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Datatypes, Variables, and Operations

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Course Outline. Introduction to java

Basics of Java Programming

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 2

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Getting started with Java

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

Software and Programming 1

Introduction to Java & Fundamental Data Types

COMP 202 Java in one week

Chapter 2: Programming Concepts

Computational Expression

Lab # 2. For today s lab:

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

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

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Values and Variables 1 / 30

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

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

An overview of Java, Data types and variables

Elementary Programming

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

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

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

Introduction to Java Programming

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

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

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

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

Full file at

Welcome to CSE 142! Whitaker Brand. University of Washington, Winter 2018

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS 302: Introduction to Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

AP Computer Science Unit 1. Writing Programs Using BlueJ

CEN 414 Java Programming

Introduction to JAVA

First Java Program - Output to the Screen

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

Introduction to Java Applications

COMP 202 Java in one week

CS 251 Intermediate Programming Java Basics

SOFT 161. Class Meeting 1.6

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

JAVA PROGRAMMERS GUIDE LESSON

AP CS Unit 3: Control Structures Notes

Software and Programming 1

Use of scanf. scanf("%d", &number);

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

CSc Introduction to Computing

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

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

Notes from the Boards Set BN19 Page

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

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

Python Unit

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

Java Bytecode (binary file)

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

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

Lecture 3 Tao Wang 1

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

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

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

Programming with Java

Transcription:

Programming with Java Variables and Output Statement Lecture 03 First stage Software Engineering Dep. Saman M. Omer 2017-2018

Objectives ü Declare and assign values to variable ü How to use eclipse ü What is the steps for writing a program ü The rules of programming language ü How to start writing a program in java ü How data will be printed ü Join messages in output commands by using the concatenation (+) operator

Outlines Ø What is a Variable? Ø Declaring a Variable Ø Assignment statement Ø Eclipse Ø Programming Languages and Programming in Java Ø A Java program Ø Output statement Ø Examples

The computer's memory q Think of the computer like a calculator for a moment. o We have already seen how to calculate values. q A flexible calculator has "memory" keys to store and retrieve a computed value. o In what situation(s) is this useful? q How can we save and restore a value that our Java program previously calculated?

What is a Variable? q Variable: A piece of your computer's memory that is given a name and type, and can store a value. q A variable is a placeholder used for storing data. q Data may be numeric, or string of characters. q The data inside variable called Value, it can be change or delete. q Named location that stores a value of one particular type

Declaring a Variable q Syntax: datatype variablename; q Example: ² int score ; ² int book ; q Names of variables should be meaningful and reflect the data they will store o This makes the logic of the program clear q score and book, are two variables of type integer, they may hold values like 1, 50, 200. q note that we use int type, because score or book, both are measured in integer numbers, (i.e, there is no 3.6 books!!!!!)

Declaring a Variable v For values that contain fraction parts like 3.5, or 7.21, we use type double: ² double weight; ² double w; ² double x; v Note: all java statements must be ended with a semicolon. (;) v Think of variables as an empty box that you can put values in. v Example: int book ; v This statement will reserve a location in computers memory. Name: book Type : int

Java Variable Naming rules 1 2 3 4 5 6 Java variable names are case sensitive. The variable name money is not the same as Money or MONEY. Characters allowed to be used in names are: 1 All English Capital Letters (Upper Case Letters ) 2 All English Small Letters (Lower Case Letters) 3 Underscore ( _ ) 4 Currency symbols ( $,, ) 5 Digits( 0, 1, 2,,8,9). Variable name must begin with either a letter or the dollar sign "$", or the underscore character "_". Digits are not allowed to be used at the beginning of the name, Spaces are also not allowed, Java keywords are not allowed to be used as variable name.

Assignment statement q Assignments allow values to be put into variables. q Simple assignment: q datatype variablename = value ; q Examples: 1. int score = 7 ; Name: score Type : int Value : 7 2. int x,y ; x = 3 ; y = 5 ; Name: y Type : int Value : 5 Name: x Type : int Value : 3

Assignment statement The variable must be declared before use.

Eclipse How to create a project and classes for writing a program In

² To create a project: File à New à Java Project ² Enter a name for the project, then click Finish

² To create a class, click on the File à New à Class ² Enter a name then finish

Programming Languages q Programming languages have a set of rules : o Syntax: refers to the structure (or form) of code that a programming language specifies. If a program contains the syntax error that will not be compiled. ² Things that can cause a compiler error are incorrect keywords, missing brackets, semicolons and many others. o Semantics: refer to the meaning of the program, symbols, characters and words are arranged with that structure. ² The most common semantic error is one in which the code uses a variable that isn t initialized properly.

Programming in Java q To create a working program in Java, you need to three steps. : Ø Create a program by typing it into a file named, say, MyCode.java. Ø Compile the program which is translate a source language into a machine language (0 and 1). Ø Run (or execute) the program.

A Java program public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class

Hello world program Here is the Hello world program in Java. public class Hello { public static void main (String args[]) { System.out.println( Hello world! ); } // end of main } // end of class Output Hello word

Analyses the program q That piece of program is a part of a complete program. q public class CLASS-NAME o o Java is referred to as an Object-Oriented Programming(OOP) language. The OOP languages require the program to be written in separate unit called classes. q All statements, and braces are needed to be written inside a class (any name) and a method called main. q public static void main (String args[]) o The main method is the method that's defined to be called at the start of an application. Without it, there is no place to start running. q What is method, what is class, both of them will be explained in next lectures.

Output statement q This is the way, always stuffs will printed on a sample text screen. ² System.out.println(variable-name); prints the value of variable <variable-name> to the user ² System.out.println( any message ); prints the message within quotes to the user ² System.out.println( hello + world + a + plus + b); assuming the value of a is 3 and b is 7, it prints helloworld3plus7 q println( ) is used for breaking current line, and starting a new one. q method print ( ) is used for printing any string or variable, directly after last print. q \n read as Slash n, can be used with method print( ), and work exactly as prinln ( ) method.

Example 1 public class Hello { public static void main (String args[]) { System.out.println( This is my first Java program! ); System.out.println( I like Java. ); System.out.println( I think Java is cool. ); } // end of main } // end of class Output This is my first Java program! I like Java. I think Java is cool. Ø Exercise: change the println to print. Lets see what is the difference?

Example 2 public class MyFirstJavaProgram { public static void main (String args[]) { int myage, myfriendage; myage = 20; myfriendage = myage + 1; System.out.println( Hello, \n I am + myage + years old,\n and \n my friend is + myfriendage + years old ); System.out.println( Goodbye ); } // end of main } // end of class Output Hello, I am 20 years old, and my friend is 21 years old Goodbye

Example 3 q What will be output from the following program?

The next lecture will explain an Input statement, Identifiers, Naming rules and Data Types