Intermediate Programming PDF
Document Details

Uploaded by EducatedElf5852
National University Clark
Tags
Summary
This document provides a lesson on intermediate programming in Java. It covers fundamental concepts such as arrays, their declaration and initialization, ways to access their elements and also the different exceptions in Java and how to handle basic errors. This helps learners solidify understanding and problem-solving skills in coding.
Full Transcript
INTERMEDIATE PROGRAMMING 2. Data organization- allows to organize related data elements into a single LESSON 1: ARRAYS entity. Arrays- a fund...
INTERMEDIATE PROGRAMMING 2. Data organization- allows to organize related data elements into a single LESSON 1: ARRAYS entity. Arrays- a fundamental data structure in 3. Iteration and processing- commonly Java that allows you to store a collection of used in loops to iterate over elements elements of the same type. and perform operations on them. Element- items that are stored in an array. 4. Versatility- can store elements of any Index- location of an element in an array, it data type. has a numerical index starting from 0. 5. Flexibility-in java, arrays have a fixed Declaration- need to specify the type of its size after initialization, but you can use elements, the array’s name, and the size of ArrayList to handle varying amounts of the array. data dynamically. Example (allocating memory): int [] 6. Widely used- foundational concept in student= new int ; many algorithms, data structures, and programming techniques Example (without initialization): int [] array; LESSON 2: JAVA EXCEPTIONS Example (with initialization): int [] array= {1,2,3,4,5}; Java Exceptions- the technical term (throw an error/exception) for when an error Example (with initialization and allocation): occurs, Java will normally stop and int [] array= new int [] {1, 2, 3, 4, 5}; generate an error message. Accessing array elements- accessed using Java try statement- allows to define a their indices block of code to be tested for errors while it Example: int [] array= {1,2,3,4,5}; is being executed. int x= array; //variable x will Catch statement- allows to define a block have the value of 3. of code to be executed if an errors occurs in the try block. For each-loop traversal- aka the enhanced for loop, a simpler way to iterate over COMPILE TIME EXCEPTIONS (CHECKED) arrays or collections. IOException- occurs when an input/output Multidimensional arrays- like a 2d array operation fails or is uninterrupted. with rows and columns Example: reading from a file that doesn’t Declaration Syntax: int [] [] matrix=new int exist. [rowSize] [columnSize]; FileNotFoundException- thrown when a file Importance of Arrays with the specified pathname does not exist. 1. Efficient storage and access- provide Example: attempting to open a file that efficient storage and retrieval of cannot be found. elements. ClassNotFoundException- thrown when an application tries to load a class through its JVM(java virtual machine) runs out of string name but no definition for the class memory. can be found. NoClassDefFoundError- thrown when the SQLException- indicates a problem when JVM tries to load a class but cannot find its interaction with a database. definition. InterruptedException- thrown when a AssertionError- thrown to indicate that an thread is waiting, sleeping, or otherwise assertion has failed. paused, and another thread interrupts it. VirtualMachineError- thrown to indicate InstantiationException- thrown when an that the JVM is broken or has run out of application tries to create an instance of a resources. class using Class.newInstance(), but the specified class object cannot be instantiated (like an abstract class). RUNTIME EXCEPTIONS (UNCHEKCED) NullPointerException- thrown when an application attempts to use a null in a case where an object is requires. ArrayIndexOutOfBoundsException- thrown when trying to access an array element LESSON 3: JAVA FUNCTIONS/METHODS with an invalid index. Functions- typically known as methods, are StringIndexOutOfBoundsException- fundamental to its structure, enabling the thrown when trying to access an invalid development of modular, reusable, and index in a string. maintainable code. NumberFormatException- thrown when Modular- breaking complex tasks into attempting to convert a string into a smaller, manageable units. numeric type. Code reusability- reducing the redundancy IndexOutOfBoundsException- indicates an by reusing code across different paths of invalid index for a list, string, or array. the program IllegalStateException- thrown when a Ease of Maintenance- simplifying method has been invoked at an debugging and updating process inappropriate time. Defining a function- function is defined UnsupportedOperationException- thrown within a class and has a return type, a when a requested operation is not name, and parameters supported. Calling a function- call it by its name using StackOverflowError- thrown when a stack an instance of the class (for non-static overflow occurs due to excessive recursion. methods) or directly from the class (for static methods). OutOfMemoryError- throw when the Java parameters- means of passing the efficiently information Conversion 1. Formal Parameters- defined in Arrays.toString(array)- converts an array functions signature and use within into human readable string function Example: int[] arr= {1,2,3}; 2. Actual Parameters- the real values provide when the function is called, they System.out.print(Arrays.toString(arr)) are a.k.a. arguments. ; Return keyword- specifies the value to be Arrays.deepToString(array)- sent by caller converts a multidimensional array into a string representation. 1. Returning Values- a method can return a value, including primitives, objects, or Example: int[][] arr= {{1,2},{3,4}}; nothing (void) System.out.print(Arrays.deepToStrin 2. Control Flow- return can also be used to g(arr)); exit a method early. Sorting Methods with no parameters- they do not Arrays.sort(array)- sorts elements of the require any arguments to be passed down array in ascending order. Use case: when called. They can perform tasks that organizing data for binary search or do not depend on external data and can be display. executed independently. Example: int [] arr= {1,2,3}; Functions with no parameters but with return values- Arrays.sort(arr); Functions with parameters and no return Arrays.parallelSort(array)- sorts large values- a.k.a. void methods, these are used arrays in parallel using multiple threads when a specific task does not require a Example: int [] arr= {1,2,3,4,5}; result to be returned. Arrays.sort(arr); Functions with parameters and return values- widely used to perform a specific operation and produce results that can be utilized in other parts of the program. This enables you to pass data to them, process Searching it, and then return the computed result Arrays.binarySearch(array,key)- back to the calling code. searches for a key (element) in a LESSON 4: JAVA BUILT IN FUNCTIONS sorted array then returns its index or a negative value if not found. Array built in functions- utility methods in java that is provided by the import Example: int [] arr= {1,2,3,4,5}; int java.util.Arrays. These functions are index=Arrays.binarySearch(arr, 3); used to manipulate and process arrays Equality- Arrays.equals(array1, array2)- checks if two one dimensional array are equal in content. Arrays.deepEquals(array1, array2)- compares two multidimensional arrays. Copying Arrays.copyOf(array, new length)- extending or truncating arrays, the extension is always 0. Arrays.copyOfRange(array, from,to) - copies a specific range of elements into a new array. Extracting subarrays. Filling Arrays.fill(array, value)- fills the entire array with a specified value Hashing Arrays.hashCode(array)- generates a hash code for the array. Stream utilities Arrays.stream(array)- converts an array into a stream for functional programming. Array Length array.length- returns the size of an array