JAVA

Java is a high-level programming language that supports the use of GUI (Graphics User Interface). Java as a programming language belongs to the family of OOP (Object Oriented Program). Java Programming language makes use of OBJECT & CLASS in the designing of a program.
An OBJECT is an entity (physical and logical entity) that has both state and behavior. An object can also be described as an instance (result) of a class. A class can be described as an entity(logical). In Java, we build applications with object and class. All programming languages that support the use of object has some distinct features such as
MESSAGE PASSING: This is the process in which the method of an object invokes the method of another object. It is called method invocation.
ENCAPSULATION: This is the process in which the state (field) and behavior (method) of an object is binded together.
ABSTRACTION: It’s a process in which relevant data are shown to the user and the irrelevant ones are hidden.

A Program in Java to display welcome to java.
Public Class Welcome{
Public Static void main (string[]args){
System.out.println(“welcome to java programming language”);
}
}

In Java program, the last curly bracket opened will be the first to be closed. In the above program, the name of the class is “welcome”, what follows is called the main method. The executable statements are written immediately after the main method.

JAVA DATA TYPE

In Java programs, the most commonly used data types are;
1. Integer
2. Double
3. Float
4. Boolean
5. Strings
6. Memo etc

In Java Programming Language, every executable statement is terminated with a semi-colon. Any executable statement not terminated will result in compilation error.

CHARACTER SET

This is the list of valid characters that are used or supported by Java programming language.
1. Alphabet A-Z ( both lower and upper case).
2. numbers (0-9)
3. Arithmetic operator (+, -, *,/)
4. special symbol like &, $, etc.

Example 1: A Java program to calculate the sum of two number.
Solution.
Public Class Addition{
Public Static void main (string[]args){
int a = 5 ;
int b = 6;
int sum = a+ b;
System.out.println(“The sum of a & b is :” +sum);
}
}.
In the above program, a and b are declared as an integer variable. The variables are initialized with value 5 & 6 respectively. The “+” symbol is the last executable statement is used for concatenation. The programs below allows a user to input values for a variable.

Example 2: Rewrite the above program, allowing a user to input its desire value for a and b.
solution.
import java.util.*;
Public Class Addition{
Public Static void main (string[]args){
int a, b, sum;
scanner input = new scanner (System.In);
System.out.println(“Enter value for a”);
a = Input.nextInt();
System.out.println(“Enter value for b”);
b = Input.nextInt();
sum = a+b;
System.out.println(“The sum of a & b is :” +sum);
}
}.
In the above program, the “import java.util.*;” is a standard library which allows
Other examples include simple interest, circumference and area of a circle, diagonal of a rectangle etc.
Example 3: A java program to compute Simple Interest
solution.
import java.util.*;
Public Class Interest{
Public Static void main (string[]args){
Int p, r, t , I;
scanner input = new scanner (System.in);
p = Input.nextint();
r = Input.nextint();
t = Input.nextint();
I = (p*r*t)/100;
System.out.println(“The Simple Interest is:”+I);
}
}

Example 4: A Program to compute circumference and area of a circle.
solution.
import java.util.*;
Public Class Interest{
Public Static void main (string[]args){
Int r;
Double circum, Area;
Double pie = 3.142;
scanner input = new scanner (System.in);
r = input.nextInt();
circum = 2 * 3.142 * r;
Area = 3.142 * Math.power(r,2);
System.out.println(“Circumference of a circle is:” +circum);
System.out.println(“Area of a circle is:” +Area);
}
}

TEXT BOX IN JAVA

In Java program, it is possible to use a text box, and this text box can be created with the use of codes which will be declared at the heading with JOPTION PANE. These text box can be message box or input box.
Example 1: A program to display Welcome to Java program.
Solution.
Import javax.swing.JOptionPane;
Public Class Messagebox{
Public Static void main ( string[]args){
JOptionPane.ShowMessage Dialog(null, ” Welcome to Java Program”, “My First Program”, JOptionPane.Information_Message);
}
}

