Professional Documents
Culture Documents
import java.io.*;
import java.util.*;
Output:-
===================================================================================
=============================================================
/*consider a method with two strings parameters as arguments and concat them
overload the same method with 3 string arguments and concat them*/
import java.io.*;
public class ofum {
output:-
hiLekha
dumbuuisback
===================================================================================
==============================================================
/*Fibonacii Series */
/* write a program to display number in 0 1 1 2 3 5 8.....pattern*/
output:-
Fibonacii Series
0
1
1
2
3
5
8
13
21
34
===================================================================================
===============================================================
/*Library fine*/
/*if the date between issue date and return date is >10 means fine is 1 rupee per
day if >20 fine is ruppes 2 else fine is 3 and cancel memner ship*/
import java.util.Scanner;
output:-
{
vowels v=new vowels();
v.check("Lekha");
Output:-
import java.util.Scanner;
Output:-
Enter age
198
Exception isillegalage: Invalid age
===================================================================================
===============================================================
/*Exception handling*/
}
output:-
The exception is:java.lang.ArithmeticException: / by zero
===================================================================================
=====================================================================
/*password validating it should have 15-60 characters and do not contain spaces*/
}
}
else
{
System.out.println("Invalid pswd");
}
if(count==1)
System.out.println("Password is valid and pswd is "+s);
}
public static void main(String[] args)
{
passwords p=new passwords();
p.check("hellolekunanabngaru");
}
Output:
===================================================================================
================================================================
/*password should be between 1-60 characteres and shouls not contain * , . ,$*/
}
}
else
{
System.out.println("Invalid pswd");
}
if(count==0)
System.out.println("Password is invalid and pswd is "+s);
else
System.out.println("password is valid");
}
public static void main(String[] args)
{
passwords p=new passwords();
p.check("hellolekunanabngaru$");
}
Output:-
===================================================================================
=============================================================
/*Taxi softwre if taxi is late by 15 min amount is 50% should be paid else late by
30 min amount should be paid is 40% .if late by 60 min free ride*/
import java.util.Scanner;
}
else
{
System.out.println("Free journey");
}
}
Output:-
or
There are two types of polymorphism in java: compile time polymorphism and runtime
polymorphism.
We can perform polymorphism in java by method overloading and method overriding.
class Bike
{
void run()
{
System.out.println("running");
}
}
class Splender extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
public static void main(String args[]){
Splender b = new Splender();//Overriding
b.run();
}
}
OutPut:-
Output:-
Element is10
sum is :30
===================================================================================
================================================================
Output:-
Sum is 30
===================================================================================
=================================================================
/*Write a method that accepts String as argument and returns array of words*/
Output:-
===================================================================================
==================================================================
/*write a method that accepts string as argument and return second word and its
length*/
public class arraywords
{
public void check(String s)
{
String array[]=s.split(" ");
System.out.println("The number of words are : "+array.length);
System.out.println("The second word is :"+array[1]);
System.out.println("The length of the second word
is:"+array[1].length());
}
public static void main(String[] args)
{
arraywords a=new arraywords();
a.check("Lekha is my sweet heart");
}
Output:-
===================================================================================
=====================================================================
Output:-
===================================================================================
=====================================================================
import java.util.Scanner;
Output:-
if passport enter yes else no
yes
Enter ssc percentage
67
Enter inter percentage
87
Enter btech percentage
89
Eligible to fly
===================================================================================
===================================================================
Output:-
Uppercase HELLO
Lowercase hello
===================================================================================
===================================================================
/*get a string from the user and return the third character from it using method*/
Output:-
===================================================================================
======================================================================
import java.util.Calendar;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long todayMilli;
long dateMilli;
long diff;
Output:-
===================================================================================
=====================================================================
/*Encapsulating a variable and performing it in function....some thing like
that...*/
OutPut:-
===================================================================================
===================================================================
/*Enum example*/
class EnumExample1{
}}
output:-
WINTER
SPRING
SUMMER
FALL
===================================================================================
==================================================================
import java.util.*;
class stringrev
{
public static void main(String args[])
{
String original, reverse = "";
Scanner br = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = br.nextLine();
output:-
Enter a string to reverse
lekha
Reverse of entered string is: ahkel
===================================================================================
======================================================================
output:
import java.util.Scanner;
if(trainingPerformance.equalsIgnoreCase("good")&&punctuality.equalsIgnoreCase("good
")
&&behaviour.equalsIgnoreCase("good"))
{
System.out.println("Employee made permanent");
}
else
{
System.out.println("Temp employee");
}
Output:
if(a[i]%3==0)
{
array[j]=a[i];
j++;
count++;
}
}
if(count!=0)
{
System.out.println("The elements that are divisible by 3 are:");
for(int i=0;i<array.length;i++)
{
System.out.println(array[i]);
}
}
else
{
System.out.println("No elements");
}
}
Output:
The elements that are divisible by 3 are:
24
33
36
===================================================================================
============================================================================
/*Finding maximum of three numbes without using and operator*/
import java.util.Scanner;
int a, b, c, d;
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
System.out.println("Largest Number:"+d);
Output:
Enter all three numbers:
5
6
7
Largest Number:7
===================================================================================
============================================================================
/*Find the sum of diagonal elements in an array*/
import java.util.Scanner;
}
System.out.println("Sum of diagonal element is: "+sum);
output:-
Enter values into the array
2
3
4
5
Sum of diagonal element is: 7
===================================================================================
=====================================================================
/*Interface example*/
--------------
public interface BasketballTeam
{
public void printBasketballName();
}
-----------
public interface FootballTeam
{
public void printFootballName();
}
-----------------
@Override
public void printFootballName() {
System.out.println("Football Team: \"" + name + " F.C.\"");
}
@Override
public void printBasketballName() {
System.out.println("Basketball Team: \"" + name + " B.C.\"");
}
/*Inheritance example*/
class Employee
{
float salary=40000;
}
------------
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:-
Programmer salary is:40000.0
Bonus of programmer is:10000
===================================================================================
==================================================================
/*password check with it should not contain special char like @ % #
and should not end with .*/
}
}
else
{
System.out.println("Invalid pswd");
}
if(count==1)
System.out.println("Password is invalid and pswd is "+s);
else
System.out.println("password is valid");
}
public static void main(String[] args)
{
passwords p=new passwords();
p.check("hellolekunanabngaru$");
}
Output:-
password is valid
===================================================================================
==================================================================
/*read a sentence and that should have 10 characters
should have space and should not contain .symbol
*/
}
}
else
{
System.out.println("Invalid sting");
}
if(count==0)
System.out.println("string is invalid and string is "+s);
else
System.out.println("string is valid");
}
public static void main(String[] args)
{
passwords p=new passwords();
p.check("good morin");
}
Output:
string is valid
===================================================================================
=========================================================================
/*admission for child if age>3 and parents educated and income>2lks*/
import java.util.Scanner;
}
Output:-
Enter age of child
4
Educational status of parents educated/not
educated
Income of parents
500000
Admission given to the child
===================================================================================
=====================================================================
/*calculate the discount price according to the bill amount if bill amount is >5000
10% discount
>10000 means 20%discount >20000 means 30% discount*/
import java.util.Scanner;
}
Output:-
Enter bill amount
20000
Bill amount is:16000.0
===================================================================================
=======================================================================
}
}
0utput:-
1
2
2
4
8
32
256
===================================================================================
====================================================================
/*email id validation @cant be at first place and @will be only once and length
should be 10-30*/
if(s.length()>10&&s.length()<=30)
{
if(s.matches("[a-zA-Z0-9\\.]+@[a-zA-Z0-9\\-\\_\\.]+\\.[a-zA-Z0-9]{3}"))
{
return true;
}
else
{
return false;
}
}
return false;
}
public static void main(String d[])
{
EmailValidator e=new EmailValidator();
boolean b=e.emailvalidate("chaithanyacsit@gmail.com");
if(b)
{
System.out.println("valid email");
}
else
{
System.out.println("Invalid email");
}
}
}
Output:-
valid email
===================================================================================
=====================================================================
/*sort a array of elements by using template function*/ same type of cose for all
template codes
import java.util.*;
public class Details
{
/*Unsorted List*/
System.out.println("Before Sorting:");
for(String counter: listofcountries)
{
System.out.println(counter);
}
/* Sort statement*/
Collections.sort(listofcountries);
/* Sorted List*/
System.out.println("After Sorting:");
for(String counter: listofcountries)
{
System.out.println(counter);
}
}
}
Output:-
Before Sorting:
India
US
China
Denmark
After Sorting:
China
Denmark
India
US
OR
-----
import java.util.*;
public class ArrayListOfInteger
{
Output:-
Before Sorting:
11
2
7
3
After Sorting:
2
3
7
11
===================================================================================
=====================================================================
---
public class interfaceeg implements operations
{
Output:
sum30
sub10
mul6
div5
===================================================================================
===========================================================
output:-
Sum is30
===================================================================================
==========================================================
/*how to access static in an int method*/
Output:-
The sum is 30
===================================================================================
===============================================================
/*count of an odd number in an array list*/
Output:-
class strng
{
public void space(String s)
{
String i=s.replaceAll(" ", "");
System.out.println("The length of the string without
spaces :"+i.length());
}
}
public class stringoverride extends strng
{
}
Output:-
Output:-
System.out.println("Odd elements");
for(int i=0;i<ocount;i++)
{
System.out.println(oarray[i]);
}
System.out.println("Total number of odd elements:"+ocount);
System.out.println("even elements");
for(int i=0;i<ecount;i++)
{
System.out.println(earray[i]);
}
System.out.println("Total number of even elements:"+ecount);
}
}
output:-
Odd elements
1
3
5
Total number of odd elements:3
even elements
2
4
Total number of even elements:2
===================================================================================
===============================================================
/*sum of individual elements in an array and reversing it*/
digit=number%10;
dsum=dsum+digit;
sum=(sum*10)+digit;
number=number/10;
}
System.out.println("The reversed element is:"+sum);
System.out.println("The sum of element is"+dsum);
}
Output:
The reversed element is:32
The sum of element is5
The reversed element is:34
The sum of element is7
The reversed element is:334
The sum of element is10
===================================================================================
================================================================
/*leap year*/
}
Output:-
leap years are:
2000
1996
2012
===================================================================================
==================================================================
}
Output:-
Output:-
import java.util.Scanner;
}
System.out.println("Sum of diagonal element is: "+sum);
Output:-
Enter values into the array
1
2
3
1
Sum of diagonal element is: 2
===================================================================================
==============================================================
/*check whether diagonal elements are same are not */
import java.util.Scanner;
}
for(int k=0;k<2;k++)
{
for(int m=0;m<2;m++)
{
if(k==m)
{
value[n]=array[k][m];
n++;
}
}
}
if(value[0]==value[1])
{
count=1;
}
else
{
count=0;
}
if(count==1)
{
System.out.println("Diagonal elements are same");
}
else
{
System.out.println("Not same");
}
}
}
Output:-
Enter values into the array
1
2
3
1
Diagonal elements are same
===================================================================================
============================================================
/* return sum of array elements using function*/
}
OutPut:-
Sum of array elements is :15
===================================================================================
=======
======================================================
Output:-
Sum of array elements is :15
===================================================================================
================================================================
import java.util.Scanner;
}
Output:-
/*Overriding example*/
class Bike
{
void run()
{
System.out.println("running");
}
}
class Splender extends Bike
{
void run()
{
System.out.println("running safely with 60km");
}
import java.util.Scanner;
if(goodCommunication.equalsIgnoreCase("good")&&technicalSkills.equalsIgnoreCase("go
od")
&&behaviour.equalsIgnoreCase("good"))
{
System.out.println("Employee promoted");
}
else
{
System.out.println("Temp employee");
}
}
}
Output:-
goodCommunication good/bad:?
good
Behaviour good/bad:?
good
technicalSkills good/bad:?
good
Employee promoted
===================================================================================
==============================================================
/* display if bookcost<=500-normal book
bookcost>500&&bookcost<=1000-costlybook
bookcost>1000-precious book*/
import java.util.Scanner;
}
Output:-
Enter the book cost
3989
Precious book
===================================================================================
==============================================================
/*consider a 2d array and display sum of non dimensional elements*/
import java.util.Scanner;
}
for(int k=0;k<3;k++)
{
for(int m=0;m<3;m++)
{
if(k!=m)
{
sum=sum+array[k][m];
}
}
}
System.out.println("Sum of non diagonal elements is"+sum);
Output:-
}
---------
Output:-
900
===================================================================================
=====================================================================
/*nth element in an array*/
}
===================================================================================
=====================================================================
/*second word in the string*/
Output:
welcome
===================================================================================
================================================================
/*static example*/
class a
{
public static int s=9;
public int y=8;
static
{
System.out.println("Welcome");
}
static
{
System.out.println("Lekha is a angry girl");
}
public static void show()
{
System.out.println(s);
//System.out.println(y);
}
public void dis()
{
System.out.println(s);
// System.out.println(y);
}
}
public class statick extends a{
}
output:-
welcome
lekha is angry girl
===================================================================================
==========================================================