Monday, June 26, 2017

MyInteger Wrapper Class

Following Code define a Class MyInteger Which acts as a Wrapper class for integer :


Code:

class MyInteger {
int i;
MyInteger(int i){
this.i=i;
}
int intValue(){
return i;
}

public String toString() {
return String.valueOf(i);
}

}
/*class MyInteger1 extends Integer {
//error because Integer is a final class
}*/
public class MyIntegerDemo {
public static void main(String[] nt) {
MyInteger mi=new MyInteger(10);
int j = mi.intValue();
System.out.println(mi);
System.out.println(j);
}
}

Output:


Friday, June 23, 2017

Copy File Contents

Following Code will help you to copy contents from one file to another with or without appending.

Code:
import java.io.*;
public class FileCopy {

void copy(File s,File d,boolean a){
FileReader r=null;
BufferedReader br=null;
FileWriter w=null;
BufferedWriter bw=null;
try {
r=new FileReader(s);
br=new BufferedReader(r);
w=new FileWriter(d,a);
bw=new BufferedWriter(w);
String l;
while((l=br.readLine())!=null){
bw.write("\n"+l);
}bw.newLine();
System.out.println("Copy Sucessful");
} catch (Exception e) {
System.out.println("Error: "+e);
//e.printStackTrace();
}
finally{
try {
r.close();
br.close();
bw.close();
w.close();
} catch (Exception e2) {
System.out.println("Error: "+e2);
}

}
}
public static void main(String[] nt) {
FileCopy fRef=new FileCopy();
File src=new File("C:/Users/mrdis/Downloads/nt.txt");
File des=new File("C:/Users/mrdis/Downloads/nti.txt");
boolean append=true;
fRef.copy(src, des, append);

}

}
Output:

After Running Three Times with append

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: