Example of Generic class with two type parameters.
class TwoGen<T,V>
{T ob1;
V ob2;
TwoGen(T o1, V o2)
{ ob1=o1;
ob2=o2;
}
void showTypes()
{System.out.println("Type of T is "+ ob1.getClass().getName());
System.out.println("Type of V is "+ ob2.getClass().getName());
}
T getob1()
{ return ob1;
}
V getob2()
{return ob2;
}
}
class GenDemo2
{public static void main(String a[])
{TwoGen<Integer,String> tgobj= new twoGen<Integer,String>(88,"Generics");
tgobj.showTypes();
int t=tgobj.getob1();
System.out.println("value of t: "+t);
String str =tgobj.getob2();
System.out.println("value of str: "+ str);
}
}
A simple Generics example.
class GenDemo
{ public static void main(String a[])
{ // Create a Gen reference for integers
Gen<Integer> iob;
iob.showType();
int v=iob.getob();
System.out.println("value: "+v);
Gen<String> strob = newGen<String>("Generics Test");
strob.showType();
String str= strob.getob();
System.out.println("value: "+ str);
}
}
class Gen<T>
{T ob;
Gen(T o)
{ob=o;
}
T getob()
{return ob;
}
void showType()
{System.out.println("Type of T is " +ob.getClass().getName());
}
}
Scanner to read various types of data from a file.
This example shows how to use scanner to read various type(like integer, double, boolean, string) of data from a file.
import java.util.*;
import java.lang.io.*;
class ScanMixed{
public static void main(String a[]) throws IOException {
int i;
double d;
boolean b;
String str;
//Write output to a file
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing Scanner 10 12.2 one true two false");
fout.close();
FileReader fin= new FileReader("Test.txt");
Scanner src= new scanner(fin);
// Read to end.
while (src.hasNext()) {
if(src.hasNextInt()) {
i= src.nextInt();
System.out.println("int: "+ i);
}
else if(src.hasNextDouble()) {
d= src.nextDouble ();
System.out.println("double: "+ d);
}
else if(src.hasNextBoolean()) {
b=src.nextBoolean();
System.out.println("boolean: "+b);
}
else {
str =src.next();
System.out.println("String: "+ str);
}
}
fin.close();
}
}
Scanner to read double numbers from a file and display their average.
It uses scanner to read double numbers from a file and display their average. It is assumed that input is terminated by the string “done”.
import java.util.*;
import java.io.*;
class AvgFile{
public static void main(String a[]) throws IOException{
int count =0;
double sum=0;
//Write output to a file.
FileWriter fout= new FileWriter ("test.txt");
fout.write("2 3.4 5 6 7.4 9.1 10.5 done");
fout.close();
FileReader fin= new FileReader ("Test.txt");
Scanner src = new Scanner (fin);
//Read and sum numbers
while(src.hasNext()) {
if(src.hasNextDouble()) {
sum+= src.nextDouble();
count++;
}
else {
String str =src.next();
if(str.equals("done")) break;
else {
System.out.println("ERROR");
return;
}
}
}
fin.close();
System.out.println("Average is " +sum/ count);
}
}
Scanner to read double numbers from keyboard and display their average.
It uses scanner to read double numbers from keyboard and display their average. It is assumed that input is terminated by the string “done”.
import java.util.*;
class AvgNums{
public static void main (String a[]){
Scanner conin = new Scanner (System.in);
int count =0;
double sum=0.0;
System.out.println("Enter Numbers:: ");
// Read and Sum numbers
while (conin.hasNext() ) {
if(conin.hasNextDouble () ) {
sum += conin.nextDouble();
count++;
}
else {
String str = conin.next();
if(string.equals ("done")) break;
else {
System.out.println("ERROR");
return;
}
}
}
System.out.println("Average is "+ sum/ count);
}
}
Demonstrates The overloading Varargs methods.
class VarArgs
{public void Test(int ... v)
{System.out.print("No. of args "+ v.length +"contents ");
for(int i : v)
{System.out.print(i+" ");}
System.out.println();
}
public void Test(boolean ... v)
{System.out.print("No. of args "+ v.length+"contents ");
for(boolean i: v)
{System.out.print(i+ " ");}
System.out.println();
}
public void Test(String str, int ... v)
{System.out.print(str+ v.length+"contents ");
for(boolean i: v)
{System.out.print(i+ " ");}
System.out.println();
}
public static void main(String a[])
{ VarArgs va = nwe VarArgs();
va.Test("Testing me:", 10, 20);
va.Test(11,12,13);
va.Test(true, false, false);
//va.Test(); // Error ambiguity between boolean and integer function
}
}
Demonstrates the VARARGS feature of java for variable arguments along with normal parameters.
Here we demonstrate how we can pass normal parameter along with varible arguments.
class VarExample
{ public void Test(String str, int ... arr)
{System.out.print(str + arr.length + "contents ");
for(int i : arr){
System.out.print(i+" ");}
System.out.println();
}
public static void main(String a[])
{VarExample ve =new VarExample();
ve.Test("First argument",1);
ve.Test("Second argument", 11, 12,13);
ve.Test(" No variable arguments ");
}
}
Demonstrates the VARARGS feature of java for variable arguments.
This example shows how to use VARAGRS feature of java for variable length arguments instead of using array.
class VarExample
{
public void test(int ... arr)
{
System.out.print("No. of arguments "+arr.length+"contents ");
for(int i: arr)
{ System.out.print( i+ " ");
}
System.out.println();
}
public static void main(String a[])
{ VarExample ve= new varExample() ;
va.Test(10);
va.Test(11,12,13,14);
va.Test();
}
}
Variable length argument implemented using array.
This Example demonstrates how we can pass variable argument using array, but this approach is old. Now a days we use Varargs feature included in J2SE 5.
class VariableArr{
public void Test(int[] arr)
{ System.out.println("No. of args "+arr.length+"contents ");
for(int i : arr)
{ System.out.print( i+" ");
}
System.out.println();
}
public static void main(String a[])
{ VariableArr va= nw VariableArr();
int a[]= {20};
int b[]={2,4,6};
int c[]= {};
va.test(a);
va.test(b);
va.test(c);
}
}
Demonstrates the PRECISION modifier in Formatter Class.
A Precision modifier can be applied to any format specifier. It follows the minimum field width specifier(if it is there) and consists of a period followed by an integer. The default precision is 6. For example, %10.4f prints a number at least ten character wide with four decimal places. In case of String, it specifies the maximum field width. For example, %5.7s prints a string at least five and not exceeding seven characters long.
import java.util.*;
class FormatterExample{
public static void main(String a[]){
Formatter fm= new Formatter();
//Format 4 decimal places
fm.format(" %.4f",123.1234567);
System.out.println(fm);
//Format to 2 decimal placesin a 16 cheracter field
fm= new Formatter();
fm.format(" %16.2e",123.1234567);
System.out.println(fm);
//print at most 15 characters in a string
fm= new Formatter();
fm.format(" %.15s","I love java.");
System.out.println(fm);
}
}


Recent Comments