Practical : 3
If Statement
Program Code: 1
class IF Statement {
Public static void main ( String [] args ) {
int number =10;
// Checks if number is less than 0 if (number<0) {
[Link](“The number is negative”);
}
[Link](“Statement outside if block”);
}
}
Output:
Program Code: 2
class Main {
public static void main (String [] args ){
//&& Operator
[Link]((5>3)&&8>5));// true
[Link]((5>3)&&(8<5));//false
// || Operator
[Link]((5<3) ||(8>5));// true
[Link]((5<3) ||(8<5));// true
[Link]((5<3) ||(8<5));// false
// ! Operator
[Link](!5==3));//true
[Link](!5>3));//false
}
}
Output:
Program Code: 3
class check EvenOdd
{
public static void main (String args [])
{
int num = 10;
[Link](“Enter an Integer number:”);
/* IF number is divisible by 2 then it’s an even number*else odd number*/
If (num%2==0)
[Link](“Entered number is even”);
else
[Link](“Entered number is odd”);
}
}
Output:.
Practical : 4
Switch-Case Statement
Program Code: 1
Public class SwitchCaseExample1 {
Public static void main(String args[]){
Int num=2;
Switch(num+2)
{
Case 1:
[Link](“Case1: Value is: “+num);
Case 2:
[Link](“Case2: Value is: “+num);
Case 3:
[Link](“Case3: Value is: “+num);
Default:
[Link](“Default: Value is: “+num); }
}
}
Output:
Program Code: 2
import [Link];
class main {
public static void main (String [] args){
// take input from users
Scanner input= new Scanner ( [Link]);
[Link](“Enter your marks:”);
double marks = [Link] Double ();
// ternary operator checks if
// marks is greater than 40
String result = (marks>40)?”pass”:”fail”;
[Link](“You”+ result+”the exam:”);
[Link]();
}
}
Output:
Program Code: 3
//Java program to check the size
// Using switch…case statement.
class Main {
public static void main (String [] args){
int number = 44;
String size ;
// Switch statement to check size
Switch (number){
Case 29:
Size =”samll”;
break;
Case 42:
Size = “ medium”;
break;
// Match the value of week
Case 44 :
Size=”Large”;
break;
Case 48:
Size = “ Extra large”;
break;
default:
Size= “ unknown”;
break;
}
[Link](“size:”+size);
}
}
Output:
Practical : 5
For Loop Statement
Program Code: 1
class main {
public static void main (String [] args){
[Link](“command-line arguments are”);
//Loop through all arguments for (String str: args){
[Link] (str);
}
}
}
Output:
Program Code: 2
//Program to print a text 5 times
class main {
public static void main (String [] args){
int n=5;
// for Loop
For (int I = 1; i<=n;++I){
[Link](“Techbajao”);
}
}
}
Output:
Program Code: 3
public class main {
public static void main (String [] args){
int rows =5,k=0;
for (int I=1; I<=rows;++I,k=0){
for (int space=1; space<=rows-I;++ space){
[Link](“”);
}
While(k!=2*i-1){
[Link](“*”);
++K;
}
[Link]();
}
}
}
Output:
Practical: 6
While, Do- While Statement
Program Code: 1
Public class DoWhileExample {
Public static void main(String[] args) {
Int i=1;
Do{
[Link](i);
I++;
}while(i<=10);
}
}
Output:
Program Code : 2
class MyLoop
{
public static void main (String args [])
{
int I=1;
do
{
[Link](i);
I++;
}
While(I<=50);
}
}
Output:
Practical: 7 And 8
Implementation of implicit type casting.
Program Code: 1
Public class ImplicitTypecastingExample {
Public static void main(String args[]) {
Byte p = 12;
[Link](“byte value : “+p);
// Implicit Typecasting
Short q = p;
[Link](“short value : “+q);
Int r = q;
[Link](“int value : “+r);
Long s = r;
[Link](“long value : “+s);
Float t = s;
[Link](“float value : “+t);
Double u = t;
[Link](“double value : “+u); }
}
Output:
Program Code: 2
// Java Program to Illustrate Automatic Type Conversion
// Main class
Class GFG {
// Main driver method
Public static void main(String[] args)
{
Int I = 100;
// Automatic type conversion
// Integer to long type
Long l = I;
// Automatic type conversion
// Automatic type conversion
// long to float type
Float f = l;
// Print and display commands
[Link](“Int value “ + i);
[Link](“Long value “ + l);
[Link](“Float value “ + f);
}
}
Output:
Practical: 9
Implementation of explicit type conversion.
Program Code: 1
// Java program to Illustrate Explicit Type Conversion
// Main class
Public class GFG {
// Main driver method
Public static void main(String[] args)
{
// Double datatype
Double d = 100.04;
// Explicit type casting by forcefully getting
// data from long datatype to integer type
Long l = (long)d;
// Explicit type casting
Int I = (int)l;
// Print statements
[Link](“Double value “ + d);
// While printing we will see that
// fractional part lost
[Link](“Long value “ + l);
// While printing we will see that
// fractional part lost
[Link](“Int value “ + i);
}
}
Output:
Program Code: 2
// Java Program to Illustrate Conversion of
// Integer and Double to Byte
// Main class
Class GFG {
// Main driver method
Public static void main(String args[])
{
// Declaring byte variable
Byte b;
// Declaring and initializing integer and double
Int I = 257;
Double d = 323.142;
// Display message
[Link](“Conversion of int to byte.”);
// I % 256
B = (byte)I;
// Print commands
[Link](“I = “ + I + “ b = “ + b);
[Link](
“\nConversion of double to byte.”);
// d % 256
B = (byte)d;
// Print commands
[Link](“d = “ + d + “ b= “ + b);
}
}
Output:
Practical: 10
Implementation of constructor and multiple constructors.
Program Code: 1
//Java Program to create and call a default constructor
Class Bike1{
//creating a default constructor
Bike1(){[Link](“Bike is created”);}
//main method
Public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Output:
Program Code: 2
//Java Program to demonstrate the use of the parameterized constructor.
Class Student4{
Int id;
String name;
//creating a parameterized constructor
Student4(int I,String n){
Id = I;
Name = n;
}
//method to display the values
Void display(){[Link](id+” “+name);}
Public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
//calling method to display the values of object
[Link]();
[Link]();
}
}
Output:
Program Code: 3
Public class ComplexNumber{
//for real and imaginary parts of complex numbers
Double real, img;
//constructor to initialize the complex number
ComplexNumber(double r, double i){
[Link] = r;
[Link] = I;
}
Public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2)
{
//creating a temporary complex number to hold the sum of two numbers
ComplexNumber temp = new ComplexNumber(0, 0);
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
//returning the output complex number
Return temp;
}
Public static void main(String args[]) {
ComplexNumber c1 = new ComplexNumber(5.5, 4);
ComplexNumber c2 = new ComplexNumber(1.2, 3.5);
ComplexNumber temp = sum(c1, c2);
[Link](“Sum is: “+ [Link]+” + “+ [Link] +”I”);
}
}
Output:
Practical: 11 And 12
Implementation of different functions of String Class.
Program Code: 1
Public class StringMethodsDemo {
Public static void main(String[] args) {
String targetString = “Java is fun to learn”;
String s1= “JAVA”;
String s2= “Java”;
String s3 = “ Hello Java “;
[Link](“Char at index 2(third position): “ + [Link](2));
[Link](“After Concat: “+ [Link](“-Enjoy-“));
[Link](“Checking equals ignoring case: “ +[Link](s1));
[Link](“Checking equals with case: “ +[Link](s1));
[Link](“Checking Length: “+ [Link]());
[Link](“Replace function: “+ [Link](“fun”, “easy”));
[Link](“SubString of targetString: “+ [Link](8));
[Link](“SubString of targetString: “+ [Link](8, 12));
[Link](“Converting to lower case: “+ [Link]());
[Link](“Converting to upper case: “+ [Link]());
[Link](“Triming string: “ + [Link]());
[Link](“searching s1 in targetString: “ + [Link](s1));
[Link](“searching s2 in targetString: “ + [Link](s2));
Char [] charArray = [Link]();
[Link](“Size of char array: “ + [Link]);
[Link](“Printing last element of array: “ + charArray[3]);
}
}
Output:
Program Code: 2
Class Str
{
Public static void main( String args[] )
{
StringBuffer s = new StringBuffer(“Coding Atharva”);
[Link](“\n String = “+s); // Will Print the string
[Link](“\n Length = “+[Link]() ); // total numbers of characters
[Link](“\n Length = “+[Link]() ); // total allocated capacity
[Link](6); // Sets the length and destroy the remaining characters
[Link](“\n After setting length String = “+s );
[Link](0,’K’); // It will change character at specified position
[Link](“\n SetCharAt String = “+s );
[Link](0,’C’);
int a = 007;
[Link](a); // It concatenates the other data type value
[Link](“\n Appended String = “+s );
[Link](6,” Atharva”); // used to insert one string or char or object
[Link](“\n Inserted String = “+s );
[Link]();
[Link](“\n Reverse String = “+s );
[Link]();
[Link](6,14); // used to delete sequence of character
[Link](“\n\n After deleting string=”+s);
}
}
Output:
Practical: 13
Implementation of Arrays in Java.
Program Code: 1
//Java Program to illustrate the use of multidimensional array
Class Testarray3{
Public static void main(String args[]){
//declaring and initializing 2D array
Int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
For(int i=0;i<3;i++){
For(int j=0;j<3;j++){
[Link](arr[i][j]+” “);
}
[Link](); }
}
}
Output:
Program Code: 2
//Java Program to print the array elements using for-each loop
Class Testarray1{
Public static void main(String args[]){
Int arr[]={33,3,4,5};
//printing array using for-each loop
For(int i:arr)
[Link](i);
}
}
Output:
Practical: 14
Implementation of Vectors in Java.
Program Code: 1
Import [Link].*;
Public class VectorInsertElementAtExample1 {
Public static void main(String arg[]) {
//Create an empty vector
Vector<Integer> vec = new Vector<>();
//Add element in the vector
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
//Printing the element
[Link](“Element in vector before insertion = “+vec);
//Insert the element at 2nd position
[Link](700, 2);
[Link](“Element in vector after insertion = “+vec); }
}
Output:
Program Code: 2
Import [Link].*;
Public class VectorExample2 {
Public static void main(String args[]) {
//Create an empty Vector
Vector<Integer> in = new Vector<>();
//Add elements in the vector
[Link](100);
[Link](200);
[Link](300);
[Link](200);
[Link](400);
[Link](500);
[Link](600);
[Link](700);
//Display the vector elements
[Link](“Values in vector: “ +in);
//use remove() method to delete the first occurrence of an element
[Link](“Remove first occourence of element 200: “+[Link]((Integer)200));
//Display the vector elements afre remove() method
[Link](“Values in vector: “ +in);
//Remove the element at index 4
[Link](“Remove element at index 4: “ +[Link](4));
[Link](“New Value list in vector: “ +in);
//Remove an element
[Link](5);
//Checking vector and displays the element
[Link](“Vector element after removal: “ +in);
//Get the hashcode for this vector
[Link](“Hash code of this vector = “+[Link]());
//Get the element at specified index
[Link](“Element at index 1 is = “+[Link](1));
}
}
Output:
Practical: 15 And 16.
Implementation of Wrapper Class to convert primitive into object and object into primitive.
Program Code: 1
// Java program to illustrate
// various Integer methods
Public class Integer_test {
Public static void main(String args[])
{
Int b = 55;
String bb = “45”;
// Construct two Integer objects
Integer x = new Integer(b);
Integer y = new Integer(bb);
// toString()
[Link](“toString(b) = “
+ [Link](b));
// toHexString(),toOctalString(),toBinaryString()
// converts into hexadecimal, octal and binary
// forms.
[Link](“toHexString(b) =”
+ [Link](b));
[Link](“toOctalString(b) =”
+ [Link](b));
[Link](“toBinaryString(b) =”
+ [Link](b));
// valueOf(): return Integer object
// an overloaded method takes radix as well.
Integer z = [Link](b);
[Link](“valueOf(b) = “ + z);
Z = [Link](bb);
[Link](“ValueOf(bb) = “ + z);
Z = [Link](bb, 6);
[Link](“ValueOf(bb,6) = “ + z);wel
// parseInt(): return primitive int value
// an overloaded method takes radix as well
Int zz = [Link](bb);
[Link](“parseInt(bb) = “ + zz);
Zz = [Link](bb, 6);
[Link](“parseInt(bb,6) = “ + zz);
// getInteger(): can be used to retrieve
// int value of system property
Int prop
= [Link](“[Link]”);
[Link](
“getInteger([Link]) = “ + prop);
[Link](“getInteger(abcd) =”
+ [Link](“abcd”));
// an overloaded getInteger() method
// which return default value if property not found.
[Link](
“getInteger(abcd,10) =”
+ [Link](“abcd”, 10));
// decode() : decodes the hex,octal and decimal
// string to corresponding int values.
String decimal = “45”;
String octal = “005”;
String hex = “0x0f”value
Integer dec = [Link](decimal);
[Link](“decode(45) = “ + dec);
Dec = [Link](octal);
[Link](“decode(005) = “ + dec);
Dec = [Link](hex);
[Link](“decode(0x0f) = “ + dec);
// rotateLeft and rotateRight can be used
// to rotate bits by specified distance
Int valrot = 2;
[Link](
“rotateLeft(0000 0000 0000 0010 , 2) =”
+ [Link](valrot, 2));
[Link](
“rotateRight(0000 0000 0000 0010,3) =”
+ [Link](valrot, 3));
}
}
Output:
Program Code: 2
Public class MyStringToInteger {
Public static void main(String a[]){
String str = “23”;
Integer I = [Link](str);
[Link](“The integer value: “+i);
}
}
Output:
Program Code: 3
Public class CharacterClassExample {
Public static void main(String[] args) {
Char ch1, ch2;
Ch1 = ‘9’;
Ch2 = ‘V’;
Boolean b1, b2;
B1 = [Link](ch1);
B2 = [Link](ch2);
String str1 = ch1 + “ is a digit is “ + b1;
String str2 = ch2 + “ is a digit is “ + b2;
[Link]( str1 );
[Link]( str2 );
}
}
Output:
Program Code: 4
Public class IntegerToNumericPrimitiveTypesExample {
Public static void main(String[] args) {
Integer intObj = new Integer(“10”);
//use byteValue method of Integer class to convert it into byte type.
Byte b = [Link]();
[Link](b);
//use shortValue method of Integer class to convert it into short type.
Short s = [Link]();
[Link](s);
//use intValue method of Integer class to convert it into int type.
Int I = [Link]();
[Link](i);
//use floatValue method of Integer class to convert it into float type.
Float f = [Link]();
[Link](f);
//use doubleValue method of Integer class to convert it into double type.
Double d = [Link]();
[Link](d);
}
}
Output:
Practical: 17
Implementation the concept of overriding.
Program Code: 1
//Java Program to illustrate the use of Java Method Overriding
//Creating a parent class.
Class Vehicle{
//defining a method
Void run(){[Link](“Vehicle is running”);}
}
//Creating a child class
Class Bike2 extends Vehicle{
//defining the same method as in the parent class
Void run(){[Link](“Bike is running safely”);}
Public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
[Link]();//calling method
}
}
Output:
Program Code: 2
Class Bank{
Int getRateOfInterest(){return 0;}
}
//Creating child classes.
Class SBI extends Bank{
Int getRateOfInterest(){return 8;}
}
Class ICICI extends Bank{
Int getRateOfInterest(){return 7;}
}
Class AXIS extends Bank{
Int getRateOfInterest(){return 9;}
}
//Test class to create objects and call the methods
Class Test2{
Public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
[Link](“SBI Rate of Interest: “+[Link]());
[Link](“ICICI Rate of Interest: “+[Link]());
[Link](“AXIS Rate of Interest: “+[Link]());
}
}
Output:
Program Code: 3
Class Animal
{
Animal getObj()
{
[Link](“Animal object”);
Return new Animal();
}
}
Class Dog extends Animal
{
Dog getObj() //Legal override after Java5 onward
{ [Link](“Dog object”);
Return new Dog();
}
Public static void main(String[] args) {
New Dog().getObj();
}
}
Output:
Practical: 18
Implementation of single and Multilevel inheritance.
Program Code: 1
// Java program to illustrate the
// concept of single inheritance
Import [Link].*;
Import [Link].*;
Import [Link].*;
Class one {
Public void print_geek()
{
[Link](“Geeks”);
}
}
Class two extends one {
Public void print_for() { [Link](“for”); }
}
// Driver class
Public class Main {
Public static void main(String[] args)
{
Two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
Program Code: 2
// Java program to illustrate the
// concept of Multilevel inheritance
Import [Link].*;
Import [Link].*;
Import [Link].*;
Class one {
Public void print_geek()
{
[Link](“Geeks”);
}
}
Class two extends one {
Public void print_for() {
[Link](“for”);
}
}
Class three extends two {
Public void print_geek()
{
[Link](“Geeks”);
}
}
// Drived class
Public class Main {
Public static void main(String[] args)
{
Three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
Program Code: 3
Class Car{
Public Car()
{
[Link](“Class Car”);
}
Public void vehicleType()
{
[Link](“Vehicle Type: Car”);
}
}
Class Maruti extends Car{
Public Maruti()
{
[Link](“Class Maruti”);
}
Public void brand()
{
[Link](“Brand: Maruti”);
}
Public void speed()
{
[Link](“Max: 90Kmph”);
}
}
Public class Maruti800 extends Maruti{
Public Maruti800()
{
[Link](“Maruti Model: 800”);
}
Public void speed()
{
[Link](“Max: 80Kmph”);
}
Public static void main(String args[])
{
Maruti800 obj=new Maruti800();
[Link]();
[Link]();
[Link]();
}
}
Output:
Program Code: 4
Class Room
{
Int length,width;
Room(int a,int b)
{
Length = a;
Width = b;
}
Void area()
{
Int area = length*width;
[Link](“The area of the room is “ +area);
}
}
Class roomvol extends Room
{
Int height;
Roomvol(int a,int b,int c)
{
Super(a,b);
Height = c;
}
Void volume()
{
Int volume = length*width*height;
[Link](“The volume of the room is “ +volume);
}
}
Class inheritance3
{
Public static void main(String args[])
{
Roomvol room2 = new roomvol(10,40,20);
[Link]();
[Link]();
}
}
Output: