Introduction to Programming

Slides:



Advertisements
Similar presentations
Chapter 2 Review Questions
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.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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;
1 Chapter 2 JAVA FUNDAMENTALS CONT’D. 2 PRIMITIVE DATA TYPES Computer programs operate on data values. Values in Java are classified according to their.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
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.
***** SWTJC STEM ***** Chapter 2-2 cg Identifiers - Naming Identifier, the name of a Java programming component. Identifier naming rules... Must.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
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.
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
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++
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e 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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
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:
Chapter 2 Variables.
Input/Output.
Crash course in the Java Programming Language
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Chapter 2: Java Fundamentals
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 2 Variables.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Introduction to Programming Java Fundamentals (Numbers, Characters, Strings) David Goldschmidt, Ph.D. Computer Science The College of Saint Rose

Common Programming Errors Examples of programming pitfalls: Don’t rush to writing code until you have sketched out an algorithm or solution Create backups of your code using flash drives, etc. Make sure every beginning has an end.... parentheses public static int void main(String[] args) { System.out.println("Quotes come in twos"); System.out.println("Answer: " + (9 + 5)); } braces quotes

Common Programming Errors More examples of programming pitfalls: Watch for misspelled Java keywords Java is a case-sensitive language, so be careful of uppercase and lowercase letters Don’t forget to end statements with a semicolon (;) Or use { to open a new block of code Watch for incompatible data types when you assign values to variables: int x; x = 928.502;

Common Programming Errors Even more examples of programming pitfalls: Watch out for integer division: Don’t use a variable unless it has been declared and assigned a value: int result; result = 1 / 2; q = 928.502; int x; System.out.println(x);

Representing Numeric Constants Use the final keyword when you declare a variable to ensure its value cannot change: Why use final? final int monthsInYear = 12; final double ourPI = 3.14159; final double rate = 0.0675; monthsInYear = 11; // ERROR! rate = 0.07; // ERROR!

Formatting Numeric Output When displaying floating-point numbers (i.e. data types float and double), you often want to format the output e.g. instead of displaying $42.934439, we would prefer to display $42.93 To do so: Use the DecimalFormat class, which is part of the Java library Create a DecimalFormat format pattern Apply the pattern to the number to be formatted

Formatting Numeric Output To use the DecimalFormat class, we first need to import the library via the import directive: import java.text.DecimalFormat; public class Name { ...

Formatting Numeric Output Next, we create a DecimalFormat pattern by adding the following code to the main method: Does formatting a number round the number? double dollarAmt = 42.92839; DecimalFormat df = new DecimalFormat( "0.00" ); System.out.print( "Cost is $" ); System.out.println( df.format( dollarAmt ) );

American Standard Code for Information Interchange ASCII & Unicode Characters Character data types are encoded as numbers Java uses two bytes to store characters as Unicode ASCII is the American Standard Code for Information Interchange

be enclosed in single quotes Representing Characters in Java Characters (e.g. letters, digits, symbols) are represented using the char data type a character value must be enclosed in single quotes char c, d, e; c = '4'; d = 'u'; e = '!'; System.out.print(c); System.out.print(d); System.out.print(e); single character … single quotes 4u! output

Character Strings A group of characters is called a character string String literals are enclosed in double quotation marks: The String class in Java allows you to create character string variables: System.out.print( "This is a string" ); String s1 = "This is a string";

(i.e. create a String object) Character Strings In Java, String objects are similar to variables: String s1 = "This is a string"; Declare variable s1 of type String (i.e. create a String object) Initialize variable s1

Character Strings We can use print and println to display String objects in Java: String firstName = "David"; String lastName = "Goldschmidt"; System.out.print( "Hello, " ); System.out.println( firstName ); System.out.print( "Bye Dr. " ); System.out.println( lastName );

The String Class & Methods Once we have a String object created, we can use various methods of the String class Determine the length of a String: String firstName = "David"; String lastName = "Goldschmidt"; int len = lastName.length(); System.out.print( "Last name has " ); System.out.println( len + " letters." );

The String Class & Methods Once we have a String object created, we can use various methods of the String class Convert a String to uppercase or lowercase: String s = "Java is so cool"; System.out.println( s.toUpperCase() ); String lower = s.toLowerCase(); System.out.println( lower );

The String Class & Methods Once we have a String object created, we can use various methods of the String class Obtain individual characters from the String: Position (or index) starts at zero String s = "Java is so cool"; char firstLetter = s.charAt( 0 ); char thirdLetter = s.charAt( 2 ); System.out.print( firstLetter ); System.out.println( thirdLetter );

Reading Keyboard Input A running Java program can have both output (System.out) and input (System.in): System.out System.in Java Program Execution

Reading Keyboard Input To read input from the keyboard, we use Java’s Scanner class Create a Scanner object associated with System.in: Scanner keyboard = new Scanner( System.in ); Declare variable keyboard of type Scanner Create a Scanner object and connect to System.in

Reading Keyboard Input Once created, use the Scanner object to read keyboard input: Scanner keyboard = new Scanner( System.in ); System.out.println( "What's your name?" ); String name = keyboard.nextLine(); System.out.println( "How old are you?" ); int age = keyboard.nextInt();

Reading Keyboard Input To use the Scanner class, we need to import in the class from the java.util package: import java.util.Scanner; public class NameGame { public static void main( String[] args ) ...

Dialog Boxes Use a dialog box to display a message graphically: import javax.swing.JOptionPane; public class DialogBoxes { public static void main( String[] args ) JOptionPane.showMessageDialog( null, "This is called a dialog box." );