CS Notes Sem 1 24-25 PDF
Document Details
Uploaded by RejoicingRomanesque5944
Tags
Summary
This document contains computer science notes for a semester (likely). It covers topics such as primitive types, strings, input/output, and methods, which are fundamental programming concepts. The note structure is organized into units.
Full Transcript
Unit 1.1: Primitive Types - Types of data that are stored in a “box” of memory space - Primitive types: store actual values - 8 total different primitive types 1. int 2. double 3. boolean 4. char - Casting a. Widening Casting (automatic)...
Unit 1.1: Primitive Types - Types of data that are stored in a “box” of memory space - Primitive types: store actual values - 8 total different primitive types 1. int 2. double 3. boolean 4. char - Casting a. Widening Casting (automatic) b. Narrowing Casting (manual) - “==” with Primitive Types: Compares actual values. - “==” with Non-primitive Types: Compares memory addresses. - equals() with Non-primitive Types: Compares actual values/content. Unit 1.2: Strings - Non-primitive types: the box contains a pointer/memory address that points to the specific location of the object in the memory space. - Strings are immutable (its value cannot be changed) - Initialization 1. String myString = “hello”; 2. String myString = new String (“hello”); - Substring: - Used to extract a segment of the string - returns a string - myString.substring (beginning index, ending index) -> returns a new string containing all of the characters in the String starting with the character at index beginning index, and ending with the character 1 index less than the ending index - String sub=myString.substring (0,2); → sub=“he” - charAt() - Used to extract only one character from the string - returns a character - char first=myString.charAt(0); → first= ‘h’ - toLowerCase() - Turns everything in the string to lower case - returns a string - String myString = “Hello”; - String myStringLower = myString.toLowerCase(); → myStringLower= “hello” - toUpperCase() - Turns everything in the string to upper case - returns a string - String myString = “Hello”; - String myStringUpper = myString.toUpperCase(); → myStringLower= “HELLO” - contains() - Check if the string contains another string - returns a boolean - String myString = “Hello”; - boolean stringContains = myString.contains(“ello”) → stringContains=true; - boolean stringContains = myString.contains(“john”) → stringContains=false; - String to Integer - String myString= “10”; - int myInt = Integer.parseInt(myString); - Integer to String - Int myInt = 10; - String myString = “”+myInt; Unit 1.3: Scanner - A class for creating Scanner objects - Initialization - Scanner scanner = new Scanner(System.in); - - Scanner methods: - - After calling a scanner method, the program will continue when the user presses the enter or return key on the keyboard. Unit 2: Methods - What is a method? - A way to bundle functionality - A form of abstraction - simplifying functionality - An action - call or invoke a method, so something happens in response - Can be repetitively used - Method example: - Components - Return Type = int - Name = add - Parameter = int a, int b - Declaring: - Name of method should be descriptive - The type of data returned must be consistent with the return type in the declaration - If the parameters are not used within the method body, then they are useless: - - Utilize narrow casting to ensure that the data returned matches with the data type if needed: - - Widening casting is automatic, hence no need to specify: - - Data type for parameters must also be specified - Void methods are methods that won’t return any value - As soon as the program encounters a return statement, it will exit the method, hence no lines under the return statement inside the method body won’t be executed - Method signature: - Name - Parameters - - There cannot be 2 or more methods in the same class that have the same method signature - 2 or more methods in the same class with the same name and different method signatures=method overloading - Calling/Invoking: - A method can directly call another method if it exists inside the same class - Static methods cannot access non-static methods, hence the photo below is unacceptable - Unit 3: Boolean Expression & If Statements - Conditional statements interrupt the sequential execution of statements - if statements affect the flow of control by executing different statements based on the value of a Boolean expression - A one-way selection (standalone if statement) is written when there is one set of statements to execute under one certain condition - In this case, the body is executed only when the Boolean condition is true - Logical operators !(not), &&(and), and ||(or) are used with Boolean values - This represents the order these operators will be evaluated - When the result of a logical expression using && or || can be determined by evaluating only the first Boolean operand, the second is not evaluated - This is known as short-circuited evaluation - Unit 4: Iteration (Loops) - For Loops - 3 Parts of the for loop header: - Initialization of loop counter (i, j, k, etc.) - Only Executed once before the first Boolean expression evaluation - This is referred as loop control variable - Conditional Statement - A boolean expression used to determine when the loop ends → hence how many iterations the loop will run for - Increment/decrement of loop counter (++/--) - The increment/decrement happens in each iteration AFTER the body has been executed and BEFORE the boolean expression is evaluated again - Example run of the for loop above: 1. Initialize i to 0 2. Evaluate if i under what condition will the loop run/stop - Loop body: The task that gets executed as long as the boolean expression is true - If there’s no update made in each iteration that moves the conditional statement to false, the loop will become infinite and never stop. - - Nested Iterations: A loop within another loop (multiple layers) → can be used to print shapes like a triangle or output information for a 2d array etc. Unit 5: Arrays - Arrays store collections of related data - Each entity in an array is referred to as an element - - Indexes: starts with 0 - for the integer array, index 0 would be 10 - for the strings array, index 0 would be We - for the boolean array, index 0 would be false - Declaration: data type + square brackets - E.g. int [] array1, String [] arrayWords, - Arrays can only store the same type of data - Instantiation: type [] name = new type [number of elements] - Integer Arrays: int[]numbers = new int; → [0,0,0,0,0] - String Arrays: String[] myArray = new String ; → [null, null, null, null, null] - Initialization (Quick Method) - Integer: int[] numbers={1, 2, 3, 4, 5}; - String: String[] phrase = {"Let", "him", "cook"}; - Double: double[] heights = {175, 182, 163, 170} - Array Length: The number of elements in the array - e.g. int length=myArray.length; - The index of the last element is the length of the array minus 1, because the index starts at 0 - A specific element can be altered in an array directly: - myArray=3 → the 3rd elements will become 3 - myArray[myArray.length-1]=1 → the last element will become 1 - Reading and extracting elements from an array: - myArray= “Allen”; - System.out.println(myArray) → “Allen” - Traversing the elements in an array: 1. Standard For Loop: 2. Enhanced For Loop: Unit 6: Files - Creating a text file - PrintStream name of reference variable=new PrintStream(name of file); - e.g. PrintStream nums=new PrintStream(“numbersList.txt”); - *The content will be overwritten if the file already exists - Writing to a text file 1. PrintStream name of reference variable=new PrintStream(name of file); 2. name of reference variable.println(what is going to be written into the file); - e.g. - PrintStream ps = new PrintStream("goatRappers.txt"); - ps.println("Biggie Smalls"); - Reading from a text file into an Array 1. - This method loops through the file to get the total number of lines which is stored in lineCount 1. File file = new File(nameofFile) -> creates a file object -