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

1 JAVA Observation Book Language Fundamentals TSR

The document outlines various Java programming assignments for the Object Oriented Programming course at Annamacharya Institute of Technology & Sciences for the academic year 2021-2022. It includes multiple assignments focusing on language fundamentals, flow control, and basic operations like string manipulation, arithmetic calculations, and conditional statements. Each assignment provides example inputs and expected outputs to guide students in their coding tasks.

Uploaded by

yoyope1783
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)
6 views

1 JAVA Observation Book Language Fundamentals TSR

The document outlines various Java programming assignments for the Object Oriented Programming course at Annamacharya Institute of Technology & Sciences for the academic year 2021-2022. It includes multiple assignments focusing on language fundamentals, flow control, and basic operations like string manipulation, arithmetic calculations, and conditional statements. Each assignment provides example inputs and expected outputs to guide students in their coding tasks.

Uploaded by

yoyope1783
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/ 24

ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI

AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

Java Fundamentals

1. Lnaguage Basics Assignments


/*Write a Program that accepts two Strings as command line arguments and generate the output in the
required format.
Example1)
If the two command line arguments are AITS and Tirupathi then the output generated should be AITS
AUTONAMOUS College Tirupathi.
Example2)
If the command line arguments are ABC and Mumbai then the output generated should be ABC
Technologies Mumbai
[Note: It is mandatory to pass two arguments in command line]
*/
public class Assignment01 {
public static void main(String[] args) {
System.out.println(args[0]+" "+" AUTONAMOUS "+" "+args[1]);
}

}
/*Write a Program to accept a String as a command line argument and print a Welcome message as
given below.

Example1)
C:\> java Sample John
O/P Expected : Welcome John
*/

public class Assignment02 {

public static void main(String[] args) {

System.out.println("Welcome"+" "+args[0]);
}

}
/*Write a Program to accept two integers as command line arguments and print the sum of the two
numbers

Example1)
C:\>java Sample 10 20
O/P Expected : The sum of 10 and 20 is 30
*/

public class Assignment03 {

Collected & Prepared By: T. SREENIVASULA REDDY Page 1 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

public static void main(String[] args) {


int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);

int sum = num1 + num2;

System.out.println("The sum of "+num1+" and "+num2+" is "+sum);


}

2. Flow Control Assignments


//Write a program to check if a given integer number is Positive, Negative, or Zero.

import java.util.Scanner;

public class Assignment01 {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
System.out.print("Enter the number:");
int num = scan.nextInt();

if(num > 0) {
System.out.println("Number is Positive");
}
else if(num < 0) {
System.out.println("Number is Negative");
}
else {
System.out.println("Number is Zero");
}

}
//Write a program to check if a given integer number is odd or even.

import java.util.Scanner;

public class Assignment02 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


Collected & Prepared By: T. SREENIVASULA REDDY Page 2 of 24
ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

System.out.print("Enter the number:");


int num = scan.nextInt();

if(num % 2 != 0) {
System.out.println("Number is Odd");
}
else {
System.out.println("Number is Even");
}
}

}
/*Write a program to check if the program has received command line arguments or not.

If the program has not received arguments then print "No Values", else print all the values in a single
line separated by ,(comma)

Example1) java Example


O/P: No values

Example2) java Example Mumbai Bangalore


O/P: Mumbai,Bangalore

[Hint: You can use length property of an array to check its length]
*/

public class Assignment03 {

public static void main(String[] args) {


if(args.length == 0) {
System.out.println("No values");
}
else {
for(String name : args) {
System.out.print(name + " ");
}
}
}

}
/*Initialize two character variables in a program and display the characters in alphabetical order.

Example1) if the first character is 's' and second character is 'e' then the output should be e,s

Collected & Prepared By: T. SREENIVASULA REDDY Page 3 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

Example2) if the first character is 'a' and second character is 'e', then the output should be a,e
*/

public class Assignment04 {

public static void main(String[] args) {

char var1 = 's';


char var2 = 'e';

if (var1 > var2)


System.out.println(var2+" , "+var1);
else
System.out.println(var1+" , "+var2);
}

}
/*Initialize a character variable in a program and

print 'Alphabhet' if the initialized value is an alphabhet,

print 'Digit' if the initialized value is a number, and

print 'Special Character', if the initialized value is anything else.


*/

