---
# System prepended metadata

title: Programming I Lab 2 - Into Java & Programming
tags: [IUG, Programming, Java]

---

<h1 style='border: none'><center>Programming I Lab 2</center></h1>
<h2 style='border: none'><center>Into Java & Programming</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/01</span></h6>

---

In this lab, we are going to have an introduction about computers, programs, and programming, after that lab you should be able to:

## Lab Objectives
- Write Java programs from scratch.
- Be able to declare and use variables.
- Understand the concept of casting.
- Be able to use libraries.

## Identifiers & Variables

### Identifiers

<p style='text-align: justify'>
The purpose of high-level programming languages is to make your life easier, so don’t waste what they’ve made by stupid mistakes!
</p>
<p style='text-align: justify'>
So in any programming language, there is something called the identifier, you use the identifier simply to identify stuff in your code, in Java the first identifier you write is your class name, this could be anything allowed (return to page 62 in your textbook), then you identify your main method by calling it main!
</p>
<p style='text-align: justify'>
And inside this code you could declare variables or constants, these also have an identifier, you can use the identifier to change the value of the variable or to get it.
</p>

**Note:** you should identify your stuff according to a convention, and java has a convention, this convention is not required but it is!
It’s very important to name your identifiers correctly so it will be easier for you and other coders to read your code.

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

### Variables

<p style='text-align: justify'>
we use variables to store stuff and retrieve them, in the early days to store or retrieve something you should know the memory location of that data, but now you could declare a variable and the compiler (or runtime environment) will do the hard job for you.
</p>

```java=
datatype variableName = value;
```
- `Datatype`: it is the type of information you want to store (e.g. int, boolean, String).
- `VariableName`: it is the identifier (name) of this variable.
- `Value`: you could declare a variable without it and then assign it later, or you can initialize it with this value.

### Data Types
The datatype here means the way you look at bytes when you write or read from this variable, please return to your textbook to understand more.
In short, we could say that there are some primitive data types: `boolean`, `char`, `byte`, `short`, `int`, `long`, `float`, and `double`, these primitive data types is the lowest level of variables in Java

**Note:** that you could use pre-defined, or define your own Non-Primitive data types (e.g. `String`)!

<center>

![Java Data Types](https://i.imgur.com/YSBA1v2.png =400x)

Java Data Types
</center>

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

## If Statments & Scanner class

### if statements
<p style='text-align: justify'>
Notice that without conditions our programs will take only one path for all inputs, this might work for some computational programs, but in real life, your program will have a lot of different paths for different inputs.
</p>

So, we could use the if statements in Java to execute different paths according to our conditions:
```java=
if (condition) {
    ...
    // Condition is true
    ...
}
```

In the condition part, we could write any boolean expression we want (e.g. `x == 10`).

### Scanner class

If we want to use the if statements, we should have an input from the user because if we don't the value of that condition will be known before executing our program (So we could just remove it!).

In this section, we will use the Scanner class (Which is just a code), this code should be imported into our program to use it.

**Note:** Look here to read about it more https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

To Import this class in our code, we should write 
```java=
import java.util.Scanner
```
After importing the Scanner, we could declare a variable with the type Scanner, then use it to read the use input.
```java=
Scanner input = new Scanner(System.in);
int test = input.nextInt();
String testString = input.next();
```

The code above will read an integer from the user, then a single word.

**Note:** You should not understand every word in this code (e.g. `new`, `System.in`), we will talk about them later.

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

## Lab Tasks

### Task 1: Gas Station Accounting Software
In this task, you should implement simple accounting software for a gas station, this gas station only sells Gasoline and Diesel, the price for Gasoline is 6.25 NIS, and for Diesel is 4.5 NIS.

You should ask the acceptance to enter the type of fuel, then let him enter the number of liters.

Note that this station has a special offer, if you ordered more than 20 liters of Diesel, you got a 10 NIS discount.

Here are some examples of the program output, note that for each run I've colored the output differently
<center>

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


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

<center>End Of Lab 2</center>

