---
# System prepended metadata

title: Programming I Lab 6 - Programming is not just a working code
tags: [IUG, Programming, Java]

---

---
description: In this lab, we are going to focus on writing code that is not just running, but easy to read, write, and edit, then we will talk about methods, which will make your code a lot easier to write as you will notice.
---
<h1 style='border: none'><center>Programming I Lab 6</center></h1>
<h2 style='border: none'><center>Programming is not just a working code</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/05</span></h6>

---

<p style='text-align:justify'>
In this lab, we are going to focus on writing code that is not just running, but easy to read, write, and edit, then we will talk about methods, which will make your code a lot easier to write as you will notice.
</p>

## Lab Objectives
- Be able to write clean code.
- Be able to indent your code.
- Be able to name identifiers according to a naming convention.
- Be able to write methods.

## Code Style

### Code Indentation

We have already talked about this topic, but it’s very important that we should mention it twice, the idea of blocks in high-level programming languages is very important.

A code block is an area that contains codes! You’ve already written a lot of blocks, for example, the body of the main method is a code block, in Java, we start the code block by writing an opening curly bracket **`{`** and we ended the block by writing a closing curly bracket **`}`**.

And as you know the code blocks could be nested (e.g. an If-block inside the main method block), now what I need you to do is whenever you open a block everything inside that block should go one level to the right (using spaces or tabs whatever you like).

<div style="position: relative; height:250px">
<div style="position: absolute; left: 0;width:60%; transform:scale(0.8); display:inline-block;transform-origin: left;">

```java=
while (a < b) {
    if (a == 5) {
        System.out.println("Test 1");
    }
    if (b - a == 1) {
        System.out.println("Test 2");
    } else {
        System.out.println("Test 3");
    }
}
```
<div style="text-align:center">Correct Indentation</div>
</div>
<div style="position: absolute; right: 0;width:60%; transform:scale(0.8); display:inline-block;transform-origin: right;">

```java=
while (a < b) {
if (a == 5) {
System.out.println("Test 1");
}
  if (b - a == 1) {
 System.out.println("Test 2");
         } else {
 System.out.println("Test 3");
}
}
```
<div style="text-align:center">Wrong Indentation</div>
</div>
</div>

### Naming Convention

In programming you will need to name a lot of things, for each thing you name, you will need to name it something very related to the real meaning of it.

The case of characters in the name is very important too to indicate the type of the thing, each programming language has its name convection.

**Naming convention for Classes:** It should be a noun starting with an upper-case character.

```java=
public class NameOfClass { }
```

**Naming convection for Methods:** It should be a verb starting with a lower-case character, and if it contains multiple words, each word after the first one should start with an upper-case character.

```java=
public int getBestStudentMark () { }
```

**Naming convention for Variables:** Same as methods, but we use nouns here.

```java=
int nameOfTheVariable = 5;
```

**Naming convention for constants:** All the character should be in upper-case, and if contains multiple words, you should separate each word using underscore.

```java=
final int NAME_OF_CONSTANTS = 18;
```

<span style="color:#e20000; font-size:10px" >**These two sections were just a revision, but for now on, I won’t mark your homework if you didn’t follow the convections.**</span>

## Methods
In this section, we are going to talk about how to write and invoke methods.

You've already written a method, and executed them without knowing, at the start of your code your write the `main` method, and you execute methods like `println`.

### Writing Methods

To write a method, you should choose a name to it (according to its job), and what parameters it should take, and what its return type.

<center>

![Define a method](https://i.imgur.com/POBOtMN.png =400x)
    
Define a method.[^1]
</center>

[^1]: Introduction to Java Programming and Data Structures by Y. Daniel Liang.

We won't talk a lot about modifiers in this lab, you should just know that we choose `public` `static` modifiers.

The return type of the method could be any type, just like variables, but also it has an extra type which is `void`, this is not actually a type, but it tells the compiler that we won't return anything.

The name of the method is just a name that we will use to execute the body of this method.

The parameters of the method are the values that the method will take, you could write methods without any parameters, a single parameter should have a type and a name.

In the body of the method, you could write any code you want, notice that the `return` keyword is used to return a value from the method (any code below the return won't be executed).

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

### Invoking a method

To invoke a method, you should just write the name of the method and pass the arguments to it, for example, to invoke the method above, we could write

```java=
int m = max(10,20);
```

### Example

In this example you will have a method that counts a single character in a string, the parameters of this method is the string, and the character you want to count, and it returns an int (which indicates the number of occurrences).

```java=
public class Example {
    public static void main (String[] args) {
        String test = "this is just a test";
        int countOfS = countCharInString(test, 's');
        System.out.println(countOfS);
        // This should print `4`
    }
    
    public static int countCharInString(String src, char c) {
        int length = src.length();
        int count = 0;
        for (int i=0;i<length;i++) {
            if (src.charAt(i) == c)
                count++;
        }
        return count;
    }
}
```

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

## Lab Tasks

### Task 1: Print that table!

In a previous homework, you were given a table that you should print, that table contains two points, and you should calculate the mid-point.

That was a hard task to define the variables for each point, and calculate each mid-point and then print each row in the table.

Now in this task, you should create a method named `printOneRow`, this method should take the two points as input, and it should return nothing (void), but don’t forget that this method should print the row! (Use printf for printing).

<center>

![Task 1 Expected Output](https://i.imgur.com/qWTaFEC.png =450x)

Task 1 Expected Output
</center>

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

### Task 2: Count the Characters

In this task, you will need to create a method that takes a string and a character as inputs, and it returns an integer that indicates the occurrence of the input character in that string. 

The whole program should ask the user for a line to enter, then you should count each character occurrence from a to z, and print the number of occurrences for each character that is above zero (Don’t forget to convert the input to lower case).

<center>

![Task 2 Expected Output](https://i.imgur.com/Jr4pcGm.png =450x)
    
Task 2 Expected Output
</center>



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

<center>End Of Lab 6</center>