Wednesday, June 21, 2017

FactoryDemo Using Interface

Following code will help in selecting plan using Run Time Polymorphism:

Code:
interface Plan{
void setDataAndPrice();
void showPlan();
}

class Plan2G implements Plan{
int data,price;
Plan2G(){
setDataAndPrice();
}
public void setDataAndPrice() {
data=1024;
price=70;
}
public void showPlan(){
System.out.println("----Plan Details---");
System.out.println("Data: "+data+"Gb---"+"Price: \u20b9"+price);
}
}
class Plan3G implements Plan{
int data,price;
Plan3G(){
setDataAndPrice();
}
public void setDataAndPrice() {
data=1024;
price=150;
}
public void showPlan(){
System.out.println("----Plan Details---");
System.out.println("Data: "+data+"Gb---"+"Price: \u20b9"+price);
}
}

class Plan4G implements Plan{
int data,price;
Plan4G(){
setDataAndPrice();
}
public void setDataAndPrice() {
data=1024;
price=250;
}
public void showPlan(){
System.out.println("----Plan Details---");
System.out.println("Data: "+data+"Gb---"+"Price: \u20b9"+price);

}
}

class PlanFactory{
static Plan p;
static Plan getPlan(int a) {
switch(a){
case 2: 
p=new Plan2G();
break;
case 3:
p=new Plan3G();
break;
case 4:
p=new Plan4G();
break;
}
return p;
}

}

public class FactoryDemo {

public static void main(String[] args) {

Plan plan = PlanFactory.getPlan(2);
plan.showPlan();
plan = PlanFactory.getPlan(3);
plan.showPlan();
plan = PlanFactory.getPlan(4);
plan.showPlan();
        
}
}
Output:

Point Of Sales

Following code will print the point of sales at any Restaurant:

Code:
interface order{
int addItem(int x);
}
class order1 {
int totalvalue;
StringBuffer order=new StringBuffer() ;
order o;
void addVegBurger(int x) {
o=new vegburger();
totalvalue+=o.addItem(x);
order.append("VegBurger    *"+x+"   : \u20b9"+x*50+"\n");
}
void addNonVegBurger(int x) {
o=new nonvegburger();
totalvalue+=o.addItem(x);
order.append("NonVegBurger *"+x+"   : \u20b9"+x*70+"\n");
}
void addFries(int x) {
o=new fries();
totalvalue+=o.addItem(x);
order.append("Fries        *"+x+"   : \u20b9"+x*50+"\n");
}
void addCoke(int x) {
o=new coke();
totalvalue+=o.addItem(x);
order.append("Coke         *"+x+"   : \u20b9"+x*40+"\n");
}
void showOrder() {
System.out.println("-------Your Order-------");
System.out.println(order);
System.out.println("Total Amount    :  \u20b9"+totalvalue);
System.out.println("       Thank You!!\n    Please Visit Again!!");

}
}
class vegburger implements order{
public int addItem(int x) {
return x*50;
}
}
class nonvegburger implements order {
public int addItem(int x) {
return x*70;
}
}
class fries implements order {
public int addItem(int x) {
return x*50;
}
}
class coke implements order {
public int addItem(int x) {
return x*40;
}
}

public class POS {

public static void main(String[] nt) {
order1 o=new order1();
o.addVegBurger(3);
o.addNonVegBurger(1);
o.addCoke(4);
o.addFries(4);
o.showOrder();
}

}

Output:


Tuesday, June 13, 2017

Dominator

Following Code will find Dominator in 1D Array:

Code:
public class Dominator {

public static void main(String[] nt) {
int[] a={-3,-3,-3,-3,-3,-3,-2,-2,-2,-2,-2,-2,-1,0,0,2,2,2,2,2,3,3,3,3};
int[] b=new int[a.length];
int i;
for(int x=0;x<a.length;x++) {
i=0;
for(int y=0;y<a.length;y++) {
if(x!=y)
if(a[x]==a[y])
{     i++;   }
}
b[x]=i;
}
int max=b[0],min=20000;
for(int x=0;x<b.length;x++) {
if(b[x]!=0) {
if(max<b[x]) {
max=b[x];
}
if(min>b[x]) {
min=b[x];
}
}
}
int j=0;
int[] c=new int[a.length];
for(int x=0;x<b.length;x++) {
if(b[x]==max) {
c[j++]=a[x];
}
}
max=c[0];
for(int x=0;x<a.length;x++) {
if(c[x]!=0)
if(max<c[x]){
max=c[x];
}
}
System.out.println("Most  Repeated Max is :"+max);
c=new int[a.length];
j=0;
for(int x=0;x<a.length;x++) {
if(b[x]==min)
{
c[j++]=a[x];
}
}
min=c[0];
for(int x=0;x<a.length;x++) {
if(c[x]!=0)
if(min>c[x]){
min=c[x];
}
}
System.out.println("Least Repeated Min is :"+min);
}

}

Output:


Monday, June 12, 2017

Group Name Change In ChatApps

Following Code will give u a small idea how that change takes place:

Code:


public class User {
    int i;
String name;
long mobile;
Group[] g=new Group[10];

User(){   //Default Constructor
i=0;
name="No Name";
mobile=0000000000;
}

User(String n,long m){   //Parameterized Constructor
i=0;
name=n;
mobile=m;
}

void createGroup(String gn)
{
   g[i++]=new Group(gn,this);
}
    
void changeGname(String gn,int i)
{
g[i].Gname=gn;
}
}


public class User {
    int i;
String name;
long mobile;
Group[] g=new Group[10];
User(){   //Default Constructor
i=0;
name="No Name";
mobile=0000000000;
}
User(String n,long m){   //Parameterized Constructor
i=0;
name=n;
mobile=m;
}
void createGroup(String gn)
{
   g[i++]=new Group(gn,this);
}
    
void changeGname(String gn,int i)
{
g[i].Gname=gn;
}
}


public class Main {

public static void main(String[] nt) {
User u1=new User("Dishant",9023074222l);
User u2=new User("Garvit",8284010455l);
User u3=new User("Piyush",9815445194l);
User u4=new User("Nikhil",9815215752l);
User u5=new User("Navneet",9888321686l);
u1.createGroup("Brothers");
User[] x={u2,u3,u4,u5};
u1.g[0].addUsers(x);
u4.g[0].showGname();
u1.changeGname("For", 0);
u2.g[0].showGname();
u2.changeGname("Life", 0);
u5.g[0].showGname();
u3.changeGname("Yeahhh", 0);
u1.g[0].showGname();
System.out.println("------------------");
u3.g[0].showUsers();
}

}

Output::

Caesar Cipher

Caesar Cipher:The Caesar cipher, also known as a shift cipher, is one of the simplest forms of encryption. It is a substitution cipher where each letter in the original message (called the plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.
In this way, a message that initially was quite readable, ends up in a form that can not be understood at a simple glance.
For example, here's the Caesar Cipher encryption of a message, using a right shift of 3.
Plaintext:  
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: 
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD


Following Code will Encrypt Plain Text Using Caesar Cipher :

Code:
import java.util.Scanner;
public class Caesar 
{
    
public static void main(String[] nt) 
{
Scanner in =new Scanner(System.in);
System.out.print("Enter Plain Text: ");
String s=in.nextLine();
char[] a=new char[s.length()];
char[] b={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{
a[i]=s.charAt(i);
}
for(int j=0;j<a.length;j++)
{  if(a[j]!=' ')
{for(int i=0;i<b.length;i++)
{
if(a[j]==b[i])
{
a[j]=b[(i+3)%26];
break;
}
}}
}
System.out.print("     Cipher Text: ");
for(char d: a)
System.out.print(d);
System.out.println();

for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
if(j>=3)
{
a[i]=b[(j-3)%26];
}
else
{
a[i]=b[(23+j)%26];
}
break;
}
}
}
System.out.print("     Plain  Text:");
for(char j:a){
System.out.print(j);
}
System.out.println();
}


}

Output:

Thursday, June 8, 2017

Reading a MD array Using Enhanced For Loop

Following Code will read 2D and 3D arrays using for each loop

Code:


public class Java3 
{
public static void main(String[] nt)
{   

int[][] nti={
       {7,6,2,3},
       {6,7},
       {2,3},
       {7,6},
       {7,5},
       {7,5,6,7}
           };
System.out.println("nti :"+nti);      //address of nti[0]
System.out.println("nti length :"+nti.length);     // length of array i.e. no of arrays inside

for(int[] i : nti)
{
for(int j : i)
{
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("---------------------");
int[][][] ntii={
       {
        {7,6,2,3},
        {6,7},
        {2,3},
        {7,6},
        {7,5,6,7}
       },
       { {7,6,2,3},
       {6,7},
       {2,3},
       {7,6},
       {7,5,6,7}
   }
           };
System.out.println("ntii :"+ntii);
System.out.println("ntii length :"+ntii.length);

for(int[][] i : ntii)
{
for(int[] j : i)
{
for(int k : j)
System.out.print(k+" ");
System.out.println();
}
System.out.println();
}
   }
}

Output:

Greatest of Five Numbers (Simple Approach)

Following code will find largest numbers out of five numbers 

Code:

public class Java2 
{
public static void main(String[] nt)
{
int a=7,b=6,c=2,d=3,e=7;
int[] nti=new int[]{a,b,c,d,e};
int[] temp=new int[5];
int max =nti[0];
for(int i=1;i<nti.length;i++)
{
if(max<nti[i])
{
max=nti[i];
}
}
int j=0;
for(int i=0;i<nti.length;i++)
{
if(nti[i]==max)
{
temp[j]=i;
j++;
}
}

if(j==1)
{
System.out.print("Greatest is: ");
}
else
{   System.out.print("Greatest are: ");
}
for(int i=0;i<j;i++)
{
 switch(temp[i])
 {
  case 0:
  System.out.print("A ");
  break;
  case 1:
  System.out.print("B ");
  break;
  case 2:
  System.out.print("C ");
  break;
  case 3:
  System.out.print("D ");
  break;
  case 4:
  System.out.print("E ");
  break;
 }
}
}
}

Output: