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

My java lab Journal

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

My java lab Journal

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

OBJECT ORIENTED PROGRAMMING USING

JAVA
CSIT120
LAB FILE

Department of Computer Science and Engineering


AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY
AMITY UNIVERSITY DUBAI

Batch (2022-2025) (Semester 3)


Bachelor of Science in Information Technology

Under the guidance of:


Faculty: Dr. Iqra Hussain
Lab Instructor: Dr Iqra Hussain
Submitted by: Josue Claver Manamou
Enrolment no: A92604922035
University Email Address: [email protected]
Submission Date: 06/12/2023

Page 1 of 26
AMITY UNIVERSITY DUBAI

Lab 1 :
1) Create a class called NumberComparison, declare two integer variables, and check whether
their sum is greater than 30. If the condition is met, it should print the following message to the
console: "The sum of a and b is greater than 30". (Use simple if statement)

A) public class NumberComparison


{ public static void main(String[] args) {

int a = 15, b = 20, sum;

sum = a + b;

if (sum > 30) {


System.out.println("The sum of a and b is greater than 30");
}

Output:

2) Create a class called StudentComparison, declare two integer variables, and checks whether
their sum is less than 15. Depending on the result, it should print one of two messages to the
console; "The sum of a and b is less than 15.“ or "The sum of a and b is greater than or equal
to 15.“ (Use if-else statement)

A) public class StudentComparison { public


static void main(String[] args) { int a = 5, b =
7, sum = a + b; if (sum < 15) {
System.out.println("The sum of a and b is less than 15");
} else {
System.out.println("The sum of a and b is greater than or equal to 15");
}
}
}

Output::

Page 2 of 26
3) 1.Create a new class called CityChecker.

2.Inside the main method, declare a string variable named city and initialize it with the value
“Berlin."

3.Use a series of if-else if-else statements to compare the city variable using the == operator.
The == operator checks if the references of the two strings are the same, not if the content of
the strings is equal.

4.In each if or else if condition, we compare the city variable with different city names using ==.

5.The reference of the city should be different from the references of the string literals in the if
conditions, none of the conditions should match.

6.Therefore, the program should enter the else block and print the value of the city variable,
which is “Berlin.“ (Use if else if ladder statement)

A) public class CityChecker { public static


void main(String[] args) { String city =
"Berlin"; if (city == "Dubai") {
System.out.println("Dubai");
}
else if (city == "Abu Dhabi") {
System.out.println("Abu Dhabi");
}
else if (city == "Berlin") {
System.out.println("Berlin");
}
}
}

Output:

4) Create a new class and name it NestedIfDemo

1.Declare an integer variable named i and initialize it with the value 10


2.Use Nested if statements in the following sequence;
1. .Create an outer if statement that checks two conditions using the || (logical OR)
operator.
2. .If either of these conditions is true, the code inside the outer if block should be
executed.

Page 3 of 26
AMITY UNIVERSITY DUBAI

3. Next create an outer if block further with two nested if statements and if these conditions
are true the code should be executed, and the output should be displayed for each
condition.
4. And If neither of the conditions in the outer if statement is true, the else block should be
executed, and it prints output accordingly.
Note: Multiple conditions should be checked and execute different code blocks based on the
results of these conditions.

A) public class NestedIfDemo { public static


void main(String[] args) { int i = 10; if (i >= 0
|| i <= 7) {
System.out.println("Code Inside Executed");
}
if (i >= 8) { if (i ==
9) {
System.out.println("i=9");
}
else if (i == 10) {
System.out.println("i=10");
}
else {
System.out.println("None of the conditions met");
}
}
}
}

Output:

5) Use Switch Statement

1.Write a code that sets a value in the num variable and then uses a switch statement to check
its value against several cases (3 minimum cases). Depending on the value of num, it should
print different messages to the console.

2.If none of the cases match, it should print "Not present" as the default message.
In this specific case, num is set to 20, it should print "It is 20" to the console.

public class SwitchStatement { public static


void main(String[] args) { int num =
4; switch (num) { case 1: {

Page 4 of 26
System.out.println("Number = 1");
break;
}
case 2: {
System.out.println("Number = 2");
break;
}
case 3: {
System.out.println("Number = 3");
break;
}
case 4: {
System.out.println("Number = 4");
break;
}
case 5: {
System.out.println("Number = 5");
break;
}
default: {
System.out.println("Not present");
break;
}
}
}
}
Output:

