Changes

Jump to: navigation, search

Teams Winter 2011/team2/lab3

4,081 bytes added, 01:25, 12 March 2011
no edit summary
=Tutorial=
In this tutorial, we are creating an RCP application to inform the user of current weather temperatures in major cities of CanadaNorth America. We will use our previous lab 2 classes and alter them according to our specification for this lab. For reference material, we are using [http://www.vogella.de/articles/EclipseRCP/article.html#architecture_overview Eclipse RCP] and [http://www.vogella.de/articles/EclipseJFaceTable/article.html#jfaceviewers Eclipse JFace Table] tutorial.
==Prerequisites==
*Download [http://www.eclipse.org/downloads/packages/eclipse-rcp-and-rap-developers/heliossr2 Eclipse RCP & RAP Developer]
[[File:Runconf2T2.png]]
==Creating a Package and Classes==
*We are now going to create a package called "ecl.team2.lab3.weathermodel". To do this, right-click the src folder, move the cursor to "New", then click packages. Enter "ecl.team2.lab3.weathermodel" in the "Name" textbox and click Finish.
[[File:PckgT2.png]][[File:Pckg2T2.png]]
*Now we are going to create classes within this package. To create a class, right-click our newly created package, highlight "New", and scroll down and click "Class". Create a class for "Weather" and "SimpleWeatherSystem".
[[File:Classl3T2.png]]
*Copy and paste the following to their respective file.
'''Weather'''
<source lang=java>
package ecl.team2.lab3.weathermodel;
 
public class Weather {
private String city;
private float tempInCelcius;
private float rainInMM;
private float snowInMM;
private float windspeedInKM;
private char windDirection;
public Weather(String pcity, float ptemp, float prain, float psnow, float pwspeed, char pwdirection){
this.setCity(pcity);
this.setTempInCelcius(ptemp);
this.setRainInMM(prain);
this.setSnowInMM(psnow);
this.setWindspeedInKM(pwspeed);
this.setWindDirection(pwdirection);
}
public void setWindDirection(char windDirection) {
this.windDirection = windDirection;
}
 
public char getWindDirection() {
return windDirection;
}
 
public void setWindspeedInKM(float windspeedInKM) {
this.windspeedInKM = windspeedInKM;
}
 
public float getWindspeedInKM() {
return windspeedInKM;
}
 
public void setSnowInMM(float snowInMM) {
this.snowInMM = snowInMM;
}
 
public float getSnowInMM() {
return snowInMM;
}
 
public void setRainInMM(float rainInMM) {
this.rainInMM = rainInMM;
}
 
public float getRainInMM() {
return rainInMM;
}
 
public void setTempInCelcius(float tempInCelcius) {
this.tempInCelcius = tempInCelcius;
}
 
public float getTempInCelcius() {
return tempInCelcius;
}
 
public void setCity(String city) {
this.city = city;
}
 
public String getCity() {
return city;
}
}
</source>
'''SimpleWeatherSystem'''
<source lang=java>
package ecl.team2.lab3.weathermodel;
 
import java.util.ArrayList;
import java.util.Random;
 
public class SimpleWeatherSystem {
ArrayList<Weather> weathers = new ArrayList<Weather>();
public SimpleWeatherSystem(){
ArrayList<String> cities = new ArrayList<String>();
cities.add("Toronto");
cities.add("New York");
cities.add("Calgary");
cities.add("Ottawa");
cities.add("Vancouver");
cities.add("Regina");
cities.add("Winnipeg");
cities.add("St. John");
cities.add("Fredericton");
cities.add("Halifax");
cities.add("Quebec City");
cities.add("Whitehorse");
cities.add("Yellowknife");
for(String c:cities){
Random randomGenerator = new Random();
float temp = randomGenerator.nextFloat()+35;
float rain = randomGenerator.nextFloat()+10;
float snow = randomGenerator.nextFloat()+10;
float windspeed = randomGenerator.nextFloat()+50;
char winddirection = '.';
int r = randomGenerator.nextInt(4);
switch(r){
case 0:
winddirection = 'E';
break;
case 1:
winddirection = 'W';
break;
case 2:
winddirection = 'N';
break;
case 3:
winddirection = 'S';
break;
}
Weather w = new Weather(c, temp, rain, snow, windspeed, winddirection);
this.weathers.add(w);
System.out.println(w.toString());
}
}
public Weather getWeather(String city) throws Exception {
// TODO Auto-generated method stub
Weather found = null;
for(Weather w:this.weathers){
city = city.toLowerCase();
String wcity =w.getCity().toLowerCase();
if(city.equals(wcity)){
found = w;
return found;
}
}
return found;
}
 
}
</source>
1
edit

Navigation menu