This question is taken from Project Euler. Link We will be showing you 2 solutions to this program: Simple but slow A little Complicated but fast The Simple but Slow Program: The logic used in the program is quite simple. …
This program will tell you the length of the hypotenuse side using the Pythagoras theorem. For more details on Pythagoras theorem, please click here. The program asks the user to input the length of two sides and then it calculates …
Ok, this java program will check if the triangle is right angled using the Pythagoras theorem. According to the Pythagoras theorem, in any right triangle, the area of the square whose side is the hypotenuse is equal to the sum …
Octal to Decimal: How to Convert Octal to decimal conversion is one of the most commonly taught problem solving exercises in computer basics. So is there an octal to decimal formula? Yes, an octal number can be converted to a …
Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. How it works: In insertion sort,elements are entered into the array(or list) 1-by-1. When the first element is …
In this applet, we’ll be making a face using different shapes. The applet package contains methods to draw an Oval, a line, rectangle, rounded rectangle, arc, etc. To make a smiley face, we will be using Ovals and arcs. Syntax …
To run a Java Applet using command prompt, we need to create a html file and link it to our applet. To do so, follow these steps: Write your java applet code in a text editor and save it as …
In this program, we will demonstrate Classes. Here, we’ve considered an example to calculate area of a rectangle. We will create a class named ‘Rectangle’. Syntax to declare a class:
Class Rectangle: Next, we create a function ‘Area’ to …
6174 is known as Kaprekar’s constant. This number is notable for the following property: Take any four-digit number, using at least two different digits. (Leading zeros are allowed.) Arrange the digits in ascending and then in descending order to get two …
In this applet, we can add two numbers using GUI, where we take input from the user through text fields.
import java.applet.*;//Importing java.applet
public class MyApplet extends Applet
TextField txt1, txt2;
public void init()//Initializing our applet
txt1 = new TextField(""); //Creates a textfield 'txt1'
txt2 = new TextField(""); //Creates a textfield 'txt2'
setBackground(Color.CYAN);//Setting background color to CYAN
add(txt1); //Adds textfield 'txt1' to your applet
add(txt2); //Adds textfield 'txt2' to your applet
public void paint(Graphics obj)//Paint method to display our message
String s1 = txt1.getText(); //Fetching data from text field 1.
String s2 = txt2.getText(); //Fetching data from text field 2.
int num1=0, num2 = 0, num3; //Declaring 3 integer variables
num1 = Integer.parseInt(s1); //Parsing the string value of text field 1 to integer
num2 = Integer.parseInt(s2); //Parsing the string value of text field 2 in integer
num3 = num1 + num2; //Performing addition
String s3 = String.valueOf(num3); //Converting the result from integer to string
obj.drawString("Result:", 40, 50);
obj.drawString(s3, 50, 50);//To display the result