1 JAVA Observation Book Language Fundamentals TSR
1 JAVA Observation Book Language Fundamentals TSR
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
}
/*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
*/
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
*/
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
import java.util.Scanner;
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;
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) {
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)
[Hint: You can use length property of an array to check its length]
*/
}
/*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
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
*/
}
/*Initialize a character variable in a program and
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");
}
}
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%.
*/
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
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
*/
}
/*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;
switch(color){
case 'R' :
case 'r' :
System.out.println("Red");
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
Example2)
C:\>java Sample
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
Example3)
C:\>java Sample 15
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;
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.
}
//Write a program to print even numbers between 23 and 57. Each number should be printed in a
separate row.
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;
}
//Write a program to print prime numbers between 10 and 99.
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;
int num = 0;
if(args.length >= 1) {
num = Integer.parseInt(args[0]);
}
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");
}
}
}
/*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;
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
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;
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("*");
}
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
System.out.println(" ");
}
Example1)
I/P: 1234
O/P:4321
Example2)
I/P:1004
O/P:4001
*/
import java.util.Scanner;
while(num > 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}
System.out.println(reverse);
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
*/
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.
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
3. Arrays Assignments
//Write a program to initialize an integer array and print the sum and average of the array.
import java.util.Arrays;
System.out.println("Array: "+Arrays.toString(array));
//print Array
import java.util.Arrays;
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
}
/*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.
*/
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.
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};
}
//Write a program to find the largest 2 numbers and the smallest 2 numbers in the given array.
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 initialize an array and print them in a sorted order.
import java.util.Arrays;
Arrays.sort(array);
//sort inbuilt function
}
/*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;
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
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
}
}
}
/*
}
*/
/*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.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
O/P:19
}
/*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
*/
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals
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++]);
}
}
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" };
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 };
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2021-2022
AK20 20APC0514 Through JAVA LAB Observation Book Programs
Language Fundamentals