0% found this document useful (0 votes)
17 views

Java Programs

Uploaded by

bujjjitamil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Programs

Uploaded by

bujjjitamil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA PROGRAMS

6. SIMPLE CALCULATOR PROGRAM


AIM:
To create a simple calculator program by using java
ALGORITHM:
1. Start the program
2. Get the first and second number from the user
3. Choose the operator for your calculation
4. Show the result in the result box.
5. Stop the program
PROGRAM:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)


{
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText() );
Result=Number1+Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.g etText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1-Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText() );
Result=Number1*Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1 .getText());
Number2=Double.parseDouble(jTextField2.getText() );
Result=Number1/Number2;
jTextField3.setText(Double.toString(Result));
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}
OUTPUT:

RESULT:

Thus the above Java Program was Executed Successfully.


7. SIMPLE INTEREST CALCULATOR PROGRAM
AIM:
To create a simple interest calculation by using java
ALGORITHM:
1. Start the program
2. Get the principal, rate and time from the user
3. Choose the button for your interest calculation
4. Show the result in the result box.
5. Stop the program

PROGRAM:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
double Principal,Rate,SInterest;
byte Time; //Expected value not more than 127 Years
Principal=Double.parseDouble(jTextField1.getText());
Rate=Double.parseDouble(jTextField2.getText());
Time=Byte.parseByte(jTextField3.getText());
SInterest=(Principal*Rate*Time)/100; //Formula to calculate SI
jTextField4.setText(Double.toString(SInterest));
}
OUTPUT:
RESULT:
Thus the above Java Program was Executed Successfully.

8. MAGIC TEXT PROGRAM

AIM:
To create a magic text by using java
ALGORITHM:
1. Start the program
2. Get the input text from the user
3. Choose the button for your magic text.
4. Show the result in the result box.
5. Stop the program
PROGRAM:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
char Star;
Star='*';
jTextField2.setText(Star+jTextField1.getText()+Star );
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
char Hash;
Hash='#';
jTextField2.setText(Hash+jTextField1.getText()+Hash );
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
char Percent;
Percent='%';
jTextField2.setText(Percent+jTextField1.getText()+Percent);
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
char Quotes;
Quotes='"';
jTextField2.setText(Quotes+jTextField1.getText()+Quotes);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}

OUTPUT:

RESULT:

Thus the above Java Program was Executed Successfully.


9. IF…ELSE STATEMENT
AIM:
To create if else program using java
ALGORITHM:
1. Start the program
2. Get the input from the user
3. Check the inputs based on conditions
4. If the conditions is true, true block will execute
5. If the condition is false, false block will execute
6. Stop the program

PROGRAM:
private void jButton1ActionPerformed (java.awt.event.ActionEvent evt)
{
if (jRadioButton1.isSelected())
jTextField1.setText("Monday");
else if (jRadioButton2.isSelected())
jTextField1.setText("Tuesday");
else if (jRadioButton3.isSelected())
jTextField1.setText("Wednesday" );
else if (jRadioButton4.isSelected())
jTextField1.setText("Thursday") ;
else if (jRadioButton5.isSelected())
jTextField1.setText("Friday");
else if (jRadioButton6.isSelected())
jTextField1.setText("Saturday") ;
else if (jRadioButton7.isSelected())
jTextField1.setText("Sunday");
else
jTextField1.setText("Day - Not Selected");
}
OUTPUT:

RESULT:

Thus the above Java Program was Executed Successfully.

10. SWITCH CASE STATEMENT


AIM:
To create a switch case statement program using java
ALGORITHM:
1. Start the program
2. Get the input with the case value
3. Match the input with case value
4. If match case, execute that case output statement
5. If no match case, default statement will be executed
6. Stop the program
PROGRAM:
private void jButton1ActionPerformed (java.awt.event.ActionEvent evt)
{
double FinalAmount=0;
double BillAmount = Double.parseDouble(jTextField1.getText());
switch(jComboBox1.getSelectedIndex())
{
case 0:
FinalAmount=BillAmount;
break;
case 1:
FinalAmount=0.90*BillAmount;
break;
case 2:
FinalAmount=0.80*BillAmount;
break;
case 3:
FinalAmount=0.70*BillAmount;
break;
default:FinalAmount=BillAmount;
}
jTextField2.setText(Double.toString(FinalAmount));
}

OUTPUT:

RESULT:
Thus the above Java Program was Executed Successfully.

You might also like