Skip to main content

Posts

Showing posts from June, 2012

Java Program to display the current time in 24-Hours Format

import java.io.*; import java.util.*; public class Time2{     Calendar c=Calendar.getInstance();     public void display(){         System.out.println("Time in 24 Hour hh:min:ss.mm format is:- ");         System.out.println((c.get(Calendar.HOUR_OF_DAY))+":"+(c.get(Calendar.MINUTE))+":"+(c.get(Calendar.SECOND))+"."+(c.get(Calendar.MILLISECOND)));     }     public static void main(String[] args){         Time2 obj=new Time2();         obj.display();     } } Still Using Opera? Try Faster UC Browser. [Download Now] Fastest Download Speed with UC Browser. New High Speed Mobile Browser, Free! Download large files with UC Browser. Click here for more info

Java Program to accept any date and print current date and check that the date excepted by the user is valid or not

The following Code is to check whether the date entered by the user is a valid date or not. Within the Code, 'Date()' class is only used to get the current system date and the other things're done by simple logic. Code is as follow:- import java.util.*; public class DateValidation{     Scanner scan=new Scanner(System.in);     String mon, da, yea, day3, day4, month3, month4, year7, year8, year9, year10, year11, year12, year5, year6;     char month1, month2, day1, day2, year1, year2, year3, year4, date2;     String date;     int month, day, year;     public void input(){         try{             System.out.println("Enter the date in dd/mm/yyyy format:- ");             date=scan.nextLine();         }  ...

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to accept any String and display the ASCII Code of all characters in the String

