Tuesday, June 27, 2017

Source Code Analysis

Following code will print the number and names of Objects,Classes,Interfaces created in java source file.

Code:


import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class SourceCode {
public static void main(String[] nt){
File src=null;
FileReader fin=null;
BufferedReader br=null;
int o=0,c=0,i=0;
try {

Date d=new Date();
long start=d.getTime();
                        src=new File("F:/Session14/src/com/auribises","CollectionsDemo.java");
fin=new FileReader(src);
br=new BufferedReader(fin);
String line="";
HashMap<String,Integer> mp=new HashMap<String,Integer>();
StringBuffer data=new StringBuffer();
StringBuffer datai=new StringBuffer();

while((line=br.readLine())!=null) {
if(line.contains("new")){
o++;
int k=line.indexOf("new");
int l=line.indexOf("(",k+3);
if(mp.containsKey(line.substring(k+3,l))){
int x=mp.get(line.substring(k+3,l));
mp.put(line.substring(k+3,l),++x);
}
else{
mp.put(line.substring(k+3,l),1);
}
}
if(line.contains("class")){
c++;
int k=line.indexOf("class");
int l=line.indexOf("{",k+6);
data.append(line.substring(k+6,l)+"\n");
}
if(line.contains("interface")){
i++;
int k=line.indexOf("interface");
int l=line.indexOf("{",k+10);
datai.append(line.substring(k+10,l)+"\n");
}
}
System.out.println(src.getName());

System.out.println("\n--------------Objects-----------------");
Set<String> k=mp.keySet();
Iterator<String> it=k.iterator();
while(it.hasNext()){
String x=it.next();
int value=mp.get(x);
System.out.println(x+" : "+value);
}
System.out.println("No. of Objects created:"+o);

System.out.println("\n--------------Classes------------------");
System.out.println(data);
System.out.println("No. of Classes created:"+c);

System.out.println("\n--------------Interfaces---------------");
System.out.println(datai);
System.out.println("No. of Interfaces created:"+i+"\n");

d=new Date();
long stop=d.getTime();
System.out.println("Time Taken:"+(stop-start)+" miliseconds");

} catch (Exception e) {
System.out.println("Some Error"+e);
}finally {
try {
fin.close();
br.close();
} catch (Exception e2) {
System.out.println("Error:"+e2);
}
}

}
}

Output:

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: