0% found this document useful (0 votes)
9 views21 pages

Java Basics: Variables, Methods, and Arrays

The document provides an overview of fundamental concepts in Computer Science I, including variables, constants, expressions, methods, control flow, loops, and arrays. It also includes examples of exam questions that require implementing methods to analyze arrays based on specific conditions. Key topics such as pass-by-value and the structure of arrays are highlighted throughout the recap session.

Uploaded by

createursite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

Java Basics: Variables, Methods, and Arrays

The document provides an overview of fundamental concepts in Computer Science I, including variables, constants, expressions, methods, control flow, loops, and arrays. It also includes examples of exam questions that require implementing methods to analyze arrays based on specific conditions. Key topics such as pass-by-value and the structure of arrays are highlighted throughout the recap session.

Uploaded by

createursite
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to

Computer Science I
Recap session

| Department of Advanced Computing Sciences


| Department of Advanced Computing Sciences
Basics
• Variables and constants
- Allow the use of a name to reference memory locations
- Have a
o name (follow the rules!)
o type (get familiar with the ones studied in the course)
o value (content)
o lifetime

| Department of Advanced Computing Sciences 3


Basics
• Variables and constants
• Expressions
- Types
- Precedence order
- Assignment operator
- etc.

| Department of Advanced Computing Sciences 4


Basics
• Variables and constants
• Expressions
• Casting

[Link]

| Department of Advanced Computing Sciences 5


Basics
• Variables and constants
• Expressions
• Casting
• Methods
- have a
o name
o input called “parameter(s)”
o output called “return value”
o body that describes what to do
o signature method name + parameters list (number, type and order)
| Department of Advanced Computing Sciences 6
Libraries
• Math
- Random
- Min/Max
- etc.
• Scanner
• Arrays
- copyOf
- toString
- etc.
• etc.

| Department of Advanced Computing Sciences


Control flow
• Types
- If
- If-else
- If-else if-else
- Switch case
• Lazy evaluation
• Nested conditionals

| Department of Advanced Computing Sciences


Conditions
Boolean expressions - Evaluation from left to right
- Evaluate true or false - Importance of parenthesis

1. Boolean variables
2. Relational operators
Operator Precedence in Java:
== != < <= > >= [Link]
3. Boolean operators
&& || !
4. Methods that return a Boolean value

E.g. (a != 4) && (b-5.6 <= a)

| Department of Advanced Computing Sciences


Loops
• while
• do-while
• for

• Nested loops (you will need it!)

| Department of Advanced Computing Sciences


Array Definition
A collection of data items, all of the same type, packaged under a
single name/identifier

- Like String or Scanner, it is not a basic data type but an “object”


- Like Scanner, it needs to be declared AND created

Scanner input; new <type>[<length>]


input = new Scanner([Link]);

| Department of Advanced Computing Sciences


Arrays
Solution: use variables that can store a number of values

myArray

Created
dynamically,
i.e. length can
be specified at
runtime

| Department of Advanced Computing Sciences


Arrays of arrays
Arrays are objects!
Arrays can hold objects!
= multi-dimensional arrays

numbermatrix


| Department of Advanced Computing Sciences
Iterating over arrays
• Printing the elements of an array:
- 1-D arrays (e.g. 1 2 3 )
for(int j=0;j<numColumns;j++)

1 2
- 2-D arrays (e.g. ) for(int i=0;i<numRows;i++)
3 4
for(int j=0;j<numColumns;j++)

- 3D arrays for(int k=0;k<numLayers;k++)


for(int i=0;i<numRows;i++)
for(int j=0;j<numColumns;j++)

| Department of Advanced Computing Sciences


Pass by value
• Java is pass-by-value but…
- Be careful with arrays!!!!
(and other objects in
the near future, aka CS2)
- Similar to:

| Department of Advanced Computing Sciences


(Potential) side effects!

x a

| Department of Advanced Computing Sciences


Let’s practice!

| Department of Advanced Computing Sciences


Example 1: Regular exam 2019-2020

Question 7. (15 points)


You are asked to write a method checkAllSmaller that takes as parameter one array of positive integers and returns the number of elements in the array
that are smaller than or equal to all their subsequent elements.
For example:
With array A={5,3,7,6,8,6,9}, your method should return 4. The bold & underlined elements are the ones that are smaller (or equal) than all their
subsequent elements. Note that by default we consider the last element of the array to satisfy the condition (i.e. the last element is always smaller or equal
than the element next to it).

With array B={6,7,4,3,2}, your method should return 1. Only the last element satisfies the condition here.

You do not need to do a validity check for the array, i.e. assume that it is always an array of positive integers.

public static int checkAllSmaller (int[] arr) {

| Department of Advanced Computing Sciences


Example 1.2: Regular exam 2019-2020

• What about checking if an element is the biggest so


far?
• Implement a method that returns the number of
elements in the array that are bigger than or equal to
all their previous elements.

| Department of Advanced Computing Sciences


Example 2: Regular exam 2019-2020

Question 8. (15 points)


In order to assist the Dutch government with increasing COVID-19 testing, we are helping a medical doctor implement a fast and
reliable test. The doctor explained it to us with medical terms but it was too complex, so this is how we understood what the doctor
would like to implement: We are given a 2-dimensional rectangular array MxN (where M is not necessarily equal to N) and the elements
contain integer values which represent some medical data. In this array, if there is at least one element that has a value equal to the sum
of all neighboring element values, then this is the condition that makes the test positive (otherwise it’s negative). In this context we
consider neighboring elements in all directions: top, bottom, left, right and also diagonally-top-left, diagonally-top-right, diagonally-
bottom-left, diagonally-bottom-right, so in total a maximum of 8 elements (can be less than 8 depending on the element in the array).
You are asked to write the method checkCOVID that takes as parameter a 2-dimensional array and returns true if the test is positive
(i.e. the condition above is satisfied) otherwise returns false.

| Department of Advanced Computing Sciences


Example 2: Regular exam 2019-2020

For example:

1 2 3 4
with array A= 4 𝟑𝟑𝟑𝟑 8 5 your method should return true, because two elements satisfy the described condition: Element 36 is
5 6 7 7
0 𝟏𝟏𝟏𝟏 1 13
the sum of all its 8 neighboring elements (1+2+3+4+8+5+6+7) and element 19 is the sum of all its 5 neighboring elements
(5+6+7+1+0).

1
5
6
2
with array B= 3 your method should return false because no element satisfies the condition.
7
4
8
public static boolean checkCOVID (int[][] arr) {

| Department of Advanced Computing Sciences

You might also like