Selected Sections of Applied Informatics LABORATORY 0

Size: px
Start display at page:

Download "Selected Sections of Applied Informatics LABORATORY 0"

Transcription

1 SSAI 2018Z: Class 1. Basics of Java programming. Page 1 of 6 Selected Sections of Applied Informatics LABORATORY 0 INTRODUCTION TO JAVA PROGRAMMING. BASIC LANGUAGE INSTRUCTIONS. CONSOLE APPLICATION. CONDITION, LOOP INSTRUCTIONS. PROGRAM ARGUMENTS. TABLIC

2 SSAI 2018Z: Class 1. Basics of Java programming. Page 2 of 6 I. Opening a new Java Application project in NetBeans IDE2 1. Open the NetBeans IDE environment by clicking the icon on the desktop.. 2. From the File menu, select New Project... or click the second button on the toolbar to the left.. 3. In the New Project window, select Java in the Categories: Java panel and in the Projects: Java Application panel and confirm with Next > button. 4. In the New Java Application window, fill in the dialog boxes as shown below (case sensitive): In the filed Project Name: type the name of the project Projekt1 In the filed Project Location: select the Z:\ folder as the destination of your project. Check the option: Create Main Class. Give the main project class named Projekt1 a name Klasa1 (instead of Main) by entering projekt1.klasa1 in the lower text field (projekt1 is the name of a Java class package); confirm with Finish button Remember the following generally accepted rules: name of the primary class must match the name of the file that stores its program, the class name must start with a letter and must not contain space characters, class names are usually written in capital letters. 5. In the Files window, view the structure and contents of the Netbeans project folders that were created for an empty Java Application in the Z folder: 6. Start an empty Java application with the F6 function key. After a successful compilation, a build folder will be created. In the Files window, look at its contents. Folder Projekt1\src\projekt1 already contains the source file of the project called Klasa1.java Folder build will be created after a successful compilation and will contain subfolders classes, and then project1, which will contain byte code of our application. Folder Projekt1\build\classes\projekt1 will contain a file named Klasa1.class II. The first program in Java 1. Type the following instructions into the Netbeans editor window public class Klasa1 { public static void main (String[] args) { System.out.println("Hallo in Java"); 2. Run the F11 project compilation and check for errors. 3. Run the program by pressing F6 and observe the results of the execution in the Output window. As a result, a welcome is displayed: Hallo in Java III. Arguments of the programme 1. Return to editing the source text of the program. Before the main() method's curly brackets close, declare two text variables a and b, and then assign to them the program arguments contained in two consecutive elements of the args array. The program's argument can be any character string that does not contain spaces. String a, b;

3 SSAI 2018Z: Class 1. Basics of Java programming. Page 3 of 6 a= args[0]; b= args[1]; 2. In the following instructions, display the values of the variables a and b: System.out.println(a); System.out.println(b); 3. Save your changes and restart the program. If you do not provide any argument, an execution error occurs, i.e. an exception, and the following message appears in the command window: Exception in thread "main" java.lang.arrayindexoutofboundsexception: denoting an attempt to refer to a non-existent element of the args array. Such an exceptional situation in the program should be predicted and handled, what we will do later in the exercise. 4. Define the arguments of the program. To do this, enter the Properties of the project using the pop-up menu. In the "Project Properties" dialog box, go to the [Run] tab. In the "Arguments" field, enter any integers separated by a space: Add an instruction displaying the sum of arguments a and b: System.out.print("Sum of arguments: "); System.out.println(a+b); then compile and run the program and analyse the result. IV. Display results in the Output window; constants and text variable 1. Delete the current content of the method main(). 2. Inside the main() method, type calling a method for displaying text: "Welcome to NetBeans". (If the line numbering is not displayed, you can select from the menu command View Show Line Numbers.) 3. If the instruction is incorrectly typed (e.g. case-insensitive), the following button will appear instead of the line number. Then point the mouse cursor over this button and a description of the error will appear in the explanation window. (Usually the beginning of the red toothed underline in a line indicates where the error occurred.) 4. At the end of the main() method, before the curly bracket that closes it, declare the String class object variable text and assign it a specific string (text string): String text = " The Java language was created by SUN. "; 5. In the next line add the instruction: System.out.println(text); 6. Start the program and check its operation.. V. Arguments and numeric variables; conversion of text to numbers; calculation of expressions and display of the result

4 SSAI 2018Z: Class 1. Basics of Java programming. Page 4 of 6 1. Return to program editing and continue to declare three variables k,m, n, type int, which will be used to store integers. Assign the k variable a constant value of 10. Assign the variables m and n to the first two arguments of the program using the parseint() conversion method of the Integer class.: int k = 10; int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); 2. Run the program without arguments. Because no elements are passed to the args array on the command line, an execution error (exception) will occur, represented by the class ArrayIndexOutOfBoundsException (exceeding the range of array indices). 3. To prevent exceptions that may occur when displaying non-existent arguments, place the instructions in V.1 inside the part try of instructions try-catch: try{ int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); System.out.println(m); System.out.println(n); catch (ArrayIndexOutOfBoundsException e) { System.out.println("No args"); (The program will try to display arguments in some modes, and in case of failure it will go to the catch part, which will intercept the exception of exceeding the range of the array and display a message.) 4. In the Projects window, right-click the name of the Project1 project. Then select Properties. In the Project Properties - Project1 window, in the Categories: panel, highlight the Run category. In the Arguments: field, type two integers separated by spaces as program arguments 5. Save changes and run the program. 6. Execute the program by providing text arguments instead of numeric ones. The given arguments cannot be converted to integers, which will result in an exception to the NumberFormatException class (incorrect format of the number). 7. Return to program edition. In the instruction try-catch add secont part: catch, which will handle exceptions to this class: catch (ArrayIndexOutOfBoundsException e) { System.out.println("No args"); catch (NumberFormatException e) { System.out.println("Incorrect argument! Enter two integers. "); 8. At the end of the try part, declare an integer variable named sum and assign the sum of three variables to it with the following instruction: int sum =k+ m + n; 9. In the next line add an instruction displaying the text "sum=" and the contents of the variable sum, the result of the summation: System.out.print("sum = "); System.out.println(sum); 10. Save the changes, compile the program and execute it with two integers separated by a space Below the sum, display the product of the variables k, m. n - in order to do so, go to the edition of the program, complete it with the appropriate instructions Check the operation of the program in case of correct and incorrect arguments. VI. Standard input/output: reading text from the keyboard and displaying in the Output window.t 1. In the NetBeans environment, create a new project using the File New Project menu command. In the New Project dialog box, select Java in the panel Categories: Java, and Java Application in the Projects panel. In the New Java Application window, give the project the name Projekt21 and the main class the name WeWy. As the destination of the project, select the working folder Z:\. At the beginning of the main() method, create a field representing a buffer reader that will read single lines of text from the keyboard BufferedReader we = new BufferedReader(new InputStreamReader(System.in)); 2. By command Fix Imports from the pop-up menu import BufferedReader and InputStreamReader classes from the java.io package of Java library resources. Import clauses will appear in the initial part of the program. In order to read the values of subsequent data from the keyboard, we will use the readline() of we object. Before reading the next data we will display a hint for the user. Below the reader's declaration add instructions: System.out.print("Your Name and Surname:"); String student = we.readline() ; Method readline()as an input/output operation may generate class execution errors (exceptions) IOException so a compilation error occurs: unreported exception java.io.ioexception; must be caught or declared to be thrown. Click on the error button and select Add throws clause for java.io.ioexception This will result addition to the header of the method main() frphrase throws IOException and transferring the handling of this error to the system.

5 SSAI 2018Z: Class 1. Basics of Java programming. Page 5 of 6 3. Start the program, then click to move the cursor to the Output window and enter your data, ending it with the ENTER key.. 4. Enter the album number and average note as the next data; these will be the values of two numeric variables - total and real.. int Index = Integer.parseInt(we.readLine()); float note = Float.parseFloat(we.readLine()); 5. Before each instruction to read the data, add an instruction that displays a hint for the user. 6. Add an instruction to display the entered data. Start the program. Observe the effect of incorrect entry of an album number or rating (e.g. letter instead of a number, comma instead of a dot). 7. To handle an exception generated by an erroneous entry, enclose the following instructi try-catch the instructions for converting text into numbers assigned to variables in point I.6 Index and note. In part catch recognize the class exception NumberFormatException and add an instruction that will display a message " incorrect number format". Check the operation of this exception. VII. Assignment instructions and data types. Data conversion. Arithmetic operations. Use of mathematical functions. 1. In the next example we will program calculations for numbers loaded from the keyboard. We make them in a new project, created as a copy of the project Projekt21. In the Projects window, right-click the Projekt21 name and select Copy from the popup menu. In the dialog box, enter the name of the new Project22 projectexpand the folders of a new project and change the name of the main class to Calculations, by clicking on the class name WeWy with the right mouse button and by selecting the following commands Refactor Rename. Double-click on the name of the Calculation class, open it in the NetBeans editor and modify it. 2. Delete the instruction to read the name and change the instruction displaying these data to the following (enter your data): System.out.println("Program author: xxxx yyyyyyyy"); 3. Change the instructions for reading the album number and rating to reading two integers (type int), modify the hints before reading each of the data: int a = Integer.parseInt(we.readLine()); int b = Integer.parseInt (we.readline()); 4. In the following lines display the sum, difference, product and quotient of numbers a and b: System.out.println("arithmetic operations on numbers " +a+ "and " +b); System.out.println("sum ="+ (a+b)); System.out.println("differential ="+ (a-b)); System.out.println("multiplication ="+a*b); System.out.println("quotient ="+a/b); 5. Where two integers are subdivided, provision must be made for the situation of subdivision by zero, which gives rise to a class exception. ArithmeticExceptionTo handle it, add after an existing phrase catch another phrase catch the character: catch(arithmeticexception e) {System.out.println("Calculation error!"); 6. Run the program and check its operation 7. Then change the type of variables a and b to the real type (double), replacing the instructions in point 3 as follows: double a = Double.parseDouble(we.readLine()); double b = Double.parseDouble(we.readLine()); 8. Run the program and check its operation 9. Inside block try... catch add further instructions to illustrate the use of built-in mathematical functions: System.out.println("square root from the number a = "+Math.sqrt(a)); System.out.println("3rd power of numbers b = "+Math.pow(b,3)); System.out.println("sinus lof number b = "+Math.sin(b)); System.out.println("Rounding of a number = "+Math.round(3.234)); System.out.println("Rounding of a number = "+Math.round(3.654)); 10. Then calculate and display the distance of the point with coordinates (a, b) from the point (-1, 1) by yourself. Check the operation of the program. VIII. Iterative sum calculation; entering, summing up and displaying program arguments, example of a conditional if instruction 1. Create another new project of the Java Application type, give the project a name Projekt23 and the main class - the name Sum. As a project destination, select a working folder Z:\

6 SSAI 2018Z: Class 1. Basics of Java programming. Page 6 of 6 2. In the content of the method main() declare the variable sum of the total type and give it its initial value 0 by means of an instruction: int sum = 0; 3. Then use the for loop to calculate the sum of the natural numbers in the range 1, 10 : for (int i = 1; i <=10; i++) { sum +=i; In the following iteration steps, the value of the variable and loop control will change from 1 (int i declaration = 1;) to 10 (condition and <= 10;) with step 1 (i++ increment operation). 4. Add an instruction displaying the calculated sum, with the comment "The sum of numbers from 1 to 10 is ". Start the program and check the displayed summation result. 5. The next task of the program will be to display the arguments of the program and calculate the sum of arguments, which are integers. Enter the program arguments by entering several integers separated by spaces in the line Arguments (command Properties Run). 6. Add an instruction displaying the number of entered program arguments in text form: "Programme has"+ args.length +" arguments" (the args.length property specifies the number of arguments) 7. Then display the values of these arguments in the for: for (int k = 0; k<args.length; k++) { System.out.println((k+1) + ": " + args[k]); 8. Correct possible errors and start the program. 9. Add another for instruction to iteratively calculate the sum of all arguments of the program that are integers: for (int i = 0; i <args.length; i++) { // i change with respect to the indexes of the array args m = Integer.parseInt(args[i]); // value of i-element assigned to a variable m sum +=m; // to add another argument to the sum Remember to clear the variable sum and declare the variable m first. Note: The args.length property specifies the length of the args array, which is the current number of arguments it contains. 10. Then add an instruction displaying the calculated sum of arguments with an appropriate comment. 11. Right-click in the code editor window and from the popup menu, select Format (or use the keyboard shortcut <Alt+Shift+F>) to align your instructions. 12. Then we will use the conditional if instruction to check if the number of arguments is even or not. int t = args.length; if( t % 2 == 0 ) System.out.println("The given number of arguments is even."); else System.out.println("The given number of arguments is odd."); 13. Correct errors and run the program.

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

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the Description Exception Handling in Java An Exception is a compile time / runtime error that breaks off the program s execution flow. These exceptions are accompanied with a system generated error message.

More information

Lesson 3: Accepting User Input and Using Different Methods for Output

Lesson 3: Accepting User Input and Using Different Methods for Output Lesson 3: Accepting User Input and Using Different Methods for Output Introduction So far, you have had an overview of the basics in Java. This document will discuss how to put some power in your program

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Data Types and the while Statement

Data Types and the while Statement Session 2 Student Name Other Identification Data Types and the while Statement In this laboratory you will: 1. Learn about three of the primitive data types in Java, int, double, char. 2. Learn about the

More information

C16b: Exception Handling

C16b: Exception Handling CISC 3120 C16b: Exception Handling Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/28/2018 CUNY Brooklyn College 1 Outline Exceptions Catch and handle exceptions (try/catch)

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5. Page 1 of 5 6.170 Laboratory in Software Engineering Java Style Guide Contents: Overview Descriptive names Consistent indentation and spacing Informative comments Commenting code TODO comments 6.170 Javadocs

More information

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library

A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library HSA to Core Java A Guide Illustrating the Core Java Equivalent to Selected Tasks Done Using the HSA Class Library The examples below compare how tasks are done using the hsa Console class with how they

More information

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Introduction Unit 4: Input, output and exceptions

Introduction Unit 4: Input, output and exceptions Faculty of Computer Science Programming Language 2 Object oriented design using JAVA Dr. Ayman Ezzat Email: ayman@fcih.net Web: www.fcih.net/ayman Introduction Unit 4: Input, output and exceptions 1 1.

More information

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos. Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose

More information

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples

Recursion. General Algorithm for Recursion. When to use and not use Recursion. Recursion Removal. Examples Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive Solutions Exercises Unit 19 1 General Algorithm for Recursion

More information

Input from Files. Buffered Reader

Input from Files. Buffered Reader Input from Files Buffered Reader Input from files is always text. You can convert it to ints using Integer.parseInt() We use BufferedReaders to minimize the number of reads to the file. The Buffer reads

More information

Lecture 11.1 I/O Streams

Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 1 OBJECT ORIENTED PROGRAMMING Lecture 11.1 I/O Streams 21/04/2014 Ebtsam AbdelHakam 2 Outline I/O Basics Streams Reading characters and string 21/04/2014 Ebtsam AbdelHakam

More information

( &% class MyClass { }

( &% class MyClass { } Recall! $! "" # ' ' )' %&! ( &% class MyClass { $ Individual things that differentiate one object from another Determine the appearance, state or qualities of objects Represents any variables needed for

More information

WOSO Source Code (Java)

WOSO Source Code (Java) WOSO 2017 - Source Code (Java) Q 1 - Which of the following is false about String? A. String is immutable. B. String can be created using new operator. C. String is a primary data type. D. None of the

More information

Workbook 5. Remember to check the course website regularly for announcements and errata: Managing errors with Java Exceptions

Workbook 5. Remember to check the course website regularly for announcements and errata: Managing errors with Java Exceptions Introduction Workbook 5 The implementation of PatternLife you wrote last week is brittle in the sense that the program does not cope well when input data is malformed or missing. This week you will improve

More information

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file. First Program (02) Creating a Java program and understanding the basic concepts. Creating a Program in JCreator It is usually a good idea to create a folder where you re going to save your Java programs.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of February 23, 2013 Abstract Handling errors Declaring, creating and handling exceptions

More information

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java Organisational issues Inf1-OOP Course Overview Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2014 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

Programming - 2. Common Errors

Programming - 2. Common Errors Common Errors There are certain common errors and exceptions which beginners come across and find them very annoying. Here we will discuss these and give a little explanation of what s going wrong and

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of February 23, 2013 Abstract Handling errors Declaring, creating and handling exceptions

More information

Full file at Chapter 2 - Inheritance and Exception Handling

Full file at   Chapter 2 - Inheritance and Exception Handling Chapter 2 - Inheritance and Exception Handling TRUE/FALSE 1. The superclass inherits all its properties from the subclass. ANS: F PTS: 1 REF: 76 2. Private members of a superclass can be accessed by a

More information

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 7 problems on the following 6 pages. You may use your single- side hand- written 8 ½ x 11 note sheet during the

More information

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit ICOM 4015 Advanced Programming Laboratory Chapter 1 Introduction to Eclipse, Java and JUnit University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís 1 Introduction This

More information

Java Exception Handling

Java Exception Handling Java Exception Handling Handling errors using Java s exception handling mechanism Approaches For Dealing With Error Conditions Use branches/decision making and return values Use Java s exception handling

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

Full file at

Full file at Chapter 1 Primitive Java Weiss 4 th Edition Solutions to Exercises (US Version) 1.1 Key Concepts and How To Teach Them This chapter introduces primitive features of Java found in all languages such as

More information

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output Manfred Kerber www.cs.bham.ac.uk/~mmk 21 October 2015 1 / 18 Manfred Kerber Classes and Objects The information

More information

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations Topic 6: Exceptions Exceptions are a Java mechanism for dealing with errors & unusual situations Goals: learn how to... think about different responses to errors write code that catches exceptions write

More information

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

Inf1-OP. Course Overview. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics Inf1-OP Course Overview Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics February 26, 2018 Administrative Stuff Who to contact for help? Lecturer: Volker

More information

Reading Input from Text File

Reading Input from Text File Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 5 Reading Input from Text File Eng. Mohammed Alokshiya November 2, 2014 The simplest

More information

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O Week 12 Streams and File I/O Overview of Streams and File I/O Text File I/O 1 I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or a file

More information

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

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: 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: Electrical and Computer Engineering building

More information

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens Organizational Issues Inf1-OOP Course Overview Ewan Klein, Perdita Stevens School of Informatics January 12, 2013 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point Numbers Type Conversion

More information

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide CS1092: Tutorial Sheet: No 3 Exceptions and Files Tutor s Guide Preliminary This tutorial sheet requires that you ve read Chapter 15 on Exceptions (CS1081 lectured material), and followed the recent CS1092

More information

For more details on SUN Certifications, visit

For more details on SUN Certifications, visit Exception Handling For more details on SUN Certifications, visit http://sunjavasnips.blogspot.com/ Q: 01 Given: 11. public static void parse(string str) { 12. try { 13. float f = Float.parseFloat(str);

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 17 January 2019 SP1-Lab1-2018-19.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1 Module Information Lectures: Afternoon

More information

Review of Basic Java

Review of Basic Java Review of Basic Java 1 Background The course is a follow-on to your first programming course in Java. It is assumed you are already familiar with the basic elements of Java (such as Console, print, int,

More information

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly

More information

Arrays. 1 Index mapping

Arrays. 1 Index mapping Arrays 1 Index mapping You are expected already to have a good understanding of arrays. We examine some array techniques in more detail, beginning with index mapping. Index mapping is a technique for compact

More information

C17: File I/O and Exception Handling

C17: File I/O and Exception Handling CISC 3120 C17: File I/O and Exception Handling Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/24/2017 CUNY Brooklyn College 1 Outline Recap and issues Exception Handling

More information

Excep&ons and file I/O

Excep&ons and file I/O Excep&ons and file I/O Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458)

More information

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150,

COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE. Instructor: Prasun Dewan (FB 150, COMP 110/401 APPENDIX: INSTALLING AND USING ECLIPSE Instructor: Prasun Dewan (FB 150, dewan@unc.edu) SCOPE: BASICS AND BEYOND Basic use: CS 1 Beyond basic use: CS2 2 DOWNLOAD FROM WWW.ECLIPSE.ORG Get the

More information

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon Who and what can help? Inf1-OP Lecturer: Timothy Hospedales TA: Natalia Zon Course Overview Web: http://www.inf.ed.ac.uk/teaching/ courses/inf1/op/ Timothy Hospedales, adapting earlier version by Perdita

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Java lecture (10.1) Exception Handling 1 Outline Exception Handling Mechanisms Exception handling fundamentals Exception Types Uncaught exceptions Try and catch Multiple catch

More information

Java Exception Handling

Java Exception Handling Java Exception Handling Handling errors using Java s exception handling mechanism Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java s exception handling

More information

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

Exception in thread main java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling. Exceptions 1 Handling exceptions A program will sometimes inadvertently ask the machine to do something which it cannot reasonably do, such as dividing by zero, or attempting to access a non-existent array

More information

Simple Java Reference

Simple Java Reference Simple Java Reference This document provides a reference to all the Java syntax used in the Computational Methods course. 1 Compiling and running... 2 2 The main() method... 3 3 Primitive variable types...

More information

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 6 Loops

More information

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions Introductory Programming Exceptions and I/O: sections 80 83 Anne Haxthausen a IMM, DTU 1 Exceptions (section 80) 2 Input and output (I/O) (sections 81-83) a Parts of this material are inspired by/originate

More information

Introductory Programming Exceptions and I/O: sections

Introductory Programming Exceptions and I/O: sections Introductory Programming Exceptions and I/O: sections 80 83 Anne Haxthausen a IMM, DTU 1 Exceptions (section 80) 2 Input and output (I/O) (sections 81-83) a Parts of this material are inspired by/originate

More information

EXCEPTIONS. Fundamentals of Computer Science I

EXCEPTIONS. Fundamentals of Computer Science I EXCEPTIONS Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458)

More information

Advanced Computer Programming

Advanced Computer Programming Programming in the Small I: Names and Things (Part II) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University

More information

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166 Lecture 20 Java Exceptional Event Handling Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Topics What is an Exception? Exception Handler Catch or Specify Requirement Three Kinds of Exceptions

More information

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002

Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002 Adding Existing Source Code in NetBeans CS288, Autumn 2005 Lab 002 Purpose This document will show how to incorporate existing source code within a NetBeans project. It will also introduce the concept

More information

C++ Support Classes (Data and Variables)

C++ Support Classes (Data and Variables) C++ Support Classes (Data and Variables) School of Mathematics 2018 Today s lecture Topics: Computers and Programs; Syntax and Structure of a Program; Data and Variables; Aims: Understand the idea of programming

More information

Express Yourself. What is Eclipse?

Express Yourself. What is Eclipse? CS 170 Java Programming 1 Eclipse and the for Loop A Professional Integrated Development Environment Introducing Iteration Express Yourself Use OpenOffice or Word to create a new document Save the file

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

2 Getting Started. Getting Started (v1.8.6) 3/5/2007 2 Getting Started Java will be used in the examples in this section; however, the information applies to all supported languages for which you have installed a compiler (e.g., Ada, C, C++, Java) unless

More information

Programming Problems 15th Annual Computer Science Programming Contest

Programming Problems 15th Annual Computer Science Programming Contest Programming Problems 15th Annual Computer Science Programming Contest Department of Mathematics and Computer Science Western Carolina University March 0, 200 Criteria for Determining Scores Each program

More information

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371

e) Implicit and Explicit Type Conversion Pg 328 j) Types of errors Pg 371 Class IX HY 2013 Revision Guidelines Page 1 Section A (Power Point) Q1.What is PowerPoint? How are PowerPoint files named? Q2. Describe the 4 different ways of creating a presentation? (2 lines each) Q3.

More information

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What

More information

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

CS 251 Intermediate Programming Java I/O Streams

CS 251 Intermediate Programming Java I/O Streams CS 251 Intermediate Programming Java I/O Streams Brooke Chenoweth University of New Mexico Spring 2018 Basic Input/Output I/O Streams mostly in java.io package File I/O mostly in java.nio.file package

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Chapter 5 Some useful classes

Chapter 5 Some useful classes Chapter 5 Some useful classes Lesson page 5-1. Numerical wrapper classes Activity 5-1-1 Wrapper-class Integer Question 1. False. A new Integer can be created, but its contents cannot be changed. Question

More information

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

3. NetBeans IDE 6.0. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 3. NetBeans IDE 6.0 Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Installing the NetBeans IDE First NetBeans IDE Project IDE Windows Source Editor Customizing the IDE References Installing the

More information

Input & Output in Java. Standard I/O Exception Handling

Input & Output in Java. Standard I/O Exception Handling Input & Output in Java Standard I/O Exception Handling Java I/O: Generic & Complex Java runs on a huge variety of plaforms to accomplish this, a Java Virtual Machine (JVM) is written for every type of

More information

VARIABLES, DATA TYPES,

VARIABLES, DATA TYPES, 1-59863-275-2_CH02_31_05/23/06 2 C H A P T E R VARIABLES, DATA TYPES, AND SIMPLE IO In this chapter, you learn how to use variables, data types, and standard input/output (IO) to create interactive applications.

More information

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

1. An operation in which an overall value is computed incrementally, often using a loop. Practice Exam 2 Part I: Vocabulary (10 points) Write the terms defined by the statements below. 1. An operation in which an overall value is computed incrementally, often using a loop. 2. The < (less than)

More information

Byte and Character Streams. Reading and Writing Console input and output

Byte and Character Streams. Reading and Writing Console input and output Byte and Character Streams Reading and Writing Console input and output 1 I/O basics The io package supports Java s basic I/O (input/output) Java does provide strong, flexible support for I/O as it relates

More information

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS) GETTING STARTED: YOUR FIRST JAVA APPLICATION 15 3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS) GETTING STARTED: YOUR FIRST JAVA APPLICATION Checklist: The most recent version of Java SE Development

More information

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

Introduction. Key features and lab exercises to familiarize new users to the Visual environment Introduction Key features and lab exercises to familiarize new users to the Visual environment January 1999 CONTENTS KEY FEATURES... 3 Statement Completion Options 3 Auto List Members 3 Auto Type Info

More information

Java Exception Handling

Java Exception Handling Java Exception Handling Handling errors using Java s exception handling mechanism Approaches For Dealing With Error Conditions Use branches/decision making and return values Use Java s exception handling

More information

1. Download the JDK 6, from

1. Download the JDK 6, from 1. Install the JDK 1. Download the JDK 6, from http://java.sun.com/javase/downloads/widget/jdk6.jsp. 2. Once the file is completed downloaded, execute it and accept the license agreement. 3. Select the

More information

Eng. Mohammed S. Abdualal

Eng. Mohammed S. Abdualal Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 3 Selections

More information

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

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors

More information

Principles of Computer Science I

Principles of Computer Science I Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120A - Fall 2004 Lecture Unit 7 Review Chapter 4 Boolean data type and operators (&&,,) Selection control flow structure if, if-else, nested

More information

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Lab 5: Java IO 12:00 PM, Feb 21, 2018 CS18 Integrated Introduction to Computer Science Fisler, Nelson Contents Lab 5: Java IO 12:00 PM, Feb 21, 2018 1 The Java IO Library 1 2 Program Arguments 2 3 Readers, Writers, and Buffers 2 3.1 Buffering

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Java Exception Handling

Java Exception Handling Java Exception Handling Handling errors using Java s exception handling mechanism Approaches For Dealing With Error Conditions Use branches/decision making and return values Use Java s exception handling

More information

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output COMP 213 Advanced Object-oriented Programming Lecture 19 Input/Output Input and Output A program that read no input and produced no output would be a very uninteresting and useless thing. Forms of input/output

More information

BBM 102 Introduction to Programming II Spring Exceptions

BBM 102 Introduction to Programming II Spring Exceptions BBM 102 Introduction to Programming II Spring 2018 Exceptions 1 Today What is an exception? What is exception handling? Keywords of exception handling try catch finally Throwing exceptions throw Custom

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ CALLING & DEFINING FUNCTIONS 2 Functions and

More information

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions COMP 213 Advanced Object-oriented Programming Lecture 17 Exceptions Errors Writing programs is not trivial. Most (large) programs that are written contain errors: in some way, the program doesn t do what

More information

Some Patterns for CS1 Students

Some Patterns for CS1 Students Some Patterns for CS1 Students Berna L. Massingill Abstract Students in beginning programming courses struggle with many aspects of programming. This paper outlines some patterns intended to be useful

More information

1 Ctrl + X Cut the selected item. 2 Ctrl + C (or Ctrl + Insert) Copy the selected item. 3 Ctrl + V (or Shift + Insert) Paste the selected item

1 Ctrl + X Cut the selected item. 2 Ctrl + C (or Ctrl + Insert) Copy the selected item. 3 Ctrl + V (or Shift + Insert) Paste the selected item Tips and Tricks Recorder Actions Library XPath Syntax Hotkeys Windows Hotkeys General Keyboard Shortcuts Windows Explorer Shortcuts Command Prompt Shortcuts Dialog Box Keyboard Shortcuts Excel Hotkeys

More information

Introduction to C/C++ Programming

Introduction to C/C++ Programming Chapter 1 Introduction to C/C++ Programming This book is about learning numerical programming skill and the software development process. Therefore, it requires a lot of hands-on programming exercises.

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

CSEN 202 Introduction to Computer Programming

CSEN 202 Introduction to Computer Programming CSEN 202 Introduction to Computer Programming Lecture 4: Iterations Prof. Dr. Slim Abdennadher and Dr Mohammed Abdel Megeed Salem, slim.abdennadher@guc.edu.eg German University Cairo, Department of Media

More information

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern School of Computer Science, University of Birmingham. Java Lecture notes. M. D. Ryan. September 2001. A very simple program Week 2: variables & expressions Variables, assignments, expressions, and types.

More information

After completing this appendix, you will be able to:

After completing this appendix, you will be able to: 1418835463_AppendixA.qxd 5/22/06 02:31 PM Page 879 A P P E N D I X A A DEBUGGING After completing this appendix, you will be able to: Describe the types of programming errors Trace statement execution

More information

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Name: Covers Chapters 1-3 50 mins CSCI 1301 Introduction to Programming Armstrong Atlantic State University Instructor: Dr. Y. Daniel Liang I pledge by honor that I will not discuss this exam with anyone

More information

4 WORKING WITH DATA TYPES AND OPERATIONS

4 WORKING WITH DATA TYPES AND OPERATIONS WORKING WITH NUMERIC VALUES 27 4 WORKING WITH DATA TYPES AND OPERATIONS WORKING WITH NUMERIC VALUES This application will declare and display numeric values. To declare and display an integer value in

More information

Lecture 4: Exceptions. I/O

Lecture 4: Exceptions. I/O Lecture 4: Exceptions. I/O Outline Access control. Class scope Exceptions I/O public class Malicious { public static void main(string[] args) { maliciousmethod(new CreditCard()); } static void maliciousmethod(creditcard

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

More information