Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

Size: px
Start display at page:

Download "Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal"

Transcription

1 Variables and Assignments CSC 121 Spring 2017 Howard Rosenthal

2 Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements Learn how to read in data from a terminal Creating your first object Using the Scanner object for interactive input Compatibility and casting 2

3 What is a Variable A variable (either primitive or a reference) is a named memory location capable of storing data of a specified type Variables indicate logical, not physical addresses Those are taken care of in the JVM and the OS Variables in Java always have a specific data type Variables must always be declared before they can be used Remember that a data type is a scheme for using bit patterns to represent a value or a reference. Think of a variable as a little box made of one or more bytes that can hold a value using a particular data type. double Cost_of_Home 128,

4 What is a Variable (2) A declaration of a variable is made if a program requires a named variable of a particular type Three things happen The variable is named with an identifier The amount of space required for the variable is defined How the bits in that space are interpreted is defined The value of a variable is always its last declared or assigned value in a program public class Example1_CH3 { public static void main ( String [] args ) { long payamount; //the declaration of the variable payamount = 123; // variable assignment System.out.println("The variable payamount contains: " + payamount); payamount = -8976; // variable reassignment System.out.println("The variable payamount contains: " + payamount); } } 4

5 Declaring Variables Variables must be declared before they are used Basic syntax: Type name1, name2, name3 ; Example: double dinner_bill, creditbalance, moneyinbank;//three variables of type double Variables can be explicitly initialized when they are declared short rent = 75; double milkprice_half = 1.99; boolean testimony = true; Can t do multiple assignments at once when declaring variables int x=y=z=0; // Incorrect syntax int x,y,z=60; // only z is initialized int x=60, y=60, z=60; //all three are initialized Don t try to assign a value that that is illegal short big_number = ; This gives an error why? 5

6 Naming Variables Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. Just like any other identifier A variable name cannot contain the space character. Do not start with a digit. A variable can be any length. Java is case sensitive. Upper and lower case count as different characters. SUM and Sum are different identifiers. A variable name cannot be a reserved word. Don t use the same name twice, even as different types it will generate an error during compilation Programmers should follow the good practice of creating meaningful identifier names that self-describe an item's purpose. Meaningful names make programs easier to maintain. userage, housesquarefeet, and numitemsonshelves. Good practice minimizes use of abbreviations in identifiers except for well-known ones like num in numpassengers. Abbreviations make programs harder to read and can also lead to confusion, such as if a chiropractor application involves number of messages and number of massages, and one is abbreviated nummsgs (which is it?). 6

7 Assigning Variables Assignment statements look like this: variablename = expression ; //The equal sign = is the assignment operator variablename is the name of a variable that has been declared previously in the program. Remember: An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value must be syntactically correct Assignment is accomplished in two steps: Evaluate the expression Store the value in the variable which appears to the left of the = sign Examples (assuming variable has been previously declared): total = 3 + 5; // total =8 price = 34.56; tax = price*0.05; //tax = While you can t do a multiple assignment in a declaration statement, you can do multiple assignments once the variables have been declared: int x,y,z; //declaration of the variables x=y=z=0; // works as an assignment statement provided the variables have been declared previously these work right to left int x=y=z=3; // This will generate a syntax error because you are first declaring the variables here x=y=z=14+5; // First calculate the value of the expression, then do the assignments 7

8 Assigning Variables (2) What does the following program fragment write? int value = 5; System.out.println("value is: " + value ); value = value + 10; System.out.println("value is: " + value ); 8

9 Assigning Variables (3) Primitive variables must be initialized before they are used They aren t given a default value They are typically initialized by reading in a value You can also explicitly initialize in the code int value; value = value + 10; // this gives an error as value wasn t initialized 9

10 final Variables final variables are really constants They are assigned a value that doesn t change final double PI = ; final int PERFECT = 100; final variables are by convention written with all capitals 10

11 An Example of a Simple Program (1) The Fibonacci sequence is 1,1,2,3,5,8,13,21,34,55. The next number is the sum of the previous two numbers. Pseudocode for printing Fibonacci series up to 50 Set the low number =1 Set the high number equal to 1 Print the low Start of Loop check that high is less than 50 Print the high Set the high equal to the low+high Set the low equal to the high -low Repeat loop 11

12 An Example of a Simple Program (2) public class Fibonacci { // Print out the Fibonacci sequence for values < 50 public static void main(string[] args) { int lo = 1; int hi = 1; System.out.println(lo); while (hi < 50) { System.out.println(hi); hi = lo + hi; // new hi lo = hi - lo; /* new lo is (sum - old lo) i.e., the old hi */ } } } Note: We are using a while construct that is formally introduced in Chapter 5 Note: also look at Fibonacci2.java 12

13 Obtaining Input Data Using Scanner Scanner is a class. You must include the following statement to use the class to create a Scanner object: import java.util.scanner; or import java.util.*; You are importing a class with all it s methods that can then be used to create objects Declare a Scanner object variable and create an object: Scanner scannername = new Scanner (System.in); new means you are creating a new object new is a reserved word System.in is a variable that tell the Java program that this input stream is coming from the keyboard You can now access seven types of primitive input: byte, short, int, long, double, float and boolean using predefined methods associated with object A Scanner object doesn t read characters We will learn a way around this when we study the String object The scannername is just another variable name 13

14 There are 7 Input Methods Available For PrimiQve Data Types ScannerName.nextByte() ScannerName.nextShort() ScannerName.nextInt() ScannerName.nextLong() ScannerName.nextDouble() ScannerName.nextFloat() ScannerName.nextBoolean() Note: the type names are capitalized in these methods Note: There is no way to directly input a char with a Scanner object What you are looking at above is the standard way objects access methods: objectname.method(par1, par2, ) - There are no parameters required for the Scanner methods 14

15 Each Scanner Object Is Created From The Scanner Class keyboard Name0f input file (System.in) Constructor used to create object Methods nextbyte() nextshort() nextint() nextlong() nextfloat() nextdouble() nextboolean() next() nextline() hasnext() o

16 An Example Showing Scanner Usage import java.util.scanner; public class ScannerUsage { public static void main(string [] args) { Scanner keyboard = new Scanner(System.in); // Creates a new Scanner called keyboard System.out.println("Enter an integer of type int"); int n1 = keyboard.nextint(); System.out.println("Enter an integer of type byte"); byte b1 = keyboard.nextbyte(); float f1; double d1; long l1; short s1; boolean bn1; } } System.out.println("Enter a floating point of type float"); f1 = keyboard.nextfloat(); System.out.println("Enter a floating point of type double and Enter an integer of type long"); d1 = keyboard.nextdouble(); l1 = keyboard.nextlong(); System.out.println("Enter an integer of short int and Enter boolean value"); s1 = keyboard.nextshort(); bn1 = keyboard.nextboolean(); System.out.println("n1 = " + n1 +"\nb1 = " + b1 + "\nf1 = " + f1 + "\nd1 = " + d1); System.out.println("l1 = " + l1 +"\ns1 = " + s1 + "\nbn1 = " + bn1); 16

17 An PracQcal Example Using Scanner import java.util.scanner; // Make the Scanner class available. public class Interest2WithScanner { public static void main(string[] args) { Scanner keyboard = new Scanner( System.in ); // Create the Scanner. double principal; // The value of the investment. double rate; // The annual interest rate. double interest; // The interest earned during the year. System.out.print("Enter the initial investment: "); principal = keyboard.nextdouble(); System.out.print("Enter the annual interest rate "); rate = keyboard.nextdouble(); rate = rate/100; interest = principal * rate; // Compute this year's interest. principal = principal + interest; // Add it to principal. System.out.print("The value of the investment after one year is $"); System.out.println(principal); // } } // end of class Interest2withScanner 17

18 CasQng (1) The value of a smaller numerical type may be assigned to a higher numerical data type automatically Do you know the order? Casting down must be done explicitly Example: int natural, bigger; double odds, othernumber; odds = 20.3; natural = 5; othernumber = (int)(odds)*natural; bigger = (int)othernumber; System.out.println(otherNumber); //prints System.out.println(bigger); //prints 100 othernumber = odds*natural; bigger = (int)othernumber; System.out.println(otherNumber); //prints System.out.println(bigger); //prints

19 CasQng (2) Remember that float is a special case that always requires casting if you are assigning a floating point number to a float variable float x = 15.0; //This creates an error float y = (float)15.0; //This is proper explicit casting float z = 15; //Implicit casting 19

20 CasQng (3) Character data may be assigned to any of the types short, int, long, double or float When this happens the ASCII/Unicode value is assigned to the numerical value double x = A ; System.out.print(x); The output is 65.0 Why? However, char chm = A ; System.out.println(chm); The output is A 20

21 Shortcuts Operator Shortcut For += x+=10 x=x+10 -= x-=10 x=x-10 *= x*=10 x=x*10 /= x/=10 x=x/10 %= x%=10 x=x%10 21

22 Prefix and PosTix for IncremenQng and DecremenQng There are two mechanisms for adding or subtracting 1 from a variable ++number; //prefix form for adding 1 to number --number; //prefix form for subtracting 1 from number number++; //postfix form for adding 1 to number number--; //postfix form for subtracting 1 from number What s the difference? In assignment statements prefix action takes effect before the assignment In assignment statements postfix action takes effect after the assignment 22

23 Prefix and PosTix for IncremenQng and DecremenQng (2) Prefix Incrementing int number =5,result; result = 3*(++number); System.out.println(result); 18 System.out.println(number); 6 Postfix Incrementing int number =5,result; result = 3*(number++); System.out.println(result); 15 System.out.println(number); 6 Use postscript incrementing carefully, it can be confusing However, it is very useful when dealing with loops 23

24 Prefix and PosTix for IncremenQng and DecremenQng (3) Here is what happens in a postfix in prior example result = 3*(number++); a. A hidden temp = number; b. number = number +1; c. result = 3*temp; What is the value printed in the following example; int number =10; number = number++; System.out.println(number); 24

25 Programming Exercise Class (1) Calculate The Product. Write a program that reads in four integers and prints their product. 25

26 Programming Exercise Class (2) Exercise 4. Area Write a program that prompts a user for the dimensions (double) of a room in feet (length, width, height) and calculates and prints out the total area of the floor, ceiling and walls. 26

27 Programming Exercise Lab (1) Exercise 11. Running Sums Write a program that accepts ten integers n1, n2, n3,. n10 and prints a running sum that is, your program should display ten sums: n1, n1+n2, n1+n2+n3 and so on. For example, if the input is 3, 28, 5, 8, 9, 10, 12, 2, 1, -19 then the output is

28 10. Larger or Smaller Programming Exercise Lab (2) Write a program that accepts 5 integers, and for each integer following the first, prints true or false depending on whether or not that integer is greater than the previous one. This program can be written more simply after we complete chapter 4. 28

Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal

Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal Variables and Assignments CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn how to formulate assignment statements

More information

Variables and Assignments CSC 121 Fall 2014 Howard Rosenthal

Variables and Assignments CSC 121 Fall 2014 Howard Rosenthal Variables and Assignments CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand variables Understand how to declare and use variables in Java Programs Learn Assignment statements for variables Learn

More information

Expressions, Data Types, Formatted Printing, Scanning CSC 123 Fall 2018 Howard Rosenthal

Expressions, Data Types, Formatted Printing, Scanning CSC 123 Fall 2018 Howard Rosenthal Expressions, Data Types, Formatted Printing, Scanning CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review the basic constructs of a Java Program Review simple Java data types and the operations on those

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

A+ Computer Science -

A+ Computer Science - import java.util.scanner; or just import java.util.*; reference variable Scanner keyboard = new Scanner(System.in); object instantiation Scanner frequently used methods Name nextint() nextdouble() nextfloat()

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Computational Expression

Computational Expression Computational Expression Scanner, Increment/Decrement, Conversion Janyl Jumadinova 17 September, 2018 Janyl Jumadinova Computational Expression 17 September, 2018 1 / 11 Review: Scanner The Scanner class

More information

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

More information

A+ Computer Science -

A+ Computer Science - Visit us at www.apluscompsci.com Full Curriculum Solutions M/C Review Question Banks Live Programming Problems Tons of great content! www.facebook.com/apluscomputerscience import java.util.scanner; Try

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Built in Libraries and objects CONTENTS: Introduction to objects Introduction to some basic Java libraries string COMP 202 Objects and Built in Libraries 1 Classes and Objects An object is an

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

More information

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Data Types Arithmetic

More information

Input. Scanner keyboard = new Scanner(System.in); String name;

Input. Scanner keyboard = new Scanner(System.in); String name; Reading Resource Input Read chapter 4 (Variables and Constants) in the textbook A Guide to Programming in Java, pages 77 to 82. Key Concepts A stream is a data channel to or from the operating system.

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Constants Data Types

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

St. Edmund Preparatory High School Brooklyn, NY

St. Edmund Preparatory High School Brooklyn, NY AP Computer Science Mr. A. Pinnavaia Summer Assignment St. Edmund Preparatory High School Name: I know it has been about 7 months since you last thought about programming. It s ok. I wouldn t want to think

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

Peer Instruction 1. Elementary Programming

Peer Instruction 1. Elementary Programming Peer Instruction 1 Elementary Programming 0 Which of the following variable declarations will not compile? Please select the single correct answer. A. int i = 778899; B. double x = 5.43212345; C. char

More information

ing execution. That way, new results can be computed each time the Class The Scanner

ing execution. That way, new results can be computed each time the Class The Scanner ing execution. That way, new results can be computed each time the run, depending on the data that is entered. The Scanner Class The Scanner class, which is part of the standard Java class provides convenient

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Data Conversion & Scanner Class

Data Conversion & Scanner Class Data Conversion & Scanner Class Quick review of last lecture August 29, 2007 ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev Numeric Primitive Data Storing

More information

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

More information

Introduction to Java Unit 1. Using BlueJ to Write Programs

Introduction to Java Unit 1. Using BlueJ to Write Programs Introduction to Java Unit 1. Using BlueJ to Write Programs 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers and letters. Think of them

More information

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class CS0007: Introduction to Computer Programming Review General Form of a switch statement: switch (SwitchExpression) { case CaseExpression1:

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming 2.1 Introduction You will learn elementary programming using Java primitive data types and related subjects, such as variables, constants, operators, expressions, and input

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

A Quick and Dirty Overview of Java and. Java Programming

A Quick and Dirty Overview of Java and. Java Programming Department of Computer Science New Mexico State University. CS 272 A Quick and Dirty Overview of Java and.......... Java Programming . Introduction Objectives In this document we will provide a very brief

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations Chapter 2 Primitive Data Types and Operations 2.1 Introduction You will be introduced to Java primitive data types and related subjects, such as variables constants, data types, operators, and expressions.

More information

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Creating

More information

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

We now start exploring some key elements of the Java programming language and ways of performing I/O

We now start exploring some key elements of the Java programming language and ways of performing I/O We now start exploring some key elements of the Java programming language and ways of performing I/O This week we focus on: Introduction to objects The String class String concatenation Creating objects

More information

Java Classes: Math, Integer A C S L E C T U R E 8

Java Classes: Math, Integer A C S L E C T U R E 8 Java Classes: Math, Integer A C S - 1903 L E C T U R E 8 Math class Math class is a utility class You cannot create an instance of Math All references to constants and methods will use the prefix Math.

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods

Computer Science 210 Data Structures Siena College Fall Topic Notes: Methods Computer Science 210 Data Structures Siena College Fall 2017 Topic Notes: Methods As programmers, we often find ourselves writing the same or similar code over and over. We learned that we can place code

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

More information

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University Elementary Programming CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Variables In a program, the variables store data Primitive type variables store single pieces

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

More information

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

Methods CSC 121 Spring 2017 Howard Rosenthal

Methods CSC 121 Spring 2017 Howard Rosenthal Methods CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

Programming Exercise 7: Static Methods

Programming Exercise 7: Static Methods Programming Exercise 7: Static Methods Due date for section 001: Monday, February 29 by 10 am Due date for section 002: Wednesday, March 2 by 10 am Purpose: Introduction to writing methods and code re-use.

More information

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Methods CSC 121 Fall 2016 Howard Rosenthal

Methods CSC 121 Fall 2016 Howard Rosenthal Methods CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what a method is in Java Understand Java s Math Class and how to use it Learn the syntax of method construction Learn both void methods

More information

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Chapter 2 Elementary Programming 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical problems

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

Basic Programming Elements

Basic Programming Elements Chapter 2 Basic Programming Elements Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Darrell Bethea May 25, 2011

Darrell Bethea May 25, 2011 Darrell Bethea May 25, 2011 Yesterdays slides updated Midterm on tomorrow in SN014 Closed books, no notes, no computer Program 3 due Tuesday 2 3 A whirlwind tour of almost everything we have covered so

More information

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Writing Programs Using BlueJ AP Computer Science Unit 1. Writing Programs Using BlueJ 1. Open up BlueJ. Click on the Project menu and select New Project. You should see the window on the right. Navigate to wherever you plan to save

More information

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

! definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. ! We often use language like Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Important Java terminology

Important Java terminology 1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

JAVA Ch. 4. Variables and Constants Lawrenceville Press

JAVA Ch. 4. Variables and Constants Lawrenceville Press JAVA Ch. 4 Variables and Constants Slide 1 Slide 2 Warm up/introduction int A = 13; int B = 23; int C; C = A+B; System.out.print( The answer is +C); Slide 3 Declaring and using variables Slide 4 Declaring

More information

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution Chapter 2: Elementary Programming CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Motivations In the preceding chapter, you learned how to

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java Java Data types and input/output Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email:

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #04: Fall 2015 1/20 Office hours Monday, Wednesday: 10:15 am to 12:00 noon Tuesday, Thursday: 2:00 to 3:45 pm Office: Lindley Hall, Room 401C 2/20 Printing

More information

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Computer Programming, I. Laboratory Manual. Experiment #3. Selections Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #3

More information

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 8  INPUT  Instructor: Craig Duckett Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment

More information

STUDENT LESSON A7 Simple I/O

STUDENT LESSON A7 Simple I/O STUDENT LESSON A7 Simple I/O Java Curriculum for AP Computer Science, Student Lesson A7 1 STUDENT LESSON A7 Simple I/O INTRODUCTION: The input and output of a program s data is usually referred to as I/O.

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information

AP Computer Science A

AP Computer Science A AP Computer Science A 1st Quarter Notes Table of Contents - section links Click on the date or topic below to jump to that section Date : 9/8/2017 Aim : Java Basics Objects and Classes Data types: Primitive

More information

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Introduction to Java Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Key Points 1) Introduce Java, a general-purpose programming language,

More information

Introduction to Computer Programming

Introduction to Computer Programming Introduction to Computer Programming Lecture 2- Primitive Data and Stepwise Refinement Data Types Type - A category or set of data values. Constrains the operations that can be performed on data Many languages

More information

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal

Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal Objects and Classes 1: Encapsulation, Strings and Things CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand objects and classes Understand Encapsulation Learn about additional Java classes The

More information

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202

More information

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it. Introduction to JAVA JAVA is a programming language which is used in Android App Development. It is class based and object oriented programming whose syntax is influenced by C++. The primary goals of JAVA

More information

Loops and Expression Types

Loops and Expression Types Software and Programming I Loops and Expression Types Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline The while, for and do Loops Sections 4.1, 4.3 and 4.4 Variable Scope Section

More information

Classes and Objects Part 1

Classes and Objects Part 1 COMP-202 Classes and Objects Part 1 Lecture Outline Object Identity, State, Behaviour Class Libraries Import Statement, Packages Object Interface and Implementation Object Life Cycle Creation, Destruction

More information

Text User Interfaces. Keyboard IO plus

Text User Interfaces. Keyboard IO plus Text User Interfaces Keyboard IO plus User Interface and Model Model: objects that solve problem at hand. User interface: interacts with user getting input from user giving output to user reporting on

More information

CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

method method public class Temperature { public static void main(string[] args) { // your code here } new

method method public class Temperature { public static void main(string[] args) { // your code here } new Methods Defining Classes and Methods (Savitch, Chapter 5) TOPICS Java methods Java objects Static keyword Parameter passing Constructors A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements COMP-202 Unit 4: Programming With Iterations CONTENTS: The while and for statements Introduction (1) Suppose we want to write a program to be used in cash registers in stores to compute the amount of money

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Lesson 7 Part 2 Flags

Lesson 7 Part 2 Flags Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means

More information

Over and Over Again GEEN163

Over and Over Again GEEN163 Over and Over Again GEEN163 There is no harm in repeating a good thing. Plato Homework A programming assignment has been posted on Blackboard You have to convert three flowcharts into programs Upload the

More information