WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.

Slides:



Advertisements
Similar presentations
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Advertisements

Yoshi
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 16: Exception.
CS0007: Introduction to Computer Programming Methods: Documentation, Reference Parameters, Modularization 2.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
CSE 252 Principles of Programming Languages Lab. Section
CS0007: Introduction to Computer Programming Introduction to Arrays.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Datalogi A 16: 21/11. Java Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Exceptions By the end of this lecture you should be able to: explain the term exception; distinguish between checked and unchecked exception classes in.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 5.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Exception Handling How to handle the runtime errors.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Exceptions 10-Nov-18.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Handling Exceptions.
Algorithm Correctness
CS2011 Introduction to Programming I Methods (II)
class PrintOnetoTen { public static void main(String args[]) {
LCC 6310 Computation as an Expressive Medium
Lecture 6: References and linked nodes reading: 16.1
Java Basics Exception Handling.
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
CMSC 202 Exceptions.
A type is a collection of values
Presentation transcript:

WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION

WEEK 2 Reference Type Arrays ArrayList Methods Pass by Value (Primitives and References)

Reference Type Arrays Reference type arrays are reference types of collection of reference types. String[] sArray = new String[4]; (1)(2) sArray[0]=new String(“Kemal”);//alias to sArray[0]=“Kemal”; //sArray[1],sArray[2],sArray[3]  null

sArray[0]=new String(“Kemal”);

Reference Type Arrays String s=“CSE252”; String[] sArray=new String[4]; sArray[0]=“Kemal”;(1) sArray[3]=s;(2) ??

String[] sArray = new String[4]; (1)(2)

Arrays(General) Array is a kind of class in Java API. The most useful variable: length: the size of an array.

Methods Functions in java. (1) Instance Methods: An instance method is a method which is associated with one object and uses the variables of that object.methodvariables int i= scannerObject.nextInt(); // nextInt is instance method. (1) Static Methods: The method which is associated with a class, not an object. System.out.println(“Static Method”);

Methods The method for returning a variable and passing parameters are same at a instance method and a class method. Here we will look at static methods because main method can only call static methods. The calling principle of a method at java is pass-by-value.

Example(1) public class FunctionExample { public static void main(String[] args){ int a=max(4,15); System.out.println(a); } static int max(int a,int b){ return a>b?a:b; }

Example(2) public class FunctionExample { public static void main(String[] args){ write(“CSE 252”); write(generateNum()); int num=generateNum(); System.out.println(num); } static void write(String s){ System.out.println(s); } static int generateNum(){ return 5; }

Example(3): public class FunctionExample { public static void main(String[] args){ String s=“Kemal”; write(s); } static void write(String s){ System.out.println(s); } static int generateNum(){ return 5; }

Pass-by-value (In primitive types) Example (4): public class FunctionExample { public static void main(String[] args){ int a=90; int b=80; swap(a,b); // not swapped !!! System.out.println(a + “ “ + b); } static void swap(int a,int b){ int tmp=a; a=b; b=tmp; }

Pass-by-value (Reference Types) public class FunctionExample { public static void main(String[] args){ int[] intArray=createArray(); System.out.println(intArray.length); // here intArray.length is 5 System.out.println(intArray[0]); // here intArray[0] is 9 } static int[] createArray(){ int[] arrayInMethod =new int[5]; arrayInMethod[0]=9; return arrayInMethod; }

Exceptions Exceptions -> runtime errors. (1) Exceptions cannot be catched at compilation time (2) It can occur when the program is running. (3) Normal flow of instructions can be distrupted with an exception. (4) When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception. An example of such a violation is an attempt to index outside the bounds of an array.

Exceptions Example: int a=scannerObj.nextInt(); this method will wait for user entrance from keyboard and it also accept user to enter a number literal (3,-2,4,5009,-80,90.3). When user enters an non number literal like 3AA4,Kemal,CSE252,443AA, a violation for semantics occurs and an exception will be thrown.

Handling Exceptions Exceptions must be handled. If an occurred exception is not handled by the program, the execution of program will be terminated with an exception message. This statement can cause fatal situations when executing I/O systems or low- level systems. Exceptions will be handled with try catch statements.

Handling Exceptions Example: int i; try{ i=scannerObject.nextInt(); }catch(Exception e){ System.out.println(“You must enter a number”); return; } System.out.println(i);