public class Assignment05 {

public static void main(String[] args) {

char var = '@';

if((var >= 65 && var <= 90) || (var >= 97 && var <= 122) ) {
System.out.println("Alphabet");
}
else if(var >= 48 && var <= 57) {
System.out.println("Number");
}
else {
System.out.println("Special Character");
}
}

Collected & Prepared By: T. SREENIVASULA REDDY Page 4 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

/*Write a program to accept gender ("Male" or "Female") and age from command line arguments and
print the percentage of interest based on the given conditions.

If the gender is 'Female' and age is between 1 and 58, the percentage of interest is 8.2%.

If the gender is 'Female' and age is between 59 and 100, the percentage of interest is 9.2%.

If the gender is 'Male' and age is between 1 and 58, the percentage of interest is 8.4%.

If the gender is 'Male' and age is between 59 and 100, the percentage of interest is 10.5%.
*/

public class Assignment06 {

public static void main(String[] args) {

int age = Integer.parseInt(args[1]);

if(args[0].equals("Female")) {
if(age >= 1 && age <= 58) {
System.out.println("the percentage of interest is 8.2%");
}
else {
System.out.println("the percentage of interest is 9.2%");
}
}
else {
if(age >= 1 && age <= 58) {
System.out.println("the percentage of interest is 8.4%");
}
else {
System.out.println("the percentage of interest is 10.5%");
}
}
}

}
/*Initialize a character variable with an alphabhet in a program.

If the character value is in lowercase, the output should be displayed in uppercase in the following
format.

Example1)
i/p:a
o/p:a->A

Collected & Prepared By: T. SREENIVASULA REDDY Page 5 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

If the character value is in uppercase, the output should be displayed in lowercase in the following
format.

Example2)
i/p:A
o/p:A->a
*/

public class Assignment07 {

public static void main(String[] args) {

char var = 'a';


if (var >= 'a' && var <= 'z')
System.out.println((char)(var-32)); //lowercase to uppercase
else
System.out.println((char)(var+32)); //uppercase to lowercase
}

}
/*Write a program to receive a color code from the user (an Alphabhet).

The program should then print the color name, based on the color code given.

The following are the color codes and their corresponding color names.
R->Red, B->Blue, G->Green, O->Orange, Y->Yellow, W->White.

If color code provided by the user is not valid then print "Invalid Code".
*/

import java.util.Scanner;

public class Assignment08 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter color code :");
char color = scan.next().charAt(0);

switch(color){
case 'R' :
case 'r' :
System.out.println("Red");

Collected & Prepared By: T. SREENIVASULA REDDY Page 6 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

break;

case 'B' :
case 'b' :
System.out.println("Blue");
break;

case 'G' :
case 'g' :
System.out.println("Green");
break;

case 'O' :
case 'o' :
System.out.println("Orange");
break;

case 'Y' :
case 'y' :
System.out.println("Yellow");
break;

case 'W' :
case 'w' :
System.out.println("White");
break;

default :
System.out.println("Invalid Code");
}
}

}
/*Write a program to receive a number and print the corresponding month name.

Example1)

C:\>java Sample 12

O/P Expected : December

Example2)

C:\>java Sample

Collected & Prepared By: T. SREENIVASULA REDDY Page 7 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

O/P Expected : Please enter the month in numbers

Example3)

C:\>java Sample 15

O/P Expected : Invalid month


*/

public class Assignment09 {

public static void main(String[] args) {

switch(args[0]) {
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;

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;

Collected & Prepared By: T. SREENIVASULA REDDY Page 8 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

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("Invalid Month");
}
}

}
//Write a program to print numbers from 1 to 10 in a single row with one tab space.

public class Assignment10 {

public static void main(String[] args) {

for(int i = 1; i <= 10; i++) {


System.out.print(i+" ");
}
}

}
//Write a program to print even numbers between 23 and 57. Each number should be printed in a
separate row.

public class Assignment11 {

public static void main(String[] args) {

for(int i = 23; i <= 57; i++) {


if(i % 2 == 0) {
System.out.println(i);
Collected & Prepared By: T. SREENIVASULA REDDY Page 9 of 24
ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

}
}
}

}
//Write a program to check if a given number is prime or not.

import java.util.Scanner;

public class Assignment12 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter the number:");
int num = scan.nextInt();
int count = 0;

for(int i = 2; i < num/2; i++) {


if(num % i == 0) {
count++;
}
else {
continue;
}
}
if(count == 0) {
System.out.println("Number is prime");
}
else {
System.out.println("Number is NOT prime");
}
}

}
//Write a program to print prime numbers between 10 and 99.

