Docsity

CMSC 131 CS class notes, Study notes of Computer science

Toggle sidebar
bg1
CMSC131
Spring SEMESTER 2022
Go to view-> and un-click "show print layout. That will fix a few things
Variables
A variable is a reference in memory that can store an explicitly specified type of value. The
main 8 primitive types are
● int
● double
● float
● boolean
● char
● long
● short
● byte
Wrapper classes
Wrapper classes wrap the primitive data types and provide useful methods and
functionality to work with them, such as parsing strings to primitives, converting primitives
to strings, and performing arithmetic operations on them.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45

Partial preview of the text

Download CMSC 131 CS class notes and more Study notes Computer science in PDF only on Docsity!

CMSC

Spring SEMESTER 2022

Go to view-> and un-click "show print layout. That will fix a few things

Variables

A variable is a reference in memory that can store an explicitly specified type of value. The

main 8 primitive types are

● int ● double ● float ● boolean ● char ● long ● short ● byte

Wrapper classes

Wrapper classes wrap the primitive data types and provide useful methods and

functionality to work with them, such as parsing strings to primitives, converting primitives

to strings, and performing arithmetic operations on them.

Wrapped class methods

● ** and intValue() / DoubleValue()** used to extract the int / double value from an Integer / Double Object. parseInt() / ParseDouble() used to parse a String value and return a corresponding Integer / Double value. ● ** booleanValue()** used to extract the boolean value from a Boolean object. parseBoolean() used to parse a String value and return a corresponding Boolean value. ● ** charValue()-** Used to extract the char value from a Character object. isDigit()- Returns true if the provided character is a digit. Returns false otherwise ● All common methods (Shares commonality with string methods) compareTo()- Compares Characters lexicographically (dictionary order) or integers numerically. returns -1 if the calling Object is less than the other Object. returns 0 if both are equal to each other.

Stringbuffer methods

.append() - Used to append a sequence of characters to the end of the current string in the StringBuffer .insert() - used to insert a sequence of characters into the middle of the current string .length() - Similar to array.length. Returns the amount of characters in a stringbuffer How to declare a stringbuffer

In this snippet above, If Stringbuffer a was just a regular string, putting these two strings

of text together would be impossible. Now with string buffers, we can append “ing” to our

existing fragment. At the end, the whole string says “ Talking ” with both fragments added

together.

Loops

Looping in general is an ideal way to repeat certain code blocks a known, or

unknown amount of times. They typically have an ending condition, but if they don't, they

will loop forever. It is important to spot any infinite loops, as it will crash your program over

time. The three main types of loops are While loops , Do-While loops , and For loops.

These three loops are good for specific purposes, but can achieve the same idea with

differing implementations.

● For loops:

For loops are very good for parsing through indexed data structures, like arrays as they often have a predetermined length. Often, a for loop is often used to iterate over the elements of an array. For example, let's say you have an array of integers called “myArray”. You could use a for loop to iterate over each element of the array and perform some operation on each element:,

In this example, we're using a for loop to iterate over each element of myArray. The loop

runs from i = 0 to i < myArray.length , and on each iteration, we print out the value

of myArray[i].

As demonstrated, for loops are really powerful paired with arrays because they are an efficient way to iterate through each element in an array and perform some operation on it. For loops also allow you to easily modify or manipulate the array elements as needed.

● While loops: While loops are often used in situations where you don't know ahead of time how many times you need to repeat the block of code, as the loop will continue to run until the condition is no longer true.

Method Implementation

▪ Method definition must match prototype ▪ Method name, parameter type, and return type all must match the prototype’s ▪ First return statement sends data back to the caller

Method prototype

▪ Gives compiler information about the method ▪ Sends compiler instructions on how how to interpret calls to the method ▪ Must have parameters’ data types ▪ Method Call

▪ Sends parameters to be used inside method

Method Types

Returning “Void” ▪ Void methods are for when we don’t need to get a value back from the method ▪ Returning void means no value is returned ▪ Declaration is similar to “regular” methods

This method returns nothing Return statements ▪ Transfers control back to the calling method

▪ Cannot be used in “void” methods ▪ Can be used multiple times in a method

o Static VS non Static Methods

▪ Static methods ▪ Static methods are methods that belong to a class rather than an instance of a class. This means that a static method can be called directly on the class itself, without the need to create an instance of that class Below, you can see that inside of the Example class, there is a static method.

In order to call this method, we would have to call it on the class it was created in

Static methods are often used to perform operations that do not depend on the state of an object, such as utility methods or helper methods. They can also be used to create factory methods that create instances of a class.

▪ Non-Static methods ▪ A non static method is a method that is inside a singular Instance of a class rather than to the class itself. This means that a non-static method can only be called upon an instance of a class, but not on the class itself

Passing Arrays by value

Arrays and arraylists are passed by reference, even though the reference itself is passed by value. This means that when an array is passed to a method, a reference to the original array is passed by value, but the method can use that reference to access and modify the elements of the original array directly.

In other words, the method has access to the same memory location where the original array is stored, and any changes made to the elements of the array within the method are reflected in the original array.

In this example, we create an array called myArray with four elements, and then we pass it to two methods: printArray and incrementArray.

The printArray method simply prints the elements of the array to the console.

The incrementArray method increments each element of the array by one.

When we run this code, we get the following output:

As you can see, the printArray method correctly prints the original array with values 1, 2, 3, and 4. However, after we call the incrementArray method, the array is modified in place, and the values are now 2, 3, 4, and 5. This demonstrates that the original array was modified directly by the incrementArray method, because arrays are passed by reference in Java.

Method Overloading/Overriding

Method overloading allows multiple methods in a class with the same name but different parameter lists. This means that you can define two or more methods with the same name in a class, as long as their parameter lists differ in the number, order, or types of parameters. In this case, the parameters are different but the Implementation is the same.

In the example above, the Calculator class defines three methods named add() , each with a different parameter list. The first method takes two int parameters and returns their sum, the second method takes two double parameters and returns their sum, and the third method takes three int parameters and returns their sum.

method in its superclass. Also, method overloading occurs within the same class, while method overriding occurs between a superclass and its subclass. In method overloading , methods may or may not have the same return type, while method overriding must have the same return type or a covariant return type.

Recursion

Recursion is the idea of a method calling itself. In the method definition, or implementation, it must call the same method. ● Recursive void method ○ Divide and conquer ■ The technique to break a larger task into subtasks

In the code below, we have a void method that takes in an integer, and a main method that passes 5 to it. The base case is the event where count is less than one, then it will print “Blast Off!” until then, countdown is recalled where the integer is decremented.

● Recursion VS iteration ○ In most cases, recursion is slower than iteration, however, it is a cleaner and easier to read solution ● Tail Recursion ○ A method that is tail recursive has the property that if no further computation occurs after the recursive call, the method immediately returns ○ May be done automatically by your compiler ● Recursive Algorithms

○ Binary search

The private binarySearch method is the recursive implementation of the binary search algorithm. If the low index is greater than the high index, the method returns -1 to indicate that the element was not found. Otherwise, it calculates the midpoint index and compares the value at that index to the target. If the midpoint value is equal to the target, the method returns the midpoint index. Otherwise, it recursively calls itself with a narrower range of the

o Bubble sort

o Insertion sort

Searching

o Binary search

2-D arrays

A 2d array represents a grid of values. Think of it as an array of arrays.

(1 would be our row number. Remember:Each row stores an array in its index)

However, if we wanted the amount of rows in the whole grid:

we would write: arr.length -This will also return 4

(Remember, arr stores 1 array in each index, meaning that returning its length will give us the amount of rows)

By doing this, we have established that it is a 4X4 Grid of increasing numbers

ArrayLists

An arraylist is a data structure that is used to store a collection of elements in a sequential order like arrays.

Unlike an array, ArrayLists hold references to objects, so we need to specify the type of the object that the ArrayList will store (if we’re using primitive types, then we’ll need to use the appropriate wrapper class, like Integer). They are also dynamic, meaning that they can increase and decrease their size depending on the amount of data inside it. This also means that they are mutable, unlike arrays which cannot be changed after casting. For example, Below is a regular array:

If you want to add a new element to this array, you would have to create a new array with a larger size, copy the contents of the old array into the new array, and then add the new element to the end:

In an arraylist, this process is easier and takes less memory. Here's how we create one and add our elements from our last array to this arraylist

Inside the arrayList:

Above, we initialized an arraylist that has the Integer Wrapper class. Inside, we allocated

spaces equal to the length of the previous array and added what was inside of the array before our

previous changes to illustrate the same example, but with an arraylist.

To add 6 to this array, we just simply call the .add method on the arraylist

Arraylist methods

● add() - Adds new element to the end of the list

● get() - returns a the value at the specified index

● remove() - deletes an element at the specified index

● size() - returns the length of the arraylist