Replacing character in a String

 import java.util.*;


public class ReplaceCharInString{
public static void main(String args[]){
String s = "xoxoxoxoxox";
System.out.println(s.replace('x', 'X'));
}
}

Printing the given string in uppercase

 public class PrintToUpperCase{

public static void main(String args[]){
String s = "I LOVE My Country And Its People";

System.out.println(s.toUpperCase());
}
}

Printing the given string to lowercase

 public class PrintToLowerCase{

public static void main(String args[]){
String s = "I LOVE My Country And Its People";

System.out.println(s.toLowerCase());
}
}

Print substring of a String

 public class PrintSubString{

public static void main(String args[]){
String s = "corejavacourse";

System.out.println(s.substring(5)); // from 5th character to end of of the string will print
System.out.println(s.substring(5,8)); //only three character will print i.e 5,6,7...
}
}

Are Strings equal

 public class IsStringEqual {


public static void main(String args[]) {
String s = "Haryana";

System.out.println(s.length());

System.out.println(s.equalsIgnoreCase("HARYANA"));
System.out.println(s.equals("HARYANA"));
System.out.println(s.equals("Haryana"));


System.out.println(s.equalsIgnoreCase("States"));
}
}

Concatenating and printing a String

 import java.util.*;


public class ConcatenateStrings {
public static void main(String args[]) {
String str1 = "My name is";
String str2 = "Sudeep Kumar Bondwal";

String str3 = str1.concat(str2);
System.out.println(str3);
}
}

Concatenating a String

 public class ConcateString1{

public static void main(String args []){
String s = "Haryana";
s += " State";
System.out.println(s);

}
}

Concatenating a String

 public class ConcateString{

public static void main(String args []){
String s = "Author";
System.out.println(s.concat(" Book"));

}
}

String to Int conversion using valueOf Method

 public class StringToIntConversion1{

public static void main(String args[]){
String s = "200";
int i = Integer.valueOf(s);

System.out.println(i);
}
}

Counting factor of a number between two specific numbers

 import java.util.*;

class PrintNoFactor{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int counter=0;
for(int i=a; i<=b; i++){
if(i%c == 0){
counter++;
}
}
System.out.println(counter);
}
}

Finding a palindromic number

 import java.util.*;


public class TestPalindrome{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter any number to check");
int n = sc.nextInt();
isPalindrome(n);
}

public static void isPalindrome(int n){
int temp =0;
int remainder = 0;
int sum =0;
temp = n;
while(n>0){
remainder=n%10;
sum = (sum*10)+remainder;
n = n/10;
}
if(sum == temp){
System.out.println("This is a palindromic number");
} else{
System.out.println("This is not a palindromic number");
}
}
}

Finding prime number using square root method

 import java.util.*;


public class PrintPrimeNo{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int n = sc.nextInt();
test_No(n);

}

public static void test_No(int n){
int flag=0;
if(n==0 || n==1){
System.out.println("This is not a prime number");
} else{
for(int i=2;i<=(int)Math.sqrt(n);i++){
if(n%1==0){
System.out.println("This is not a prime number");
flag=1;
break;
}
}
}
if(flag ==0){
System.out.println("This is a prime number");
}
}
}

Fibonacci Series

 public class PrintFibonacciSeries{

public static void main(String args[]){
int n1=0;
int n2=1;
int n3=0;
int count=10;

System.out.print(n1+" "+n2+" ");
for(int i=2;i<count;i++){
n3=n1+n2;
n1=n2;
n2=n3;
System.out.print(n3+" ");
}
}
}

Printing Prime Number

 import java.util.*;


public class ListPrimeNo{
public static void main(String args[]){
int count =0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting number");
int start = sc.nextInt();
System.out.println("Enter the ending number");
int end = sc.nextInt();
System.out.println("Total Prime no between "+start+" " + "and"+" " + end+" "+"are : ");
for(int i=start;i<=end;i++){
if(Test_Prime(i)){
System.out.print(i+","+" ");
count += 1;
}
}
System.out.println();
System.out.println("Total Prime numbers are equal to = "+count);
}

public static boolean Test_Prime(int n){
if(n<=1){
return false;
}
for(int i=2;i<=(int)Math.sqrt(n);i++){
if(n%i==0){
return false;
}
}
return true;
}
}

Armstrong Number

 import java.util.*;


public class ArmStrongNo {

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter starting number number");
int start = sc.nextInt();

System.out.println("Enter ending number");
int end = sc.nextInt();
for (int i = start; i <= end; i++) {
if (isArmStrong(i)) {
System.out.print(i);
}
}
}

public static boolean isArmStrong(int n) {
int temp;
int digit = 0;
int sum = 0;
int last = 0;
temp = n;
while (temp > 0) {
temp = temp / 10;
digit += 1;
}
temp = n;
while (temp > 0) {
last = temp % 10;
sum += Math.pow(last, digit);
temp = temp / 10;
}

if (n == sum) {
return true;
} else {
return false;
}
}
}

POLYMORPHISM

 

import bank;


class Person{
String name;
int age;

public void printInfo(String name){
System.out.println("Only name method was called");
System.out.println(name);

}
public void printInfo(int age){
System.out.println("Only age method was called");
System.out.println(age);
}
public void printInfo(String name, int age){
System.out.println("Both name and age method was called");
System.out.println(name+""+age);
}
}

