I want to learn Java!

3 replies [Last post]
Offline
Last seen: 1 year 42 weeks ago
Joined: 07/27/2010
Posts:

Want to learn Java.

tim.speed's picture
Offline
Last seen: 3 days 7 hours ago
Joined: 02/24/2010
Posts:
Here is a great place to

Here is a great place to start:
http://en.wikibooks.org/wiki/Java_Programming

-----------------
Tim Speed
Compilr Developer and CTO

Offline
Last seen: 1 year 34 weeks ago
Joined: 09/26/2010
Posts:
hiii

i want to know about it..

package assignment1;
/*
*Program Name : Map.java
*Project Name : Assignment 1 Search Lab
*Program Desc : Program to model the Map from the input files. The program also has methods to search for the shortest distance
*Author : Abhiroop Gupta
*/
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.LinkedList;
public class Map {

protected HashMap adjacencyMapCity=null;
protected HashMap adjacencyMapSLD=null;

protected HashMap openList=null;
protected HashMap closedList=null;
Map()
{

adjacencyMapCity=new HashMap();
adjacencyMapSLD=new HashMap();

openList=new HashMap();
closedList=new HashMap();
//ReadMap();

}

private boolean addCity(String cityName)
{
if(adjacencyMapCity.containsKey(cityName))
{
return false;
}
else
{
adjacencyMapCity.put(cityName, new LinkedList());

return true;
}
}
private boolean addSLDData(String cityName)
{
if(adjacencyMapSLD.containsKey(cityName))
{
return false;
}
else
{
adjacencyMapSLD.put(cityName, new LinkedList());

return true;
}
}
public boolean addPath(Object v1, Object v2)
{
LinkedList l = (LinkedList)adjacencyMapCity.get(v1);
l.add(v2);
return true;
}
public boolean addSLDPath(Object v1, Object v2)
{
LinkedList l = (LinkedList)adjacencyMapSLD.get(v1);
l.add(v2);
return true;
}
public String generateMap()
{
String returnValue="";
File MapFile= new File("Input\\map.csv");
File SLDFile = new File("Input\\sld.csv");
FileInputStream fisMap=null;
BufferedInputStream bisMap=null;
DataInputStream disMap=null;
FileInputStream fisSLD=null;
BufferedInputStream bisSLD=null;
DataInputStream disSLD=null;
try
{
fisMap=new FileInputStream(MapFile);
bisMap=new BufferedInputStream(fisMap);
disMap=new DataInputStream(bisMap);

fisSLD=new FileInputStream(SLDFile);
bisSLD=new BufferedInputStream(fisSLD);
disSLD=new DataInputStream(bisSLD);

String strCItiesSLD=disSLD.readLine();

StringTokenizer stkCities=new StringTokenizer(strCItiesSLD);
int i=0;
String nxtCity=null;
while(stkCities.hasMoreTokens())
{
nxtCity=stkCities.nextToken(",");
addCity(nxtCity);
addSLDData(nxtCity);
i++;

}
String[] arrCities = new String[i+1];
stkCities=new StringTokenizer(strCItiesSLD);
i=0;

while(stkCities.hasMoreTokens())
{

arrCities[i]=stkCities.nextToken(",");
i++;
}

StringTokenizer stkCityData=null;
StringTokenizer stkSLDData=null;
while(disMap.available()!=0)
{

String cityData=disMap.readLine();
stkCityData =new StringTokenizer(cityData);

String strCity1=stkCityData.nextToken(",");
String strCity2=stkCityData.nextToken(",");
int distance=Integer.parseInt(stkCityData.nextToken(",").toString());

addPath(strCity1, new City(strCity2, distance));
addPath(strCity2,new City(strCity1, distance));

}

while(disSLD.available()!=0)
{
String SLDData=disSLD.readLine();
stkSLDData=new StringTokenizer(SLDData);
int j=0;
String City1=stkSLDData.nextToken(",");
while(stkSLDData.hasMoreTokens())
{
String City2=arrCities[j];
int distance=Integer.parseInt(stkSLDData.nextToken(",").toString());
j++;
addSLDPath(City1, new City(City2, distance));

}

}
fisMap.close();
bisMap.close();
disMap.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
return returnValue;
}
}
public String getShortestPath(String City1,String City2)
{
String strPath="";
openList.put(City1, new CityQueueData(City1, City1, 0, getSLDCost(City1, City2)));
while(!openList.isEmpty())
{
String strMinCostCity=getMinCostCity();
closedList.put(strMinCostCity, openList.get(strMinCostCity));

CityQueueData cityQueueData=(CityQueueData)openList.get(strMinCostCity);
//openList.remove(strMinCostCity);
LinkedList llRoutes=(LinkedList)adjacencyMapCity.get(strMinCostCity);
for(int i=0;i"+currentCity.getStrCityName();
int pathCost=cityQueueData.getPathCost()+currentCity.getIntDistance();
int estimatedDistance=pathCost + getSLDCost(currentCity.getStrCityName(), City2);
if(closedList.containsKey(currentCity.getStrCityName()))
{
CityQueueData closedElement=(CityQueueData)closedList.get(currentCity.getStrCityName());
if(closedElement.getEstimatedCost()>estimatedDistance)
{
closedElement.setEstimatedCost(estimatedDistance);
closedElement.setStrPath(path);
closedList.remove(currentCity.getStrCityName());
closedList.put(currentCity.getStrCityName(), closedElement);
}
}
else if(openList.containsKey(currentCity.getStrCityName()))
{
CityQueueData openElement=(CityQueueData)openList.get(currentCity.getStrCityName());
if(openElement.getEstimatedCost()>estimatedDistance)
{
openElement.setEstimatedCost(estimatedDistance);
openElement.setStrPath(path);
openList.remove(currentCity.getStrCityName());
openList.put(currentCity.getStrCityName(), openElement);
}

}
else
{
CityQueueData newQueueElement=new CityQueueData(path, currentCity.getStrCityName(), pathCost, estimatedDistance);
openList.put(currentCity.getStrCityName(), newQueueElement);
}

}
openList.remove(strMinCostCity);
}
if(closedList.containsKey(City2))
{
CityQueueData destCity=(CityQueueData)closedList.get(City2);
strPath=destCity.getStrPath();
}
return strPath;
}
public String getMinCostCity()
{
Set keySet=openList.keySet();
Iterator i=keySet.iterator();
int minCost=-1;
String MinCostCity="";

while(i.hasNext())
{
CityQueueData currentCity=(CityQueueData)openList.get(i.next().toString());
if(minCost<0)
{
minCost=currentCity.getEstimatedCost();
MinCostCity=currentCity.getStrCityName();
}
else if(minCost>currentCity.getEstimatedCost())
{
minCost=currentCity.getEstimatedCost();
MinCostCity=currentCity.getStrCityName();
}
}
return MinCostCity;
}
public int getSLDCost(String City1,String City2)
{
int retrunValue=0;

LinkedList llRoutes =(LinkedList)adjacencyMapSLD.get(City1);

for(int i=0;i>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package assignment1;
/*
*Program Name : City.java
*Project Name : Assignment 1 Search Lab
*Program Desc : DAO for each City
*Author : Abhiroop Gupta
*/
public class City {

private String strCityName;
private int intDistance;

public String getStrCityName() {
return strCityName;
}
public void setStrCityName(String strCityName) {
this.strCityName = strCityName;
}
public City(String strCityName, int intDistance) {

this.strCityName = strCityName;
this.intDistance = intDistance;

}
public int getIntDistance() {
return intDistance;
}
public void setIntDistance(int intDistance) {
this.intDistance = intDistance;
}

}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

package assignment1;

public class CityQueueData {
public CityQueueData(String strPath, String strCityName, int pathCost,
int estimatedCost) {
super();
this.strPath = strPath;
this.strCityName = strCityName;
this.pathCost = pathCost;
this.estimatedCost = estimatedCost;
}
private String strPath;
private String strCityName;
private int pathCost;
private int estimatedCost;
public String getStrPath() {
return strPath;
}
public void setStrPath(String strPath) {
this.strPath = strPath;
}
public String getStrCityName() {
return strCityName;
}
public void setStrCityName(String strCityName) {
this.strCityName = strCityName;
}
public int getPathCost() {
return pathCost;
}
public void setPathCost(int pathCost) {
this.pathCost = pathCost;
}
public int getEstimatedCost() {
return estimatedCost;
}
public void setEstimatedCost(int estimatedCost) {
this.estimatedCost = estimatedCost;
}

}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

package assignment1;
/*
*Program Name : NavigateMap.java
*Project Name : Assignment 1 Search Lab
*Program Desc : Program to query the Create and query distances in the map
*Author : Abhiroop Gupta
*/
import java.io.*;
import java.io.ObjectInputStream.GetField;
public class NavigateMap {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

try
{
Map myMap=new Map();
myMap.generateMap();
File sourceFile = new File("Input\\source.txt");
File destFile=new File("Input\\destination.txt");
FileInputStream fisSource=null;
BufferedInputStream bisSource=null;
DataInputStream disSource=null;
FileInputStream fisDest=null;
BufferedInputStream bisDest=null;
DataInputStream disDest=null;

fisSource=new FileInputStream(sourceFile);
bisSource=new BufferedInputStream(fisSource);
disSource=new DataInputStream(bisSource);

fisDest=new FileInputStream(destFile);
bisDest=new BufferedInputStream(fisDest);
disDest=new DataInputStream(bisDest);

String strSourceCity=disSource.readLine();
String strDestinationCity =disDest.readLine();

String Path=myMap.getShortestPath(strSourceCity, strDestinationCity);
if(Path.equals(""))
{
System.out.println("No Path Exists");
}
else
System.out.println(Path);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}

}

}

tim.speed's picture
Offline
Last seen: 3 days 7 hours ago
Joined: 02/24/2010
Posts:
The Java Docs are a good way

The Java Docs are a good way to learn more about what each class and method does:

http://download.oracle.com/javase/6/docs/api/

-----------------
Tim Speed
Compilr Developer and CTO