CPIT 305 Advanced Programming

Size: px
Start display at page:

Download "CPIT 305 Advanced Programming"

Transcription

1 M. G. Abbas Malik CPIT 305 Advanced Programming Assistant Professor Faculty of Computing and IT University of Jeddah

2 Advanced Programming Course book: Introduction to Java Programming: Comprehensive Edition, 10th Edition, Y. Daniel Liang, Pearson Reference BooK: Java How to Program, 9th Edition, Paul Deitel and Harvey Deitel, Deitel. Discrete Mathematics and its Application, Kenneth H. Rosen, 7th Edition, McGraw-Hill Java Docs: Course Home: 75% attendance is compulsory to sit in the final exam M. G. Abbas Malik, FCIT, UoJ 2

3 Advanced Programming Topics Covered Recursion Revisited Review of Object Oriented Concepts Exception Handling File and Stream Handling Binary I/O Multithreading and Parallel Programming Networking - Socket Programming Java Database Programming M. G. Abbas Malik, FCIT, UoJ 3

4 Exception Chapters: 12 Handling M. G. Abbas Malik, FCIT, UoJ 4

5 Exception handling enables a program to deal with exceptional situations and continue its normal execution M. G. Abbas Malik, FCIT, UoJ 5

6 ArrayIndexOutOfBoundsException If you access an array using an index that is out of bounds, you will get a runtime error with an ArrayIndexOutOfBoundsException int array[10]; // create 10 elements array array[10]; // Run time Error M. G. Abbas Malik, FCIT, UoJ 6

7 InputMismatchException If you enter a double value when your program expects an integer, you will get a runtime error with an InputMismatchException Scanner input = new Scanner(System.in); int x; // create an int x = input.nextint(); // Run time Error when users gives 2.34 M. G. Abbas Malik, FCIT, UoJ 7

8 Runtime errors are thrown as exceptions. An exception is an object that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate abnormally. Exceptions are thrown from a method. The caller of the method can catch and handle the exception. M. G. Abbas Malik, FCIT, UoJ 8