public class Assignment13 {

public static void main(String[] args) {

int flag;
for(int i = 10; i <= 99; i++) {
flag = 1;
for(int j = 2; j <= i/2; j++) {
Collected & Prepared By: T. SREENIVASULA REDDY Page 10 of 24
ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

if(i % j == 0) {
flag = 0;
break;
}
}
if(flag == 1) {
System.out.print(i+" ");
}
}
}

}
/*Write a Java program to find if the given number is prime or not.

Example1)
C:\>java Sample
O/P: Please enter an integer number

Example2)
C:\>java Sample 1
O/P:1 is neither prime nor composite

Example3)
C:\>java Sample 0
O/P: 0 is neither prime nor composite

Example4)
C:\>java Sample 10
O/P: 10 is not a prime number

Example5)
C:\>java Sample 7
O/P : 7 is a prime number
*/

import java.util.Scanner;

public class Assignment14 {

public static void main(String[] args) {

int num = 0;
if(args.length >= 1) {
num = Integer.parseInt(args[0]);
}

Collected & Prepared By: T. SREENIVASULA REDDY Page 11 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

else {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter an integer number ");
num = scan.nextInt();
}

if(num == 0 || num == 1) {
System.out.println(num + " is neither prime nor composite");
}
else {
if(isPrime(num))
System.out.println(num + " is a prime number");
else
System.out.println(num + " is a not prime number");
}
}

static boolean isPrime(int num) {


for(int i=2; i <= num/2; i++) {
if(num % i == 0)
return false;
}
return true;
}

}
/*Write a program to print the sum of all the digits of a given number.

Example1)
I/P:1234
O/P:10
*/

import java.util.Scanner;

public class Assignment15 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter the number: ");
int num = scan.nextInt();
int sum;

for(sum = 0; num > 0; num = num/10)

Collected & Prepared By: T. SREENIVASULA REDDY Page 12 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

sum = sum + (num % 10);

System.out.println(sum);
}

}
/*Write a program to print * in Floyds format (using for and while loop)
*
* *
* * *

Example1)
C:\>java Sample
O/P: Please enter an integer number

Example2)
C:\>java Sample 3
O/P :
*
* *
* * *
--------------------------------------------------------------------------
*/

import java.util.Scanner;

public class Assignment16 {

public static void main(String[] args) {

int count;
if(args.length == 0) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer number:");
count = scan.nextInt();
}
else {
count = Integer.parseInt(args[0]);
}
System.out.println("Using For Loop :");
for(int i = 1; i <= count; i++) {
for(int j = 1; j <=i; j++) {
System.out.print("*");
}

Collected & Prepared By: T. SREENIVASULA REDDY Page 13 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

System.out.println(" ");
}

System.out.println("Using While Loop :");


int i = count, j;

while(i >= 1){


j = i;
while(j <= count){
System.out.print("*");
j++;
}
i--;
System.out.println(" ");
}
}
}
/*Write a program to reverse a given number and print

Example1)
I/P: 1234
O/P:4321

Example2)
I/P:1004
O/P:4001
*/

import java.util.Scanner;

public class Assignment17 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.print("Enter the number :");
int num = scan.nextInt();
int digit, reverse = 0;

while(num > 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
System.out.println(reverse);

Collected & Prepared By: T. SREENIVASULA REDDY Page 14 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

}
/*Write a Java program to find if the given number is palindrome or not

Example1)
C:\>java Sample 110011
O/P: 110011 is a palindrome

Example2)
C:\>java Sample 1234
O/P: 1234 is not a palindrome
*/

public class Assignment18 {

public static void main(String[] args) {

int num = Integer.parseInt(args[0]);


int temp = num, digit, reverse = 0;

while(temp > 0) {
digit = temp % 10;
reverse = reverse * 10 + digit;
temp = temp / 10;
}

if(num == reverse) {
System.out.println(num + " "+ "is a palindrome");
}
else {
System.out.println(num + " "+ "is not a palindrome");
}
}

}
//Write a program to print first 5 values which are divisible by 2, 3, and 5.

