Free JavaScripts provided
by The JavaScript Source
public class ClockApplet extends Applet implements Runnable{
Thread t,t1;
public void start(){
t = new Thread(this);
t.start();
}
public void run(){
t1 = Thread.currentThread();
while(t1 == t){
repaint();
try{
t1.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
Calendar cal = new GregorianCalendar();
String hour = String.valueOf(cal.get(Calendar.HOUR));
String minute = String.valueOf(cal.get(Calendar.MINUTE));
String second = String.valueOf(cal.get(Calendar.SECOND));
g.drawString(hour + ":" + minute + ":" + second, 20, 30);
}
}
Keyword DescriptionJava Tutorial: Data Type, Abstract Data Type and Data Structure
byte Byte-length integer
short Short integer
int Integer
long Long integer
float Single-precision floating point
double Double-precision floating point
char A single character
boolean A boolean value (true or false)
/*This program Demonstrates the proper use of Number Formats in common java programming scenarios */
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* Class provides common functions on number formats.
*/
public class NumberUtility {
/**
* Method takes Object as parameter and returns decimal number.
* if argument is float or double and contains tailing zeros
* it removes them. If argument is float or double then no change in return type.
* Change the Format of the Number by changing the String Pattern
*/
public static String changeToDecimalFormat(Object number) {
BigDecimal bdNumber = new BigDecimal(number.toString());
bdNumber = bdNumber.stripTrailingZeros(); //Returns a BigDecimal with any trailing zero's removed
String pattern = "###,##0.0###########"; //To apply formatting when the number of digits in input equals the pattern
DecimalFormat newFormat = new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.US));
return newFormat.format(bdNumber);
}
/* Method takes Object as parameter and removes commas from the parameter */
public static double removeCommasFromNumber(Object number) {
try {
StringBuffer inputNo = new StringBuffer(number.toString());
if (inputNo.length() > 0) {
while (inputNo.indexOf(",") != -1) {
inputNo.deleteCharAt(inputNo.indexOf(","));
}
} else {
return 0.0;
}
return Double.parseDouble(inputNo.toString());
} catch (NumberFormatException e) {
return 0.0;
}
}
/* Some times its required to have a fixed set of decimal places for a
* number. We can set that by changing the precision number for a particular
* input BigDecimal Input String
*/
public static String changeToRequiredDecimals(String bigDecimalString,
int precision) {
String newFormattedString = null;
String afterDecimal = null;
if (bigDecimalString == null || bigDecimalString.length() == 0) {
return "0.0";
}
if (bigDecimalString.contains(".")) {
afterDecimal = bigDecimalString.substring(bigDecimalString
.indexOf(".") + 1);
int length = Math.abs((afterDecimal.length() - precision));
if (afterDecimal.length() < precision) {
newFormattedString = bigDecimalString;
for (int i = 0; i < length; i++) {
newFormattedString = newFormattedString + "0";
}
} else if (afterDecimal.length() > precision) {
newFormattedString = bigDecimalString.substring(0,
bigDecimalString.length() - length);
if (precision == 0) {
newFormattedString = newFormattedString.substring(0,
newFormattedString.indexOf("."));
} else {
newFormattedString = bigDecimalString;
}
} else {
if (precision > 0)
newFormattedString = bigDecimalString + ".";
else
newFormattedString = bigDecimalString;
for (int i = 0; i < precision; i++) {
newFormattedString = newFormattedString + "0";
}
}
}
return newFormattedString;
}
public static void main(String args[]){
int intVar = 10;
double doubleVar = 10.504000;
float floatVar = 343534534348.5687654F;
String commaString = "343,534,535,000.0";
BigDecimal bdNumber = new BigDecimal("1234.8765");
System.out.println(NumberUtility.changeToDecimalFormat(new Integer(intVar)));
System.out.println(NumberUtility.changeToDecimalFormat(new Double(doubleVar)));
System.out.println(NumberUtility.changeToDecimalFormat(new Float(floatVar)));
System.out.println(NumberUtility.removeCommasFromNumber(commaString));
System.out.println(NumberUtility.changeToRequiredDecimals(bdNumber.toString(), 8));
}
}
/*( This program Demonstrates the proper use of Date functionality in common java programming scenarios*/
)
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtility {
/* Add Day/Month/Year to a Date
add() is used to add values to a Calendar object.
You specify which Calendar field is to be affected by the operation
(Calendar.YEAR, Calendar.MONTH, Calendar.DATE).
*/
public static void addToDate(){
System.out.println("In the ADD Operation");
// String DATE_FORMAT = "yyyy-MM-dd";
String DATE_FORMAT = "dd-MM-yyyy"; //Refer Java DOCS for formats
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
Date d1 = new Date();
System.out.println("Todays date in Calendar Format : "+c1);
System.out.println("c1.getTime() : "+c1.getTime());
System.out.println("c1.get(Calendar.YEAR): " + c1.get(Calendar.YEAR));
System.out.println("Todays date in Date Format : "+d1);
c1.set(1999,0 ,20); //(year,month,date)
System.out.println("c1.set(1999,0 ,20) : "+c1.getTime());
c1.add(Calendar.DATE,40);
System.out.println("Date + 20 days is : " + sdf.format(c1.getTime()));
System.out.println();
System.out.println();
}
/*Substract Day/Month/Year to a Date
roll() is used to substract values to a Calendar object.
You specify which Calendar field is to be affected by the operation
(Calendar.YEAR, Calendar.MONTH, Calendar.DATE).
Note: To substract, simply use a negative argument.
roll() does the same thing except you specify if you want to roll up (add 1)
or roll down (substract 1) to the specified Calendar field. The operation only
affects the specified field while add() adjusts other Calendar fields.
See the following example, roll() makes january rolls to december in the same
year while add() substract the YEAR field for the correct result
*/
public static void subToDate(){
System.out.println("In the SUB Operation");
String DATE_FORMAT = "dd-MM-yyyy";
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
Calendar c1 = Calendar.getInstance();
c1.set(1999, 0 , 20);
System.out.println("Date is : " + sdf.format(c1.getTime()));
// roll down, substract 1 month
c1.roll(Calendar.MONTH, false);
System.out.println("Date roll down 1 month : " + sdf.format(c1.getTime()));
c1.set(1999, 0 , 20);
System.out.println("Date is : " + sdf.format(c1.getTime()));
c1.add(Calendar.MONTH, -1);
// substract 1 month
System.out.println("Date minus 1 month : " + sdf.format(c1.getTime()));
System.out.println();
System.out.println();
}
public static void daysBetween2Dates(){
Calendar c1 = Calendar.getInstance(); //new GregorianCalendar();
Calendar c2 = Calendar.getInstance(); //new GregorianCalendar();
c1.set(1999, 0 , 20);
c2.set(1999, 0 , 22);
System.out.println("Days Between "+c1.getTime()+"\t"+ c2.getTime()+" is");
System.out.println((c2.getTime().getTime() - c1.getTime().getTime())/(24*3600*1000));
System.out.println();
System.out.println();
}
public static void daysInMonth() {
Calendar c1 = Calendar.getInstance(); //new GregorianCalendar();
c1.set(1999, 6 , 20);
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH);
// int days = c1.get(Calendar.DATE);
int [] daysInMonths = {31,28,31,30,31,30,31,31,30,31,30,31};
daysInMonths[1] += DateUtility.isLeapYear(year) ? 1 : 0;
System.out.println("Days in "+month+"th month for year "+year+" is "+ daysInMonths[c1.get(Calendar.MONTH)]);
System.out.println();
System.out.println();
}
public static void getDayofTheDate() {
Date d1 = new Date();
String day = null;
DateFormat f = new SimpleDateFormat("EEEE");
try {
day = f.format(d1);
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("The dat for "+d1+" is "+day);
System.out.println();
System.out.println();
}
public static void validateAGivenDate() {
String dt = "20011223";
String invalidDt = "20031315";
String dateformat = "yyyyMMdd";
Date dt1=null , dt2=null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
sdf.setLenient(false);
dt1 = sdf.parse(dt);
dt2 = sdf.parse(invalidDt);
System.out.println("Date is ok = " + dt1 + "(" + dt + ")");
}
catch (ParseException e) {
System.out.println(e.getMessage());
}
catch (IllegalArgumentException e) {
System.out.println("Invalid date");
}
System.out.println();
System.out.println();
}
public static void compare2Dates(){
SimpleDateFormat fm = new SimpleDateFormat("dd-MM-yyyy");
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.set(2000, 02, 15);
c2.set(2001, 02, 15);
System.out.print(fm.format(c1.getTime())+" is ");
if(c1.before(c2)){
System.out.println("less than "+c2.getTime());
}else if(c1.after(c2)){
System.out.println("greater than "+c2.getTime());
}else if(c1.equals(c2)){
System.out.println("is equal to "+fm.format(c2.getTime()));
}
System.out.println();
System.out.println();
}
public static boolean isLeapYear(int year){
if((year%100 != 0) || (year%400 == 0)){
return true;
}
return false;
}
public static void main(String args[]){
addToDate();
subToDate();
daysBetween2Dates(); //The "right" way would be to compute the julian day number of both dates and then do the substraction.
daysInMonth();
validateAGivenDate();
compare2Dates();
getDayofTheDate();
}
}
/* Display Triangle as follow
1
2 4
3 6 9
4 8 12 16 ... N (indicates no. of Rows) */
class Output3{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print((i*j)+" ");
}
System.out.print("\n");
}
}
}
/* Display Triangle as follow
0
1 0
1 0 1
0 1 0 1 */
class Output2{
public static void main(String args[]){
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print(((i+j)%2)+" ");
}
System.out.print("\n");
}
}
}
/* Display Triangle as follow : BREAK DEMO.
1
2 3
4 5 6
7 8 9 10 ... N */
class Output1{
public static void main(String args[]){
int c=0;
int n = Integer.parseInt(args[0]);
loop1: for(int i=1;i<=n;i++){
loop2: for(int j=1;j<=i;j++){
if(c!=n){
c++;
System.out.print(c+" ");
}
else
break loop1;
}
System.out.print("\n");
}
}
}
/* Display Triangle as follow : BREAK DEMO.
1
2 3
4 5 6
7 8 9 10 ... N */
class Output1{
public static void main(String args[]){
int c=0;
int n = Integer.parseInt(args[0]);
loop1: for(int i=1;i<=n;i++){
loop2: for(int j=1;j<=i;j++){
if(c!=n){
c++;
System.out.print(c+" ");
}
else
break loop1;
}
System.out.print("\n");
}
}
}
/* Display Triangle as follow : BREAK DEMO.
1
2 3
4 5 6
7 8 9 10 ... N */
class Output1{
public static void main(String args[]){
int c=0;
int n = Integer.parseInt(args[0]);
loop1: for(int i=1;i<=n;i++){
loop2: for(int j=1;j<=i;j++){
if(c!=n){
c++;
System.out.print(c+" ");
}
else
break loop1;
}
System.out.print("\n");
}
}
}
/*Write a program to find average of consecutive N Odd no. and Even no. */
class EvenOdd_Avg{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
while(n > 0){
if(n%2==0){
cntEven++;
sumEven = sumEven + n;
}
else{
cntOdd++;
sumOdd = sumOdd + n;
}
n--;
}
int evenAvg,oddAvg;
evenAvg = sumEven/cntEven;
oddAvg = sumOdd/cntOdd;
System.out.println("Average of first N Even no is "+evenAvg);
System.out.println("Average of first N Odd no is "+oddAvg);
}
}
/* Write a program to generate Harmonic Series.
Example :
Input - 5
Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
double result = 0.0;
while(num > 0){
result = result + (double) 1 / num;
num--;
}
System.out.println("Output of Harmonic Series is "+result);
}
}
/* switch case demo
Example :
Input - 124
Output - One Two Four */
class SwitchCaseDemo{
public static void main(String args[]){
try{
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
String result=""; //contains the actual output
while(reverse > 0){
remainder = reverse % 10;
reverse = reverse / 10;
switch(remainder){
case 0 :
result = result + "Zero ";
break;
case 1 :
result = result + "One ";
break;
case 2 :
result = result + "Two ";
break;
case 3 :
result = result + "Three ";
break;
case 4 :
result = result + "Four ";
break;
case 5 :
result = result + "Five ";
break;
case 6 :
result = result + "Six ";
break;
case 7 :
result = result + "Seven ";
break;
case 8 :
result = result + "Eight ";
break;
case 9 :
result = result + "Nine ";
break;
default:
result="";
}
}
System.out.println(result);
}catch(Exception e){
System.out.println("Invalid Number Format");
}
}
}
/* Write a program to find whether no. is palindrome or not.
Example :
Input - 12521 is a palindrome no.
Input - 12345 is not a palindrome no. */
class Palindrome{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //used at last time check
int reverse=0,remainder;
while(num > 0){
remainder = num % 10;
reverse = reverse * 10 + remainder;
num = num / 10;
}
if(reverse == n)
System.out.println(n+" is a Palindrome Number");
else
System.out.println(n+" is not a Palindrome Number");
}
}
/* Write a program to Find whether number is Prime or Not. */
class PrimeNo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i
if(num%i==0)
{
System.out.println(num+" is not a Prime Number");
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}
/*Write a program to find whether given no. is Armstrong or not.
Example :
Input - 153
Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int n = num; //use to check at last time
int check=0,remainder;
while(num > 0){
remainder = num % 10;
check = check + (int)Math.pow(remainder,3);
num = num / 10;
}
if(check == n)
System.out.println(n+" is an Armstrong Number");
else
System.out.println(n+" is not a Armstrong Number");
}
}
/* Write a program to Display Invert Triangle.
Example:
Input - 5
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/
class InvertTriangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
while(num > 0){
for(int j=1;j<=num;j++){
System.out.print(" "+num+" ");
}
System.out.print("\n");
num--;
}
}
}
/*Write a program to generate a Triangle.
eg:
1
2 2
3 3 3
4 4 4 4 and so on as per user given number */
class Triangle{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
for(int i=1;i<=num;i++){
for(int j=1;j<=i;j++){
System.out.print(" "+i+" ");
}
System.out.print("\n");
}
}
}
/* Write a program to convert given no. of days into months and days.
(Assume that each month is of 30 days)
Example :
Input - 69
Output - 69 days = 2 Month and 9 days */
class DayMonthDemo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int days = num%30;
int month = num/30;
System.out.println(num+" days = "+month+" Month and "+days+" days");
}
}
/* Write a program to Swap the values */
class Swap{
public static void main(String args[]){
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
System.out.println("\n***Before Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
//Swap logic
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("\n***After Swapping***");
System.out.println("Number 1 : "+num1);
System.out.println("Number 2 : "+num2);
}
}
/* Program to Display Multiplication Table */
class MultiplicationTable{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
System.out.println("*****MULTIPLICATION TABLE*****");
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
System.out.print(" "+i*j+" ");
}
System.out.print("\n");
}
}
}
Input - 5
Output - 1 2 3 4 5 */
class Join{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
String result = " ";
for(int i=1;i<=num;i++){
result = result + i + " ";
}
System.out.println(result);
}
}
import java.awt.*; public class Chess extends Frame public int qizi; Label msg=new Label("Hello, This is the WeiQi game"); public int[][] bd=new int[20][20]; public int[][] rstct=new int[20][20]; Client cl; int movedx,movedy; Button quit=new Button(" Quit the Game "); Chess(String name1,String name2,int can_move,Client mcl) player1=name1; movedx=-1; setLayout(new BorderLayout()); Panel p=new Panel(); add("North",p); public void oppmove(int y,int x) public void checkSurround(int qi) int counter=999; for (int i=1;i<=19;i++) public void paint(Graphics g) for (int i=1;i<=19;i++) for (int i=1;i<=19;i++) if (movedx!=-1) }; public boolean mouseDown(Event e,int x,int y) public boolean action(Event e,Object arg) }; |