public class TestPolymorphism{
public static void main(String args[]){
Person p1 = new Person();
p1.name = "Sud";
p1.age = 24;

p1.printInfo(p1.age);
p1.printInfo(p1.name);
p1.printInfo(p1.name, p1.age);

bank.Account account1 = new bank.Account(); //encapsulation
account1.name = "customer1";
}
}

Multiple Inheritance Using Interface

 interface Animal{

int eyes = 2; //this value is fixed cant be changed
public void walks();
}
interface Herbivore{

}

class Horse implements Animal, Herbivore{ //multiple inheritance not in class
                                                but
public void walks(){ // can be achieved using interface;
System.out.println("Walks on 4 legs");
}
}

public class TestInterface{
public static void main(String args[]){
Horse h1 = new Horse();
h1.walks();
}
}

Abstraction

 

abstract class Animal {
Animal() {
System.out.println("You are creating an animal");
}

public abstract void walk() {}

public void eat() {
System.out.println("animals eat");
}
}

class Horse extends Animal {

Horse() {
System.out.println("You have created a Horse");
}

public void walk() {
System.out.println("Walks on 4 legs");
}
}

class Chicken extends Animal {

public void walk() {
System.out.println("walk on 2 legs");
}
}

public class TestAbstraction {

public static void main(String args[]) {
Horse h1 = new Horse();
h1.walk();
h1.eat();
// Animal animal = new Animal();
// animal.walk();
}
}

Inheritance

 

class Shape{
public void area(){
System.out.println("Displays area");
}
}

class Triangle extends Shape{
//shape->triangle -> Single level inheritance;
public void area(int l, int h){
System.out.println(1/2*l*h);
}

}

class EquilateralTriangle extends Triangle{ /
/ shape -> triangle -> equilateral-> multilevel inheritance
public void area(int l, int h){
System.out.println(1/2*l*h);
}
}

class Circle extends Shape{
// Shape-> triangle
public void area(int r){
//Shape -> Circle -> Hierarchial Inheritance
//Single base class and multiple derived classes
}
System.out.println(3.14*r*r);
}



public class TestInheritance{
public static void main(String args[]){
Triangle t1 = new Triangle();
t1.color = "Red";
}
}

Copy Constructor

 import java.util.*;


class Student1 {

String name;
int age;

public void printInfo() {
System.out.println(this.name);
System.out.println(this.age);
}

Student1(Student1 s2) {
this.name = s2.name;
this.age = s2.age;
}

Student1() {}
}

public class OOPS3 {

public static void main(String args[]) {
Student1 s1 = new Student1(); //This is a Constructor:- to construct object
s1.name = "aman";
s1.age = 24;

Student1 s2 = new Student1(s1);
s2.printInfo();
}
}

Parametric Constructor

 

import java.util.*;
class Student2{
String name;
int age;
public void printInfo(){
System.out.println(this.name);
System.out.println(this.age);
}
Student2(String name, int age){
this.name = name; //this.name here name is name of object of class Student
this.age = age; // and name is parameter which is passed;

}
}


public class OOPS2{
public static void main(String args[]){
Student2 s1 = new Student2("Sud",24); //This is a Constructor:- to construct object

s1.printInfo();
}
}

Constructor

 


class Student {
String name;
int age;

public void printInfo() {
System.out.println(this.name);
System.out.println(this.age);
}

Student() {
System.out.println("Constructor called");
}
}

public class OOPS1 {
public static void main(String args[]) {
Student s1 = new Student();// This is a Constructor:- to construct object

s1.name = "Sudeep";
s1.age = 24;

s1.printInfo();
}
}

Class and Object

 

class Pen{
    String color;
String type; //ballpoint, gel

public void write(){
System.out.println("Writing something");
}

public void printColor(){
System.out.println(this.color);
}
public void printType(){
System.out.println(this.type);
}

}


public class OOPS{
public static void main(String args[]){
Pen pen1 = new Pen();
pen1.color = "blue";
pen1.type = "gel";

Pen pen2 = new Pen();
pen2.color = "black";
pen2.type = "ballpoint";

pen1.write();
pen1.write();

pen1.printColor();
pen2.printColor();

pen1.printType();
pen2.printType();
}
}

Sorting an ArrayList

 import java.util.*;


public class SortEleOfArraylist{
public static void main(String args[]){
ArrayList<String> colors = new ArrayList<String>();
colors.add("Violet");
colors.add("Indigo");
colors.add("Green");
colors.add("Yellow");
colors.add("Orange");
colors.add("Red");

Collections.sort(colors);
System.out.println("The sorted Array is :"+colors);
// for(String i : colors)
// System.out.println(i);
}
}

Shuffling an ArrayList

 import java.util.*;


public class ShuffleEleOfArraylist{
public static void main(String args[]){
ArrayList<String> colors = new ArrayList<String>();
colors.add("Violet");
colors.add("Indigo");
colors.add("Green");
colors.add("Yellow");
colors.add("Orange");
colors.add("Red");

Collections.shuffle(colors);
System.out.println("The Shuffled Array is :"+colors);
}
}

Searching specific element of an ArrayList

 import java.util.*;


public class SearchEleOfArraylist{
public static void main(String args[]){
ArrayList<String> colors = new ArrayList<String>();
colors.add("Violet");
colors.add("Indigo");
colors.add("Green");
colors.add("Yellow");
colors.add("Orange");
colors.add("Red");

for(int i=0; i<colors.size();i++){
if(colors.get(i)=="Green"){
System.out.println("Green color found at index "+i);
}
}
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...