CompSci 125 Lecture 05. Programming Style, String Class and Literals, Static Methods, Packages

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

16-Dec-10. Consider the following method:

Pace University. Fundamental Concepts of CS121 1

Packages & Random and Math Classes

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Using Classes and Objects

Full file at

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

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

CS 211: Existing Classes in the Java Library

Object-Based Programming. Programming with Objects

Java By Abstraction - Test-B (Chapters 1-6)

CompSci 125 Lecture 02

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

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Methods CSC 121 Spring 2017 Howard Rosenthal

Using Classes and Objects. Chapter

CS 335 Lecture 02 Java Programming

4. Java Project Design, Input Methods

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CompSci 125 Lecture 07. Chapter 4: Object-Oriented Development

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Introduction to Computer Science Unit 2. Notes

CMSC 132: Object-Oriented Programming II

Methods CSC 121 Fall 2016 Howard Rosenthal

Java Libraries. Lecture 6 CGS 3416 Fall September 21, 2015

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Java Identifiers, Data Types & Variables

ECE 122. Engineering Problem Solving with Java

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Formatting Output & Enumerated Types & Wrapper Classes

Example: Tax year 2000

Lecture 10 Declarations and Scope

COMP6700/2140 Std. Lib., I/O

Programming with Java

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

Lab5. Wooseok Kim

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

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Introduction to Computer Science Unit 2. Notes

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Java Bytecode (binary file)

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chap. 3. Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L,

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

1 Shyam sir JAVA Notes

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

CS112 Lecture: Primitive Types, Operators, Strings

AP Computer Science Java Mr. Clausen Program 6A, 6B

Such JavaScript Very Wow

Using Classes and Objects

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

COMP-202 Unit 5: Basics of Using Objects

double float char In a method: final typename variablename = expression ;

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Singleton. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 29

PIC 20A The Basics of Java

Topics. The Development Process

Chapter Goals. Chapter 7 Designing Classes. Discovering Classes Actors (end in -er, -or) objects do some kinds of work for you: Discovering Classes

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Methods CSC 121 Fall 2014 Howard Rosenthal

Mr. Monroe s Guide to Mastering Java Syntax

Advanced Object Oriented Programming EECS2030Z

JAVA: A Primer. By: Amrita Rajagopal

AP Computer Science Unit 1. Programs

Objects and Classes -- Introduction

Text User Interfaces. Keyboard IO plus

CS 101 Fall 2006 Midterm 1 Name: ID:

Using Classes. GEEN163 Introduction to Computer Programming

CIS133J. Working with Numbers in Java

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

Programming by Delegation

CHAPTER 7 OBJECTS AND CLASSES

Chapter 2: Data and Expressions

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

Methods and Data (Savitch, Chapter 5)

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

CS110D: PROGRAMMING LANGUAGE I

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CT 229 Java Syntax Continued

CS Programming I: Primitives and Expressions

class objects instances Fields Constructors Methods static

CMSC 132: Object-Oriented Programming II

Introduction to Computer Science I

Objectives. Introduce static keyword examine syntax describe common uses

CS 231 Data Structures and Algorithms, Fall 2016

Objects Classes Strings Review CSC 123 Fall 2018 Howard Rosenthal

Transcription:

CompSci 125 Lecture 05 Programming Style, String Class and Literals, Static Methods, Packages

Homework Update HW2 Due 9/13

Programming Assignment Update p1: Traffic Applet due Sept 21

Programming Style

Review: Two Audiences for Commercial Software 1. Java: You need to correctly express your intentions to the Java Runtime Environment (JRE) 2. Other Programmers: You need to explain what you wrote, how it works and perhaps why you did it this way to other programmers

Code Health Commercial source code lives for years after its written after you have been promoted or moved to another project Other programmers will live with your source code You will become known by the code you leave behind Healthy code is easy to repair Healthy code is easy to enhance

Programming Style Contributes to Code Health Programming Style refers to the appearance of source code Ugly code may work correctly But not for long! Ugly code can become error prone. http://developers.sun.com/sunstudio/products/archive/whitepapers/java-style.pdf

Programming Style: Indentation public static void main(string[] args) {!!//Statements! }!

Programming Style: Identifier Names Terse names usually acceptable for throw-away variables whose meaning is obvious and scope is small: int i;! double x;! Descriptive names for everything important/far-reaching: int employeeage;! double altitudeinfeetabovegroundlevel;!

Programming Style: Identifier Capitalization Names of variables and methods begin with lower-case: int enginerpm;! public static int getnumberofcylinders() { }! Capitalize first letter of class names: public class Dragon { }! Use all capital letters for constant names: final double PI=3.1415926536;!

String Class and Literals

Review: Objects and their Reference Variables An object is an instance of a class constructed in memory An object has an address but not a name Usually, we won t know the address of an object We locate objects with reference variables The value of a reference variable is an address of an object

Sometimes we say, A reference variable points to an object String employeename = new String( Jim Conrad );! employeename Jim Conrad

Aliases: More than one variable may reference a single object String employeename = new String( Jim Conrad );! String instructorname = employeename;! employeename Jim Conrad instructorname

Aliases: Each Invocation of a constructor builds a new instance of the class String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! employeename Jim Conrad instructorname Jim Conrad

Java Implements a Short-cut for Building String Literals String employeename = Jim Conrad ;! employeename Jim Conrad

But the short-cut doesn t always build a new String object!!! String employeename = Jim Conrad ;! String instructorname = Jim Conrad ;! employeename Jim Conrad instructorname

Garbage Collection (Re-cycling) of Unreferenced Objects String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! instructorname = employeename;! employeename Jim Conrad instructorname Jim Conrad

Null Reference String employeename = new String( Jim Conrad );! String instructorname = new String( Jim Conrad );! instructorname = null;! employeename instructorname Jim Conrad Jim Conrad An Unreferenced object

String Methods

Review Methods Methods define the behaviors of an object An object is an instance of some class and implements the methods defined in that class A method consists of a sequence of Java instructions to be performed by an object A constructor is a special method that builds a new object

Methods Defined in the Built-In String Class The Built-In String class defines many methods Textbook Section 3.2 lists some These methods determine what a String object can do, its behaviors http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/string.html

Example String Methods String(String s)!!//constructs new String object! char charat(int index)!//extracts char at position! int length()!!//retrieves number of chars! boolean equals(string s)!//true if content identical to s!

Invoking Methods on a String Object String s = ABC ;! String q = Kyle ;! int numberofcharactersins = s.length();! char charat0 = s.charat(0);! char charat1 = s.charat(1);! char charat2 = s.charat(2);! boolean siskyle = s.equals( Kyle );! boolean qiskyle = q.equals( Kyle );!

Packages

Built-In Java Classes Organized into Packages Java includes hundreds of pre-defined classes These classes are organized into packages Collectively known as the Java Application Programming Interface (API) You must import a class to use it in your program Except classes in java.lang are automatically imported import java.util.scanner;!//imports Scanner class! import java.util.*;!!//imports entire java.util package!

The import Statement import java.util.random;! Package Name

The import Statement import java.util.random;! Class Name

Documentation http://docs.oracle.com/javase/6/docs/api Often easier to google for what you want google java String google java Scanner google java Graphics google java Color

Packages java.util.scanner: Text parser java.util.random: Generates random numbers

static Methods Methods ordinarily implement behaviors of an object Static Methods are defined in and associated with a class Not associated with any object! Useful for mathematical functions And a Java application s main method

java.lang.math The Math class defines only static methods! Math.abs(n) --- Absolute value of int n Math.pow(x,y) --- x y Math.sin(x) --- Trig function, sin(x) where x is in radians Math.sqrt(x) --- Square root of x