MATLAB Answers

Convert JAVA code to MATLAB code

43 views (last 30 days)
Partha
Partha on 3 Apr 2015
Answered: hp on 2 Apr 2021
Can any one convert the java code (see the attachments) to MATLAB code (m file)
  2 Comments
sambhav jain
sambhav jain on 19 Feb 2020
is there any sofware available for conversion of java code to matlab format?

Sign in to comment.

Answers (2)

praneeth ranga
praneeth ranga on 3 Sep 2017
Edited: Walter Roberson on 4 Sep 2017
package kcjava;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Element;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import java.util.*;
class GraphOpers{
LinkedList<String> nonLeafNodes(LinkedList<GraphData> ld){
LinkedList<String> al = new LinkedList<String>();
for(int i=0;i<ld.size();i++){
for(int j=0;j<ld.size();j++){
for(int k=0;k+1<=ld.get(j).pred.length();k=k+2){
if(ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !al.contains(ld.get(i).act) && !ld.get(j).pred.equals("NULL")){
al.add(ld.get(i).act);
break;
}
}
}
}
return al;
}
GraphData findNode(LinkedList<GraphData> ld,String ss){
for(int i=0;i<ld.size();i++){
if(ld.get(i).act.equals(ss)){
return ld.get(i);
}
}
return null;
}
int sizeofGraph(LinkedList<GraphData> ld,int n){
LinkedList<String> al = new LinkedList<String>();
for(int i=0;i<ld.size();i++){
GraphData dd = ld.get(i);
if(dd.pred.length()>1 && !al.contains(dd.pred) && !dd.pred.equals("NULL")){
al.addLast(dd.pred);
n=(int) (n-Math.floor(dd.pred.length()/2));
}
}
return n;
}
int[][] makeEdge(int g[][],int i,int j){
g[i][j]=1;
return g;
}
int fact(int n){
int j=1;
for(int i=2;i<=n;i++){
j=j*i;
}
return j;
}
float add(float z){
float res=z,temp=z;
for(int i=1;i<15;i++){
temp=temp*z*z/(2*i+1);
res=res+temp;
}
return res;
}
float erf(float z){
float pi = (float) 3.1415926,res=0;
for(int i=0;i<25;i++){
res=(float) (res+(Math.pow(-1,i)*Math.pow(z,2*i+1))/(fact(i)*(2*i+1)));
}
res=(float) (res*2/Math.sqrt(pi));
return res;
}
}
class GraphData{
String act;
String pred;
float period;
float sd;
int start;
int end;
GraphData(String act,String pred,float period,float sd,int start,int end){
this.act=act;
this.pred=pred;
this.sd=sd;
this.period=period;
this.start=start;
this.end=end;
}
}
class GraphNode{
float es;
float lc;
GraphNode(float es,float lc){
this.es=es;
this.lc=lc;
}
}
public class CPM {
public static void main(String[] args) {
LinkedList<GraphData> ld = new LinkedList<GraphData>();
LinkedList<GraphNode> ln = new LinkedList<GraphNode>();
LinkedList<Integer> lres = new LinkedList<Integer>();
LinkedList<Integer> tf = new LinkedList<Integer>();
LinkedList<Integer> ff = new LinkedList<Integer>();
LinkedList<String> al = new LinkedList<String>();
LinkedList<String> lc = new LinkedList<String>();
LinkedList<String> al1 = new LinkedList<String>();
Scanner sc = new Scanner(System.in);
System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
GraphOpers go = new GraphOpers();
int n,sog,cou=2;
float dt,v=0;
System.out.println("Enter number of activities:");
n= sc.nextInt();
System.out.println("Enter tm, tp, to,activity, predecessor:");
for(int i=0;i<n;i++){
float tm,tp,to;
tm=sc.nextFloat();to=sc.nextFloat();tp=sc.nextFloat();
float j=(to+4*tm+tp)/6;
float sd=(tp-to)/6;
GraphData d= new GraphData(sc.next(),sc.next(),j,sd,0,0);
ld.add(d);
}
al=go.nonLeafNodes(ld);
sog=go.sizeofGraph(ld,n)+2-n+al.size();
for(int i=0;i<ld.size();i++){
if(ld.get(i).pred.equals("NULL") && !ld.get(i).act.equals("DU")){
ld.get(i).start=1;
ld.get(i).end=cou;
cou++;
}
else if(!ld.get(i).pred.equals("NULL") && al.contains(ld.get(i).act)){
for(int j=0;j<ld.size();j++){
for(int k=0;k+1<=ld.get(j).pred.length();k=k+2){
GraphData gd1,gd2;
if(ld.get(j).pred.length()>1 && !al1.contains(ld.get(i).act) && ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !ld.get(j).pred.equals("NULL")){
if(k-2>=0){
al1.add(ld.get(i).act);
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
gd2=go.findNode(ld,ld.get(j).pred.substring(k-2,k-1));
ld.get(i).start=gd1.end;
ld.get(i).end=gd2.end;
for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
ld.get(i).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
cou++;
ld.add(d);
}
}
break;
}
else{
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
al1.add(ld.get(i).act);
ld.get(i).start=gd1.end;
ld.get(i).end=cou;
cou++;
/*for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
ld.get(i).end=cou;
cou++;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
ld.add(d);
}
}*/
break;
}
}
else if(ld.get(j).pred.length()<=1 && !al1.contains(ld.get(i).act) && ld.get(i).act.equals(ld.get(j).pred.substring(k,k+1)) && !ld.get(j).pred.equals("NULL")){
gd1=go.findNode(ld,ld.get(i).pred.substring(0,1));
al1.add(ld.get(i).act);
ld.get(i).start=gd1.end;
ld.get(i).end=cou;
cou++;
/* for(int kk=0;kk<i;kk++){
if(ld.get(i).start==ld.get(kk).start && ld.get(i).end==ld.get(kk).end && i!=kk){
sog++;
cou++;
ld.get(i).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(kk).end);
ld.add(d);
}
}*/
break;
}
}
}
}
else if(!ld.get(i).pred.equals("NULL") && !al.contains(ld.get(i))){
GraphData gd =go.findNode(ld,ld.get(i).pred.substring(0,1));
ld.get(i).start=gd.end;
ld.get(i).end=cou;
}
}
for(int i=0;i<sog;i++){
GraphNode gn = new GraphNode(0,0);
ln.add(gn);
}
/*for(int i=0;i<ld.size();i++){
for(int j=0;j<ld.size();j++){
if(ld.get(i).start==ld.get(j).start && ld.get(i).end==ld.get(j).end && i!=j){
GraphNode gn = new GraphNode(0,0);
ln.add(gn);
cou++;
ld.get(j).end=cou;
GraphData d= new GraphData("DU","NULL",0,0,cou,ld.get(i).end);
ld.add(d);
}
}
}*/
for(int i=0;i<ld.size();i++){
System.out.println(ld.get(i).act+":("+ld.get(i).start+","+ld.get(i).end+")");
}
for(int i=0;i<ld.size();i++){
if(ld.get(i).pred.equals("NULL") && !ld.get(i).act.equals("DU")){
ln.get(ld.get(i).end-1).es=ld.get(i).period;
}
if(ln.get(ld.get(i).end-1).es==0){
ln.get(ld.get(i).end-1).es=ln.get(ld.get(i).start-1).es+ld.get(i).period;
}
if(ln.get(ld.get(i).end-1).es>0 && ln.get(ld.get(i).end-1).es<ln.get(ld.get(i).start-1).es+ld.get(i).period){
ln.get(ld.get(i).end-1).es=ln.get(ld.get(i).start-1).es+ld.get(i).period;
}
}
ln.get(sog-1).lc=ln.get(sog-1).es;
for(int i=ld.size()-1;i>=0;i--){
if(ln.get(ld.get(i).start-1).lc==0){
ln.get(ld.get(i).start-1).lc=ln.get(ld.get(i).end-1).lc-ld.get(i).period;
}
if(ln.get(ld.get(i).start-1).lc>ln.get(ld.get(i).end-1).lc-ld.get(i).period){
ln.get(ld.get(i).start-1).lc=ln.get(ld.get(i).end-1).lc-ld.get(i).period;
}
}
ln.get(0).lc=0;
for(int i=0;i<ln.size();i++){
if(ln.get(i).es==ln.get(i).lc){
lres.add(i+1);
}
}
System.out.print("Critical Path: ");
for(int i=0;i<ld.size();i++){
for(int j=0;j<lres.size()-1;j++){
//System.out.println(lres.get(j)+" "+ld.get(i).start+" "+lres.get(j+1)+" "+ld.get(i).end);
if(lres.get(j)==ld.get(i).start && lres.get(j+1)==ld.get(i).end){
System.out.print(ld.get(i).act+" ");
lc.add(ld.get(i).act);
v=v+ld.get(i).sd*ld.get(i).sd;
}
}
}
System.out.println();
System.out.println("Variance:"+v);
v=(float)Math.sqrt(v);
for(int i=0;i<ld.size();i++){
if(ld.get(i).start>=1){
tf.add((int) (ln.get(ld.get(i).end-1).lc-ln.get(ld.get(i).start-1).es-ld.get(i).period));
ff.add((int) (ln.get(ld.get(i).end-1).es-ln.get(ld.get(i).start-1).es-ld.get(i).period));
}
}
for(int i=0;i<ld.size();i++){
if(tf.get(i)==0 && ff.get(i)==0){
lres.add(i+1);
}
}
System.out.println("Enter Desired Time:");
dt=sc.nextFloat();
float ct=ln.get(ln.size()-1).es;
NormalDistribution nd=new NormalDistribution();
float z1=(dt-ct)/v;
System.out.println("Z:"+z1);
float z=go.erf((float) (z1/1.414));
z=(z+1)/2;
System.out.println("Probability:"+z);
}
}
  1 Comment
Walter Roberson
Walter Roberson on 4 Sep 2017
This does not appear to be an Answer to this question ?

Sign in to comment.


hp
hp on 2 Apr 2021
i have stemmercode in Java kindly convert it to the matlab code

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!