Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.

Slides:



Advertisements
Similar presentations
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Java Coding 3 David Davenport Computer Eng. Dept.,
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 2 Elementary Programming
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Applications
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
IDENTIFIERS CSC 111.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Chapter 2: Java Fundamentals
elementary programming
Chapter 2: Java Fundamentals
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Java Coding 6 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

From problem to program… The story so far... Java Source Code Java bytecode Machine code Problem Algorithm Data/Memory requirements

Need Java Syntax for… Algorithm (in pseudo-code) Sequence, Decision & Repetition, of Data flow operations Input, Output & Assignment Data/Memory requirements Meaningfully named memory locations Restriction on data (data types) Variables or Constants & initial value Plus comments & methods!

Comments & White space Comments Syntax: // any text on remainder of current line /* any text across multiple lines */ Examples: // Author: David. // Date: Oct /* This program blah, blah, blah */ Java ignores line endings, blanks lines & white space! Layout program code for ease of reading!

Identifiers User-defined names Used for variables, constants, methods, etc. Any sequence of letters, digits and the underscore character only. First character may not be a digit! Upper and lower case are considered different (i.e. case sensitive!) Cannot use Java reserved words i.e. words such as while, for, class, if, etc. CS101 rule: Names must be meaningful!

Data Types Primitive int, long, … (numeric integer) float, double, … (numeric real) char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char) boolean - true / false Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values anything & everything else! (we will come to these shortly)

Primitive Numeric Types Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value ,768 -2,147,483, x  3.4 x 10  38 7 significant digits  1.7 x 10  significant digits Max Value ,767 2,147,483,647 9 x 10 18

Data Types Primitive int, long, … (numeric integer) float, double, … (numeric real) char - any character, e.g. A, a, B, b, 3, ?, &, … (Java uses ISO Unicode standard, 16 bit/char) boolean - true / false Non-primitive String - any sequence of zero or more characters enum – an ordered set of user-defined values anything & everything else! (we will come to these shortly)

Declaring Variables Syntax: Type Any Java type Name (identifier) Convention: first letter of embedded words capital, except first! Examples: int age; double area; long initialSpeed; char letterGrade; char lettergrade; boolean exists; CAUTION Java is case sensitive! type name;

Declaring Constants Syntax: Type Any Java type Name (identifier) Convention: all capital letters! Value (literal, variable, constant, expression) Examples: final int SPEEDOFLIGHT = 300; final double TAXRATE = 27.5; final float PI = 3.142; final String COMPANY = “Bilkent”; Literal values String use “…” char use ‘.’ final type name = value;

Output (1) Syntax: where output is Literal value eg. “The area is “, ‘?’, 12.5, … Named variable or constant eg. area, userName, TAXRATE, … Expression eg. 2 * PI * radius, “The area is “ + area System.out.println( output ); Value is output exactly as is! Value in named memory location is output Resulting value of expression is output Note use of + for string concatenation

Output (2) Use To output the value & leave text cursor on current line. System.out.print( output ); System.out.println( “Welcome to CS101”); System.out.println( “The tax rate is “ + TAXRATE + ‘%’); System.out.println( “Welcome to CS101”); System.out.print( “The tax rate is “); System.out.print( TAXRATE); System.out.println( ‘%’); System.out.println(); Output blank line!

Input Syntax: Examples stringVariable = scan.next(); intVariable = scan.nextInt(); doubleVariable = scan.nextDouble(); userName = scan.next(); age = scan.nextInt(); salary = scan.nextDouble(); str = scan.nextLine(); Standard from Java5.0 on Invalid input may give run-time error! Must include: import java.util.Scanner; Scanner scan = new Scanner( System.in); Variables must be declared before use

Assignment Syntax: where expression is operand operator operand & Operand is Literal value Named Variable or constant Result of method call Expression! Operator is +, -, *, /, % (modulus, remainder after integer division) resultVariable = expression; Result of expression must be of suitable type to put into resultVariable!

Assignment Examples What is the value of z? Evaluation rules Bracketed sub-expressions first Operator precedence ( * & / then + & - ) Left to right sum = firstNumber + secondNumber; net = gross * ( 1 – TAXRATE/100); count = count + 1; c = Math.sqrt( a * a + b * b ); Z = / 3 – 1;

Outline Java Program The CS101 console template… import java.util.Scanner; /** 1.00, date */ public class ClassName { public static void main( String[] args) { // constants // variables // program code } ClassName.java In Java program = class Classname Convention: first letters capitalised Filename & classname MUST be the same.

A Complete Example (1) Problem – find area & circumference… Algorithm Data requirements 1. Print welcome message 2. Ask for & get radius from user 3. Compute area as pi.radius.radius 4. Compute circumference as 2.pi.radius 5. Report area, circumference & radius L radius- int L area, circumference - double PI – double, constant = 3.142

A Complete Example (2) import java.util.Scanner; /** 1.00, 2005/10/07 */ public class AreaCircum { public static void main( String[] args) { // constants // variables // 1. Print welcome message // 2. Ask for & get radius from user // 3. Compute area as pi.radius.radius // 4. Compute circumference as 2.pi.radius // 5. Report area, circumference & radius } AreaCircum.java

A Complete Example (3) import java.util.Scanner; /** * AreaCircum - computes area & circum of circle given radius * David 1.00, 2005/10/07 */ public class AreaCircum { public static void main( String[] args) { // constants final double PI = 3.142; // variables intradius; doublearea; doublecircumference; Header has been edited to include program description & author name AreaCircum.java

A Complete Example (3) Scanner scan = new Scanner( System.in); // 1. Print welcome message System.out.println( "Welcome to area circumference finder."); // 2. Ask for & get radius from user System.out.print( "Please enter the radius: "); radius = scan.nextInt(); // 3. Compute area as pi.radius.radius area = PI * radius * radius; // 4. Compute circumference as 2.pi.radius circumference = 2 * PI * radius; // 5. Report area, circumference & radius System.out.print( "The area of a circle of radius "); System.out.print( radius); System.out.print( " is "); System.out.println( area); System.out.print( "and its circumference is "); System.out.print( circumference); System.out.println(); } } // end of class AreaCircum Added line to get Keyboard input. Steps 2 & 5 expanded as per original algorithm.