Example 2: A program to Sum two numbers using JOption Pane.
Solution.
Import javax.swing.JOptionPane;
Public Class Additionbox{
Public Static void main ( string[]args){
String first string = JOptionPane.ShowInputDialog(null, “Enter first number”);
int first = Integer.ParseInt(first string);
String second string = JOptionPane.ShowInputDialog(null, “Enter second number”);
int second = Integer.ParseInt(second string);
int sum = first + second;
JOptionPane.ShowMessage Dialog(null, “The sum of two number is:” +sum);
}
}.

CONTROL STRUCTURE IN JAVA PROGRAMMING LANUGUAGE.

Control Structure is a series of programming that determines the order in which a program is executed. There are three (3) types of control structure used in Java programming language which are:
1. Sequence
2. Selection
3. Iteration.

SEQUENCE CONTROL STRUCTURE.

This is the kind of control structure in which the execution of a program is carried out in sequential order. All the above programs ( such as Simple interest, Circumference of a circle etc) are good examples of Sequence control structure.

SELECTION CONTROL STRUCTURE
This type of control structure allows a condition to be stated, depending on the true value (i.e. true or false) of the condition, an action is meant to be carried out. There is three (3) types of selection control structure, which are Single, Binary and Multiple Selection.

1. Single Selection: This is the type of selection control structure in which a condition is stated and action is to be carried out depending on how true the condition is.
Example 1: A Program to compute 10% commission if sales is greater or equals 500.
solution.
import java.util.*;
Public Class Commission{
Public Static void main (string[]args){
String Sales String = JOptionPane.ShowInputDialog(null, “Enter sales amount”);
int Sales = Integer.ParseInt(Sales string);
if (Sales >= 500){
Double comm = (10/100) * Sales;
JOptionPane.ShowMessage Dialog(null, “The Commission is:” +comm);
}
}
}

2. Binary Selection: This is the type of selection control structure in which a condition is stated. Depending on the truth value (true or false) of the condition, an action is carried out.
Example 1: A Program to compute 10% commission if sales is greater or equals 500.
solution.
import java.util.*;
Public Class Commission{
Public Static void main (string[]args){
String Sales String = JOptionPane.ShowInputDialog(null, “Enter sales amount”);
int Sales = Integer.ParseInt(Sales string);
If (Sales < 500) {
JOptionPane.ShowMessage Dialog(null, “There is no Commission”);
}
Else {
Double comm = (10/100) * Sales;
JOptionPane.ShowMessage Dialog(null, “The Commission is:” +comm);
}
}
}

Example 2: A program to compute quadratic equation.
solution.
import java.util.*;
Public Class Commission{
Public Static void main (string[]args){
int a, b, c;
double D, x1, x2;
scanner input = new scanner (System.in);
a = Input.nextint();
b = Input.nextint();
c = Input.nextint();
D = (Math.power(b,2)) -(4*a*c);

If (D < 0){
system.out.println (” Real Root Exist”);
}

Else {
x1 = (-b – Math.sqrt(D))/ (2*a);
x2 = (-b + Math.sqrt(D))/ (2*a);
System.out.println(“X1 = ” +x1)
System.out.println(“X2 = ” +x2)
}
}
}.

ITERATION.

This is the type of control structure in which an action is repeatedly carried out until a certain condition is met. There are various looping statement that is used under Iteration control structure such Do While. For Next loop etc.

Example 1: A program to compute factorial of N number.
solution.
import java.util.*;
Public Class Factorial{
Public Static void main (string[]args){
int n, nfact;
nfact =1;
scanner input = new scanner (system.in);
n = input.nextint();
For ( int m = n; n >= 1; n–){
nfact = nfact * m ;
}
System.out.println(“Factorial is :” +nfact);
}
}.

Example 2: A java program to compute first 50 Even numbers and their sum
solution.
import java.util.*;
Public Class EvenNum{
Public Static void main (string[]args){
int even, sum ;
sum = 0;
For (even = 0; even <= 50; even +=2){
sum = sum + even;
sytem.out.println(” The following are the even numbers between 1-50 and the sum”);
Sytem.out.println(even ” and ” sum );
}
}
}.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here