Monday, June 26, 2017

MyScanner Class

Following Code will define a class MyScanner to take input from user using InputStreamReader ..

Code:


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

class MyScanner {
InputStreamReader i;
BufferedReader br;
public MyScanner(InputStream x) {
i=new InputStreamReader(x);
br=new BufferedReader(i);
}
public int nextInt() {
int a=0;
try {
a =Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public float nextFloat() {
float a=0;
try {
a =Float.parseFloat(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public double nextDouble() {
double a=0;
try {
a =Double.parseDouble(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public long nextLong() {
long a=0;
try {
a =Long.parseLong(br.readLine());
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public String nextLine() {
String a=null;
try {
a =br.readLine();
} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}
public String next() {
String a="";
int ch=0;
try {
while(true) {
ch=br.read();
if(ch==32 || ch==10){
break;
}
a=a.concat(""+(char)ch);
}

} catch (Exception e) {
System.out.println("Error:"+e);
}
return a;
}

/*class MyScanner1 extends Scanner {
// error because Scanner is final class
}*/

}
public class MyScannerDemo {
public static void main(String[] nt) {
MyScanner in=new MyScanner(System.in);
//Character
char ch=in.next().charAt(0);
System.out.println("Entered Character: "+ch);
//Integer
int i=in.nextInt();
System.out.println("Entered Integer: "+i);


}
}

Output:

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::