The following Code is to get the ASCII Code ( American Standard Code for Information Interchange ) of any String entered by the user. Code:- import java.util.*; public class StringASCII{     Scanner scan=new Scanner(System.in);     String string;     int ascii;     char asc;     public void input(){         try{             System.out.println("Enter any String: ");             string=scan.nextLine();         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0);         }   ...

Java Program to accept integer numbers using Scanner Loop and find the sum of them

The following Code is to explain the use of #hasNetInt() of 'Scanner' Class in java.util package. It's an boolean method use to check the input is an 'Integer' value or not. Code:- import java.util.*; public class ScannerLoop{     Scanner scan=new Scanner(System.in);     int n, sum=0;     public void input(){         try{             System.out.println("Enter number. Non-Number will stop input: ");             while(scan.hasNextInt()){                 n=scan.nextInt();                 compute();             }             scan.clos...

Java Program to accept a String and convert all lower case letters to upper case and upper case to lower

The following Code is to convert all the lower case letters to upper and upper case to lower. 'StringBuffer' Class is used because 'String' class is immutable but the 'StringBuffer' class is mutable. Which means that once the value stored in the 'String' type variable, it's value can't be changed but in 'StringBuffer' it can be changed. Code:- import java.util.*; public class CaseConversion {     int i;     String string;     char casse;     String temp;     Scanner scan = new Scanner(System.in);     public void input() {         try {             System.out.println("Enter any String: ");             string = scan.nextLine();         } catch (Exception e) {         ...

Java Program to accept the temperature in Fahrenheit and convert it to Celsius

The following Code is to accept the temperature by the user in fahrenheit and convert it to celsius. Another Exception that can be thrown is 'InputMismatchException'. Code:- import java.util.*; public class TemperatureConversion{     Scanner scan=new Scanner(System.in);     float fah, cel;     public void input(){         try{             System.out.println("Enter Temperature in Fahrenheit: ");             fah=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);         }  ...

Java Program to show the current Day of Year, Month and Week

import java.io.*; import java.util.*; public class Date2{     Calendar c=Calendar.getInstance();     public void display(){         System.out.println("Date in Day_Of_Year/Day_Of_Month/Day_Of_Week format:- ");         System.out.println((c.get(Calendar.DAY_OF_YEAR))+"/"+(c.get(Calendar.DAY_OF_MONTH))+"/"+(c.get(Calendar.DAY_OF_WEEK)));     }     public static void main(String[] args){         Date2 obj=new Date2();         obj.display();     } }

Java Program to show the current time

import java.io.*; import java.util.*; public class Time{     Calendar c= Calendar.getInstance();     public void display(){         System.out.println("Time Format is hh/min/ss.ms");         System.out.println("Current time is "+(c.get(Calendar.HOUR))+":"+(c.get(Calendar.MINUTE))+":"+(c.get(Calendar.SECOND))+"."+(c.get(Calendar.MILLISECOND)));     }     public static void main(String[] args){         Time time=new Time();         time.display();     } }

Java Program to accept any number and check if the number is perfect square or not

The following Code is to check whether the number entered by the user is an Perfect Square of any number or not. Perfect Square of a number is checked by squaring any particular number and if the squared number gives out the number, matching the number we started from, than the Perfect Square of the number we started from will be the number we squared. E xample: 25 (5 2 ) Code:- import java.util.*; public class PerfectSquare{     Scanner scan=new Scanner(System.in);     int number;     double sqrt;     public void input(){         try{             System.out.println("Enter any number: ");             number=scan.nextInt();         }         catch(NumberFormatException e){      ...

Java Progarm to accept the name, class, roll no and 3 subject marks and display all the details with total and average marks

import java.util.*; public class Mark{     Scanner scan=new Scanner(System.in);     int rollNo, clas;     String name;     float mark1, mark2, mark3, total, average;     public void input(){         try{             System.out.println("Enter the Name of the Student: ");             name=scan.nextLine();         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0);         }         try{             Sys...

Java Program to accept the amount and display that amount in terms of Notes

The following Code is to get the possible number of notes within a particular amount. Code:- import java.util.*; public class NoteNumber{     Scanner scan=new Scanner(System.in);     int amount,amt, r1, r2, r5, r10, r20, r50, r100, r500, r1000;     public void input(){         try{             System.out.println("Enter the Total Amount: ");             amount=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);         }     }     public void compute()...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

Java Program to calculate the Volume of a Cylinder

import java.io.*; import java.util.*; public class Cylinder{     Scanner scan=new Scanner(System.in);     double volume, radius, height;     public void input(){         try{             System.out.println("Enter the Radius: ");             radius=scan.nextInt();             System.out.println("Enter the Height: ");             height=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);       ...

Java Program to accept any Character and display if the Character is a digit, lower/upper case letter or special symbol

The following Code is to check whether the character entered by the user is in lower case or in upper case or the character is a special symbol. Code:- import java.util.*; public class CharacterTest{     char ch;     String cha;     Scanner scan=new Scanner(System.in);     public void input(){         try{             System.out.println("Enter any Character: ");             cha=scan.next();             ch=cha.charAt(0);         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0); ...

Java Program to accept any integer number and print its length

The following code is to get the total length or total number of number(s) entered, at once, by the user. Code:- import java.util.*; public class IntegerToString{     Scanner scan=new Scanner(System.in);     int number;     String string;     public void input(){         try{             System.out.println("Enter any number: ");             number=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);         }     }     public void compu...

Java Program to accept number of week's day (1-7) and print its equivalent name of the day of the week

The following code is to print the particular 'Day' on the particular 'Day Number' of a week. Code:- import java.util.*; public class Day{     Scanner scan=new Scanner(System.in);     int dayNumber;     public void input(){         try{             System.out.println("Enter any week day number (1-7): ");             dayNumber=scan.nextInt();         }         catch(final NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);         }     }     public void compute(){     ...

Java Program to accept two dates and display the difference between them

The following code is to get the difference between the two valid dates in milliseconds, seconds, minutes, hours and days. Code:- import java.util.*; public class DateDiff{     Scanner scan=new Scanner(System.in);     int year, month, date;     public void input(){         int year, month, date;         try{             System.out.println("Enter 1st Year in \"yyyy\" format: ");             this.year=scan.nextInt();             System.out.println("Enter 1st Month in \"mm\" format: ");             this.month=scan.nextInt();             System.out.println("Enter 1st Date in \"dd\" format: ");...

Java Program to accept any ASCII Code and print the character relevant to that code

The following code is to convert the ASCII Code to its character form. Code:- import java.util.*; public class CharacterASCII{     Scanner scan=new Scanner(System.in);     int asc;     char character;     String ascii;     public void input(){         try{             System.out.println("Enter any ASCII Code to print the character of the code: ");             asc=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);         }     } ...

Java Program to accept any String and show the frequency of every character of the String

The following code is the print the frequency or the total occurrence of the characters in a String entered by the user. Code:- import java.util.*; public class Frequency{     Scanner scan=new Scanner(System.in);     String string;     int occurance=0, count=0, max=0, gap=0, length;     public void input(){         try{             System.out.println("Enter any String to print its Frequency: ");             string=scan.nextLine();         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0);         ...

Java Program to show the current date with the format of "mm/dd/yyyy"

import java.io.*; import java.util.*; public class DateCalendar{     Calendar c= Calendar.getInstance();     public void display(){         System.out.println("Date Format is mm/dd/yyyy");         System.out.println("Today is "+(c.get(Calendar.MONTH)+1)+"/"+(c.get(Calendar.DATE))+"/"+(c.get(Calendar.YEAR)));     }     public static void main(String[] args){         DateCalendar calendar=new DateCalendar();         calendar.display();     } }

Java Program to show even/odd numbers between starting point to ending point

import java.io.*; import java.util.*; public class First_Even_Odd{     Scanner scan=new Scanner(System.in);     int i, starts, ends;     String choice;     public void input(){         try{             System.out.println("Enter \"Even\" to print even numbers between beg - last or \"Odd\" for odd numbers.");             choice=scan.next();         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0);         }         try{           ...

What is a facebook Poke?

Have you ever been poked on Facebook and wondered exactly what it meant? Well, in this article, we will take a look at the meaning of a Facebook poke. It might just mean much more than you think. Whenever I go on Facebook, I see a little portion of the screen saying that people have poked me. Facebook claims that a poke can be used "to say hello" to your friends. Thus, I see this poke feature on Facebook as a way that my friends are saying, "Hello, I am thinking about you and I miss you." Thus, I think a poke feels pretty good. After you receive a poke, you then have the option of poking back. This could be translated as a way of saying, "Thanks for thinking of me. I feel the same way." However, this is just the surface meaning of a poke on Facebook, but if we dig deeper, we may find that a Facebook poke means much more. Such was the case with my friend Bob. Bob recently asked me, "Richard, what does a Facebook poke mean...

Java Program to reverse a number using "this" keyword

import java.io.*; import java.util.*; public class ThisReverse{     Scanner scan=new Scanner(System.in);     int number, remainder, reverse=0;     public void input(){         try{             System.out.println("Enter a number: ");             number=scan.nextInt();         }         catch(Exception e){             System.out.println("Error Code: "+e);             System.exit(0);         }     }     public void compute(){         int number;         number=this.number;    ...

Java Program to check the accepted number is Armstrong / Palindrome / Perfect number

The following code is to accept any number and check whether it's an Armstrong / Palindrome / Perfect number, depending on the user's choice. Code:- import java.util.*; public class Number{     Scanner scan=new Scanner(System.in);     int num,choice=0,i,r,s=0,n;     public void input()throws IOException{         try{             System.out.println("Enter any number: ");             num=scan.nextInt();             System.out.println("1 to check "+num+" is Armstrong.\n2 to check "+num+" is Palindrome.\n3 to check "+num+" is Perfect.\nEnter Your Choice: ");             choice=scan.nextInt();         }         ca...

Java Program to develop a small cricket applet

import java.applet.Applet; import java.awt.*; public class SlimeCricket2 extends Applet implements Runnable {     private int p1Diam = 75; //100     private int p2Diam = 75; //100     private int ballRad = 13; //25     private int nWidth, nHeight;     private int p1X, p1Y;     private int p2X, p2Y;     private int p3X = 1000-p1Diam/2, p3Y;     private int p1Col, p2Col;     private Color slimeColours[] = {Color.yellow, new Color(0, 0, 128), new Color(164, 164, 255), Color.black,             new Color(0, 100, 0), new Color(0, 162, 0), new Color(0, 0, 210), new Color(128, 78, 0), Color.red, Color.black};     private Color slimeColours2[] = {new Color(0, 100, 0), Color.red, Color.yellow, Color.gray,             Color.white, Color.yell...