public class Assignment19 {

public static void main(String[] args) {

int count = 0, num = 1;

while(count < 5) {
Collected & Prepared By: T. SREENIVASULA REDDY Page 15 of 24
ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

if(num % 2 == 0 && num % 3 == 0 && num % 5 == 0) {


System.out.print(num + " ");
count++;
}
num++;
}
}

3. Arrays Assignments
//Write a program to initialize an integer array and print the sum and average of the array.

import java.util.Arrays;

public class Assignment01 {

public static void main(String[] args) {

int[] array = {19, 14, 20, 18, 2};


int sum = 0;

System.out.println("Array: "+Arrays.toString(array));
//print Array

for(int i = 0; i < array.length; i++) {


//sum
sum = sum + array[i];
}
System.out.println("The sum of the array is: "+sum);
double avg = (sum/array.length);
//average
System.out.println("The average of the array is: "+avg);
}
}
//Write a program to initialize an integer array and find the maximum and minimum value of the array.

import java.util.Arrays;

public class Assignment02 {

public static void main(String[] args) {

int[] array = {19, 14, 20, 18, 2};

Collected & Prepared By: T. SREENIVASULA REDDY Page 16 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

System.out.println("Array: "+Arrays.toString(array));
//print Array

int max = array[0];


//max value
for(int i = 1; i < array.length; i++) {
if(array[i] > max)
max = array[i];
}
System.out.println("The maximum value of Array is: "+max);

int min = array[0];


//min value
for(int i = 1; i < array.length; i++) {
if(array[i] < min)
min = array[i];
}
System.out.println("The minimum value of Array is: "+min);
}

}
/*Write a program to initialize an integer array with values and check if a given number is present in the
array or not.

If the number is not found, it will print -1 else it will print the index value of the given number in the
array.

Example 1) If the Array elements are {1,4,34,56,7} and the search element is 90, then the output
expected is -1.

Example 2)If the Array elements are {1,4,34,56,7} and the search element is 56, then the output
expected is 3.
*/

public class Assignment03 {

public static void main(String[] args) {


int[] array = {1, 4, 34, 56, 7};
int key = 92;
int i, flag = 0;

for(i = 0; i < array.length; i++) {


if(array[i] == key) {
flag = 1;
break;

Collected & Prepared By: T. SREENIVASULA REDDY Page 17 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

}
}
if(flag == 1) {
System.out.println(i+1);
}
else {
System.out.println("-1");
}
}

}
//Initialize an integer array with ascii values and print the corresponding character values in a single row.

public class Assignment04 {

public static void main(String[] args) {

int[] ascii = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
86, 87, 88, 89, 90};

for(int i = 0; i < ascii.length; i++) {


System.out.print((char)ascii[i]+" ");
}
}

}
//Write a program to find the largest 2 numbers and the smallest 2 numbers in the given array.