6) Create a program to display numbers from 10-20 using a while loop

A) public class whileLoop { public static void


main(String[] args) { int num = 10;
while (num <= 20) {
System.out.println(num); num+
+;
}
}
}

Output:

Page 5 of 26
AMITY UNIVERSITY DUBAI

7) Create a program to display numbers from 30-40 using a for loop

A) public class forLoop { public static void


main(String[] args) { int num;
for (num = 30; num <= 40; num++) {
System.out.println(num);
}
}
}

Output:

8) Create a program to display numbers from 40-50 using a do-while loop

A) public class doWhileLoop { public static


void main(String[] args) { int num = 40; do {
System.out.println(num); num+
+;
} while (num <= 50);
}
}

Output:

Page 6 of 26
Page 7 of 26
a

Lab 2

1) Create program to illustrate the following

a) Create an array of 5 integers


b) Put some values in the array
c) Print each value to standard output

A) public class array { public static void

main(String[] args) {

int[] arr;

arr = new int[5];

arr[0]=1;
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;

for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}

}
}

10 .Create a class named multiDimensional.

A) public class multiDimensional { public static


void main(String[] args) { int[][] arr = {{1,2,3},
{4,5,6},{7,8,9}};

for(int i=0;i<3;i++) { for(int


j=0;j<3;j++) {
System.out.print(arr[i][j]);
}
System.out.println();
Page 8 of 26
}
}
}

11) Write a program to display the sum of the first 10 odd numbers

A) public class sumOfOdd { public static


void main(String[] args) {
int count = 0, sum = 0, num = 1;
;
while (count < 10) { if (num % 2 !
= 0) { sum = sum + num;
count++;
}
num++;
}
System.out.println(sum);
}
}

12) Write switch statement to print the current month of the year

A) public class switchCase { public static


void main(String[] args) { int m = 10; switch
(m) { case 1:
System.out.println("January");
break;

case 2:
System.out.println("February");
break;

case 3:
System.out.println("March");
break;

case 4:
System.out.println("April");
break;

Page 9 of 26
a

case 5:
System.out.println("May");
break;

case 6:
System.out.println("June");
break;

case 7:
System.out.println("July");
break;

case 8:
System.out.println("August");
break;

case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;

case 11:
System.out.println("November");
break;

case 12:
System.out.println("December");
break;

default:
System.out.println("Enter a valid month number");
break;

}
}
}

Page 10 of 26
13) Write program to compute the factorial of a number (7) A) public
class factorial { public static void main(String[] args) { int fact = 1; for (int i
= 1; i <= 7; i++) { fact = fact * i;
}
System.out.println(fact);
}
}

14) Write a program to check if the number entered by user is odd or even

A)
import java.util.Scanner; public class
scanner { public static void main(String[]
args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: "); int
x = input.nextInt(); if (x == 0) {
System.out.println("The number entered is 0");
}
else if (x % 2 == 0) {
System.out.println("The number entered is even");
}
else {
System.out.println("The number entered is odd");
}
}
}

Lab 3 15)
Create a calculator program in Java using the package
Instructions
a) Start with the Add and then Sub, Mul, Div
b) Further create the main class named Calculator. In this class, import all te packages that
we have created above. It will include all the classes in the calculator class.

Page 11 of 26
a

Addition package
Calculator; import
java.util.*; public
class Addition { int s;
public void sum() {
System.out.println("Enter the first number: ");
Scanner input = new Scanner(System.in); int
x = input.nextInt();
System.out.println("Enter the second number: ");
int y = input.nextInt();

s = x+y;

System.out.println("Sum = " +s);


}
}

Subtraction package
Calculator; import
java.util.Scanner; public
class Subtraction { int s;
public void diff() {

Scanner input = new Scanner(System.in);

System.out.println("Enter the first number: ");


int x = input.nextInt();

System.out.println("Enter the second number: ");

int y = input.nextInt(); s = x-y;

Page 12 of 26
System.out.println("Difference = " + s);

}
}

