Java Basic Logic Building Programs
![]() |
| 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);
}
}
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 greater");
}else if(b1>c1) {
System.out.println("b1 is greater");
}else {
System.out.println("c1 Is greater");
}
}
}
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 Address;
String email;
int contact;
//To print Data
public void printData(){
System.out.println("Name of person is: "+ first_name +" "+ last_name );
System.out.println("Address: "+ Address);
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;
Address = add;
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
Address: Kathmandu Nepal
Email ID [email protected]
Mobile Number: 123456789
Example of Method overloading
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);
}
}
Program to insert array element on desired array location
package RandomPractice;
import java.util.Scanner;
public class InsertElementInArray {
public void insertArray(){
try {
int[] arr = new int[5];
System.out.println("Enter the element of array");
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("Printing elements before insert");
for (int i : arr) {
System.out.println(i);
}
System.out.println("Enter the location for storing element value");
int location = scanner.nextInt();
System.out.println("Enter the value that which ou want to insert");
int value = scanner.nextInt();
for (int i = arr.length - 1; i > location; i--) {
arr[i] = arr[i - 1];
}
arr[location] = value;
for (int i : arr) {
System.out.println(i);
}
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("maximum array size is 5 i.e 0 to 4");
}
}
public static void main(String[] args) {
InsertElementInArray insertElementInArray = new InsertElementInArray();
insertElementInArray.insertArray();
}
}
NOTE: We won’t let you copy these codes, You have to write these codes yourself. Otherwise, our effort will be not effective
Read about Strandhogg 2.0 android bug

