Sunday, November 8, 2009

Guessing the Birth date.!

import javax.swing.JOptionPane;

public class GuessConfirmationDate{
public static void main(string[] args){

String set1=
"1 3 5 7\n"+
"9 11 13 15\n"+
"17 19 21 23\n"+
"25 27 30 31\n";

String set2=
"2 3 6 7\n"+
"10 11 14 15\n"+
"18 19 22 23"+
"26 27 30 31";

String set3=
"4 5 6 7\n"+
"12 13 14 15\n"+
"20 21 22 23\n"+
"28 29 30 31";

String set4=
"8 9 10 11\n"+
"12 13 14 15\n"+
"24 25 26 27\n"+
"28 29 30 31\n";

String set5=
"16 17 18 19\n"+
"20 21 22 23\n"+
"24 25 26 27\n"+
"28 29 30 31\n";

int date=0;

int answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set1);

if(answer=JOptionPane.YES_OPTION)
date+=1;

int answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set2);
if(answer=JOptionPane.YES_OPTION)
date+=2;

int answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set3);
if(answer=JOptionPane.YES_OPTION)
date+=3;

int answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set3);
if(answer=JOptionPane.YES_OPTION)
date+=4;

int answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set4);
if(answer=JOptionPane.YES_OPTION)
date+=8;

nt answer=JOptionPane.showConfirmDialog(null,"Is ur birth date in these numbers?" +set5);
if(answer=JOptionPane.YES_OPTION)
date+=16;

JOptiopnPane.showMessageDialod(null,"Your birth date is"+ date +"!");
}
}
This is not my own program.But its a very good example of the GUI confirmation dialog box.However the logic is a bit complicated.

Friday, September 11, 2009

Displaying the Current Time - Java code

This is one of the best programs I've read so far. Its a simple program but the logic is awesome. The currentTimeMillis method in the System class returns the current time in millisecond elapsed since the time 00:00:00 in January 1970 when Unix was formally launched. It is called as 'Unix epoch'.
[This program is from Java programming by Daniel liang.]

