---
# System prepended metadata

title: Programming I Lab 7 - More about Methods & Intro to Arrays
tags: [IUG, Programming, Java]

---

---
description: In this lab, we are going to talk more about methods, and the scope of variables, then we will talk about the arrays which is a collection of values.
---
<h1 style='border: none'><center>Programming I Lab 7</center></h1>
<h2 style='border: none'><center>More about Methods & Intro to Arrays</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/11/12</span></h6>

---

<p style='text-align:justify'>
In this lab, we are going to talk more about methods, and the scope of variables, then we will talk about the arrays which is a collection of values.
</p>

## Lab Objectives
- Understand the concepts of overloading.
- Be able to overload methods.
- Understand the scope of variables.
- Be able to use simple arrays.

## Methods

### Overloading Methods:
When you define a method you should define its name and the list of parameters it will take, and when you call it you should call it with all the parameters values.

But sometimes you want to give some default values, so when you call that method you will pass some parameters, and the other will be your default value.

Defining a method with the same name as other methods with a different list of parameters is called **overloading**, all you will have to do is define the method again but with a different parameters list, for example: 
```java=
public static void printHi (String firstName, String lastName) {
    System.out.println ("Hi " + firstName + " " + lastName + "!");
}
public static void printHi (String fullName) {
    System.out.println ("Hi " + fullName + "!");
}
public static void printHi () {
    System.out.println ("Hi !");
}
```

So as you see, there are three methods called printHi, but each one takes a different number of parameters, so the first one takes the first and last name, the second one takes only the full name, and the last one doesn’t take any input.

And we can call each method like this:
```java=
printHi ("Mohammed", "ALMadhoun"); // Output: Hi Mohammed ALMadhoun!
printHi ("Mohammed ALMadhoun"); // Output: Hi Mohammed ALMadhoun!
printHi (); // Output: Hi !
```

### Scope of Variables

If you understand the meaning of block, you should easily understand the concept of the scope, the variable scope is just its block, so in short, the scope of your variable is just its block,

but be aware the method could only access the variables that you’ve passed to it as a parameter.

```java=
public static void main (String[] args) {
    int x = 0;
    for (int i=0; i<10; i++){
        // This is the scope of `i`
        x += i;
        printNum (x);
    }
    // You can't use `i` here.
}
public static void printNum (int y) {
    // You can't use `x` or `i` here, but the value of `x` is in `y`
    System.out.println (y);
}
```

<div style="page-break-after: always;"></div>

## Arrays

One of the most brilliant things in programming is arrays, arrays are just a list of items, but they will let you create super useful programs.

You've already used arrays without noticing, the string type is just an array of characters, and you can get each element value using `charAt` method.

To declare a list of items, you should know the numbers of items you want to store, for example, if you want to store one hundred integer you can write:
```java=
int[] listOfInts = new int[100];
```
Notice that the type of variable is `int` but followed by `[]`, which indicates it's an array, finally, we created a space for these 100 ints by writing `new int[100]`.

Now if you want to access any element you can access it using its index (which starts from 0), so to fill the array with the value 1 we can write:
```java=
for (int i=0;i<listOfInts.length;i++) {
    listOfInts[i] = 1;
}
```
So to access the element, we write the identifier of the array followed by `[index]`, and you can use this as a regular int variable.

**Note:** don't limit your imagination, you could read some strings from a user using `Scanner`, and store these values in an array for later!

## Lab Tasks

### Task 1: Min family!
In this task, you should write a min method which could take 2, 3, or 4 int vaiables and return the minimum number, use this code to test it:
```java=
public static void main(String[] args) {
    int a = min(1, 8);            // a should be 1
    int b = min(5, 23, 13);       // b should be 5
    int c = min(44, 4, 18, 32);   // c should be 4
}
```

### Task 2: Playing with arrays.

In this task, you should create three methods:
- int[] readIntsFile (String filename)
- double getIntsAvg (int[] array)
- int getIntsMax (int[] array)
- int getIntsMin (int[] array)

The first method should read a file that contains ints (an example of the file below).
The other three methods should calculate the average, max, and min to an array of ints.

You can use the following code to complete all the methods:
```java=
public static void main(String[] args) {
    int[] intsArray = readIntsFile ("test.txt");
    System.out.printf("Max = %d\n", getIntsMax(intsArray));
    System.out.printf("Min = %d\n", getIntsMin(intsArray));
    System.out.printf("Avg = %.2f\n", getIntsAvg(intsArray));
}
public static int[] readIntsFile(String filename) {
    int[] values = null;
    try {
        Scanner fileInput = new Scanner(new File(filename));
        int count = fileInput.nextInt();
        values = new int[count];
        for(int i=0;i<count;i++){
            values[i] = fileInput.nextInt();
        }
    } catch (Exception e){
        System.out.println("Can't read file!");
        System.exit(1);
    }
    return values;
}
```

Example of `test.txt`:
```
5
100
200
231
321
122
```

###### tags: `Programming` `Java` `IUG`

<center>End Of Lab 7</center>