Multiplication package
Calculator; import
java.util.Scanner; public
class Multiplication { int
s; public void prod() {

Scanner input = new Scanner(System.in);

System.out.println("Enter the first number:


"); int x = input.nextInt();

System.out.println("Enter the second

number: "); int y = input.nextInt(); s = x*y;

System.out.println("Product = " + s);

}
}

Division package
Calculator; import
java.util.Scanner;
public class Division {

int s,r; public

void div() {

Scanner input = new Scanner(System.in);

System.out.println("Enter the first number:


"); int x = input.nextInt();

System.out.println("Enter the second number: ");


Page 13 of 26
int y = input.nextInt();

s = x/y;
r=x
%y;

System.out.println("Quotient = " + s);


System.out.println("Remainder = " + r);

}
}

Main Calculator
package Calculator;

import Calculator.Addition; import


Calculator.Subtraction; import
Calculator.Multiplication; import
Calculator.Division; import
java.util.Scanner; public class calc
{ public static void main(String[] args) {

Addition add = new Addition();


Subtraction sub = new Subtraction();
Multiplication mul = new

Multiplication(); Division div = new

Division(); int c;

System.out.println("Choose the operation");


System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");

Page 14 of 26
Scanner input = new

Scanner(System.in); c =

input.nextInt(); switch(c) { case 1:

add.sum();
break;

case 2:
sub.diff();
break;

case 3:
mul.prod();
break;

case 4:
div.div();
break;

default:
System.out.println("Select the options from 1-4 ");
}

Output:

Page 15 of 26
Lab 4

16) Use switch statement to print the days of the week

import java.util.Scanner; public class


week { public static void main(String[]
args) {

System.out.println("Enter a number from 1-7");

Scanner input = new

Scanner(System.in); int x =

input.nextInt(); switch(x) {

case 1:
System.out.println("Monday");
break;

case 2:
System.out.println("Tuesday");
break;

case 3:
System.out.println("Wednesday");
break;

case 4:
System.out.println("Thursday");
break;

case 5:
Page 16 of 26
System.out.println("Friday");
break;

case 6:
System.out.println("Saturday");
break;

case 7:
System.out.println("Sunda
y"); break; default:

System.out.println("Select from 1-7");


}
}
}

Output:

17) Calculate Fibonacci numbers in Java (user should enter any number from the
keyboard)

import java.util.Scanner; public class


fibonacci { public static void
main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of Fibonacci numbers to be
generated: "); int n = scanner.nextInt();
System.out.println("The first " + n + "
numbers:"); int firstNum = 0, secondNum =
1; for (int i = 1; i <= n; i++) {
System.out.print(firstNum + " "); int
nextNum = firstNum +
secondNum; firstNum =
secondNum; secondNum =
nextNum;
}
}
}

Page 17 of 26
Output:

18- Standard Deviation :

public class StandardDeviation {


public static double calculateSD(double numArray[]) {
int length = numArray.length;
double sum = 0.0, standardDeviation = 0.0;

for (double num : numArray) {


sum += num;
}

double mean = sum / length;

for (double num : numArray) {


standardDeviation += Math.pow(num - mean, 2);
}

return Math.sqrt(standardDeviation / length);


}

public static void main(String[] args) {


double[] numbers = {10.2, 13.4, 15.8, 12.7, 14.5}; // Replace this array with
your set of numbers
double sd = calculateSD(numbers);
System.out.format("Standard Deviation = %.2f", sd);
}
}

Output :

For the error, the Compiler didn’t find the main class (LABOOP) for us.

Page 18 of 26
Java program to find the occurrence of vowels in
19-
words

public class VowelOccurrence {


public static void countVowels(String str) {
int countA = 0, countE = 0, countI = 0, countO = 0, countU = 0;

for (char ch : str.toLowerCase().toCharArray()) {


switch (ch) {
case 'a':
countA++;
break;
case 'e':
countE++;
break;
case 'i':
countI++;
break;
case 'o':
countO++;
break;
case 'u':
countU++;
break;
}
}

System.out.println("Occurrences of A: " + countA);


System.out.println("Occurrences of E: " + countE);
System.out.println("Occurrences of I: " + countI);
System.out.println("Occurrences of O: " + countO);
System.out.println("Occurrences of U: " + countU);
}

public static void main(String[] args) {


String word = "Hello World"; // Replace this string with your input
countVowels(word);
}
}

Output :
Page 19 of 26
20- Palindrome Checking :

public class PalindromeChecker {


public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;

while (left < right) {


if (str.charAt(left) != str.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

public static void main(String[] args) {


String word = "tuesday"; // Replace this string with the one you want to check
if (isPalindrome(word.toLowerCase())) {
System.out.println(word + " is a palindrome.");
} else {
System.out.println(word + " is not a palindrome.");
}
}
}

Output :

21- Program to check prime number and calculate the sum :

Page 20 of 26
public class PrimeNumberArray {
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}

public static void main(String[] args) {


int[] numbers = {2, 5, 6, 7, 11, 12, 13}; // Replace this array with your set of
numbers
int sumOfPrimes = 0;

for (int number : numbers) {


if (isPrime(number)) {
System.out.println(number + " is a prime number.");
sumOfPrimes += number;
}
}

System.out.println("Sum of prime numbers in the array: " + sumOfPrimes);


}
}
Output :

Lab (07 NOVEMBER 2023) : Exceptions and Strings

Write programs to show the different types of Exceptions being handled using the
try and catch block which include:

Page 21 of 26
22- Arithmetic Exception :
package Exceptions; public class
arithmetic { public static void
main(String[] args) { try {
int x = 10 / 0;
} catch (ArithmeticException e)
{ System.out.println(e);
}
}
}

Output:

23- Null Pointer Exception :

package Exceptions; public class


numberFormat { public static void
main(String[] args) { String x = "Hello,
World!";

try { int i = Integer.parseInt(x);

} catch (NumberFormatException e)
{ System.out.println(e);
}

}
}

Output:

24- Number Format Exception

package Exceptions;
public class nullPointer {
public static void
Page 22 of 26
main(String[] args)
{ String x = null; try { if
(x.equals("notNull")) {
System.out.println("Same");
}
} catch (NullPointerException e)
{ System.out.println(e);
}
}
}

Output:

25- ArrayIndexOutOfBounds Exception

package Exceptions; public class


ArrayException { public static void
main(String[] args) {
int[] x;
x = new int[5];
try {
System.out.println(x[7]);
} catch (ArrayIndexOutOfBoundsException e)
{ System.out.println(e);
}
}
}

Output:

26) Write a program to perform String functions in Java

a)Length of the string, number of vowels


import java.util.Scanner; public class
vowelAndLength { public static void
main(String[] args) {
Scanner input = new
Scanner(System.in); String text; int
count = 0;

Page 23 of 26
System.out.println("Enter the string");
text = input.nextLine();
for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == 'a' ||
text.charAt(i) == 'e' || text.charAt(i) == 'i' ||
text.charAt(i) == 'o'
|| text.charAt(i) == 'u')
{ count++;
}
}

System.out.println("There are " + count + " vowels");


System.out.println("The length of the string is: " +text.length());
}
}

27- ExceptionOrdering :

public class ExceptionOrdering {


public static void main(String[] args) {
try {
int[] arr = new int[5];
System.out.println(arr[10]);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception caught: " + e);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception caught: " + e);
} catch (Exception e) {
System.out.println("Generic Exception caught: " + e);
}
}
}
28- Propagation

public class ExceptionPropagation {


void method1() {
int result = 50 / 0; // ArithmeticException occurs here
}

Page 24 of 26
void method2() {
method1();
}

void method3() {
try {
method2();
} catch (Exception e) {
System.out.println("Exception handled");
}
}

public static void main(String[] args) {


ExceptionPropagation obj = new ExceptionPropagation();
obj.method3();
System.out.println("Program continues after exception handling");
}
}

31- Creation of a StringBuffer and modification.

public class StringBufferModification {


public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("Object Oriented Programming");
buffer.replace(0, buffer.length(), "Java");
System.out.println(buffer);
}
}

33- StringBuffer replacement.

public class StringBufferReplacement {


public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("Programming");
buffer.replace(3, 6, "Java");
System.out.println(buffer);
}
}

Page 25 of 26
Page 26 of 26

You might also like