Core Java Program Set 2
Program 11: SortDemo.java
//Bubble Sort in Java
public class SortDemo
{
public static void main(String[] args)
{
int
arr[] ={3,60,35,2,45,320,5};
int
n = arr.length;
System.out.println("Array
Before Sort");
for(int
i=0; i < n; i++)
{
System.out.print(arr[i]
+ " ");
}
System.out.println();
int temp = 0;
for(int
i=0; i < n; i++){
for(int
j=1; j < (n-i); j++)
{
if(arr[j-1]
> arr[j]){
//swap
elements
temp
= arr[j-1];
arr[j-1]
= arr[j];
arr[j]
= temp;
}
}
}
System.out.println("Array
After Sort");
for(int
i=0; i < n; i++){
System.out.print(arr[i]
+ " ");
}
}
}
Program 12: PrimeExample.java
//Prime Number Program in Java
import java.util.Scanner;
public class PrimeExample
{
public
static void main(String args[])
{
int
i,n,flag=0;
Scanner
sc=new Scanner(System.in);
System.out.println("Enter
number you want to check:");
n=sc.nextInt();
if(n==0||n==1)
{
System.out.println(n+"
is not prime number");
}
else
{
for(i=2;i<n;i++)
{
if(n%i==0)
{
System.out.println(n+"
is not prime number");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(n+"
is prime number");
}
}//end
of else
}
}
Program 13: MyDateTime.java
import java.util.Date;
public class MyDateTime
{
public static void main(String a[])
{
Date d = new Date();
System.out.println("Todays Date is: "+d);
System.out.println("Current Date: "+d.getDate());
System.out.println("Current Day: "+d.getDay());
System.out.println("Current Year: "+d.getYear());
System.out.println("Current Hours: "+d.getHours());
System.out.println("Current Minutes: "+d.getMinutes());
System.out.println("Current Seconds: "+d.getSeconds());
long millis = System.currentTimeMillis();
System.out.println("Current time in millis: "+millis);
d
= new Date(millis);
System.out.println(d);
Date
current = new Date();
//create
a date one day after current date
long nextDay = System.currentTimeMillis() + 1000*60*60*24;
//create date object
Date next = new Date(nextDay);
long
prevDay = System.currentTimeMillis() - 1000*60*60*24;
//create date object
Date prev = new Date(prevDay);
System.out.println("current
date is :" + current);
System.out.println("prev
date is :" + prev);
System.out.println("next
date is :" + next);
//compare
both dates
if(next.after(current)){
System.out.println("The date is future day");
} else {
System.out.println("The date is older than current day");
}
//compare both dates
if(prev.before(current)){
System.out.println("The date is older than current day");
} else {
System.out.println("The date is future day");
}
}
}
Program 14: StringDemo.java
public class StringDemo
{
public
static void main(String[] args)
{
String str = "Java String";
System.out.println("Original
String = " + str);
// Get the character at positions 0 and 10.
int
index1 = str.charAt(0);
int
index2 = str.charAt(5);
// Print out the results.
System.out.println("The
character at position 0 is " + (char)index1);
System.out.println("The
character at position 5 is " + (char)index2);
System.out.println("The
string length of '"+str+"' is: "+str.length());
System.out.println("String
in lowercase: " + str.toLowerCase());
System.out.println("String
in uppercase: " + str.toUpperCase());
String
substr = str.substring(5, 11);
System.out.println("The
sub string is: "+ substr);
String
str1 = "This is Exercise 1";
String str2 = "This is Exercise 2";
//
Concatenate the two strings together.
String str3 = str1.concat(str2);
// Display the new String.
System.out.println("The
concatenated string: " + str3);
//
Compare the two strings.
int
result = str1.compareTo(str2);
//
Display the results of the comparison.
if
(result < 0)
{
System.out.println("\""
+ str1 + "\"" + " is less than " +
"\""
+ str2 + "\"");
}
else
if (result == 0)
{
System.out.println("\""
+ str1 + "\"" + " is equal to " +
"\""
+ str2 + "\"");
}
else
// if (result > 0)
{
System.out.println("\""
+ str1 + "\"" + " is greater than " +
"\""
+ str2 + "\"");
}
String
inputValue = " Matrumandir ";
System.out.println(inputValue);
//
removing the white spaces
String
newValue = inputValue.trim();
System.out.println(newValue);
}
}
Program 15: StrBufDemo.java
public class StrBufDemo
{
public
static void main(String[] args)
{
StringBuffer
sb1 = new StringBuffer("Hello Java World");
System.out.println(sb1);
sb1.delete(6,
11);
System.out.println("Delete
method demo: " + sb1);
sb1.insert(5,
" Hi");
System.out.println("Inser
Operation: "+sb1);
sb1.replace(6,
8, "My");
System.out.println("Replace
Operation: "+sb1);
StringBuffer
sb4 = new StringBuffer("ABCDE");
System.out.println("Reverse
of ABCDE: "+ sb4.reverse());
sb4.setCharAt(3,
'x');
System.out.println("Replacing
char at index 3: "+ sb4);
}
}
Program 16: VectorDemo.java
import java.util.*;
public class VectorDemo
{
public static void main(String a[])
{
Vector v1 = new Vector();
//adding
elements to the end
v1.add("23.52");
v1.add("48.75");
v1.add("44.44");
v1.add("First");
v1.add("Second");
v1.add("Third");
v1.add("1");
v1.add("2");
v1.add("3");
System.out.println(v1);
//adding element at specified index
v1.add(2,"Random");
System.out.println(v1);
//getting elements by index
System.out.println("Element at index 3 is: "+v1.get(3));
//getting first element
System.out.println("The first element of this vector is:
"+v1.firstElement());
//getting last element
System.out.println("The last element of this vector is:
"+v1.lastElement());
//how to check vector is empty or not
System.out.println("Is this vector empty? "+v1.isEmpty());
System.out.println("Vector
Size:"+v1.size());
System.out.println("Display
Vector using Iterator");
Iterator
itr = v1.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println("Display
Vector using Enumeration");
Enumeration
enm = v1.elements();
while(enm.hasMoreElements()){
System.out.println(enm.nextElement());
}
v1.clear();
System.out.println("After clear vector:"+v1);
System.out.println("Is
this vector empty? "+v1.isEmpty());
}
}
Program 17: HashtableDemo.java
import java.util.*;
public class HashtableDemo
{
public static void main(String a[])
{
//Create hashtable instance
Hashtable ht = new Hashtable();
//add key-value pair to hashtable
ht.put("one", "1 INSERTED");
ht.put("two", "2 INSERTED");
ht.put("three","2 INSERTED");
System.out.println(ht);
//getting value for the given key from hashtable
System.out.println("Value of key 'two': "+ht.get("two"));
System.out.println("Is Hashtable empty? "+ht.isEmpty());
ht.remove("three");
System.out.println(ht);
System.out.println("Size of the Hashtable: "+ht.size());
if(ht.containsKey("one")){
System.out.println("The Hashtable contains key one");
} else {
System.out.println("The Hashtable does not contains key one");
}
if(ht.containsValue("one")){
System.out.println("The Hashtable contains value one");
} else {
System.out.println("The Hashtable does not contains value
one");
}
}
}
Comments
Post a Comment