Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

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

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

Data Conversion & Scanner Class

Chapter. Let's explore some other fundamental programming concepts

Computational Expression

CS111: PROGRAMMING LANGUAGE II

ECE 122 Engineering Problem Solving with Java

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

CS111: PROGRAMMING LANGUAGE II

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

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Chapter 2: Data and Expressions

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Chapter 2: Data and Expressions

STUDENT LESSON A7 Simple I/O

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

COMP 202 Java in one week

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

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

AP Computer Science Unit 1. Writing Programs Using BlueJ

AP Computer Science Unit 1. Programs

More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

COMP 202. Java in one week

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

First Java Program - Output to the Screen

CS110: PROGRAMMING LANGUAGE I

Programming with Java

COMP 202 Java in one week

Lecture 5: Methods CS2301

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Introduction to Java Unit 1. Using BlueJ to Write Programs

Method. Why Write Methods? Overview. CS256 Computer Science I Kevin Sahr, PhD. Lecture 21: Writing Class Methods. ClassName.methodName(arguments)

I. Variables and Data Type week 3

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

Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

AP Computer Science Unit 1. Writing Programs Using BlueJ

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 2: Basic Elements of Java

Full file at

2: Basics of Java Programming

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Object Oriented Programming. Java-Lecture 1

Full file at

Week 6 CS 302. Jim Williams, PhD

Basic Computation. Chapter 2

Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

1. An operation in which an overall value is computed incrementally, often using a loop.

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

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

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computational Expression

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

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

Midterms Save the Dates!

Introduction to Computer Science Unit 2. Exercises

Selection Statements and operators

Lecture Notes for CS 150 Fall 2009; Version 0.5

CMPT 125: Lecture 4 Conditionals and Loops

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

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

CMPT 125: Lecture 3 Data and Expressions

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Introduction to Computer Science Unit 2. Notes

Lecture 3: Variables and assignment

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Using Java Classes Fall 2018 Margaret Reid-Miller

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Building Java Programs

Chapter 2: Data and Expressions

AP CS Unit 3: Control Structures Notes

Handout 4 Conditionals. Boolean Expressions.

B.V. Patel Institute of BMC & IT, UTU 2014

Building Java Programs

Programming Exercise 7: Static Methods

Lecture Set 2: Starting Java

Building Java Programs

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Introduction to Computer Science Unit 2. Notes

Lecture Set 2: Starting Java

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Section 2: Introduction to Java. Historical note

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

Practice Midterm 1. Problem Points Score TOTAL 50

2.8. Decision Making: Equality and Relational Operators

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

CS1083 Week 2: Arrays, ArrayList

Supplementary Test 1

Character Stream : It provides a convenient means for handling input and output of characters.

SDKs - Eclipse. SENG 403, Tutorial 2

Logic & program control part 2: Simple selection structures

Lab5. Wooseok Kim

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Transcription:

CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we now now that this argument is an array of Strings allows information to be passed into the application 2 Constants A constant is a variable that holds the same value during its entire existence the constant must be initialized with that value when it is declared The compiler will issue an error if you try to change the value of a constant In Java, we use the final modifier to declare a constant EX: final int MIN_HEIGHT = 100; 3 Why Use Constants? Constants give meaning to otherwise unclear literal values ( magic numbers ) For example, MIN_HEIGHT conveys more meaning than the literal 100 Constants facilitate program maintenance If a constant value changes and is used in multiple places, its value need only be updated in one place Constants tell the compiler that a value should not change, avoiding inadvertent errors by other programmers 4 3 4

Instance Variable Constants Sometimes an instance variable is a constant For example, every array object has an instance variable length that holds the number of elements in the array length is declared with final, since the size of an array can t change once it is created It s OK to make constant instance variables public because they re constants external users can t change them 5 5 Instance Variable Access Instance variables can be accessed using the dot operator syntax: objname.varname EX: if we have an array named grades, the number of elements in grades is given by grades.length 6 6 EX: Array Output Loop EX: output all of the elements in an array nums for (int i = 0; i < nums.length; i++) Output.showMessage( i + : + nums[i]); note that we don t need to know ahead of time how many elements are in nums 7 Simple Output: The println Method The println method can be used to produce text output System.out.println ("Whatever you are, be a good one."); object method name single String parameter The System.out object represents a destination (the monitor screen) to which we can send output The System.out object is automatically created for your program 8

Outputting Values We can use String concatenation and the println method to output values System.out.println (num1 + plus + num2 + is + sum); 9 Text Input The Scanner class provides convenient methods for reading input values of various types A Scanner object can be set up to read input from various sources, including the user typing values on the keyboard Keyboard input is represented by the System.in object 10 Creating a Scanner Object The Scanner class is part of the java.util package, and therefore must be imported by your programs The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); 11 Reading Keyboard Input Once created, the Scanner object can be used to invoke various input methods, such as: String answer = scan.nextline(); The nextline method reads all of the input until the end of the line is found The Scanner class provides methods for inputting integers, doubles, etc. from the keyboard 12

Often we perform an operation on a variable, and then store the result back into that variable Java provides assignment operators to simplify that process For example, the statement is equivalent to num += count; num = num + count; 13 There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y 14 The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-min) % num; is equivalent to result = result / ((total-min) % num); 15 The behavior of some assignment operators depends on the types of the operands If the operands to the += operator are strings, the assignment operator performs string concatenation The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+) 16

Lecture 25 Vocabulary constant magic numbers final modifier assignment operators 17