SlideShare a Scribd company logo
1 of 10
PathOfMostResistance.java
package acyclicDigraph;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PathOfMostResistance {
/**
* @param args
* enter full path of file as args[0]
*/
public static void main(String[] args) {
PathOfMostResistance fileHandler = new PathOfMostResistance();
List<String[]> rows = fileHandler.loadFile(args[0]);
loadValues(rows);
}
/**
* Loads values to object NodeCollection
* @param rows
* @return
*/
private static NodeCollection loadValues(List<String[]> rows) {
NodeCollection nodeCollection = new NodeCollection();
int key = 0;
for (int i = 0; i < rows.size(); i++) {
nodeCollection.increaseLineNumber();
String[] line = rows.get(i);
int lineLength = line.length;
for (int l = 0; l < lineLength; l++) {
int name = Integer.parseInt(line[l]);
nodeCollection.addNode(key, name, i + 1, l);
key++;
}
}
nodeCollection.setRelationshipValues(nodeCollection);
return nodeCollection;
}
/**
* @param fileName
* @return
*/
private List<String[]> loadFile(String fileName) {
List<String[]> list = new ArrayList<String[]>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
try {
String line = null;
while ((line = br.readLine()) != null) {
String record[] = line.split(" ");
list.add(record);
}
} catch (IOException e) {
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
NodeCollection.java
package acyclicDigraph;
import java.util.Vector;
public class NodeCollection{
Node root = null;
private int lineNumber = 0;
private Vector<Node> nodes = new Vector<Node>();
/**
* @param nodeCollection
* @return
*/
public NodeCollection setRelationshipValues(NodeCollection nodeCollection){
int size = nodeCollection.getLength();
for(int i = 0; i < size; i++){
int row = nodeCollection.findNode(i).getLevel();
if(nodeCollection.hasNode(i + row)){
nodeCollection.findNode(i).setLeftChild(nodeCollection.findNode(i + row));
nodeCollection.findNode(i).getLeftChild().setRightParent(nodeCollection.find
Node(i));
}
if(nodeCollection.hasNode(i + row + 1)){
nodeCollection.findNode(i).setRightChild(nodeCollection.findNode(i + row +
1));
nodeCollection.findNode(i).getRightChild().setLeftParent(nodeCollection.find
Node(i));
}
Node node = nodeCollection.findNode(i);
int leftLeftTotal = node.hasLeftParent() ?
node.getLeftParent().getLeftAccumulativeTotal() : 0;
int leftRightTotal = node.hasLeftParent() ?
node.getLeftParent().getRightAccumulativeTotal() : 0;
int rightLeftTotal = node.hasRightParent() ?
node.getRightParent().getLeftAccumulativeTotal() : 0;
int rightRightTotal = node.hasRightParent() ?
node.getRightParent().getRightAccumulativeTotal() : 0;
int leftTotal = 0;
int rightTotal = 0;
if(leftLeftTotal > leftRightTotal){
leftTotal = leftLeftTotal;
nodeCollection.findNode(i).setBest(true);
} else {
leftTotal = leftRightTotal;
nodeCollection.findNode(i).setBest(true);
}
if(rightLeftTotal > rightRightTotal){
rightTotal = rightLeftTotal;
nodeCollection.findNode(i).setBest(true);
} else {
rightTotal = rightRightTotal;
nodeCollection.findNode(i).setBest(true);
}
int presentName = node.getName();
nodeCollection.findNode(i).setLeftAccumulativeTotal(leftTotal +
presentName);
nodeCollection.findNode(i).setRightAccumulativeTotal(rightTotal +
presentName);
}
int length = nodeCollection.getLength();
int lastLineNumber = nodeCollection.findNode(length - 1).getLevel();
Node bottomNode = nodeCollection.findNode(length - 1);
for(int i = length - lastLineNumber; i < length; i++){
Node presentNode = nodeCollection.findNode(i);
if((presentNode.getLeftAccumulativeTotal() >
bottomNode.getLeftAccumulativeTotal()) ||
(presentNode.getLeftAccumulativeTotal() >
bottomNode.getRightAccumulativeTotal()) ||
(presentNode.getRightAccumulativeTotal() >
bottomNode.getLeftAccumulativeTotal()) ||
(presentNode.getRightAccumulativeTotal() >
bottomNode.getRightAccumulativeTotal())){
bottomNode = presentNode;
}
}
showHighestPath(bottomNode);
return nodeCollection;
}
/**
* @param bottomNode
*/
private void showHighestPath(Node bottomNode) {
int accumLeft = bottomNode.getLeftAccumulativeTotal();
int accumRight = bottomNode.getRightAccumulativeTotal();
System.out.println("Highest Node Value: " + (accumLeft > accumRight ?
accumLeft : accumRight));
System.out.println("Walking from bottom to top, the first node is: "
+ bottomNode.getName());
while(bottomNode != null){
accumLeft = bottomNode.getLeftAccumulativeTotal();
accumRight = bottomNode.getRightAccumulativeTotal();
if(accumLeft > accumRight){
bottomNode = bottomNode.getLeftParent();
if(bottomNode != null) {
System.out.println("Going Left to: " +
bottomNode.getName());
}
} else {
bottomNode = bottomNode.getRightParent();
if(bottomNode != null) {
System.out.println("Going right to: " +
bottomNode.getName());
}
}
}
}
/**
*
*/
void increaseLineNumber() {
this.lineNumber++;
}
/**
* @return
*/
int getLineNumber() {
return this.lineNumber;
}
/**
* @param key
* @param name
* @param row
* @param position
*/
void addNode(int key, int name, int row, int position) {
Node newNode = new Node(key, name, row, position);
if (key == 0) {
this.root = newNode;
}
nodes.add(key, newNode);
}
/**
* @param listNumber
* @return
*/
Node findNode(int listNumber) {
return nodes.get(listNumber);
}
/**
* @param nodeNumber
* @return
*/
boolean hasNode(int nodeNumber) {
return nodes.size() >= nodeNumber + 1;
}
/**
* @return
*/
public int getLength() {
return nodes.size();
}
/**
* @return
*/
public Node getRoot() {
return root;
}
/**
* @param root
*/
public void setRoot(Node root) {
this.root = root;
}
}
Node.java
package acyclicDigraph;
/**
* @author Edward Cleveland
* @date 05262015
*
*/
public class Node {
private int key;
private int name;
private int row;
private Node leftParentNode;
private Node rightParentNode;
private Node leftChildNode;
private Node rightChildNode;
private int length;
private boolean best = false;
private int leftAccumulativeTotal = 0;
private int rightAccumulativeTotal = 0;
/**
* @param key
* @param name
* @param row
* @param position
*/
Node(int key, int name, int row, int position) {
this.key = key;
this.name = name;
this.setLevel(row);
}
/**
* @param key
* @param name
* @param leftParent
* @param rightParent
* @param leftChild
* @param rightChild
*/
Node(int key, int name, Node leftParent, Node rightParent, Node leftChild,
Node rightChild) {
this.key = key;
this.name = name;
this.leftParentNode = leftParent;
this.rightParentNode = rightParent;
this.leftChildNode = leftChild;
this.rightChildNode = rightChild;
}
/**
* @param length
*/
public void setLength(int length) {
this.length = length;
}
/**
* @return
*/
public int getLength() {
return length;
}
/**
* @param key
*/
public void setKey(int key) {
this.key = key;
}
/**
* @return
*/
public int getKey() {
return key;
}
/**
* @param name
*/
public void setName(int name) {
this.name = name;
}
/**
* @return
*/
public int getName() {
return name;
}
/**
* @param leftParentNode
*/
public void setLeftParent(Node leftParentNode) {
this.leftParentNode = leftParentNode;
}
/**
* @return
*/
public boolean hasLeftParent() {
return leftParentNode != null;
}
/**
* @return
*/
public Node getLeftParent() {
return leftParentNode;
}
/**
* @param rightParentNode
*/
public void setRightParent(Node rightParentNode) {
this.rightParentNode = rightParentNode;
}
/**
* @return
*/
public boolean hasRightParent() {
return rightParentNode != null;
}
/**
* @return
*/
public Node getRightParent() {
return rightParentNode;
}
/**
* @param leftChildNode
*/
public void setLeftChild(Node leftChildNode) {
this.leftChildNode = leftChildNode;
}
/**
* @return
*/
public boolean hasLeftChild() {
return leftChildNode != null;
}
/**
* @return
*/
public Node getLeftChild() {
return leftChildNode;
}
/**
* @param rightChildNode
*/
public void setRightChild(Node rightChildNode) {
this.rightChildNode = rightChildNode;
}
/**
* @return
*/
public boolean hasRightChild() {
return rightChildNode != null;
}
/**
* @return
*/
public Node getRightChild() {
return rightChildNode;
}
/**
* @return
*/
public int getLevel() {
return row;
}
/**
* @param row
*/
public void setLevel(int row) {
this.row = row;
}
/**
* @return
*/
public int getLeftAccumulativeTotal() {
return leftAccumulativeTotal;
}
/**
* @param leftAccumulativeTotal
*/
public void setLeftAccumulativeTotal(int leftAccumulativeTotal) {
this.leftAccumulativeTotal = leftAccumulativeTotal;
}
/**
* @return
*/
public int getRightAccumulativeTotal() {
return rightAccumulativeTotal;
}
/**
* @param rightAccumulativeTotal
*/
public void setRightAccumulativeTotal(int rightAccumulativeTotal) {
this.rightAccumulativeTotal = rightAccumulativeTotal;
}
/**
* @return
*/
public boolean isBest() {
return best;
}
/**
* @param best
*/
public void setBest(boolean best) {
this.best = best;
}
}

More Related Content

What's hot

OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerMario Fusco
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharpDhaval Dalal
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersLin Yo-An
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
 
C# features through examples
C# features through examplesC# features through examples
C# features through examplesZayen Chagra
 
Hello Swift 3/5 - Function
Hello Swift 3/5 - FunctionHello Swift 3/5 - Function
Hello Swift 3/5 - FunctionCody Yun
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMMario Fusco
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsBrian Moschel
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascriptReece Carlson
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2Zaar Hai
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointerLei Yu
 

What's hot (20)

OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharp
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
MP in Clojure
MP in ClojureMP in Clojure
MP in Clojure
 
OSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP hatersOSDC.TW - Gutscript for PHP haters
OSDC.TW - Gutscript for PHP haters
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
Swift 2
Swift 2Swift 2
Swift 2
 
C# features through examples
C# features through examplesC# features through examples
C# features through examples
 
Hello Swift 3/5 - Function
Hello Swift 3/5 - FunctionHello Swift 3/5 - Function
Hello Swift 3/5 - Function
 
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STMConcurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascript
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 

Viewers also liked

Uc 2 Gia Bao Nhieu
Uc 2 Gia Bao NhieuUc 2 Gia Bao Nhieu
Uc 2 Gia Bao Nhieugus170
 
đau Khớp đầu Gối Chân
đau Khớp đầu Gối Chânđau Khớp đầu Gối Chân
đau Khớp đầu Gối Chânreyes407
 
Curriculum Vitae AJR 2015
Curriculum Vitae AJR 2015Curriculum Vitae AJR 2015
Curriculum Vitae AJR 2015Alan Robertson
 
Dau Khop Hang Benh Gi
Dau Khop Hang Benh GiDau Khop Hang Benh Gi
Dau Khop Hang Benh Gidrucilla827
 
Vien Khop Nhan Hung
Vien Khop Nhan HungVien Khop Nhan Hung
Vien Khop Nhan Hungteisha541
 
Discussion week 7 c7 (powepoint)
Discussion week 7   c7 (powepoint)Discussion week 7   c7 (powepoint)
Discussion week 7 c7 (powepoint)franzettamcneil
 

Viewers also liked (11)

Rd 21-03-2006
Rd 21-03-2006Rd 21-03-2006
Rd 21-03-2006
 
Uc 2 Gia Bao Nhieu
Uc 2 Gia Bao NhieuUc 2 Gia Bao Nhieu
Uc 2 Gia Bao Nhieu
 
đau Khớp đầu Gối Chân
đau Khớp đầu Gối Chânđau Khớp đầu Gối Chân
đau Khớp đầu Gối Chân
 
Curriculum Vitae AJR 2015
Curriculum Vitae AJR 2015Curriculum Vitae AJR 2015
Curriculum Vitae AJR 2015
 
Dau Khop Hang Benh Gi
Dau Khop Hang Benh GiDau Khop Hang Benh Gi
Dau Khop Hang Benh Gi
 
S ni p 4.09-91
S ni p 4.09-91S ni p 4.09-91
S ni p 4.09-91
 
Aruna kumar K R resume
Aruna kumar K R  resumeAruna kumar K R  resume
Aruna kumar K R resume
 
Vien Khop Nhan Hung
Vien Khop Nhan HungVien Khop Nhan Hung
Vien Khop Nhan Hung
 
Final report - New Innovation
Final report - New Innovation Final report - New Innovation
Final report - New Innovation
 
Illustration-2016:2
Illustration-2016:2Illustration-2016:2
Illustration-2016:2
 
Discussion week 7 c7 (powepoint)
Discussion week 7   c7 (powepoint)Discussion week 7   c7 (powepoint)
Discussion week 7 c7 (powepoint)
 

Similar to PathOfMostResistance Java File Analyzer

Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfarjuncorner565
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdfangelsfashion1
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfaksahnan
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfFashionBoutiquedelhi
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfanwarsadath111
 
ItemNodeh include ltiostreamgt include ltstring.pdf
ItemNodeh    include ltiostreamgt include ltstring.pdfItemNodeh    include ltiostreamgt include ltstring.pdf
ItemNodeh include ltiostreamgt include ltstring.pdfacmefit
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdffmac5
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
 
public final category Huffman non-public Huffman() ;personal stati.pdf
public final category Huffman non-public Huffman() ;personal stati.pdfpublic final category Huffman non-public Huffman() ;personal stati.pdf
public final category Huffman non-public Huffman() ;personal stati.pdfapposekitchenpvd
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfbirajdar2
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfmallik3000
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageAnıl Sözeri
 
The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189Mahmoud Samir Fayed
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdffathimafancyjeweller
 

Similar to PathOfMostResistance Java File Analyzer (20)

Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
 
ItemNodeh include ltiostreamgt include ltstring.pdf
ItemNodeh    include ltiostreamgt include ltstring.pdfItemNodeh    include ltiostreamgt include ltstring.pdf
ItemNodeh include ltiostreamgt include ltstring.pdf
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 
public final category Huffman non-public Huffman() ;personal stati.pdf
public final category Huffman non-public Huffman() ;personal stati.pdfpublic final category Huffman non-public Huffman() ;personal stati.pdf
public final category Huffman non-public Huffman() ;personal stati.pdf
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
 
Java Generics
Java GenericsJava Generics
Java Generics
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdfPLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189The Ring programming language version 1.6 book - Part 84 of 189
The Ring programming language version 1.6 book - Part 84 of 189
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 

PathOfMostResistance Java File Analyzer

  • 1. PathOfMostResistance.java package acyclicDigraph; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class PathOfMostResistance { /** * @param args * enter full path of file as args[0] */ public static void main(String[] args) { PathOfMostResistance fileHandler = new PathOfMostResistance(); List<String[]> rows = fileHandler.loadFile(args[0]); loadValues(rows); } /** * Loads values to object NodeCollection * @param rows * @return */ private static NodeCollection loadValues(List<String[]> rows) { NodeCollection nodeCollection = new NodeCollection(); int key = 0; for (int i = 0; i < rows.size(); i++) { nodeCollection.increaseLineNumber(); String[] line = rows.get(i); int lineLength = line.length; for (int l = 0; l < lineLength; l++) { int name = Integer.parseInt(line[l]); nodeCollection.addNode(key, name, i + 1, l); key++; } } nodeCollection.setRelationshipValues(nodeCollection); return nodeCollection; } /** * @param fileName * @return */ private List<String[]> loadFile(String fileName) { List<String[]> list = new ArrayList<String[]>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(fileName)); try { String line = null;
  • 2. while ((line = br.readLine()) != null) { String record[] = line.split(" "); list.add(record); } } catch (IOException e) { } } catch (FileNotFoundException e1) { e1.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } return list; } } NodeCollection.java package acyclicDigraph; import java.util.Vector; public class NodeCollection{ Node root = null; private int lineNumber = 0; private Vector<Node> nodes = new Vector<Node>(); /** * @param nodeCollection * @return */ public NodeCollection setRelationshipValues(NodeCollection nodeCollection){ int size = nodeCollection.getLength(); for(int i = 0; i < size; i++){ int row = nodeCollection.findNode(i).getLevel(); if(nodeCollection.hasNode(i + row)){ nodeCollection.findNode(i).setLeftChild(nodeCollection.findNode(i + row)); nodeCollection.findNode(i).getLeftChild().setRightParent(nodeCollection.find Node(i)); } if(nodeCollection.hasNode(i + row + 1)){ nodeCollection.findNode(i).setRightChild(nodeCollection.findNode(i + row + 1)); nodeCollection.findNode(i).getRightChild().setLeftParent(nodeCollection.find Node(i)); } Node node = nodeCollection.findNode(i);
  • 3. int leftLeftTotal = node.hasLeftParent() ? node.getLeftParent().getLeftAccumulativeTotal() : 0; int leftRightTotal = node.hasLeftParent() ? node.getLeftParent().getRightAccumulativeTotal() : 0; int rightLeftTotal = node.hasRightParent() ? node.getRightParent().getLeftAccumulativeTotal() : 0; int rightRightTotal = node.hasRightParent() ? node.getRightParent().getRightAccumulativeTotal() : 0; int leftTotal = 0; int rightTotal = 0; if(leftLeftTotal > leftRightTotal){ leftTotal = leftLeftTotal; nodeCollection.findNode(i).setBest(true); } else { leftTotal = leftRightTotal; nodeCollection.findNode(i).setBest(true); } if(rightLeftTotal > rightRightTotal){ rightTotal = rightLeftTotal; nodeCollection.findNode(i).setBest(true); } else { rightTotal = rightRightTotal; nodeCollection.findNode(i).setBest(true); } int presentName = node.getName(); nodeCollection.findNode(i).setLeftAccumulativeTotal(leftTotal + presentName); nodeCollection.findNode(i).setRightAccumulativeTotal(rightTotal + presentName); } int length = nodeCollection.getLength(); int lastLineNumber = nodeCollection.findNode(length - 1).getLevel(); Node bottomNode = nodeCollection.findNode(length - 1); for(int i = length - lastLineNumber; i < length; i++){ Node presentNode = nodeCollection.findNode(i); if((presentNode.getLeftAccumulativeTotal() > bottomNode.getLeftAccumulativeTotal()) || (presentNode.getLeftAccumulativeTotal() > bottomNode.getRightAccumulativeTotal()) || (presentNode.getRightAccumulativeTotal() > bottomNode.getLeftAccumulativeTotal()) || (presentNode.getRightAccumulativeTotal() > bottomNode.getRightAccumulativeTotal())){ bottomNode = presentNode; } } showHighestPath(bottomNode);
  • 4. return nodeCollection; } /** * @param bottomNode */ private void showHighestPath(Node bottomNode) { int accumLeft = bottomNode.getLeftAccumulativeTotal(); int accumRight = bottomNode.getRightAccumulativeTotal(); System.out.println("Highest Node Value: " + (accumLeft > accumRight ? accumLeft : accumRight)); System.out.println("Walking from bottom to top, the first node is: " + bottomNode.getName()); while(bottomNode != null){ accumLeft = bottomNode.getLeftAccumulativeTotal(); accumRight = bottomNode.getRightAccumulativeTotal(); if(accumLeft > accumRight){ bottomNode = bottomNode.getLeftParent(); if(bottomNode != null) { System.out.println("Going Left to: " + bottomNode.getName()); } } else { bottomNode = bottomNode.getRightParent(); if(bottomNode != null) { System.out.println("Going right to: " + bottomNode.getName()); } } } } /** * */ void increaseLineNumber() { this.lineNumber++; } /** * @return */ int getLineNumber() { return this.lineNumber; } /** * @param key * @param name * @param row * @param position */ void addNode(int key, int name, int row, int position) { Node newNode = new Node(key, name, row, position); if (key == 0) {
  • 5. this.root = newNode; } nodes.add(key, newNode); } /** * @param listNumber * @return */ Node findNode(int listNumber) { return nodes.get(listNumber); } /** * @param nodeNumber * @return */ boolean hasNode(int nodeNumber) { return nodes.size() >= nodeNumber + 1; } /** * @return */ public int getLength() { return nodes.size(); } /** * @return */ public Node getRoot() { return root; } /** * @param root */ public void setRoot(Node root) { this.root = root; } } Node.java package acyclicDigraph; /** * @author Edward Cleveland * @date 05262015 * */
  • 6. public class Node { private int key; private int name; private int row; private Node leftParentNode; private Node rightParentNode; private Node leftChildNode; private Node rightChildNode; private int length; private boolean best = false; private int leftAccumulativeTotal = 0; private int rightAccumulativeTotal = 0; /** * @param key * @param name * @param row * @param position */ Node(int key, int name, int row, int position) { this.key = key; this.name = name; this.setLevel(row); } /** * @param key * @param name * @param leftParent * @param rightParent * @param leftChild * @param rightChild */ Node(int key, int name, Node leftParent, Node rightParent, Node leftChild, Node rightChild) { this.key = key; this.name = name; this.leftParentNode = leftParent; this.rightParentNode = rightParent; this.leftChildNode = leftChild; this.rightChildNode = rightChild; } /** * @param length */ public void setLength(int length) { this.length = length; } /** * @return */ public int getLength() {
  • 7. return length; } /** * @param key */ public void setKey(int key) { this.key = key; } /** * @return */ public int getKey() { return key; } /** * @param name */ public void setName(int name) { this.name = name; } /** * @return */ public int getName() { return name; } /** * @param leftParentNode */ public void setLeftParent(Node leftParentNode) { this.leftParentNode = leftParentNode; } /** * @return */ public boolean hasLeftParent() { return leftParentNode != null; } /** * @return */ public Node getLeftParent() { return leftParentNode; } /** * @param rightParentNode */ public void setRightParent(Node rightParentNode) {
  • 8. this.rightParentNode = rightParentNode; } /** * @return */ public boolean hasRightParent() { return rightParentNode != null; } /** * @return */ public Node getRightParent() { return rightParentNode; } /** * @param leftChildNode */ public void setLeftChild(Node leftChildNode) { this.leftChildNode = leftChildNode; } /** * @return */ public boolean hasLeftChild() { return leftChildNode != null; } /** * @return */ public Node getLeftChild() { return leftChildNode; } /** * @param rightChildNode */ public void setRightChild(Node rightChildNode) { this.rightChildNode = rightChildNode; } /** * @return */ public boolean hasRightChild() { return rightChildNode != null; } /** * @return */
  • 9. public Node getRightChild() { return rightChildNode; } /** * @return */ public int getLevel() { return row; } /** * @param row */ public void setLevel(int row) { this.row = row; } /** * @return */ public int getLeftAccumulativeTotal() { return leftAccumulativeTotal; } /** * @param leftAccumulativeTotal */ public void setLeftAccumulativeTotal(int leftAccumulativeTotal) { this.leftAccumulativeTotal = leftAccumulativeTotal; } /** * @return */ public int getRightAccumulativeTotal() { return rightAccumulativeTotal; } /** * @param rightAccumulativeTotal */ public void setRightAccumulativeTotal(int rightAccumulativeTotal) { this.rightAccumulativeTotal = rightAccumulativeTotal; } /** * @return */ public boolean isBest() { return best; }
  • 10. /** * @param best */ public void setBest(boolean best) { this.best = best; } }