public class Assignment05 {

public static void main(String[] args) {

int[] array = {19, 14, 20, 18, 2};


//sort
for(int i = 0; i < array.length-1; i++) {
for(int j = 0; j < array.length-1; j++) {
if(array[j] > array[j+1]) {
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
System.out.println("Largest two numbers are "+array[array.length-1]+",
"+array[array.length-2]);
Collected & Prepared By: T. SREENIVASULA REDDY Page 18 of 24
ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

System.out.println("Smallest two numbers are "+array[0]+", "+array[1]);


}

}
//Write a program to initialize an array and print them in a sorted order.

import java.util.Arrays;

public class Assignment06 {

public static void main(String[] args) {

int[] array = {19, 14, 20, 18, 2};

Arrays.sort(array);
//sort inbuilt function

/*for(int i = 0; i < array.length-1; i++) { //sort logic


for(int j = 0; j < array.length-1; j++) {
if(array[j] > array[j+1]) {
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
*/

System.out.print("Sorted Array : ");


for(int element : array) {
System.out.print(element + " ");
}
}

}
/*Write a program to remove the duplicate elements in an array and print the same.
Example)
I/P:{12,34,12,45,67,89}
O/P:{12,34,45,67,89}
*/

import java.util.Arrays;

public class Assignment07 {

Collected & Prepared By: T. SREENIVASULA REDDY Page 19 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

public static void main(String[] args) {

int[] array = {12, 34, 12, 45, 67, 89};

Arrays.sort(array);
//sort
int[] temp = new int[array.length];
int j = 0;
//Using temporary array
for (int i = 0; i < array.length-1; i++){
if (array[i] != array[i+1]){
temp[j++] = array[i];
}
}
temp[j++] = array[array.length-1];
for (int i = 0; i < j; i++){
System.out.print(temp[i]+" "); //last element
}
}

}
/*

for(int i = 0; i < array.length; i++) { //Without using temporary array


if(i != array.length-1) {
if(array[i] != array[i+1])
System.out.print(array[i]+" ");
}
else
System.out.println(array[i]); //last element
}
}

}
*/
/*Write a program to print the sum of the elements of an array following the given below condition.

If the array has 6 and 7 in succeeding orders, ignore the numbers between 6 and 7 and consider the
other numbers for calculation of sum.

Eg1) Array Elements - 10,3,6,1,2,7,9


O/P: 22
[i.e 10+3+9]

Eg2) Array Elements - 7,1,2,3,6

Collected & Prepared By: T. SREENIVASULA REDDY Page 20 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

O/P:19

Eg3) Array Elements - 1,6,4,7,9


O/P:10
*/

public class Assignment08 {

public static void main(String[] args) {

int[] array= {10, 3, 6, 1, 2, 7, 9};


int sum = 0;
int flag = 0;
for(int i = 0; i < array.length; i++) {
if(array[i] == 6)
flag = 1;
else if(array[i] == 7) {
flag = 0;
i++;
}
if(flag != 1)
sum = sum + array[i];
}
System.out.println(sum);
}

}
/*Write a program to reverse the elements of a given 2*2 array. Four integer numbers needs to be
passed as Command Line arguments.

Example1)
C:\>java Sample 1 2 3
O/P: Please enter 4 integer numbers

Example2)
C:\>java Sample 1 2 3 4
O/P:
The given array is :
12
34
The reverse of the array is :
43
21
*/

Collected & Prepared By: T. SREENIVASULA REDDY Page 21 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

public class Assignment09 {

public static void main(String[] args) {

if(args.length != 4) {
System.out.println("Please enter 4 integer numbers");
}
else {
int[][] array = new int[2][2];
int x = 0;
//storing elements
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
array[i][j] = Integer.parseInt(args[x++]);
}
}

System.out.println("The given array is :");


for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}

System.out.println("The reverse of the array is :");


for(int i = array.length-1; i >= 0; i--) {
for(int j = array.length-1; j >= 0; j--) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}

4. Employe Information - Input & Display


public class Employee {

private int emp_id[] = { 1001, 1002, 1003, 1004, 1005, 1006, 1007 };
private String emp_name[] = { "Ashish", "Sushma", "Rahul", "Chahat", "Ranjan", "Suman", "Tanmay" };
private String joining_date[] = { "01/04/2009", "23/08/2012", "12/11/2008", "29/01/2013",
"16/07/2005", "1/1/2000",
"12/06/2006" };

Collected & Prepared By: T. SREENIVASULA REDDY Page 22 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

private char desig_code[] = { 'e', 'c', 'k', 'r', 'm', 'e', 'c' };
private String dept[] = { "R&D", "PM", "Acct", "Front Desk", "Engg", "Manufacturing", "PM" };
private int basic[] = { 20000, 30000, 10000, 12000, 50000, 23000, 29000 };
private int hra[] = { 8000, 12000, 8000, 6000, 20000, 9000, 12000 };
private int it[] = { 3000, 9000, 1000, 2000, 20000, 4400, 10000 };

private int calcDA(int i) {


char designation = desig_code[i];
switch(designation) {
case 'e': return 20000;
case 'c': return 32000;
case 'k': return 12000;
case 'r': return 15000;
case 'm': return 40000;
}
return 0;
}

private char getDesigCode(int id) {


return desig_code[getIndex(id)];
}

private int getIndex(int id) {


int index = -1;
for(int i = 0; i < emp_id.length; i++) {
if(id == emp_id[i]) {
index = i;
break;
}
}
return index;
}
public int getSalary(int id) {
int index = getIndex(id);
if(index == -1) return -1;
return (basic[index] + hra[index] - it[index] + calcDA(index));
}

public String getName(int id) {


return emp_name[getIndex(id)];
}

public String getDept(int id) {


return dept[getIndex(id)];
}

Collected & Prepared By: T. SREENIVASULA REDDY Page 23 of 24


ANNAMACHARYA INSTITUTE OF TECHNOLOGY & SCIENCES :: TIRUPATHI
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals

public boolean empExists(int id) {


if(getIndex(id) != -1)
return true;
else
return false;
}

public String getDesignation(int id) {


char d = getDesigCode(id);
switch(d) {
case 'e': return "Engineer";
case 'c': return "Consultant";
case 'k': return "Clerk";
case 'r': return "Receptionist";
case 'm': return "Manager";
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length != 1) {
System.out.println("Usage: java Employee emp_id");
}
else {
int emp_id = Integer.parseInt(args[0]);
Employee emp = new Employee();
if(emp.empExists(emp_id)) {
System.out.println("Emp No. Emp Name Department Designation
Salary");
System.out.printf("%7d ", emp_id);
System.out.printf("%8s ", emp.getName(emp_id));
System.out.printf("%10s ", emp.getDept(emp_id));
System.out.printf("%11s ", emp.getDesignation(emp_id));
System.out.printf("%6d\n", emp.getSalary(emp_id));
}
else {
System.out.println("There is no employee with empid: " + emp_id);
}
}
}

Collected & Prepared By: T. SREENIVASULA REDDY Page 24 of 24

You might also like