Technology Get the latest on technology, electronics and software…

Anyone know programming in java? Need help ASAP

Thread Tools
 
Old Apr 24, 2012 | 01:23 PM
  #1  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
Exclamation Anyone know programming in java? Need help ASAP

Hi guys, I was wondering if anyone knows how to program in java. I posted it in ramblings figuring there must be someone in this field.

Specifically, I need help using Dijkstra's algorithm.

The task is to determine the shortest path from a given node to each other node. I only need to track the cost, and NOT route of each path. The graph is represented by an adjacent matrix with all nodes connected.
Reply
Old Apr 24, 2012 | 01:31 PM
  #2  
doopstr's Avatar
Team Owner
20 Year Member
Liked
Loved
Community Favorite
 
Joined: Jan 2001
Posts: 25,967
Likes: 2,685
From: Jersey
http://renaud.waldura.com/doc/java/dijkstra/


BTW, if that's what you want then you suck at interwebz and need to pick a new field
Reply
Old Apr 24, 2012 | 01:36 PM
  #3  
TeknoKing's Avatar
Race Director
 
Joined: Jun 2003
Posts: 10,497
Likes: 279
^
Reply
Old Apr 24, 2012 | 02:04 PM
  #4  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
well i knew that lol. i can google on my own.
i have written some code but don't know if it seems right. can you take a look.

public class Final_Start {

final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;

int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };


// Finds the shortest path from passed start node to all others
void shortestPath(int start)
{
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}


} // end shortestPath

public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");

} // end showSolution

public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
Reply
Old Apr 24, 2012 | 05:14 PM
  #5  
#1 STUNNA's Avatar
Sanest Florida Man
Photogenic
Photoriffic
Shutterbug
Community Influencer
 
Joined: Aug 2007
Posts: 46,058
Likes: 11,814
From: Florida
this is all I need to know about Java

Start Button > Control Panel > Programs and Features > Java Update ## > Change/Remove

Fuck that shit!
Reply
Old Apr 24, 2012 | 06:08 PM
  #6  
doopstr's Avatar
Team Owner
20 Year Member
Liked
Loved
Community Favorite
 
Joined: Jan 2001
Posts: 25,967
Likes: 2,685
From: Jersey
Java is such crap on the workstation but it's amazing how many backend systems are running Java application servers to do serious stuff.
Reply
Old Apr 24, 2012 | 06:19 PM
  #7  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
so can anyone tell me if i'm doing it right?
Reply
Old Apr 24, 2012 | 06:33 PM
  #8  
ez12a's Avatar
the overexplainer
 
Joined: Feb 2011
Posts: 3,287
Likes: 385
From: OC, CA
eh i learned a bit of java my first year but ended up declaring something else. Nothing really stuck, sorry. I know basic javascript? I know that's not even close to being the same. I can kind of read what you're doing with but i cant help Going through the array with a for loop while incrementing the array position and also nested arrays or something. Fun stuff.

Android apps are more or less java are they not? They run in a java VM.

Last edited by ez12a; Apr 24, 2012 at 06:39 PM.
Reply
Old Apr 24, 2012 | 06:48 PM
  #9  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
yup, android apps are java based
Reply
Old Apr 24, 2012 | 07:27 PM
  #10  
TeknoKing's Avatar
Race Director
 
Joined: Jun 2003
Posts: 10,497
Likes: 279
objective programming, so much fun, I don't miss it one bit.

Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).

If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)
Reply
Old Apr 24, 2012 | 08:05 PM
  #11  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
Originally Posted by TeknoKing
objective programming, so much fun, I don't miss it one bit.

Your objective is to recreate the existing Dijkstra class? Did you try compiling it? I'm not very excited about the nested loops, do they traverse properly? plot it out on paper (backwards).

If I'll get bored, I'll trace it (after 9 hours of staring at 3 screens, I'm not bored yet, sorry)


Actually, I don't even have to use the Dijkstra class. The task is to determine the shortest path from a given node to each other node. I only need to track the cost, and NOT route of each path. I already know what they are, but just want the program to figure it out and print them.

It compiles properly, but when I run the program it only prints this:
"The shortest path from A to each node is:"

I want this output:
Node: B - Cost: 3
Node: C - Cost: 9
Node: D - Cost: 3
Node: E - Cost: 9
Node: F - Cost: 2

P.S. If anyone can figure it out I'll send them a gift. lol
Reply
Old Apr 24, 2012 | 10:30 PM
  #12  
honda_nut's Avatar
Thread Starter
2024 Genesis G70 3.3T
15 Year Member
Liked
Loved
Community Favorite
 
Joined: Aug 2007
Posts: 864
Likes: 144
From: Norcal
Well, I got it to work. I was missing a print statement
Only problem is that it prints out extra stuff. The stuff in red is all I want.

4 3 9 3 9 2
3 2 6 6 8 1
9 6 8 8 4 7
3 6 8 6 12 5
9 8 4 12 8 7
2 1 7 5 7 2
The shortest path from A to each node is:




public class Final_Start {

final int NODES = 6;
final int INFINITY = 65535;
final int X = INFINITY;

int[][] weights = new int[][] {
// A B C D E F
{ X, 5, 15, 3, 9, 2 },
{ 5, X, 6, 14, 11, 1 },
{15, 6, X, 8, 4, 12 },
{ 3, 14, 8, X, 13, 10 },
{ 9, 11, 4, 13, X, 7 },
{ 2, 1, 12, 10, 7, X } };


// Finds the shortest path from passed start node to all others
void shortestPath(int start) {
for (int k=0; k<6; k++){
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
if(weights[i][k] != X && weights[k][j] != X && weights[i][k]+weights[k][j] < weights[i][j]){
weights[i][j] = weights[i][k]+weights[k][j];
}
}
}
}

for (int r=0; r<6; r++) {
for (int c=0; c<6; c++) {
if(weights[r][c] == X){
System.out.print("n/a");
}else{
System.out.print(" " + weights[r][c]);
}
}
System.out.println(" ");
}

} // end shortestPath

public void showSolution(int start) {
System.out.println("The shortest path from " + (char)('A' + start) + " to each node is:\n");

} // end showSolution

public static void main(String args[]) {
Final_Start fs = new Final_Start();
fs.shortestPath(0);
fs.showSolution(0);
}
}
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
joflewbyu2
5G TLX (2015-2020)
139
Oct 8, 2015 11:16 AM
njresendez
3G TL Photograph Gallery
3
Sep 21, 2015 06:32 PM
Skirmich
2G TL (1999-2003)
37
Sep 15, 2015 06:41 PM
Billu99tl
3G TL Problems & Fixes
1
Sep 13, 2015 11:30 AM




All times are GMT -5. The time now is 08:14 PM.