ASSIGNME
NT
Name: Maryam Balarabe Adamu
Reg.No: NID/CSE/21/005
Course: Introduction to OOP
using Java
Department: Software
Engineering
Semester: 4th Semester
Date: 16th September 2024
2a) Write a Java Program that will calculate:
I. the area of a circle where area = pi * radius ii. circumference
of circle where circumference = 2 * pi * Radius
Assume radius = 3
public class CircleCalculator {
public static void main(String[] args) {
double radius = 3.0;
double pi = Math.PI;
double area = pi * radius * radius;
double circumference = 2 * pi * radius;
System.out.println("Area of the circle: " + area);
System.out.println("Circumference of the circle: " +
circumference);
}
}
3a) Write a Java program that will calculate the sum of three
integer number.b) List 4 data types supported by Java Program
3a)
public class SumOfThreeNumbers {
public static void main(String[] args) {
int num1 = 10;
int num2 = 15;
int num3 = 20;
int sum = num1 + num2 + num3;
System.out.println("The sum of the three numbers is: " +
sum);
}
}
3b) Four Data Types Supported by Java
1. int(Integer)
2. double(Floating-point number)
3. char (Character)
4. boolean (True/False)
4. a) State tree types of comment you know in Java
b) What do you understand by Casting
4a) Three Types of Comments in Java
1. Single-line comment: Starts with // and comments out a single
line.
// This is a single-line comment
2. Multi-line comment: Enclosed between /* and */, used to
comment multiple lines.
/* This is a multi-line
comment */
3. Documentation comment:Starts with /** and is used for
generating Java documentation.
/**
* This is a documentation comment
* which can be used for generating API docs.
*/
4b) What is Casting?
Casting: In Java is the process of converting one data type into
another. There are two types:
- Implicit Casting (Widening): Automatically done by Java
when converting a smaller type to a larger type (e.g., int to
double).
- Explicit Casting (Narrowing): Requires the programmer to
explicitly convert a larger type to a smaller type (e.g., double to
int).
int num = 10;
double d = num; // Implicit casting
double val = 9.8;
int i = (int) val; // Explicit casting
5.a) Write a java program that will sum the element in an array
using for loop int[] arrayN = { 10, 23, 2, 30, 60, 98, 18}
public class ArraySum {
public static void main(String[] args) {
int[] arrayN = {10, 23, 2, 30, 60, 98, 18};
int sum = 0;
for (int i = 0; i < arrayN.length; i++) {
sum += arrayN[i];
}
System.out.println("The sum of the array elements is: " +
sum);
}
}
6b) Write a Java Program that will print integer numbers
between 1 to 100.
public class PrintNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
}
}