0% found this document useful (0 votes)
10 views

STUDY INDIVIDUAL ASSIGNMENT

Uploaded by

ibn moussa
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)
10 views

STUDY INDIVIDUAL ASSIGNMENT

Uploaded by

ibn moussa
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
You are on page 1/ 5

NLAB INNOVATION ACADEMY

P.O. Box 105758, Dar Es Salaam, Tanzania.

DIPLOMA IN INFORMATION
COMMUNICATION TECHNOLOGY

MODULE NAME: Object Oriented Programming


(OOP)
MODULE CODE: 1 ITT 06102
NATURE OF TASK: INDIVIDUAL TASK
SUBMISION DATE: 11/11/2024
Name : ALLY SADIKI

Question:
1. What is a variable?
2. Explain the rules and conventions for naming variables in Java.
3. List and briefly explain all the primitive data types in Java. Provide examples of each data type.
4. What is the difference between declaring a variable and initializing it? Provide an example to illustrate
the difference.
5. List all arithmetic operators available in Java and explain their functions with examples.
6. Describe the purpose of relational operators in Java. Give examples of how they are used. 7. Explain the
logical operators (&&, |, !) with examples of their use.
8. Explain how assignment operators work, providing examples.
9. What is type casting in Java?
10.Explain the difference between implicit and explicit type casting. Give examples of each type of
casting.
11.Explain how string concatenation works in Java. Provide an example of concatenating two strings.
12.What is the difference between int and double data types?
13.Why would you use a boolean data type in a program?What is the output of the following expression: 5
+ 3 * 2? Explain the result.
14.If a variable is declared as final int MAX = 100;, what does the final keyword indicate? Can the value
of MAX be changed later? Why or why not?
15.What happens if you try to divide an integer by zero in Java? Explain the result.
16.Why is it important to understand data types and operators when writing Java programs? 17.How does
incorrect use of data types and operators lead to errors in your code?
1.What is a variable?
A variable can be thought of as a labeled box in which you store data that your program
will use or change while it is running. Each such box has a name, a type and a value;
each variable does, too. Variables are a necessity because they enable us to provide a
way of organizing and tracking information in our programs.

2. Rules and Conventions for Naming Variables in Java


Rules (what you must follow):

Variable names must start with letter, $, or _.


They can contain numbers, but only after the first character.
Java will stop you naming variables names reserved words class, int, public etc
Variable names are case sensitive: myVar and myvar are two different variables.
Conventions (what is considered good practice but not enforced):

Use camelCase where every word, except the first, starts with a capital letter; such as
totalScore or isAvailable. Use names that indicate what your program is doing.

3. Primitive Data Types in Java Java has eight "primitive" types that are designed to hold
specific kinds of data:
byte: Smallest integer; range is -128 to 127. Example: byte age = 25; short: Slightly bigger
integer; range is -32,768 to 32,767. Example: short distance =2000;
int: Standard whole number; range is approximately -2 billion to 2 billion.
Examples: int population = 1000000;

long: Very large whole number; usage when it is out of int range.
Examples: long distanceToMoon = 384400000L;

float: Decimal number with lesser precision.


Examples: float price = 19.99f;

double: Decimal number with higher precision.


Examples: double weight = 70.5;

char: Single character, such as a letter or symbol.


Examples: char grade = 'A';

boolean: The only values this holds are true or false.


Examples: boolean isAvailable = true;
4. Declaring vs. Initializing a Variable
Declaring: Telling Java that a variable exists and giving it a type and a name, but not a
value yet.
int score; // Just declares that "score" is an integer

Initializing: Actually giving that variable a value.


score = 100; // Now we’ve assigned it a value of 100

Or you can do both at once:


int score = 100; // Declares and gives it a value right away

5. Arithmetic Operators in Java


Java includes the standard math operators we’re used to, like +, -, *, /, and %. Here’s
what each one does:
1. + (Addition): Adds two numbers.
int total = 10 + 5; // 15

2. - (Subtraction): Subtracts one number from another.


int result = 10 - 3; // 7

3. * (Multiplication): Multiplies two numbers.


int area = 4 * 2; // 8

4. / (Division): Divides one number by another.


int quotient = 10 / 2; // 5

5. % (Modulus): Returns the remainder after division.


int remainder = 10 % 3; // 1

6. Purpose of Relational Operators in Java


Relational operators help compare two values and return either true or false, which is
helpful when making decisions in our code:
==: Checks if two values are equal.
!=: Checks if two values are not equal.
>: Checks if one value is greater than another.
<: Checks if one value is less than another.
>=: Checks if one value is greater than or equal to another.
<=: Checks if one value is less than or equal to another.

Example:
int age = 20;
boolean isAdult = age >= 18; // true, since 20 is greater than 18
7. Logical Operators (&&, ||, !)
1. && (AND): True if both conditions are true.
boolean canVote = (age >= 18) && (citizen == true);

2. || (OR): True if at least one condition is true.


boolean openOrClosed = (time < 9) || (time > 17);

3. ! (NOT): Flips the value; true becomes false, and vice versa.
boolean notWeekend = !(isWeekend); // true if isWeekend is false

9. Type Casting in Java


Type casting means converting a variable from one type to another. For example, you
might want to turn an int into a double or vice versa.

10. Implicit vs. Explicit Type Casting


• Implicit Casting: Java automatically converts smaller types to larger types, like
from int to double.
int count = 10;
double countInDouble = count; // Automatically becomes 10.0

• Explicit Casting: You manually convert larger types to smaller types, like from
double to int.
double price = 9.99;
int priceInt = (int) price; // Manually turns 9.99 into 9

11. String Concatenation in Java


In Java, you can combine, or “concatenate,” strings with the + operator.
String fi rstName = "Ally";
String lastName = "Sadiki";
String fullName = fi rstName + " " + lastName; // "Ally Sadiki"

13. Purpose of boolean and the Expression 5 + 3 * 2


boolean: Useful for storing simple true or false values, perfect for conditions incode.
Expression: 5 + 3 * 2
Multiplication happens before addition, so it’s 5 + (3 * 2), which equals 5 + 6, giving 11.

14. Meaning of final int MAX = 100;


When you declare a variable with final, it becomes a constant, meaning it cannot be
changed later in the code. In this example, MAX will always be 100.

15. Division by Zero with Integers in Java


Dividing by zero in Java with integers causes an error called ArithmeticException, as
dividing by zero is undefined in math.
16. Why Understanding Data Types and Operators is Important
Knowing data types and operators helps you write code that works properly. You’ll
know exactly how to store data and which operations are allowed, making your code
efficient and error-free.

17. How Misusing Data Types and Operators Causes Errors


Using the wrong type or operator can lead to different types of errors:
• Syntax errors if the code doesn’t make sense to Java (e.g., dividing by zero).
• Runtime errors if something goes wrong when the program runs.
• Logic errors if the program runs but doesn’t give the right result.

You might also like