Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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.
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
CMT Programming Software Applications
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
VB .NET Programming Fundamentals
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
String Escape Sequences
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;
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
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.
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.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
1 Operations Making Things Happen (Chap. 3) Expressions.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
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.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
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.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
Information and Computer Sciences University of Hawaii, Manoa
Introduction to Computer Science / Procedural – 67130
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Statements, Comments & Simple Arithmetic
Type Conversion, Constants, and the String Object
Chapter 2: Basic Elements of Java
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Expressions and Assignment
elementary programming
Anatomy of a Java Program
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators

In this class, we will cover: Building a Java class Using Java variables and data types Method Definitions Computing with Java

Building a Java Clss Previous lab and homework demonstrated a simple Java application. public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello World”); } } Be familiar with the basic components of this sample. All applications will have this framework. Remember: Class names should begin with a capital.

Building a Java Class Java code generally consists of: –Comments –Variable definitions e.g. –int aNumber = 1; –boolean isFound = true; –double anotherNumber = 1.50; – String s = “example of a string”; note the naming conventions –Method definitions e.g. –the main method in applications public static void main (String[] args) { } –public computeTax(); again note the naming conventions

Building a Java Class Comments –Single line // compiler ignores everything to end of line –Multi-line /* compiler ignores everything in between */ –Multi-line (documentation) /** compiler ignores everything in between */ Used for JavaDoc –Comments do not appear in executable code

Variable Definitions Variable definitions –Variable: name of place in memory that can contain data –All variables have: Data type  kind of data variable can contain Name  identifier that refers to the variable Value  the default or specified value –also called the literal of the statement Semicolon –Remember: All java statements end with a semicolon! –e.g. String s = “MC697”; int count = 5;

Variable Definitions Initializing Variables –Assignment operator (=) Used to assign value to a variable –char c = ‘a’; - note the single quotes –boolean b = true; –double d = 1.25; Important: This is different from the comparative equals (==) If variable is not initialized, most will default to null. All variables should be initialized.

Data Types Declaring Variables –Variable data type must be declared prior to initialization –There are two basic data types: Primitive data types –Eight available primitive data types –Primitive data types are not capitalized in variable declarations »int aNumber = 5; Reference data types –These data types are capitalized. »String s = “example string”;

Data Types Using Reference Variables –Uses class name as a data type –Points to an instance of that class –Example: »String s = “Hello World”; »Employee emp1;

Variable Constants Using Constants –Variable with a value that doesn’t change –Keyword final –Denotes value cannot change –Example: final double SALES_TAX_RATE = 4.5; note the naming convention other examples?

Method Defnitions Methods contain: –access modifier defines who can call this method –return type defines what this method will return –static optional - we will discuss this later –method name should begin with a lower case –argument definition section defines what input is expected in this method –block of code with statements

Statements Within a Method This is the logic of the program. –Can define and assign variables. –Can invoke another method Send the method a message asking it to execute Message is sent in the argument section. –e.g. System.out.println(“output from method”); –What is the argument in this method?

Computing with Java Changing Data Types –If changing data type results in no loss of precision, can be done implicitly: int c = 5; double a, b = 3.5; a = b + c; –What is it called when you explicitly change the data type?

Computing with Java –Casting allows data type changes explicitly with loss of precision: int a, c = 5; double b = 3.5; a = (int) b + c;

Computing with Java Operators –Arithmetic operators (+,-,*,/) addition, subtraction, multiplication, division Precedence using parentheses Remainder (modulus) operator (%) –Produces remainder from division of two integers Concatenation operator (+) –Joins String literals and variables –Automatically converts numeric and Boolean values to strings before use in println method Math Class –Methods for exponentiation, rounding, etc.

Computing with Java Special Operators –For writing shortcut code Increment operator (++) –Add one to a variable Decrement operator (--) –Subtract one from a variable Assignment operator with arithmetic operators: total = total + 5; What is another way of writing this statement?

Review Questions from Reading: Which primitive data types are you most likely to use? In the following variable definitions: String s = “sample string”; char char1 = ‘a’; Why is String capitalized and char not? What is Casting?