Professional Documents
Culture Documents
Exception
Exception
import java.util.LinkedList;
import java.util.Queue;
TestLinkedList.testIt();
}
class TestLinkedList{
for(int i=0;i<=5;i++) {
queue.add(i);
}
System.out.println("Printing Before operations...");
System.out.println(queue.size());
System.out.println(queue);
// System.out.println("Poll operation...");
// queue.poll();
//
// System.out.println(queue.size());
// System.out.println(queue);
System.out.println("Peek operation...");
queue.peek();
System.out.println(queue.size());
System.out.println(queue);
}
}
---------------------
package collections2.comparableANDcomparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
this.size = size;
this.brand = brand;
}
}
}
int a=tv1Brand.compareTo(tv2Brand);
return a;
}
}
al.add(tv1);
al.add(tv2);
al.add(tv3);
al.add(tv4);
System.out.println("Before Sorting..............");
Iterator<HDTV> itrr=al.iterator();
while (itrr.hasNext()) {
HDTV tv=itrr.next();
System.out.println(tv.getSize()+"\t"+tv.getBrand());
}
Collections.sort(al,new SizeComparator());
System.out.println("After Sorting..............");
Iterator<HDTV> itr=al.iterator();
while (itr.hasNext()) {
HDTV tv=itr.next();
System.out.println(tv.getSize()+"\t"+tv.getBrand());
}
System.out.println("---------------------------------------------");
System.out.println("Before Sorting..............");
for (HDTV a : al) {
System.out.println(a.getBrand());
}
System.out.println("---------------------------------------------");
System.out.println("After Sorting..............");
for (HDTV a : al) {
System.out.println(a.getBrand());
}
}
}
--------------
package comparableANDcomparator;
this.size = size;
this.brand = brand;
System.out.println("------------------------------------------"+"\n");
@Override
public int compareTo(HDTVV tv) {
/*
if (tv1.compareTo(tv2)>0) {
int b=tv2.compareTo(tv1);
System.out.println(b); // 1
if(a>b)
{
System.out.println(tv1.getBrand() + " is better.");
}else if (a<b) {
}else {
}
}
---------------
package collections2.equalsandhashcode;
int a=3;
int b=3;
if(s1==s2)
System.out.println("s1 and s2 are equal");
else
System.out.println("s1 and s2 are not equal");
if(a==b)
System.out.println("a and b are equal");
else
System.out.println("a and b are not equal");
}
------------
package collections2.equalsandhashcode;
import java.util.ArrayList;
import java.util.HashSet;
//If we want to compare two objects based upon equals() method, then
//we have to override both equals() and hashCode() .
Emp(int age)
{
System.out.println("Emp constructor called......"+age);
this.age=age;
@Override
public boolean equals(Object o)
{
Emp e=(Emp)o;
if(this.age==e.age)
return true;
else
return false;
@Override
public int hashCode()
{
return age;
}
}
System.out.println("Starting main.....");
System.out.println("Ending main.....");
}
-----------------
package collections2.equalsandhashcode;
import java.util.ArrayList;
import java.util.HashSet;
//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)
{
System.out.println("Emp constructor called......"+age);
this.age=age;
@Override
public boolean equals(Object o)
{
Emp e=(Emp)o;
if(this.age==e.age)
return true;
else
return false;
@Override
public int hashCode()
{
return age;
}
}
System.out.println("Starting main.....");
System.out.println("Ending main.....");
}
----------------
import java.util.*;
}
}
}
-------------------
import java.util.*;
public class ArrayException {
public int size,index;
public int arr[];
public String getPriceDetails()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
try {
size=sc.nextInt();
size=Integer.parseInt(String.valueOf(size));
arr=new int[size];
System.out.println("Enter the price details");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Enter the index of the array element you want to
access");
index=sc.nextInt();
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(System.in);
ArrayException ex=new ArrayException();
System.out.println(ex.getPriceDetails());
}
}
---------------------------