public class ShowCurrentTime
{
public static void main(String args[])
{
long totalMilliseconds=System.currentTimeMillis();
long totalSeconds=totalMillisecond/1000;

int currentSecond=(int)(totalSeconds%60);

long totalMinutes=totalSeconds/60;

int currentMinute=(int)(totalMinutes%60);

long totalHours=totalMinutes/60;

int currentHour=(int)(totalHours%24);

System.out.println("Current time is:"+currentHour+""+currentMinute+""+currentSecond+" "GMT");
}
}

Sunday, August 30, 2009

JDBC Project

This is an application for a Hospital website. It contains a form where the Patient's details can be entered and it will be stored in a database(Sql). However there r many errors in this project. I'm not able to execute this program in my computer coz I dont have SQL installed in my system. If u can solve this, please mail the edited code to vishwas.24@gmail.com

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class LifelineHospitals implements ActionListener
{
Label Heading,lpName,lpGender,lpAge,lpDOB,lpOccupation,lpGuardian,error;

TextField pnameField,paddressField,pageField,pgenderField,pdobField,pguardianField;

Button Submit,Exit;
Prepared Statement stat;
Statement stmt;
ResultSet rs;
Font F;
Panel P1;
Frame F1;

/*Constructor*/
public LifelineHospitals
{
try
{
//Type 4 driver
Class.forName("com.microsoft.Sqlserver.jdbc.SQLServerDriver");
//Connection to the Data Source

con=DriverManager.getConnection("jdbc:Sqlserver://Sqlserver1;databaseName=Library'user=user1;password=password#1234");

//Create a Statement object

stmt=con.createStatement();
}
Catch(Exception e)
{
System.out.println("Error:"+e);
}
}
public void Compshow()
{
F1=new Frame("LifelineHospitals");
P1=new Panel();
Heading=new Label("Patients Details");
lpName=new Label("Patient's name");
lpAge=new Label("Age");
lpDOB=new Label("DOb");
lpGender=new Label("Gender");
lpGuardian=new Label("Guardian");
lpOccupation=new Label("Occupation");

pnameField=new TextField(50);
pgenderField=new TextField(8);
pageField=new TextField(3);
pdobField=new TextField(10);
poccupationField=new TextField(20);
pguardianField=new TextField(25);

Submit=new Button("Submit");
Exit=new Button("exit");

P1.setLayout(null);
Heading.setBounds(250,35,200,40);
P1.add(Heading);
lpName.setBounds(75,90,200,25);
pnameField.setBounds(400,90,100,25);
P1.add(pName);
P1.add(pnameField);

lpGender.setBounds(75,120,200,30);
pgenderField.setBounds(400,150,150,25);
P1.add(lpGender);
P1.add(pgenderField);

lpAge.setBounds(75,150,200,30);
pageField.setBounds(400,150,150,25);
P1.add(lpAge);
P1.add(pageFild);

lpDOB.setBounds(75,180,200,30);
lpdobField.setBounds(400,180,250,25);
P1.add(lpDOB);
P1.add(pdobField);

lpOccupation.setBounds(75,210,200,30);
poccupationField.setBounds(400,210,200,25);
P1.add(lpGuardian);
P1.add(pguardianField);

Submit.setBounds(175,350,100,30);
Exit.setBounds(325,350,100,30);
P1.add(Submit);
P1.add(Exit);
F1.add(P1);
F1.setSize(680,500);
F1.setVisible(true);
Submit.addActionListerner(this);
Exit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Exit");
{
/*Terminate the windows form*/
System.exit(0);
}
if(ae.getActionCommand()=="Submit");
{
//Insert a row into the Publisher Table
try
{
stat=con.prepareStatement("INSERT INTO PUBLISHERS VALUES(?,?,?,?,?,?)");
string pName=pnameField.getText();
string pGender=pgenderField.getText();
string pAge=pageField.getText();
string pDOB=pdobField.getText();
string pOccupation=poccupationField.getText();
string pGuardian=pguardianField.getText();

stat.setString(1,pName);
stat.setString(2,pGender);
stat.setString(3,pAge);
stat.setString(4,pDOB);
stat.setString(5,pOccupation);
stat.setString(6,pGuardian);
stat.executeUpdate();
pnameField.setText("");
pgenderField.setText("");
pageFiels.setText("");
pdobField.setText("");
poccupationField.setText("");
pguardianField.setText("");
}
Catch(Exception e)
{
System.ouyt.println("Error:"+e);
error.setText("The statement cannot be inserted");
}
}
} /*public void class is closed*/

public static void main(string args[])
{
LifelineHospitals p=new LifelineHospitals;
p.Compshow();
}
}

Sunday, August 9, 2009

Comparing two numbers - java code

import java.io.*;
class Comparing{
public static void main(String[] args) {
int a=24, b=25;
if (a == b){
System.out.println("Both are equal");
}
else if(a>b){
System.out.println("a is greater than b");
}
else{
System.out.println("b is greater than a");
}
}
}

To find the largest number - java code

import java.io.*;
class largernumber
{
public static void main(String[] args) {
int x=800, y=7000, z=80000;
if (x>y){
if (x>z){
System.out.println("x is greater");
}
else{
if(z>y){
System.out.println("z is greater");
}
else{
System.out.println("y is greater");
}
}
}
else{
if (y>z){
System.out.println("y is greater");
}
}
}
}

Friday, August 7, 2009

Leap year - java code

import java.io.*;
class Leapyear
{
public static void main(String[] args) throws IOException
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the year");
int year=Integer.parseInt(object.readLine());
System.out.println("The year entered is:"+year);
if (year%4==0){
System.out.println("The given year is a leap year");
}
else{
System.out.println("This is not a leap year");
}
}
}

Thursday, August 6, 2009

Palindrome number

//To check if the number is a Palindrome
import java.io.*;

public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num);
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}