package collectionTest;
import [Link];
import [Link];
public class QueueTest {
public static void main(String[] args) {
[Link]();
}
class TestLinkedList{
public static void testIt() {
Queue<Integer> queue=new LinkedList<Integer>();
for(int i=0;i<=5;i++) {
[Link](i);
}
[Link]("Printing Before operations...");
[Link]([Link]());
[Link](queue);
// [Link]("Poll operation...");
// [Link]();
//
// [Link]([Link]());
// [Link](queue);
[Link]("Peek operation...");
[Link]();
[Link]([Link]());
[Link](queue);
}
}
---------------------
package [Link];
//Comparator interface Example
//Comparator interface is used to compare "two different objects"
import [Link];
import [Link];
import [Link];
import [Link];
class HDTV { // POJO class
private int size;
private String brand;
HDTV(int size, String brand) {
[Link] = size;
[Link] = brand;
}
public int getSize() {
return size;
}
public void setSize(int size) {
[Link] = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
[Link] = brand;
}
class SizeComparator implements Comparator<HDTV> {
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = [Link]();
int tv2Size = [Link]();
if (tv1Size > tv2Size)
return 1;
else if (tv1Size < tv2Size)
return -1;
else
return 0;
}
}
class BrandComparator implements Comparator<HDTV> {
public int compare(HDTV tv1, HDTV tv2) {
String tv1Brand = [Link]();
String tv2Brand = [Link]();
int a=[Link](tv2Brand);
return a;
}
}
public class ComparatorTest {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung"); // A-65, a-97
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");
HDTV tv4 = new HDTV(77, "LG");
ArrayList<HDTV> al = new ArrayList<HDTV>();
[Link](tv1);
[Link](tv2);
[Link](tv3);
[Link](tv4);
// [Link]() and [Link]();
[Link]("Before Sorting..............");
Iterator<HDTV> itrr=[Link]();
while ([Link]()) {
HDTV tv=[Link]();
[Link]([Link]()+"\t"+[Link]());
}
[Link](al,new SizeComparator());
[Link]("After Sorting..............");
Iterator<HDTV> itr=[Link]();
while ([Link]()) {
HDTV tv=[Link]();
[Link]([Link]()+"\t"+[Link]());
}
[Link]("---------------------------------------------");
[Link]("Before Sorting..............");
for (HDTV a : al) {
[Link]([Link]());
}
[Link]("---------------------------------------------");
[Link](al, new BrandComparator());
[Link]("After Sorting..............");
for (HDTV a : al) {
[Link]([Link]());
}
}
}
--------------
package comparableANDcomparator;
//Comparable interface example
//By Default implemented by String and
//Wrapper classes(Integer,Float,Byte,Short,Character,Long ...)
class HDTVV implements Comparable<HDTVV> {
private int size;
private String brand;
HDTVV(int size, String brand) {
[Link] = size;
[Link] = brand;
[Link]("inside constructor size is:"+[Link]);
[Link]("inside constructor brand is:"+[Link]);
[Link]("------------------------------------------"+"\n");
public int getSize() {
return size;
}
public void setSize(int size) {
[Link] = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
[Link] = brand;
}
@Override
public int compareTo(HDTVV tv) {
if ([Link]() > [Link]())
return 1;
else if ([Link]() < [Link]())
return -1;
else
return 0;
}
public class ComparableTest {
public static void main(String[] args) {
HDTVV tv1 = new HDTVV(60, "Samsung");
HDTVV tv2 = new HDTVV(80, "Sony");
[Link]("TV1 Size is----"+[Link]());
[Link]("TV2 Size is----"+[Link]());
/*
if ([Link](tv2)>0) {
[Link]([Link]() + " is better.");
[Link]("------------------------------"+"\n");
}
else {
[Link]("------------------------------"+"\n");
[Link]([Link]() + " is better.");
}*/
int a=[Link](tv2); //60 and 80
[Link](a); // -1
int b=[Link](tv1);
[Link](b); // 1
if(a>b)
{
[Link]([Link]() + " is better.");
}else if (a<b) {
[Link]([Link]() + " is better.");
}else {
[Link]("both are of equal size");
}
}
---------------
package [Link];
public class PrimitiveEqualTest {
public static void main(String[] args) {
int a=3;
int b=3;
String s1="abc"; //literals
String s2="abc";
if(s1==s2)
[Link]("s1 and s2 are equal");
else
[Link]("s1 and s2 are not equal");
if(a==b)
[Link]("a and b are equal");
else
[Link]("a and b are not equal");
}
------------
package [Link];
import [Link];
import [Link];
//If we want to compare two objects based upon equals() method, then
//we have to override both equals() and hashCode() .
class Emp extends Object
{
int age;
Emp(int age)
{
[Link]("Emp constructor called......"+age);
[Link]=age;
@Override
public boolean equals(Object o)
{
Emp e=(Emp)o;
if([Link]==[Link])
return true;
else
return false;
@Override
public int hashCode()
{
return age;
}
}
public class EqualsHashCodeTest1 {
public static void main(String[] args) {
[Link]("Starting main.....");
Emp e1=new Emp(22);
Emp e2=new Emp(23);
HashSet<Object> hs=new HashSet<Object>();
[Link](e1);
[Link](e2);
[Link]([Link](new Emp(22))); // false
ArrayList<Object> al=new ArrayList<Object>();
[Link](e1);
[Link](e2);
[Link]([Link](new Emp(22))); // false
[Link]("Ending main.....");
}
-----------------
package [Link];
import [Link];
import [Link];
//If we want to compare two objects based upon equals() method, then
//we have to override both equals() and hashCode() .
class Emp extends Object
{
int age;
Emp(int age)
{
[Link]("Emp constructor called......"+age);
[Link]=age;
@Override
public boolean equals(Object o)
{
Emp e=(Emp)o;
if([Link]==[Link])
return true;
else
return false;
@Override
public int hashCode()
{
return age;
}
}
public class EqualsHashCodeTest1 {
public static void main(String[] args) {
[Link]("Starting main.....");
Emp e1=new Emp(22);
Emp e2=new Emp(23);
HashSet<Object> hs=new HashSet<Object>();
[Link](e1);
[Link](e2);
[Link]([Link](new Emp(22))); // false
ArrayList<Object> al=new ArrayList<Object>();
[Link](e1);
[Link](e2);
[Link]([Link](new Emp(22))); // false
[Link]("Ending main.....");
}
----------------
import [Link].*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner([Link]);
int s[]= new int[100];
[Link]("Enter the number of seats to be booked:");
int seats= [Link]();
[Link]("Enter the seat number 1");
int n= [Link]();
[Link]("Enter the seat number 2");
int n2= [Link]();
[Link]("Enter the seat number 3");
int n3= [Link]();
[Link]("Enter the seat number 4");
int n4= [Link]();
[Link]("Enter the seat number 5");
int n5= [Link]();
try {
s[5]= 5;
[Link]("The seats booked are: "+s);
}catch (ArrayIndexOutOfBoundsException e){
[Link](e);
}
}
}
-------------------
import [Link].*;
public class ArrayException {
public int size,index;
public int arr[];
public String getPriceDetails()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the number of elements in the array");
try {
size=[Link]();
size=[Link]([Link](size));
arr=new int[size];
[Link]("Enter the price details");
for(int i=0;i<size;i++)
{
arr[i]=[Link]();
}
[Link]("Enter the index of the array element you want to
access");
index=[Link]();
return("The array element is "+arr[index]);
}
catch(ArrayIndexOutOfBoundsException e)
{
return("Array index is out of range");
}
catch(InputMismatchException e)
{
return("Input was not in the correct format");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
ArrayException ex=new ArrayException();
[Link]([Link]());
}
}
---------------------------