Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.

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.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2: Using Data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
JavaScript, Third Edition
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;
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
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,
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.
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 2: Using Data.
Chapter 2 Elementary Programming
C++ Programming: Basic Elements of C++.
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.
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.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Introduction to Programming
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
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: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
Chapter Topics The Basics of a C++ Program Data Types
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
elementary programming
Anatomy of a Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Presentation transcript:

Using Data Within a Program Chapter 2

 Classes  Methods  Statements  Modifiers  Identifiers

 Classes  The class is the essential Java construct.  A class is a template or blueprint for objects.  Methods  Main method is a collection of statements that performs a sequence of operations  Statements  Represents an action or a sequence of actions  Every statement in Java ends with a semicolon (;) Class { main { Statements; }

4  Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used.  Common modifiers:  public and static public static void main {  Other modifiers:  private, final, abstract, and protected.

Three types of comments in Java 1. Line comment: A line comment is preceded by two slashes (//) in a line 2. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines 3. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command.

 Used to represent a piece of data  Actually represents memory location where data is stored  Identifier used to name variable Should begin with lower case letter Subsequent words use upper case Should be meaningful  Example: salesTaxRate

 Constant  Cannot be changed while program is running  Variable  Named memory location  Use to store value  Can hold only one value at a time  Value can change

 Data type  Type of data that can be stored  How much memory item occupies  What types of operations can be performed on data

 Type int  Store integers, or whole numbers  Value from –2,147,483,648 to +2,147,483,647  Variations of the integer type  byte  short  long

 Floating-point number  Contains decimal positions  Floating-point data types  float  double  Significant digits  Refers to mathematical accuracy

 Boolean logic  Based on true-or-false comparisons  Boolean variable  Can hold only one of two values  true or false boolean isItPayday = false;  Relational operator  Compares two items

 char data type  Holds any single character  Place constant character values within single quotation marks char myMiddleInitial = 'M';  String  Built-in class  Store and manipulate character strings  String constants written between double quotation marks String myName = “Tim”;

 Arithmetic with variables or constants of same type  Result of arithmetic retains same type  Arithmetic operations with operands of unlike types  Java chooses unifying type for result  Unifying type  Type to which all operands in expression are converted for compatibility

 Every variable in a Java program must be declared before it is used  A variable declaration tells the compiler what kind of data (type) will be stored in the variable  The type of the variable is followed by one or more variable names separated by commas, and terminated with a semicolon  Variables are typically declared just before they are used or at the start of a block  Basic types in Java are called primitive types int numberofdays; double totalWeight;

 Variable given no particular value when declared  Assignment gives it a value  Syntax variable = expression ; Expression evaluated Value stored in memory location specified by variable

16  Declare multiple variables in separate statements on different lines int myAge = 25; int yourAge = 19;

 To find the area of a circle, we use the formula:  Area = (Pi) * (radius) 2 radius A = π r 2

public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } no value radius no value area allocate memory for radius and area

public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 20 radius area compute area and assign it to variable area assign value to radius Print message to console

 System.out is an object that is part of the Java language  println is a method invoked by the System.out object that can be used for console output  The data to be output is given as an argument in parentheses  A plus sign is used to connect more than one item  Every invocation of println ends a line of output System.out.println("The answer is " + 42);

 Another method that can be invoked by the System.out object is print  The print method is like println, except that it does not end a line  With println, the next output goes on a new line  With print, the next output goes on the same line

 Libraries in Java are called packages  A package is a collection of classes that is stored in a manner that makes it easily accessible to any program  In order to use a class that belongs to a package, the class must be brought into a program using an import statement  Use dialog box to display: import javax.swing.JOptionPane; JOptionPane.showMessageDialog()

import javax.swing.*; public class YourNameDialog{ public static void main(String[ ] args){ String result; result = JOptionPane.showInputDialog (null, "What is your name? "); JOptionPane.showMessageDialog (null, "Hello " + result); } }

 Input data through the keyboard by using the Scanner class in the utility library import java.util.Scanner;  Create a Scanner object Scanner input = new Scanner(System.in);  To obtain the value use the methods:  next()for string data type  nextByte()for byte  nextShort()for short integer  nextInt()for integer  nextLong()for long integer  nextFloat()for floating point  nextDouble()for double floating point  nextBoolean() for boolean value

import java.util.Scanner; public class ComputeArea { public static void main(String[ ] args) { double radius; double area; // Assign a radius System.out.print("Enter your radius: "); Scanner input = new Scanner(System.in); radius = input.nextDouble(); // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }

import javax.swing.*; public class ComputeArea { public static void main(String[ ] args) { double radius; double area; String input; // Assign a radius input = JOptionPane.showInputDialog (null, "Enter the radius "); radius = Double.parseDouble(input); // Compute area area = radius * radius * ; // Display results JOptionPane.showMessageDialog (null, "The area for the circle of radius " + radius + " is " + area); } Dialog Box only uses String data Converts the string to a double format

 A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter a whole number or decimal number: ");  Always echo all input that a program receives from the keyboard  In this way a user can check that he or she has entered the input correctly