Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CMT Programming Software Applications
Review… Yong Choi BPA CSUB. Access Modifier public class HelloWorldApp –More detailes of Java key (reserved) wordJava key (reserved) word –The keyword,
Outline Java program structure Basic program elements
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2: Introduction to C++.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright 2008 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading: self-check: #1-14.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Language and SW Dev’t
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
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: Java Fundamentals
Week 1 Algorithmization and Programming Languages.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Java.
Java Fundamentals Part Topics –Simple java program –The print and println Methods –Java API –Variables and Literals –Primitive Data Types –Arithmetic.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
Chapter 1.2 Introduction to C++ Programming
Lecture 2a- Java Fundamentals
Working with Java.
Chapter 1.2 Introduction to C++ Programming
Lecture 2: Data Types, Variables, Operators, and Expressions
CSE 190D, Winter 2013 Building Java Programs Chapter 1
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
University of Central Florida COP 3330 Object Oriented Programming
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
null, true, and false are also reserved.
2.1 Parts of a C++ Program.
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 1: Computer Systems
elementary programming
Module 2 - Part 1 Variables, Assignment, and Data Types
CSE 142, Spring 2012 Building Java Programs Chapter 1
Zorah Fung University of Washington, Spring 2015
Chap 2. Identifiers, Keywords, and Types
Agenda Types and identifiers Practice Assignment Keywords in Java
Zorah Fung University of Washington, Winter 2016
Presentation transcript:

Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

Our First Java Program Java programs are written in a text editor or IDE and saved with a “.java ” filename extension // This is Java public class Simple { public static void main( String[] args ) { System.out.println( "Hello!" ); } Save this file as Simple.java (names must match) This is a comment Curly brackets (a.k.a. braces) must match up This line of code prints a message to the console window

Comments Add comments to Java programs to describe what your program does This helps others (and you!) understand your code Information to include: Your name(s) Date Description Version etc.

Comments Types of comments in Java include: Line-comments Block comments “Javadoc” comments // This is a line-comment /* This is a block-comment */ /** * Describes a section of Java code and * automatically generates HTML documentation */

Compiling a Java Program public class Simple {... Java program Java Compiler Java byte code (“.class” file)

Executing a Java Program Java byte code (“.class” file) Precompiled libraries (i.e. byte code) Java Virtual Machine

Keywords in Java Java contains keywords that are understood by the Java compiler e.g. public, static, class, void, etc. Keywords are always lowercase Keywords cannot be used for any other purpose (e.g. for class names, variable names, etc.)

Keywords in Java abstract assert boolean break byte case catch char class const continue default do double else enum extends false for final finally float goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while

What’s in a Name? Names are important in a Java program A class name uniquely identifies a Java program Class names must start with a letter and contain only letters and numbers Names could also contain underscore ‘ _ ’ or dollar sign ‘ $ ’ characters, but these should be avoided public class WelcomeToJava {... }

Class and Method Headers Java programs usually contain a class header and a method header The class header identifies the name of the class (i.e. program) and where it begins: The method header identifies the name of the method (e.g. main ) and where the method begins: public class Simple { public static void main( String[] args ) {

Java Statements A Java statement is a line of valid Java code Most Java statements must end in a semicolon ‘ ; ’ Class and method headers must not end with a semicolon System.out.println( "Hello!" ); System.out.print( "Welcome to" ); System.out.println( " Java." );

Java Output Use print and println to display output to the console window Use println to go to the next line Sample output: System.out.println( "Hello!" ); System.out.print( "Welcome to" ); System.out.println( " Java." ); Hello! Welcome to Java.

Java Output Also use special Java escape sequences to go to the next line, tab forward, etc. \n newline Advances the cursor to the next line for subsequent printing \t tabCauses the cursor to skip over to the next tab stop \\ backslashCauses a backslash to be printed \' single quoteCauses a single quotation mark to be printed \" double quoteCauses a double quotation mark to be printed

Java Output Incorporate Java escape sequences into the text you display Sample output: System.out.println( "\tHello!\n" ); System.out.print( "Welcome to" ); System.out.println( " \"Java\"" ); Hello! Welcome to "Java"

Java Data Types A data type represents a set of allowed values (and operations on those values) Data type int is a numerical data type representing integers Data type float is a numerical data type representing floating-point numbers Data type double is a numerical data type representing double-precision floating-point numbers Other data types are byte, short, long, boolean, and char

Java Data Types byte 1 byteIntegers in the range -128 to +127 short 2 bytesIntegers in the range of -32,768 to +32,767 int 4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647 long 8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 float 4 bytesFloating-point numbers in the range of ±3.410E-38 to ±3.410E38, with 7 digits of accuracy double 8 bytesFloating-point numbers in the range of ±1.710E-308 to ±1.710E308, with 15 digits of accuracy use int for your integers use double for your floating-point numbers

Java Data Types A data type represents a set of allowed values and operations on those values Arithmetic operators include addition ( + ), subtraction ( - ), multiplication ( * ), division ( / ), and modulus division ( % ) Binary operators require two operands (e.g. 9 * 87 ) Unary operators require one operand (e.g. -37 ) modulus division calculates the remainder after integer division (e.g. 9 % 2 equals 1 ) negation operator

Variables As in mathematics, a variable is a symbolic name used to represent numeric (or other) values In Java, variables must be declared and must have a specific data type: int x; float y; double average; long q; specify the data type specify the variable name

Assigning Values to Variables Assign values to variables using the = operator: int x; float y; double average; long q; x = 47; y = F; average = 42.7; q = 875L; specify the variable name specify the value

Displaying Variables as Output Use print and println to display the values of variables: Sample output: System.out.print( "Value of x is: " ); System.out.println( x ); System.out.print( "Value of y is: " ); System.out.println( y ); Value of x is: 47 Value of y is:

Displaying Variables as Output Use the concatenation operator ( + ) to simplify your print and println statements: Sample output: System.out.println( "Value of x is: " + x ); System.out.println( "Value of y is: " + y ); Value of x is: 47 Value of y is: