J.43 The length field of an array object makes the length of the array available. J.44 ARRAYS

Similar documents
CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Appendix 3. Description: Syntax: Parameters: Return Value: Example: Java - String charat() Method

More non-primitive types Lesson 06

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CHAPTER 6 MOST COMMONLY USED LIBRARIES

"Hello" " This " + "is String " + "concatenation"

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Programming with Java

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Creating Strings. String Length

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

Lab 14 & 15: String Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

6.Introducing Classes 9. Exceptions

String. Other languages that implement strings as character arrays

STUDENT LESSON A10 The String Class

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

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

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

String related classes

Introduction to Programming Using Java (98-388)

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Using Java Classes Fall 2018 Margaret Reid-Miller

Lecture Notes K.Yellaswamy Assistant Professor K L University

Building Strings and Exploring String Class:

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

- Thus there is a String class (a large class)

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

Lesson:9 Working with Array and String

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

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

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

Class Library java.lang Package. Bok, Jong Soon

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

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

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Eng. Mohammed Abdualal

Index COPYRIGHTED MATERIAL

More on variables and methods

Brief Summary of Java

Midterms Save the Dates!

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Array. Prepared By - Rifat Shahriyar

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

Java Strings Java, winter semester

Pace University. Fundamental Concepts of CS121 1

Object Oriented Programming

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Prelim One Solution. CS211 Fall Name. NetID

CSE 143 Au03 Midterm 2 Page 1 of 7

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

USING LIBRARY CLASSES

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Std 12 Lesson-10 Exception Handling in Java ( 1

Fall 2017 CISC124 10/1/2017

Programming II (CS300)

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) - All Sections Final Examination


21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Introduction to Java

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Full file at

Object Oriented Programming Exception Handling

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

Here is a hierarchy of classes to deal with Input and Output streams.

Modern Programming Languages. Lecture Java Programming Language. An Introduction

5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

Declarations and Access Control SCJP tips

Introduction Unit 4: Input, output and exceptions

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Operators and Expressions

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Preview from Notesale.co.uk Page 9 of 108

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

String is one of mostly used Object in Java. And this is the reason why String has unique handling in Java(String Pool). The String class represents

WES-CS GROUP MEETING #9

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Selected Questions from by Nageshwara Rao

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

CSC Java Programming, Fall Java Data Types and Control Constructs

Exceptions - Example. Exceptions - Example

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

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

Review. Single Pixel Filters. Spatial Filters. Image Processing Applications. Thresholding Posterize Histogram Equalization Negative Sepia Grayscale

A couple of decent C++ web resources you might want to bookmark:

Transcription:

ARRAYS A Java array is an Object that holds an ordered collection of elements. Components of an array can be primitive types or may reference objects, including other arrays. Arrays can be declared, allocated, and / or initialized all in one compact statement. J.43 The length field of an array object makes the length of the array available. Java supports arrays of arrays, rather than 2D or multi-dimensional arrays. Example: public class myarray { public static void main (String[] args) { double[] [] mat = {{1., 2., 3., 4., {5., 6., 7., 8., {9., 10., 11., 12., {13., 14., 15., 16.; J.44 int[] ai; int[] ia = new int[3]; char ac[] = { n, o, t ; for (int y = 0; y < mat.length; y++) { for (int x = 0; x < mat[y].length; x++) System.out.print(mat[y][x] + " "); System.out.println(); The length of an array is fixed at creation and cannot be changed, but a new, longer array instance can be assigned to the Object. What does it print? Is the array stored in row-major or column-major order?

J.45 J.46 STRINGS String Methods The String class provides read-only strings and supports operations on them A String can be created implicitly either by using a quoted string (e.g. "grass") or by the concatenation of two String objects, using the + operator. A String can also be created using new and a String constructor. String astring = new String(); String bstring = new String("grass ); length() returns the number of characters charat(i) returns the character at position i. for (int i = 0; i < str.length(); i++) System.out.println(charAt(i)); indexof(char ch) returns the first position of character ch in a String. lastindexof(char ch) returns the last position of character ch in a String.

String Comparisons The method equals returns true if it is passed a reference to a String object with the same contents as a given String. J.47 Since you cannot modify existing strings, there are methods to create new strings from existing ones. J.48 The method equalsignorecase works like equals, but ignores upper/lower case distinctions. The method compareto returns an int that is less than, equal to, or greater than zero for comparisons and sorting. public String substring(int beginindex, int endindex) public String replace(char oldchar, char newchar) public String concat(string str) Example: Part of a Binary Search while (lo <= hi) { int mid = lo + (hi - lo) / 2; int cmp = key.compareto(table[mid]); if (cmp == 0) return mid; else if (cmp < 0) hi = mid - 1; else lo = mid + 1; public String tolowercase() public String touppercase() public String trim()

J.49 J.50 EXCEPTIONS An exception is a condition that arises during execution. An exception is thrown when an error condition is encountered. It is then caught by code that receives it and deals with it. Java exceptions are objects that extend the class Throwable or one of its subclasses, usually the subclass Exception. Class Throwable and its subclasses have two constructors, one with no arguments and one with a String argument for an error message. Defining Exceptions Programmers can define their own exception types and write code to throw them and catch them. Example with Attributes and Values: public class NoSuchAttributeException extends Exception { public String attrname; public Object newvalue; NoSuchAttributeException(String name, Object value) { super("no attribute named \"" + name + "\" found"); attrname = name; newvalue = value;

J.51 J.52 Throwing Exceptions Methods that will check for errors and throw exceptions use the throws clause to tell the compiler what kind of exceptions they may throw the throw statement to perform the throwing Example: public void replacevalue(string name, Object newvalue) throws NoSuchAttributeException { Attr attr = find(name); if (attr == null) throw new NoSuchAttributeException(name newvalue); attr.valueof(newvalue); If your method invokes a method that lists a checked exception it its throwable clause, it can do one of three things Catch the exception and handle it. Catch the exception and map it into one of your own exceptions by throwing an exception of a type declared in your own throws clause Declare the exception in your throws clause and let it pass through your method, possibly adding a finally clause that cleans up first.

J.53 Finally J.54 Catching Exceptions Syntax: try <block> catch (<exception_type> <identifier>) <block> catch(<exception_type> <identifier>) <block>... finally <block> Example: try{ attributedobj.replacevalue( Age, new Integer(8)); catch(nosuchattributeexception e) { Attr attr = new Attr(e.attrName, e.newvalue); attributedobj.add(attr); The finally clause of a try statement provides a mechanism for executing a section of code whether or not an exception is thrown. This example closes a file when its work is done, even if an error occurs: public boolean searchfor(string file, String word) throws StreamException { Stream input = null; try { input = new Stream(file);* while (!input.eof()) if (input.next() == word) return* true; return* false; *finally { if (input!= null) input.close(); * This can fail. * Finally is executed before any return.