Creates a table of squares and cubes using minimum field width using Formatter Class.
This example demonstrates the use of formatter class and also using minimum field width.
Output::
1 1 1
2 4 8
3 9 27
. . .
. . .
import java.util.*;
class FormatterExample{
public static void main(String a[]){
Formatter fm= new Formatter();
for(int j=1; j<=5; j++){
fm.format("%5d %5d %5d", i, i*i, i*i*i);
System.out.println(fm);
}
}
}
Demonstrates a field width specifier in Formatter Class.
Output::
10.123450
10.123450
00010.123450
import java.util.*;
class FormatterExample{
public static void main(String a[]){
Formatter fm= new Formatter();
fm.format("%f %n %12f %n %012f", 10.12345, 10.12345, 10.12345);
System.out.println(fm);
}
}
Demonstrates the %n and %% format specifiers.
This example demonstrates the use of format specifiers %n( new line) and %%(to print ‘%’ character) in java using Formatter Class.
import java.util.*;
class FormatterExample{
public static void main(String a[]){
Formatter fm= new Formatter();
fm.format("Download %n is %d%% completed",45);
}
}
Formatter Class- Demonstrates the use of %g format specifier.
This example demonstrates the use of %g format specifier.
import java.util.*
class FormaterExample {
public static void main(String a[]){
Formatter fm=new Formatter();
for(double i=100; i<1.0e+10; i*=100){
fm.format("%g",i);
System.out.println(fm);
}
}
}
Formatter Class-Simple example.
Posted by admin in Java, Uncategorized on June 2nd, 2010
A simple example to understand concept of Formatter class.
import java.util.*;
class FormatterExample{
public static void mainn( String a[]){
Formatter fm= new Formattre();
fm.format("Java %s easy %d %f", "is so", 1,91.3);
System.out.println(fm);
}
}
everyNth
Given a non-empty string and an int N, return the string made starting with char 0, and then every Nth char of the string. So if N is 3, use char 0, 3, 6, … and so on. N is 1 or more.
everyNth(”Miracle”, 2) ? “Mrce”
everyNth(”abcdefg”, 2) ? “aceg”
everyNth(”abcdefg”, 3) ? “adg”
public String everyNth(String str, int n) {
//char c[]=str.toCharArray();
String str1="";
for(int i=0;i<str.length();i++)
{ if(i%n ==0)
str1=str1+str.substring(i,i+1);
}
return str1;
}
endUp
Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.
endUp(”Hello”) ? “HeLLO”
endUp(”hi there”) ? “hi thERE”
endUp(”hi”) ? “HI”
public String endUp(String str) {
if(str.length() < 3) return(str.toUpperCase());
else return str.substring(0,str.length()-3)+str.substring(str.length()-3).toUpperCase();
}
lastDigit
Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % “mod” operator computes remainders, so 17 % 10 is 7.
lastDigit(7, 17) ? true
lastDigit(6, 17) ? false
lastDigit(3, 113) ? true
public boolean lastDigit(int a, int b) {
if(a%10 == b%10) return true;
else return false;
}
stringE
Return true if the given string contains between 1 and 3 ‘e’ chars.
stringE(”Hello”) ? true
stringE(”Heelle”) ? true
stringE(”Heelele”) ? false
public boolean stringE(String str) {
int n=0;
char str1[]=str.toCharArray();
for(int i=0;i<str1.length;i++)
if(str1[i]=='e') n++;
if(n==1 || n==3) return true;
else return false;
}
max1020
Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.
max1020(11, 19) ? 19
max1020(19, 11) ? 19
max1020(11, 9) ? 11
public int max1020(int a, int b) {
int temp;
if(a>b) temp=a;
else temp=b;
if (temp >= 10 && temp <= 20) return temp;
if (b >= 10 && b <= 20) return b;
if (a >= 10 && a <= 20) return a;
return 0;
}


Recent Comments