Commit 0c9e9df0 authored by Rohan Sakhale's avatar Rohan Sakhale

Updated date and added more post

parent 9ff02e26
Pipeline #4575794 passed with stage
in 2 minutes and 41 seconds
---
title: Armstrong Number Program
date: 2016-10-11 16:56:37
date: 2012-01-27 08:03:45
categories:
- Codes
tags:
......
---
title: Calculate Celsius to Fahrenheit Temperature in Java
date: 2012-06-16 15:31:13
categories:
- Codes
tags:
- code-example
- java
- swing
---
In-order to convert from Celsius to Fahrenheit, we have to apply a simple logic onto Celsius "Multiply by 9, then divide by 5, then add 32".
Our code provides a simple GUI, where the user has to enter the temperature in Celsius and after clicking on convert displays the Fahrenheit temperature below button in the form of JLabel.
The Class has been designed by extending JPanel which can be called from other classes and added onto the JFrame for displaying.
#### Screenshot
![Calculate Celsius to Fahrenheit Temperature in Java Screenshot](/images/output_celsius_fahrenheit_java.jpg)
#### C2F.java
```java
package com.rohansakhale.treedemo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author RohanSakhale
*/
public class C2F extends JPanel {
JTextField tfInputTemp;
JLabel lInputTemp, lAnswer;
JButton bCalc;
JPanel InputTemp, collections;
public C2F() {
tfInputTemp = new JTextField(15);
lInputTemp = new JLabel("Enter Temperature in Celsius: ");
lAnswer = new JLabel("Answer: ");
bCalc = new JButton("Convert");
InputTemp = new JPanel();
collections = new JPanel();
collections.setLayout(new BoxLayout(collections, BoxLayout.Y_AXIS));
InputTemp.add(lInputTemp);
InputTemp.add(tfInputTemp);
collections.add(InputTemp);
collections.add(bCalc);
collections.add(lAnswer);
bCalc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!tfInputTemp.getText().equals("")) {
double temp = Double.parseDouble(tfInputTemp.getText());
double fahrenheit = ((temp * 9) / 5) + 32;
lAnswer.setText("Answer: " + fahrenheit + " F");
}
}
});
add(collections);
}
}
class C2FMainDemo{
public static void main(String[] args) {
JFrame c2fFrame = new JFrame("Celsius to Fahrenheit Demo Program");
C2F c2f = new C2F();
c2fFrame.add(c2f);
c2fFrame.setSize(400, 150);
c2fFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c2fFrame.setVisible(true);
}
}
```
\ No newline at end of file
---
title: Calculate Fahrenheit to Celsius Temperature in Java
date: 2012-06-16 15:31:03
categories:
- Codes
tags:
- code-example
- java
- swing
---
In-order to convert from Fahrenheit to Celsius, we have to apply a simple logic onto Celsius "Deduct 32, then multiply by 5, then divide by 9".
Our code provides a simple GUI, where the user has to enter the temperature in Fahrenheit and after clicking on convert displays the Celsius temperature below button in the form of JLabel.
The Class has been designed by extending JPanel which can be called from other classes and added onto the JFrame for displaying.
#### Screenshot:
![Calculate Fahrenheit to CelsiusTemperature in Java](/images/output_fahrenheit_celsius_java.jpg)
#### F2C.java
```java
package com.rohansakhale.treedemo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author RohanSakhale
*/
public class F2C extends JPanel{
JTextField tfInputTemp;
JLabel lInputTemp, lAnswer;
JButton bCalc;
JPanel InputTemp, collections;
public F2C() {
tfInputTemp = new JTextField(15);
lInputTemp = new JLabel(\"Enter Temperature in Fahrenheit: \");
lAnswer = new JLabel(\"Answer: \");
bCalc = new JButton(\"Convert\");
InputTemp = new JPanel();
collections = new JPanel();
collections.setLayout(new BoxLayout(collections, BoxLayout.Y_AXIS));
InputTemp.add(lInputTemp);
InputTemp.add(tfInputTemp);
collections.add(InputTemp);
collections.add(bCalc);
collections.add(lAnswer);
bCalc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!tfInputTemp.getText().equals(\"\")) {
double temp = Double.parseDouble(tfInputTemp.getText());
double celsius = (temp - 32 ) * 5 / 9;
lAnswer.setText(\"Answer: \" + celsius + \" C\");
}
}
});
add(collections);
}
}
class F2CMainDemo{
public static void main(String[] args) {
JFrame f2cFrame = new JFrame(\"Celsius to Fahrenheit Demo Program\");
F2C f2c = new F2C();
f2cFrame.add(f2c);
f2cFrame.setSize(400, 150);
f2cFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2cFrame.setVisible(true);
}
}
```
\ No newline at end of file
---
title: Concurrent Chat Application in Java
date: 2016-10-11 20:57:45
date: 2012-06-16 15:50:12
categories:
- Codes
tags:
......
---
title: Convert Integer to Binary
date: 2016-10-11 17:22:49
date: 2012-02-12 23:56:09
categories:
- Codes
tags:
......
---
title: Create Web Service using PHP & JSON
date: 2016-10-11 18:09:47
date: 2012-05-24 16:36:40
categories:
- Codes
tags:
......
---
title: Draw Line using Bresenham's Algorithm
date: 2016-10-11 16:55:06
date: 2011-11-12 12:25:40
categories:
- Codes
tags:
......
---
title: Draw Line using DDA Algorithm
date: 2016-10-11 16:53:37
date: 2011-11-12 12:11:39
categories:
- Codes
tags:
......
---
title: Factorial Number Program in PHP
date: 2016-10-11 17:45:01
date: 2012-06-06 00:02:03
categories:
- Codes
tags:
......
---
title: Factorial Program in Java
date: 2012-06-16 15:31:33
categories:
- Codes
tags:
- code-example
- java
---
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,
```
5! = 5 x 4 x 3 x 2 x 1 = 120
```
The following code creates a GUI for calculating factorial numbers upto 170.
The user has to enter the number and click on Calculate which displays the answer on JLabel below the button.
The Class has been designed by extending JPanel which can be called from other classes and added onto the JFrame for displaying.
#### Screenshot
![Factorial Program in Java](/images/output_factorial_java.jpg)
#### Factorial.java
```java
package com.rohansakhale.fact;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author RohanSakhale
*/
public class Factorial extends JPanel {
JTextField tfNumber;
JLabel lEnterNumber, lAnswer;
JButton bCalc;
JPanel number, collection;
public Factorial() {
tfNumber = new JTextField(15);
lEnterNumber = new JLabel("Enter Number: ");
lAnswer = new JLabel("Answer: ");
bCalc = new JButton("Calculate");
number = new JPanel();
collection = new JPanel();
collection.setLayout(new BoxLayout(collection, BoxLayout.Y_AXIS));
number.add(lEnterNumber);
number.add(tfNumber);
collection.add(number);
collection.add(bCalc);
collection.add(lAnswer);
bCalc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!tfNumber.getText().equals(")) {
double num = Integer.parseInt(tfNumber.getText());
if(num <= 170){
double fact = 1;
for(double i = 1;i<=num;i++){
fact = fact * i;
}
lAnswer.setText("Answer: " + fact);
tfNumber.setText(");
tfNumber.requestFocus();
}else{
JOptionPane.showMessageDialog(null, "Please enter number upto 170", "Error", JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null, "Please enter number upto 170", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
add(collection);
}
}
class FactorialDemo{
public static void main(String[] args) {
JFrame factFrame = new JFrame("Factorial Demo Program");
Factorial fact = new Factorial();
factFrame.add(fact);
factFrame.setSize(400,150);
factFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
factFrame.setVisible(true);
}
}
```
\ No newline at end of file
---
title: File IO Operation in Java
date: 2016-10-11 17:37:31
date: 2012-06-27 14:25:44
categories:
- Codes
tags:
......
---
title: Funny Program
date: 2016-10-11 17:11:08
date: 2012-01-27 08:03:25
categories:
- Codes
tags:
......
---
title: JList Java Swing Component Demo
date: 2016-10-11 16:51:53
date: 2011-11-12 08:14:02
categories:
- Codes
tags:
......
---
title: JTree Java Swing Component Demo
date: 2016-10-11 16:52:51
date: 2011-11-12 10:33:50
categories:
- Codes
tags:
......
---
title: JTree Simple Practice Application in Java
date: 2012-06-16 15:24:18
categories:
- Codes
tags:
- code-example
- java
- swing
---
JTree is a control that displays a set of hierarchical data as an outline.
We will use it as a menu to navigate through various small applications we made previously.
The tree will basically contain each program as a node when selected using mouse, that particular program is displayed onto the JFrame.
Programs used in this code are already being posted before for practice, I will name the Class & the link to find the class
1. [Simple Calculator in Java]()
1. [Factorial Program in Java]()
1. [Calculate Celsius to Fahrenheit Temperature in Java]()
1. [Calculate Fahrenheit to Celsius Temperature in Java]()
#### Screenshot
![JTree Simple Practice Application in Java Output 1](/images/output_jtree_prac_app_1.jpg)
![JTree Simple Practice Application in Java Output 2](/images/output_jtree_prac_app_2.jpg)
![JTree Simple Practice Application in Java Output 3](/images/output_jtree_prac_app_3.jpg)
![JTree Simple Practice Application in Java Output 4](/images/output_jtree_prac_app_4.jpg)
![JTree Simple Practice Application in Java Output 5](/images/output_jtree_prac_app_5.jpg)
#### TreeDemo.java
```java
package com.rohansakhale.treedemo;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
/**
*
* @author RohanSakhale
*/
public class TreeDemo {
static String lastComponent = \"\";
public static void main(String[] args) {
final JFrame jf = new JFrame(\"Practice Applications\");
DefaultMutableTreeNode pracApps = new DefaultMutableTreeNode(\"Practice Apps\", true);
DefaultMutableTreeNode calc = new DefaultMutableTreeNode(\"Calculator\");
DefaultMutableTreeNode fact = new DefaultMutableTreeNode(\"Factorial\");
DefaultMutableTreeNode temperature = new DefaultMutableTreeNode(\"Temperature\", true);
DefaultMutableTreeNode c2f = new DefaultMutableTreeNode(\"C to F\");
DefaultMutableTreeNode f2c = new DefaultMutableTreeNode(\"F to C\");
temperature.add(c2f);
temperature.add(f2c);
pracApps.add(calc);
pracApps.add(fact);
pracApps.add(temperature);
final JTree tree = new JTree(pracApps);
// JTree here can act as a menu for Practice Applications
// We add it on the west side of the JFrame
jf.add(BorderLayout.WEST, tree);
/*
* Classes created here within the same package can also be found online
* Calc = http://rohansakhale.com/codes/39/simple-calculator-in-java
* Factorial = http://rohansakhale.com/codes/40/factorial-program-in-java
* C2F = http://rohansakhale.com/codes/41/calculate-celsius-to-fahrenheit-temperature-in-java
* F2C = http://rohansakhale.com/codes/42/calculate-fahrenheit-to-celsius-temperature-in-java
*
*/
final Calc calculatorPanel = new Calc();
final Factorial factorialPanel = new Factorial();
final C2F c2fObj = new C2F();
final F2C f2cObj = new F2C();
// Lets change the panel on center of JFrame
// by checking the mouse click
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
TreePath path = tree.getPathForLocation(me.getX(), me.getY());
if (path != null) {
// Check the last added component and remove it
if (!lastComponent.equals(\"\")) {
if (lastComponent.equals(\"Factorial\")) {
jf.remove(factorialPanel);
} else if (lastComponent.equals(\"C to F\")) {
jf.remove(c2fObj);
} else if (lastComponent.equals(\"Calculator\")) {
jf.remove(calculatorPanel);
} else if (lastComponent.equals(\"F to C\")) {
jf.remove(f2cObj);
}
}
// Add the new component on the frame
// Depending on the Selected Tree Node
if (path.toString().contains(\"Calculator\")) {
jf.add(calculatorPanel);
lastComponent = \"Calculator\";
} else if (path.toString().contains(\"Factorial\")) {
jf.add(factorialPanel);
lastComponent = \"Factorial\";
} else if (path.toString().contains(\"C to F\")) {
jf.add(c2fObj);
lastComponent = \"C to F\";
} else if (path.toString().contains(\"F to C\")) {
jf.add(f2cObj);
lastComponent = \"F to C\";
}
jf.repaint();
}
}
});
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
```
\ No newline at end of file
---
title: Java Socket Program to check Prime Number
date: 2016-10-11 17:25:39
date: 2012-02-13 15:14:51
categories:
- Codes
tags:
......
---
title: Login Validation Program
date: 2016-10-11 16:50:10
date: 2011-11-12 07:59:07
categories:
- Codes
tags:
......
---
title: Midpoint Circle Algorithm
date: 2016-10-11 16:55:59
date: 2011-11-14 18:58:45
categories:
- Codes
tags:
......
---
title: PHP Factorial program using GMP Functions
date: 2012-06-04 17:13:14
categories:
- Codes
tags:
- code-example
- php
---
Regular working with arbitrary number is usually a tough job, normally for generating factorial number using the regular looping methods you can generate only upto 170, and it doesn't work for huge numbers.
**GMP** is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating point numbers.
GMP functions allow you to work with arbitrary-length integers using the GNU MP library.
You may need to do the initializations of GMP onto the server side if it don't work directly, read on php.net for it.
For complete reference on GMP functions [visit here](http://www.php.net/manual/en/ref.gmp.php).
The following code accepts a number from user and on the server side it is checked if numeric and passed onto the `gmp_fact()` method which returns the factorial of it and output is displayed on the screen.
#### gmp_try.php
```php
<?php
if (isset($_POST[\'submit\']) and $_POST[\'submit\'] == \'Generate\') {
if (isset($_POST[\'num\']) and is_numeric($_POST[\'num\'])) {
$num = $_POST[\'num\'];
$fact = gmp_fact($num);
echo \'Factorial of Number (\' . $num . \') is\' . $fact . \'<br />\';
} else {
echo \'Enter proper number<br />\';
}
}
?>
<form action=\"\" method=\"post\">
Enter Number: <input type=\"text\" name=\"num\" /><br />
<input type=\"submit\" name=\"submit\" value=\"Generate\" />
</form>
```
\ No newline at end of file
---
title: Pascal Traingle
date: 2016-10-11 17:17:05
date: 2012-02-07 08:25:19
categories:
- Codes