---
# System prepended metadata

title: 'Programming I Lab 4 - Mathematical Functions, Characters, and Strings.'
tags: [IUG, Programming, Java]

---

---
description: In this lab, we are going to talk about mathematical functions and how to use them, then we will talk about the characters, and the methods that you can use in the `Character` class, finally, we will talk about strings, and the methods you can use on strings.
---
<h1 style='border: none'><center>Programming I Lab 4</center></h1>
<h2 style='border: none'><center>Mathematical Functions, Characters, and Strings.</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/10/15</span></h6>

---

<p style='text-align:justify'>
In this lab, we are going to talk about mathematical functions and how to use them, then we will talk about the characters, and the methods that you can use in the `Character` class, finally, we will talk about strings, and the methods you can use on strings.
</p>

## Lab Objectives
- Understand how to use methods.
- Be able to use mathematical functions.
- Understand the characters.
- Be able to print formatted strings.
- Be able to use `for loop`.


## Math Class

<p style='text-align:justify'>
As we’ve talked before a `class` is some kind of container that contains methods and other stuff, for the name of the class you can expect what to find inside it, for example, you will find mathematical functions inside the Math class!
</p>

You can find all the Math class methods in the official Java documentation here:
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
<p style='text-align:justify'>
Now if you’re too lazy to look at the documentation, you could return to your assigned textbook on page 174.
</p>

Example:
```java=
double angleInDegrees = 30;
double angleInRadians = Math.toRadians(angleInDegrees);
```

## Characters

<p style='text-align:justify'>
To make our program looks like a real program, we need to print normal characters for the user, now talking more about characters, there are a lot of characters that we couldn’t write in our program, something like backspace!
</p>

<p style='text-align:justify'>
Starting from what we know, that everything inside the computer is just a sequence of bytes, the characters also the same, so there is something called encoding, that matches a numeric value to a character.
</p>

<p style='text-align:justify'>
And there’s a lot of encoding methods, but the most popular ones are ASCII and Unicode, the Unicode encoding contains the ASCII encoding, so anything you could write in ASCII could be presented in Unicode.
</p>

Take a look at the ASCII table here: 

<center>

![Ascii Table](https://i.imgur.com/Z7Pm7TK.png =500x)

Ascii Table [^1]
</center>

[^1]: https://desme.rrscollege.org/extended-ascii-chart/


<p style='text-align:justify'>
Now what I mostly need you to know, that there are some characters that we couldn’t write, so the solution for this issue is to write something that tells the programming language that I want this character.
</p>

The character `\` is called the escaping character, you can write for example `\n` to print a newline character (Yes new line is a character!), and if you want to print the escaping character itself you can write `\\`, or print a double quote inside a string you can write:


```java=
System.out.println ("I will print a double quote \" and it's not the end");
```
### Character Class
<p style='text-align:justify'>
Same as Math class, but from its name, this class contains methods and codes that are related to Characters, you can find the official Java documentation here:
</p>

https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html

<p style='text-align:justify'>
You can also return to page 174 in your assigned textbook to know about some important methods inside this class.
</p>

Example:

```java=
char test =  `A`;
boolean isTestUpperCase = Character.isUpperCase(test);
```

## Strings

Strings are more than one character in one variable, there are methods inside this class, for example, the method `length` is used to get the length (number of characters) in a string, also there is the method `charAt`, which can be used to get a single character in the string.

```java=
String testString = "Hi I'm mohammed";
char secondChar = testString.charAt(1);
```

This code will print the second character of my testString, so this should print the character `i`.

You can also search in a string using other methods, for example, `indexOf` method will return the index of the string or character if it is in your string, or `-1` if it is not.

You can learn more from the docs here:

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

Return to page 152 in your assigned textbook

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

## Formatting output

It is very important to organize and format your output, so there’s a method you can use to do that easily, `System.out.printf`.

<p style='text-align:justify'>
you should give this method a string with some special characters, these characters describe another type of data, and then you provide this method with the values of this data (return to table 4.11 at page 168 in your assigned textbook).
</p>

This method will make your life much easier for printing stuff in pretty formatting.


```java=
System.out.printf ("%.2f %d %10d", 0.54321, 123, 123);
// The output will be "0.54 123        123"
```

## For loop (Extra)

In the previous lab, we've talked about while loop, the for loop is very similar to that, but it has its special syntax,  the syntax is easier for a lot of cases.

For example, if we want to print numbers from 0 to 9, using while the code will be:

```java=
int counter = 0;
while (counter < 10) {
    System.out.println(counter);
    counter++;
}
```

The syntax for `for loop` is  like this
```java=
for (Initialization; Condition; Iteration) {
    ...
}
```
`Initialization` statement will be executed one time at the beginning of the loop.
`Condition` statement will be the loop condition.
`Iteration`  statement will be executed after the end of each iteration.

So to print numbers  from 0 to 9 using for loop the code will be:

```java=
for(int i=0; i<10; i++){
    System.out.println(i);
}
```

## Lab Tasks

### Task 1: Simple Trigonometry Calculator

In this task, you need to write a program that asks the user to enter an angle in degrees, and after that, you should print the sin, cos, tan of this angle.
<center>

![Task 1 Expected Output](https://i.imgur.com/VyI5hm1.png =500x)
Task 1 Expected Output
</center>


**Note:** You should use `printf`, and print only 3 floating digits.

### Task 2: The Encryptor!

In this task,  you should create a program that encrypts text, the user should input a string, then you should add `1` to each character in this string and print it to the user,  the program can also decrypt the messages by subtracting `1` from the string.

You should know that you will use `charAt` method and loops in this task.
<center>

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


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

<center>End Of Lab 4</center>