Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

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.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
String Escape Sequences
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS177 Week2: Recitation Primitive data types and Strings with code examples.
August 6, 2009 Data Types, Variables, and Arrays.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
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[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Building java programs, chapter 3 Parameters, Methods and Objects.
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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[]
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Lecture 2 Data Types Richard Gesick.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
OPERATORS (2) CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Fundamentals 2.
Chapter 2: Java Fundamentals
Building Java Programs
Unit 3: Variables in Java
Names of variables, functions, classes
Just Enough Java 17-May-19.
Chapter 2 Primitive Data Types and Operations
Building Java Programs
Presentation transcript:

Java Methods

Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another  Arithmetic operators

Declaration vs. initialization  The following is JUST declaration  int sum;  double x,y;//note, 2 variables declared  Declaration including initialization  int mileage = suburban.getMileage();  Declaration and initialization are often separate  UrRobot karel;  karel = new UrRobot(1,1,East,0);

Declaration vs. initialization  Additional notes:  int x = 5; //assigning x the value of 5  x is referencing the value of 5  final double gravity = 9.8;  The reserved word final makes the variable become a constant  Useful for values that should never be changed

Fields vs. Local Variables  Fields – declared within a class, but outside of any constructor or method  - fields are accessible to the entire class  - fields are typically declared private // let us start observing this style rule  Local variables – temporary variables declared inside a constructor or method  Every time a method is called, that variable is re- initialized  Note: your program can have fields and local variables of the same name  avoid this!

Parameters  Parameters are considered a third type of variable in addition to fields and local variables  Parameters – variables passed to constructors or methods  - act like local variables  public void moveSteps (int step); public static void main(String[] args) { moveSteps(int x = 6);}  It is bad convention to declare and initialize when you call a method, but this demonstrates that each method creates a new reference for the parameter, so you can give it a new name

Primitive Data types  There are 8 – listed on page 128  We will use: boolean, char, int, double  Boolean – true or false  int – for use of integers – cuts off any decimal approximations (no rounding!  5.9 = 5)  double – for use of decimals  char – unicode character set- ASCII code  If you had char x = 46, what does System.out.println(x) do?

Unicode values  When declaring and initializing a char type variable, use single quotations:  char initial = ‘a’;  Fact!  unicode values are 16 bit  To see all unicode values, copy the following: for(x=0; x<26; x++) { for(y=0; y<10; y++) //will do values from 0 to 260 { System.out.printf(" %4d = %4c", 10*x+y, 10*x+y); } System.out.println(); }

Casting  Suppose you had the following:  double hour = 3.5;  int miles = 20;  Check to see if the following compiles:  double mph = miles/hour;  Int and double are apples and oranges!  The fix  a cast  double mph = (double)miles/hour;

Arithmetic Expressions  Arithmetic expressions  You should know the following arithmetic operators: +,-,*,/  An important one you need to know:   modulus - %   modulus will give you the remainder of a quotient  Ex) 10%3 will give you 1

Arithmetic Expressions  You can use modulus as an effective even or odd evaluator  If (x%2==0)  if (x is even)  if( x%2!=0)  if(x is odd)  The modulus of a number is never larger than it’s divisor  i.e. (255%16 = 15)

Compound Assignment  When doing arithmetic operations to update a variable, it is recommended to use compound assignment  Take x = x +y;   x+=y;  You can do this for any operator

Pre/Post Increment  It is also recommended to use pre and post increment/decrement operators  Post increment: operator applied after the expression is used  x++ (x= x +1) & x-- (x = x-1)  Pre increment: operator applied before  ++x (x=x+1)  If y = 0, x =3, what is the difference between:  y = x++ and y = ++x ?  Use a System.out.println(y) to check  Can you explain it?

Strings  Strings are not primitive data types; they are objects derived from the String class  We call them “literals”  Ex): String str = “karel”;   the variable str references the String “karel”  Concatenation: String str = “karel” + “J”;  System.out.println(str);  karelJ

Escape sequences  These are specific character combinations in a String that the compiler recognizes  ex) \n  newline (check page 131 for the rest)  System.out.println(“Hi, how \n are you”);  What does that look like?

Converting to a string  For primitive values:  Just concatenate an empty string!  Ex) int amount = 15;  System.out.println(“” + amount);  Or  String amount = “” + amount;

Work  Page 143 – finish the Pie Chart program!  Exercises: 1,2,4,5,6,7,10,12,13,18,19