My java lab Journal
My java lab Journal
JAVA
CSIT120
LAB FILE
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)
sum = a + b;
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)
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)
Output:
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.
Output:
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.
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:
Output:
Page 5 of 26
AMITY UNIVERSITY DUBAI
Output:
Output:
Page 6 of 26
Page 7 of 26
a
Lab 2
main(String[] args) {
int[] arr;
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]);
}
}
}
11) Write a program to display the sum of the first 10 odd numbers
12) Write switch statement to print the current month of the year
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;
Subtraction package
Calculator; import
java.util.Scanner; public
class Subtraction { int s;
public void diff() {
Page 12 of 26
System.out.println("Difference = " + s);
}
}
Multiplication package
Calculator; import
java.util.Scanner; public
class Multiplication { int
s; public void prod() {
}
}
Division package
Calculator; import
java.util.Scanner;
public class Division {
void div() {
s = x/y;
r=x
%y;
}
}
Main Calculator
package Calculator;
Division(); int c;
Page 14 of 26
Scanner input = new
Scanner(System.in); c =
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
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:
Output:
17) Calculate Fibonacci numbers in Java (user should enter any number from the
keyboard)
Page 17 of 26
Output:
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
Output :
Page 19 of 26
20- Palindrome Checking :
Output :
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;
}
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:
} catch (NumberFormatException e)
{ System.out.println(e);
}
}
}
Output:
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:
Output:
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++;
}
}
27- ExceptionOrdering :
Page 24 of 26
void method2() {
method1();
}
void method3() {
try {
method2();
} catch (Exception e) {
System.out.println("Exception handled");
}
}
Page 25 of 26
Page 26 of 26