# Java Basic Logic Building Programs and examples

## JAVA Basic concepts and examples

 JAVA Basic concepts

## JAVA first program ‘Hello World’

Java Basic Logic Building Programs | Hello World is the simplest program in every programming language. You should start with this program if you are new in programming.

public class HelloWorld {

public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.printWord(); //method call

}
public void printWord(){ /* method definition */
System.out.println(“Hello World”);
}
}

### Program to find factorial Using JAVA

Factorial of a positive number n is given by:

`factorial of n (n!) = 1 * 2 * 3 * 4 * ... * n`

class Factorial{
public static void main(String args[]){
Factorial obj = new Factorial();
//Method call
obj.findFact();

}
//Method definition
public void findFact(){
int i;
int num = 5;
int fact = 1;
for (i=1; i<=num; i++){
fact = fact * i;
}
System.out.println(“Factorial of “+ num + ” is: “+ fact);
}
}

When you run this program, output will be:

`Factorial of 10 = 120`

### Program to perform normal mathematics operation in JAVA

public class MathOperation {
public static void main(String[] args) {
MathOperation mathOperation = new MathOperation();
mathOperation.sumTotal();
mathOperation.multi();
mathOperation.division();
mathOperation.difference();
}

public void sumTotal(){
int a = 80;
int b = 50;
int sum = a + b;
System.out.println(“Sum is”+ sum);
}
public void multi(){
int a = 80;
int b = 50;
int mul = a * b;
System.out.println(“multiplication” + mul);
}
public void difference(){
int a = 80;
int b = 50;
int min = a – b;
int div = a / b;
System.out.println(“Difference” + min);
}
public void division(){
int a = 80;
int b = 50;
int div = a / b;
System.out.println(“Division” + div);
}
}

### Program to find prime numbers in JAVA

public static void main( String[] args){
System.out.println(“2 is prime number “+ isPrimeNumber(2));
System.out.println(“10 is prime number “+ isPrimeNumber(10));

}
public static boolean isPrimeNumber(int num) {
if(num<=1){ return false; } for(int i=2; i

Output:
2 is prime number true
10 is prime number false

### If-else condition in JAVA

public class IfElseConcept {

public static void main(String[] args) {
int a = 100;
int b = 20;

if(b>a) {
System.out.println(“b is greater than a”);
}else {
System.out.println(“a is greater than b”);

}
//Comparison Operators
// < > <= >= == !=

int c = 40;
int d = 40;
if(c==d) {
System.out.println(“C and D are Equal”);
}else {
System.out.println(“C and D are not equal”);
}
//WAP to find out the highest number
int a1 = 10000;
int b1 = 2000;
int c1 = 300;

//Nested If-Else
if(a1>b1 & a1 > c1) {//false & false = false
System.out.println(“a1 is greator”);
}else if(b1>c1) {
System.out.println(“b1 is greator”);
}else {
System.out.println(“c1 Is greator”);
}

}

}

### To print the Star pattern Program in JAVA

```public class PrintStar {
public static void main(String[] args){
PrintStar obj = new PrintStar();        obj.Star();        obj.revStar();
}
public void Star(){
int i;        int j;        for(i=1; i<=6; i++)
{
for(j=1; j<i; j++)
{
System.out.print("*");            }
System.out.println("");        }
System.out.println(""); //To separate two modules of star    }```

public void revStar(){
for(int i = 5; i>=1; i–)
{
for(int j = 1; j<=i; j++)
{
System.out.print(“*”); }
System.out.println(); }
}
} //End of class

Java Basic Logic Building Programs and examples

Output:
*
**
***
****
*****

*****
****
***
**
*

### Program for Basic Constructor Example

public class PersonDetails {

//Defining Variables
String first_name;
String last_name;
String email;
int contact;
//To print Data
public void printData(){
System.out.println(“Name of person is: “+ first_name +” “+ last_name );
System.out.println(“Email ID “+ email);
System.out.println(“Mobile Number: “+ contact);
}

//Building Parameterized Constructor

public PersonDetails( String fname, String lname, String add, String mail, int con){
first_name = fname;
last_name = lname;
email = mail;
contact = con;
}

//Main Method

public static void main(String[] args) {
//Passing parameter values
PersonDetails personDetails = new PersonDetails(“Santosh”, “Adhikari”, “Kathmandu Nepal”, “[email protected]”,123456789);
personDetails.printData();

}
}

OUTPUT:
Name of person is: Santosh Adhikari
Email ID [email protected]
Mobile Number: 123456789

package PolyMorphism;

public class PolyMorphismDemo {
public void sum(int a, int b){
int total = a + b;
System.out.println(total);
}
public void sum(float a, float b){
float total = a + b;
System.out.println(total);
}

public void sum( int a, int b, int c){
int total = a + b + c;
System.out.println(total);
}

public static void main(String[] args) {
PolyMorphismDemo polyMorphismDemo = new PolyMorphismDemo();
polyMorphismDemo.sum(10,20, 21);
}
}

NOTE: We won’t let you copy these codes, You have to write these codes yourself. Otherwise, our effort will be not effective