9 Division by ZERO - Run Time Error/Exception LISTING 12.1 Quotient.java 1 import java.util.scanner; 2 3 public class Quotient { 4 public static void main(string[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter two integers 8 System.out.print("Enter two integers: "); 9 int number1 = input.nextint(); 10 int number2 = input.nextint(); System.out.println(number1 + " / " + number2 + " is " + (number1 / 13 number2)); 14 } 15 } M. G. Abbas Malik, FCIT, UoJ 9

10 Division by ZERO - Run Time Error/Exception LISTING 12.2 QuotientWithIf.java 1 import java.util.scanner; 2 3 public class QuotientWithIf { 4 public static void main(string[] args) { 5 Scanner input = new Scanner(System.in); 6 7 // Prompt the user to enter two integers 8 System.out.print("Enter two integers: "); 9 int number1 = input.nextint(); 10 int number2 = input.nextint(); if (number2!= 0) If Condition help us to avoid Division by ZERO Run Time Error/Exception 13 System.out.println(number1 + " / " + number2 + " is " + (number1 / number2)); 14 else 15 System.out.println("Divisor cannot be zero "); 16 } 17 } M. G. Abbas Malik, FCIT, UoJ 10

11 Division by ZERO - Run Time Error/Exception LISTING 12.3 QuotientWithMethod.java 1 import java.util.scanner; 2 3 public class QuotientWithMethod { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) { 6 System.out.println("Divisor cannot be zero"); 7 System.exit(1); 8 } 9 10 return number1 / number2; 11 } public static void main(string[] args) { 14 Scanner input = new Scanner(System.in); // Prompt the user to enter two integers 17 System.out.print("Enter two integers: "); 18 int number1 = input.nextint(); 19 int number2 = input.nextint(); int result = quotient(number1, number2); 22 System.out.println(number1 + " / " + number2 + " is + result); 23 } 24 } M. G. Abbas Malik, FCIT, UoJ 11

12 Division by ZERO - Run Time Error/Exception LISTING 12.4 QuotientWithException.java 1 import java.util.scanner; 2 3 public class QuotientWithException { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) 6 throw new ArithmeticException("Divisor cannot be zero"); 7 8 return number1 / number2; 9 } public static void main(string[] args) { 12 Scanner input = new Scanner(System.in); // Prompt the user to enter two integers 15 System.out.print("Enter two integers: "); 16 int number1 = input.nextint(); 17 int number2 = input.nextint(); 18 Handling Division by ZERO Run Time Error/Exception 19 try { 20 int result = quotient(number1, number2); 21 System.out.println(number1 + " / " + number2 + " is " 22 + result); 23 } 24 catch (ArithmeticException ex) { 25 System.out.println("Exception: an integer " + 26 "cannot be divided by zero "); 27 } System.out.println("Execution continues..."); 30 } 31 } M. G. Abbas Malik, FCIT, UoJ 12

13 Exception is an object created from an exception class - in Division by ZERO the exception is java.lang.arithmeticexception When an exception is thrown, the normal execution flow is interrupted. Throws and Exception: exception is passed from the point where it is occur to the point where the method is called - pass the exception from one place to another M. G. Abbas Malik, FCIT, UoJ 13

14 Division by ZERO - Run Time Error/Exception LISTING 12.4 QuotientWithException.java 1 import java.util.scanner; 2 3 public class QuotientWithException { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) 6 throw new ArithmeticException("Divisor cannot be zero"); 7 8 return number1 / number2; 9 } public static void main(string[] args) { 12 Scanner input = new Scanner(System.in); // Prompt the user to enter two integers 15 System.out.print("Enter two integers: "); 16 int number1 = input.nextint(); 17 int number2 = input.nextint(); try { 20 int result = quotient(number1, number2); 21 System.out.println(number1 + " / " + number2 + " is " 22 + result); 23 } 24 catch (ArithmeticException ex) { 25 System.out.println("Exception: an integer " + 26 "cannot be divided by zero "); 27 } System.out.println("Execution continues..."); 30 } 31 } M. G. Abbas Malik, FCIT, UoJ 14

15 Try Catch Code Block - Handle Exception in an appropriate way and stop your program from crashing in the middle of execution try { Java Code to Run; A Statement or a method call that may throw an exception More Code to Run; } catch (ExceptionType identifier_variable) { Code to process the exception; } M. G. Abbas Malik, FCIT, UoJ 15

16 Try Catch Code Block - an exception can be thrown directly in the Try block using throw statement try { Java Code to Run; throw Exception ( It is an exception ); More Code to Run; } catch (ExceptionType identifier_variable) { Code to process the exception; } M. G. Abbas Malik, FCIT, UoJ 16

17 Try Catch Code Block - an exception may be thrown by the invoked method in Try block try { Java Code to Run; mymethod(); More Code to Run; } catch (ExceptionType identifier_variable) { Code to process the exception; } M. G. Abbas Malik, FCIT, UoJ 17

18 Division by ZERO - Run Time Error/Exception LISTING 12.4 QuotientWithException.java 1 import java.util.scanner; 2 3 public class QuotientWithException { 4 public static int quotient(int number1, int number2) { 5 if (number2 == 0) 6 throw new ArithmeticException("Divisor cannot be zero"); 7 8 return number1 / number2; 9 } public static void main(string[] args) { 12 Scanner input = new Scanner(System.in); // Prompt the user to enter two integers 15 System.out.print("Enter two integers: "); 16 int number1 = input.nextint(); 17 int number2 = input.nextint(); 18 We have defined the method thus we can handle the exception either in the main method or in the method itself 19 try { 20 int result = quotient(number1, number2); 21 System.out.println(number1 + " / " + number2 + " is " 22 + result); 23 } 24 catch (ArithmeticException ex) { 25 System.out.println("Exception: an integer " + 26 "cannot be divided by zero "); 27 } System.out.println("Execution continues..."); 30 } 31 } M. G. Abbas Malik, FCIT, UoJ 18

19 In case of library method, we cannot handle the exception in them We have to handle the exception in the calling method/code that we are writing/designing M. G. Abbas Malik, FCIT, UoJ 19

20 Input Miss Match Exception LISTING 12.5 InputMismatchExceptionDemo.java 1 import java.util.*; 2 3 public class InputMismatchExceptionDemo { 4 public static void main(string[] args) { 5 Scanner input = new Scanner(System.in); 6 boolean continueinput = true; 7 8 do { 9 try { 10 System.out.print("Enter an integer: "); 11 int number = input.nextint(); // Display the result 14 System.out.println("The number entered is " + number); continueinput = false; 17 } 18 catch (InputMismatchException ex) { 19 System.out.println("Try again. (" + "Incorrect input: an integer is required)"); 20 input.nextline(); // Discard input 21 } 22 } while (continueinput); 23 } 24 } M. G. Abbas Malik, FCIT, UoJ 20

21 Input Miss Match Exception LISTING 12.5 InputMismatchExceptionDemo.java 1 import java.util.*; 2 3 public class InputMismatchExceptionDemo { 4 public static void main(string[] args) { 5 Scanner input = new Scanner(System.in); 6 boolean continueinput = true; 7 8 do { 9 try { 10 System.out.print("Enter an integer: "); 11 int number = input.nextint(); // Display the result 14 System.out.println("The number entered is " + number); continueinput = false; This piece of code is not executed 17 } 18 catch (InputMismatchException ex) { 19 System.out.println("Try again. (" + "Incorrect input: an integer is required)"); 20 input.nextline(); // Discard input 21 } 22 } while (continueinput); 23 } 24 } M. G. Abbas Malik, FCIT, UoJ 21

22 Types of Exceptions Exceptions are objects, and objects are defined using classes The root class for exceptions is java.lang.throwable All Java exception classes inherit directly or indirectly from Throwable. You can create your own exception classes by extending Exception or a subclass of Exception. M. G. Abbas Malik, FCIT, UoJ 22

23 Types of Exceptions The class names Error, Exception, and RuntimeException are somewhat confusing. All three of these classes are exceptions, and all of the errors occur at runtime. M. G. Abbas Malik, FCIT, UoJ 23

24 Types of Exceptions Errors Exceptions are objects, and objects are defined using classes System errors are thrown by the JVM and are represented in the Error class. Error class describes internal system errors, though such errors rarely occur If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully M. G. Abbas Malik, FCIT, UoJ 24

25 Types of Exceptions Errors M. G. Abbas Malik, FCIT, UoJ 25

26 Types of Exceptions Exceptions Exceptions are represented in the Exception class, which describes errors caused by your program and by external circumstances These errors can be caught and handled by your program M. G. Abbas Malik, FCIT, UoJ 26

27 Types of Exceptions Exceptions M. G. Abbas Malik, FCIT, UoJ 27

28 Types of Exceptions Runtime Exceptions Runtime exceptions are represented in the RuntimeException class, which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM M. G. Abbas Malik, FCIT, UoJ 28

29 Types of Exceptions Runtime Exceptions RuntimeException, Error, and their subclasses are known as Unchecked Exceptions All others are Checked Exceptions M. G. Abbas Malik, FCIT, UoJ 29

30 Types of Exceptions Checked Exceptions Compiler forces the programmer to check and deal with them in a try-catch block or declare it in the method header M. G. Abbas Malik, FCIT, UoJ 30

31 Types of Exceptions Unchecked Exceptions Unchecked exceptions reflect programming logic errors that are unrecoverable NullPointerException If we access an object through a reference variable before an object is assigned to it // Array variable is reference type variable int [] intarray; intarray[4] = 3; // throws a NullPointerException M. G. Abbas Malik, FCIT, UoJ 31

32 Types of Exceptions Unchecked Exceptions Unchecked exceptions reflect programming logic errors that are unrecoverable NullPointerException IndexOutOfBoundsException M. G. Abbas Malik, FCIT, UoJ 32

33 What RuntimeException will the following programs throw, if any? M. G. Abbas Malik, FCIT, UoJ 33

34 What RuntimeException will the following programs throw, if any? M. G. Abbas Malik, FCIT, UoJ 34

35 What RuntimeException will the following programs throw, if any? M. G. Abbas Malik, FCIT, UoJ 35

36 Multiple Catch Blocks to handle multiple exceptions try { Java Code to Run; A Statement or a method call that may throw an exception More Code to Run; } catch (ExceptionType1 identifier_variable) { Code to process the exception; } catch (ExceptionType2 identifier_variable) { Code to process the exception; } M. G. Abbas Malik, FCIT, UoJ 36

37 Multiple Catch Blocks to handle multiple exceptions M. G. Abbas Malik, FCIT, UoJ 37

38 Java s Exception Handling Model 1.Declaring an exception 2.Throwing an exception 3.Catching and exception M. G. Abbas Malik, FCIT, UoJ 38

39 M. G. Abbas Malik, FCIT, UoJ 39

40 Declaring an Exception Java requires that all checked exceptions thrown by a method must be declared explicitly in the method header M. G. Abbas Malik, FCIT, UoJ 40

41 Declaring an Exception Java requires that all checked exceptions thrown by a method must be declared explicitly in the method header M. G. Abbas Malik, FCIT, UoJ 41

42 Throwing an Exception M. G. Abbas Malik, FCIT, UoJ 42

43 Getting Information from Exception M. G. Abbas Malik, FCIT, UoJ 43

44 finally clause The code in the finally block is executed under all circumstances, regardless of whether an exception occurs in the try block or is caught. M. G. Abbas Malik, FCIT, UoJ 44

45 Re-throwing Exception M. G. Abbas Malik, FCIT, UoJ 45

46 Defining Custom Exception Class M. G. Abbas Malik, FCIT, UoJ 46

47 Advanced Programming Topics Covered Recursion Revisited Review of Object Oriented Concepts Exception Handling File and Stream Handling Binary I/O Multithreading and Parallel Programming Networking - Socket Programming Java Database Programming M. G. Abbas Malik, FCIT, UoJ 47

CS 111 Classes I 1. Software Organization View to this point:

CS 111 Classes I 1. Software Organization View to this point: CS 111 Classes I 1 Software Organization View to this point: Data Objects and primitive types Primitive types operators (+, /,,*, %). int, float, double, char, boolean Memory location holds the data Objects

More information

Introduction to Java

Introduction to Java Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high

More information

What are exceptions? Bad things happen occasionally

What are exceptions? Bad things happen occasionally What are exceptions? Bad things happen occasionally arithmetic: 0, 9 environmental: no space, malformed input undetectable: subscript out of range, value does not meet prescribed constraint application:

More information

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit. Scanner The Scanner class is intended to be used for input. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit. For example, suppose the input

More information

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10) File class in Java File Input and Output (Savitch, Chapter 10) TOPICS File Input Exception Handling File Output Programmers refer to input/output as "I/O". The File class represents files as objects. The

More information

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs. http://www.tutorialspoint.com/java/java_exceptions.htm JAVA - EXCEPTIONS Copyright tutorialspoint.com An exception orexceptionalevent is a problem that arises during the execution of a program. When an

More information

Basics of Java Programming Input and the Scanner class

Basics of Java Programming Input and the Scanner class Basics of Java Programming Input and the Scanner class CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

System.out.println(\nEnter Product Number 1-5 (0 to stop and view summary) : Benjamin Michael Java Homework 3 10/31/2012 1) Sales.java Code // Sales.java // Program calculates sales, based on an input of product // number and quantity sold import java.util.scanner; public class

More information

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION 1. Write a loop that computes (No need to write a complete program) 100 1 99 2 98 3 97... 4 3 98 2 99 1 100 Note: this is not the only solution; double sum

More information

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)

More information

Designing with Exceptions. CSE219, Computer Science III Stony Brook University http://www.cs.stonybrook.edu/~cse219

Designing with Exceptions. CSE219, Computer Science III Stony Brook University http://www.cs.stonybrook.edu/~cse219 Designing with Exceptions CSE219, Computer Science III Stony Brook University http://www.cs.stonybrook.edu/~cse219 Testing vs. Debugging Testing Coding Does the code work properly YES NO 2 Debugging Testing

More information

Crash Course in Java

Crash Course in Java Crash Course in Java Based on notes from D. Hollinger Based in part on notes from J.J. Johns also: Java in a Nutshell Java Network Programming and Distributed Computing Netprog 2002 Java Intro 1 What is

More information

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015 RESEARCH ARTICLE An Exception Monitoring Using Java Jyoti Kumari, Sanjula Singh, Ankur Saxena Amity University Sector 125 Noida Uttar Pradesh India OPEN ACCESS ABSTRACT Many programmers do not check for

More information

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner java.util.scanner java.util.scanner is a class in the Java API used to create a Scanner object, an extremely versatile object that you can use to input alphanumeric characters from several input sources

More information

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1 Exception Handling Error handling in general Java's exception handling mechanism The catch-or-specify priciple Checked and unchecked exceptions Exceptions impact/usage Overloaded methods Interfaces Inheritance

More information

D06 PROGRAMMING with JAVA

D06 PROGRAMMING with JAVA Cicles Formatius de Grau Superior Desenvolupament d Aplicacions Informàtiques D06 PROGRAMMING with JAVA Ch15 Exception Handling PowerPoint presentation, created by Angel A. Juan - ajuanp(@)gmail.com, for

More information

Part I. Multiple Choice Questions (2 points each):

Part I. Multiple Choice Questions (2 points each): Part I. Multiple Choice Questions (2 points each): 1. Which of the following is NOT a key component of object oriented programming? (a) Inheritance (b) Encapsulation (c) Polymorphism (d) Parallelism ******

More information

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

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

AP Computer Science Java Mr. Clausen Program 9A, 9B AP Computer Science Java Mr. Clausen Program 9A, 9B PROGRAM 9A I m_sort_of_searching (20 points now, 60 points when all parts are finished) The purpose of this project is to set up a program that will

More information

Using Files as Input/Output in Java 5.0 Applications

Using Files as Input/Output in Java 5.0 Applications Using Files as Input/Output in Java 5.0 Applications The goal of this module is to present enough information about files to allow you to write applications in Java that fetch their input from a file instead

More information

AP Computer Science File Input with Scanner

AP Computer Science File Input with Scanner AP Computer Science File Input with Scanner Subset of the Supplement Lesson slides from: Building Java Programs, Chapter 6 by Stuart Reges and Marty Stepp (http://www.buildingjavaprograms.com/ ) Input/output

More information

Course Intro Instructor Intro Java Intro, Continued

Course Intro Instructor Intro Java Intro, Continued Course Intro Instructor Intro Java Intro, Continued The syllabus Java etc. To submit your homework, do Team > Share Your repository name is csse220-200830-username Use your old SVN password. Note to assistants:

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution Topic 11 Scanner object, conditional execution "There are only two kinds of programming languages: those people always [complain] about and those nobody uses." Bjarne Stroustroup, creator of C++ Copyright

More information

Lecture J - Exceptions

Lecture J - Exceptions Lecture J - Exceptions Slide 1 of 107. Exceptions in Java Java uses the notion of exception for 3 related (but different) purposes: Errors: an internal Java implementation error was discovered E.g: out

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 www.virtualians.pk

www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 Which of the following is a general purpose container? JFrame Dialog JPanel JApplet Which of the following package needs to be import while handling

More information

Fundamentals of Java Programming

Fundamentals of Java Programming Fundamentals of Java Programming This document is exclusive property of Cisco Systems, Inc. Permission is granted to print and copy this document for non-commercial distribution and exclusive use by instructors

More information

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius Programming Concepts Practice Test 1 1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius 2) Consider the following statement: System.out.println("1

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

More information

AP Computer Science Java Subset

AP Computer Science Java Subset APPENDIX A AP Computer Science Java Subset The AP Java subset is intended to outline the features of Java that may appear on the AP Computer Science A Exam. The AP Java subset is NOT intended as an overall

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: 3.3-3.4 self-check: #16-19 exercises: #11 videos: Ch. 3 #4 Interactive programs We have written programs that print

More information

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html CS 259: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 319 8/19/2015 Install

More information

Java Application Developer Certificate Program Competencies

Java Application Developer Certificate Program Competencies Java Application Developer Certificate Program Competencies After completing the following units, you will be able to: Basic Programming Logic Explain the steps involved in the program development cycle

More information

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement CompSci 125 Lecture 08 Chapter 5: Conditional Statements Chapter 4: return Statement Homework Update HW3 Due 9/20 HW4 Due 9/27 Exam-1 10/2 Programming Assignment Update p1: Traffic Applet due Sept 21 (Submit

More information

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq Introduction to Programming using Java wertyuiopasdfghjklzxcvbnmqwertyui

More information

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75 Preet raj Core Java and Databases CS4PR Time Allotted: 3 Hours Final Exam: Total Possible Points 75 Q1. What is difference between overloading and overriding? 10 points a) In overloading, there is a relationship

More information

Chulalongkorn University International School of Engineering Department of Computer Engineering 2140105 Computer Programming Lab.

Chulalongkorn University International School of Engineering Department of Computer Engineering 2140105 Computer Programming Lab. Chulalongkorn University Name International School of Engineering Student ID Department of Computer Engineering Station No. 2140105 Computer Programming Lab. Date Lab 2 Using Java API documents, command

More information

CS 121 Intro to Programming:Java - Lecture 11 Announcements

CS 121 Intro to Programming:Java - Lecture 11 Announcements CS 121 Intro to Programming:Java - Lecture 11 Announcements Next Owl assignment up, due Friday (it s short!) Programming assignment due next Monday morning Preregistration advice: More computing? Take

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: 3.3-3.4 self-check: #16-19 exercises: #11 videos: Ch. 3 #4 Interactive programs We have written programs that print

More information

Java from a C perspective. Plan

Java from a C perspective. Plan Java from a C perspective Cristian Bogdan 2D2052/ingint04 Plan Objectives and Book Packages and Classes Types and memory allocation Syntax and C-like Statements Object Orientation (minimal intro) Exceptions,

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java CSCI/CMPE 3326 Object-Oriented Programming in Java Class, object, member field and method, final constant, format specifier, file I/O Dongchul Kim Department of Computer Science University of Texas Rio

More information

CSE 1020 Introduction to Computer Science I A sample nal exam

CSE 1020 Introduction to Computer Science I A sample nal exam 1 1 (8 marks) CSE 1020 Introduction to Computer Science I A sample nal exam For each of the following pairs of objects, determine whether they are related by aggregation or inheritance. Explain your answers.

More information

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

public static void main(string args[]) { System.out.println( "f(0)=" + f(0));

public static void main(string args[]) { System.out.println( f(0)= + f(0)); 13. Exceptions To Err is Computer, To Forgive is Fine Dr. Who Exceptions are errors that are generated while a computer program is running. Such errors are called run-time errors. These types of errors

More information

Explain the relationship between a class and an object. Which is general and which is specific?

Explain the relationship between a class and an object. Which is general and which is specific? A.1.1 What is the Java Virtual Machine? Is it hardware or software? How does its role differ from that of the Java compiler? The Java Virtual Machine (JVM) is software that simulates the execution of a

More information

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007 The Java Type System By now, you have seen a fair amount of Java. Time to study in more depth the foundations of the language,

More information

COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005. Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005. Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19 Term Test #2 COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005 Family Name: Given Name(s): Student Number: Question Out of Mark A Total 16 B-1 7 B-2 4 B-3 4 B-4 4 B Total 19 C-1 4

More information

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

The following program is aiming to extract from a simple text file an analysis of the content such as:

The following program is aiming to extract from a simple text file an analysis of the content such as: Text Analyser Aim The following program is aiming to extract from a simple text file an analysis of the content such as: Number of printable characters Number of white spaces Number of vowels Number of

More information

Chapter 2 Introduction to Java programming

Chapter 2 Introduction to Java programming Chapter 2 Introduction to Java programming 1 Keywords boolean if interface class true char else package volatile false byte final switch while throws float private case return native void protected break

More information

Java Interview Questions and Answers

Java Interview Questions and Answers 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

More information

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006 Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006 Final Examination January 24 th, 2006 NAME: Please read all instructions carefully before start answering. The exam will be

More information

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasket { public static void main(string[]

More information

Java Programming Language

Java Programming Language Lecture 1 Part II Java Programming Language Additional Features and Constructs Topics in Quantitative Finance: Numerical Solutions of Partial Differential Equations Instructor: Iraj Kani Subclasses and

More information

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language Translation Translating to Java Introduction to Computer Programming The job of a programmer is to translate a problem description into a computer language. You need to be able to convert a problem description

More information

An Overview of Java. overview-1

An Overview of Java. overview-1 An Overview of Java overview-1 Contents What is Java Major Java features Java virtual machine Java programming language Java class libraries (API) GUI Support in Java Networking and Threads in Java overview-2

More information

Teach Yourself Java in 21 Minutes

Teach Yourself Java in 21 Minutes Teach Yourself Java in 21 Minutes Department of Computer Science, Lund Institute of Technology Author: Patrik Persson Contact: klas@cs.lth.se This is a brief tutorial in Java for you who already know another

More information

public static void main(string[] args) { System.out.println("hello, world"); } }

public static void main(string[] args) { System.out.println(hello, world); } } Java in 21 minutes hello world basic data types classes & objects program structure constructors garbage collection I/O exceptions Strings Hello world import java.io.*; public class hello { public static

More information

Stack Allocation. Run-Time Data Structures. Static Structures

Stack Allocation. Run-Time Data Structures. Static Structures Run-Time Data Structures Stack Allocation Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers,

More information

Sample CSE8A midterm Multiple Choice (circle one)

Sample CSE8A midterm Multiple Choice (circle one) Sample midterm Multiple Choice (circle one) (2 pts) Evaluate the following Boolean expressions and indicate whether short-circuiting happened during evaluation: Assume variables with the following names

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2015 Week 2b: Review of Week 1, Variables 16 January 2015 Birkbeck

More information

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013 Masters programmes in Computer Science and Information Systems Object-Oriented Design and Programming Sample module entry test xxth December 2013 This sample paper has more questions than the real paper

More information

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts Third AP Edition Object-Oriented Programming and Data Structures Maria Litvin Phillips Academy, Andover, Massachusetts Gary Litvin Skylight Software, Inc. Skylight Publishing Andover, Massachusetts Skylight

More information

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0 The following applies to all exams: Once exam vouchers are purchased you have up to one year from the date of purchase to use it. Each voucher is valid for one exam and may only be used at an Authorized

More information

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4 JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4 NOTE: SUN ONE Studio is almost identical with NetBeans. NetBeans is open source and can be downloaded from www.netbeans.org. I

More information

Software Construction

Software Construction Software Construction Debugging and Exceptions Jürg Luthiger University of Applied Sciences Northwestern Switzerland Institute for Mobile and Distributed Systems Learning Target You know the proper usage

More information

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime? CSCI 312 - DATA COMMUNICATIONS AND NETWORKS FALL, 2014 Assignment 4 Working as a group. Working in small gruops of 2-4 students. When you work as a group, you have to return only one home assignment per

More information

Assignment 4 Solutions

Assignment 4 Solutions CSCI 312 - DATA COMMUNICATIONS AND NETWORKS FALL, 2014 Assignment 4 Solutions Working as a pair Working in pairs. When you work as a pair you have to return only one home assignment per pair on a round.

More information

AP Computer Science Static Methods, Strings, User Input

AP Computer Science Static Methods, Strings, User Input AP Computer Science Static Methods, Strings, User Input Static Methods The Math class contains a special type of methods, called static methods. A static method DOES NOT operate on an object. This is because

More information

Software Engineering Techniques

Software Engineering Techniques Software Engineering Techniques Low level design issues for programming-in-the-large. Software Quality Design by contract Pre- and post conditions Class invariants Ten do Ten do nots Another type of summary

More information

JAVA INTERVIEW QUESTIONS

JAVA INTERVIEW QUESTIONS JAVA INTERVIEW QUESTIONS http://www.tutorialspoint.com/java/java_interview_questions.htm Copyright tutorialspoint.com Dear readers, these Java Interview Questions have been designed especially to get you

More information

Dennis Olsson. Tuesday 31 July

Dennis Olsson. Tuesday 31 July Introduction Tuesday 31 July 1 2 3 4 5 6 7 8 9 Composit are used in nearly every language. Some examples: C struct Pascal record Object Oriented - class Accessing data are containers for (one or) several

More information

Classes Dennis Olsson

Classes Dennis Olsson 1 2 3 4 5 Tuesday 31 July 6 7 8 9 Composit Accessing data are used in nearly every language. Some examples: C struct Pascal record Object Oriented - class are containers for (one or) several other values.

More information

13 File Output and Input

13 File Output and Input SCIENTIFIC PROGRAMMING -1 13 File Output and Input 13.1 Introduction To make programs really useful we have to be able to input and output data in large machinereadable amounts, in particular we have to

More information

In this Chapter you ll learn:

In this Chapter you ll learn: Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then stop. Lewis

More information

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320 CRN:22430/21519 Pace University Spring 2006 CS122/504 Computer Programming II Instructor Lectures Office Hours Dr. Lixin Tao, ltao@pace.edu, http://csis.pace.edu/~lixin Pleasantville Office: G320, (914)773-3449

More information

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved. Chapter 20 Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved. 20.1 Readers, Writers, and Streams Two ways to store data: Text

More information

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java. CS 3: Computer Programming in Java Introduction to Java CS 3: Computer Programming in Java Objectives Begin with primitive data types Create a main class with helper methods Learn how to call built-in class methods and instance methods

More information

6.1. Example: A Tip Calculator 6-1

6.1. Example: A Tip Calculator 6-1 Chapter 6. Transition to Java Not all programming languages are created equal. Each is designed by its creator to achieve a particular purpose, which can range from highly focused languages designed for

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1-5.2 self-check: #8-17 exercises: #3-6, 10, 12 videos: Ch. 5 #1-2 1 The Random class A Random object generates pseudo-random* numbers.

More information

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces. OBJECTIVES OF JAVA Objects in java cannot contain other objects; they can only have references to other objects. Deletion of objects will be managed by Run time system. An Object can pass a message to

More information

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program CS 2112 Lecture 27 Interpreters, compilers, and the Java Virtual Machine 1 May 2012 Lecturer: Andrew Myers 1 Interpreters vs. compilers There are two strategies for obtaining runnable code from a program

More information

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders 1.00 Lecture 1 Course Overview Introduction to Java Reading for next time: Big Java: 1.1-1.7 Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

More information

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard INPUT & OUTPUT I/O Example Using keyboard input for characters import java.util.scanner; class Echo{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard

More information

An Exception Monitoring System for Java

An Exception Monitoring System for Java An Exception Monitoring System for Java Heejung Ohe and Byeong-Mo Chang Department of Computer Science, Sookmyung Women s University, Seoul 140-742, Korea {lutino, chang@sookmyung.ac.kr Abstract. Exception

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

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1 Interactive Programs with Scanner reading: 3.3-3.4 1 Interactive programs We have written programs that print console

More information

Building a Multi-Threaded Web Server

Building a Multi-Threaded Web Server Building a Multi-Threaded Web Server In this lab we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous

More information

Application Programming Interface

Application Programming Interface Application Programming Interface Java Card Platform, Version 2.2.1 Sun Microsystems, Inc. 4150 Network Circle Santa Clara, California 95054 U.S.A. 650-960-1300 October 21, 2003 Java Card Specification

More information

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1 The Java Series Java Essentials I What is Java? Basic Language Constructs Slide 1 What is Java? A general purpose Object Oriented programming language. Created by Sun Microsystems. It s a general purpose

More information

3 length + 23 size = 2

3 length + 23 size = 2 This pledged exam is closed textbook, open class web site, and two pieces of paper with notes. You may not access any other code or websites including your own. Write your email id and name on every page

More information

WRITING DATA TO A BINARY FILE

WRITING DATA TO A BINARY FILE WRITING DATA TO A BINARY FILE TEXT FILES VS. BINARY FILES Up to now, we have looked at how to write and read characters to and from a text file. Text files are files that contain sequences of characters.

More information

C++ INTERVIEW QUESTIONS

C++ INTERVIEW QUESTIONS C++ INTERVIEW QUESTIONS http://www.tutorialspoint.com/cplusplus/cpp_interview_questions.htm Copyright tutorialspoint.com Dear readers, these C++ Interview Questions have been designed specially to get

More information

JAVA ARRAY EXAMPLE PDF

JAVA ARRAY EXAMPLE PDF JAVA ARRAY EXAMPLE PDF Created By: Umar Farooque Khan 1 Java array example for interview pdf Program No: 01 Print Java Array Example using for loop package ptutorial; public class PrintArray { public static

More information

Habanero Extreme Scale Software Research Project

Habanero Extreme Scale Software Research Project Habanero Extreme Scale Software Research Project Comp215: Java Method Dispatch Zoran Budimlić (Rice University) Always remember that you are absolutely unique. Just like everyone else. - Margaret Mead

More information

C# and Other Languages

C# and Other Languages C# and Other Languages Rob Miles Department of Computer Science Why do we have lots of Programming Languages? Different developer audiences Different application areas/target platforms Graphics, AI, List

More information

More on Objects and Classes

More on Objects and Classes Software and Programming I More on Objects and Classes Roman Kontchakov Birkbeck, University of London Outline Object References Class Variables and Methods Packages Testing a Class Discovering Classes

More information

Simple Java I/O. Streams

Simple Java I/O. Streams Simple Java I/O Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for data (sometimes both) An input stream may be associated with the keyboard An

More information

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program Free Java textbook available online "Thinking in Java" by Bruce Eckel, 4th edition, 2006, ISBN 0131872486, Pearson Education Introduction to the Java programming language CS 4354 Summer II 2015 The third

More information

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms. COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms 1 Activation Records activation declaration location Recall that an activation

More information