Crash course in the Java Programming Language

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Java Syntax Part I Comments Identifiers Primitive Data Types Assignment.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Some basic I/O.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
String Escape Sequences
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Introduction to Programming
Copyright Curt Hill Variables What are they? Why do we need them?
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
By Mr. Muhammad Pervez Akhtar
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
Fundamentals 2.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Lecture Notes – Basics (Ch1-6)
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
IDENTIFIERS CSC 111.
Computers & Programming Languages
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Chapter 2: Java Fundamentals
elementary programming
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Crash course in the Java Programming Language

Hello World in Java import java.util.*; /** A class for producing simple greetings. */ public class Greeter { private String name; Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. public Greeter(String aName) name = aName; } Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. public String sayHello() return "Hello, " + name + "!";

Hello World Tester import java.io.*; import java.util.*; public class GreeterTester { public static void main(String[] args) Greeter worldGreeter = new Greeter("World"); String greeting = worldGreeter.sayHello(); System.out.println(greeting); }

Identifiers (names) Java is case sensitive Identifiers can begin with an underscore or letter and contain only letters, underscores, or numbers We choose identifiers for variables, classes, methods, and packages

Identifiers variables must start with a letter or underscore consists of letters, digits & underscores cannot be a reserved word cannot be true, false, or null firstScore score1 Score 3scores score-1 myScore Capitalize “middle” words Usually start variables with lowercase, classes with uppercase – class name and file name must be identical. Java is case sensitive!!!! Score score

Primitive Data Types Integer data types: int byte short long Floating-point types: double float Character type: char Logical Type: boolean

Primitive Types

Declaring Variables Syntax: Examples: int num1; double num2, num3; type identifier1, identifier2, … ; Examples: int num1; double num2, num3; double num4; We must declare a variable before it can be used!

Assignment Statement Syntax: variable = expression; expression is evaluated and then the value of expression is stored in variable. We should initialize our variables. int num1 = 0; double num2 = 0.0, num3 = 0.0; Incorrect: int 0 = num1; In Java, 0 is treated as an integer while 0.0 is treated as a floating-point number. We may assign a value to a variable more than once. However, a variable can only retain its most recently assigned value.

Operators and Precedence ( ) / * % ++ -- prefix ++ -- postfix + - < > <= >= == != && || = += -= /= *=

Literals is a constant value that appears directly in a program. Character Data Type: represent a single character //initialize a character data type named “firstLetter” with value ‘A’ char firstLetter = 'A'; A character literal is enclosed in single quotation marks. Which of the following is a character literal? ‘a’ ‘A’ ‘ ‘ ‘$’ ‘7’ ‘ab’ ‘ a ‘ “a” A string literal must be enclosed in double quotation marks. The String type String a = “Chapter”; String b = “Chapter” + 2; (note: please ignore the smart quotes )

Standard I/O import java.util.*; ... System.out.print( ); System.out.println() // must declare a scanner object for input Scanner keyboard=new (System.in); someInt = keyboard.nextInt(); somestr = keyboard.next(); someReal= keyboard.nextDouble();

Formatting Output import java.text.*; DecimalFormat moneyStyle = new DecimalFormat("$0.00"); double amount = 4.56; System.out.println(moneyStyle.format(amount)); ====================================================== DecimalFormat fiveDecimals = new DecimalFormat("0.00000"); const double PI = 3.14159; System.out.println(fiveDecimals.format(PI));

If Statement Syntax: if (BooleanExpression) Single Java Statement; where Statement is any Java statement including a block {}. Semantics: If BooleanExpression is true, then execute the Single Java Statement. (Otherwise, skip the Statement.)

Lab Time: Files and Exceptions Exercise: Work along with me. On your USB drive: Create a NetBeans Projects directory Continues in Next Presentation