Documente Academic
Documente Profesional
Documente Cultură
00 Lecture 9
1
SimpleCity
public class SimpleCity {
private String name;
private double avgTemperature;
avgTemperature;
private double precipAmt;
precipAmt;
public SimpleCity(String
SimpleCity(String n, double a, double p) {
name= n;
avgTemperature=
avgTemperature= a;
precipAmt=
precipAmt= p;
}
public String getName()
getName() {
return name;
}
public double getAvgTemperature()
getAvgTemperature() {
return avgTemperature;
avgTemperature;
}
public void setAvgTemperature(double
setAvgTemperature(double t) {
avgTemperature=
avgTemperature= t;
}
public double getPrecipAmt()
getPrecipAmt() {
return precipAmt;
precipAmt;
} }
SimpleWeatherTest
public class SimpleWeatherTest {
public static void main(String[] args)
args) {
SimpleCity boston=
boston= new SimpleCity("Boston",
SimpleCity("Boston", 40.0, 0.0);
SimpleCity cambridge=
cambridge= new SimpleCity("Cambridge",
SimpleCity("Cambridge", 40.0, 0.0);
System.out.println
System.out.println("Boston:
println("Boston: " + boston.
boston.getAvgTemperature());
getAvgTemperature());
System.out.println
System.out.println("Cambridge:
println("Cambridge: "+
cambridge.
cambridge.getAvgTemperature());
getAvgTemperature());
}
}
2
Passing Arguments
SimpleWeatherTest
Communi-
main(…){… cation only
City boston= … via arg list,
boston.setAvgTemp(41.0); return value
…
Argument 1
SimpleCity boston
Method/Object Exercise
• We now change SimpleCity and
SimpleWeatherTest slightly
– We rename them City and WeatherTest
– We also introduce a simple Weather class
– We’ll look at them briefly on the next slides
3
Weather class
public class Weather {
private double avgTemperature;
avgTemperature;
private double precipAmt;
precipAmt;
public Weather(double a, double p) {
avgTemperature=
avgTemperature= a;
precipAmt=
precipAmt= p;
}
public void setAvgTemp(double
setAvgTemp(double t) {
avgTemperature=
avgTemperature= t;
}
public void setPrecipAmt(double
setPrecipAmt(double pr) {
precipAmt=
precipAmt= pr;
}
public String toString()
toString() {
return ("Temperature: "+avgTemperature
"+avgTemperature+
avgTemperature+
" ; Precipitation: "+precipAmt
"+precipAmt);
precipAmt);
}
}
City class
public class City {
private String name;
private Weather cityWeather;
cityWeather;
4
WeatherTest
public class WeatherTest {
public static void main(String[] args)
args) {
Weather today= new Weather(40.0, 0.0);
City boston=
boston= new City("Boston", today);
City cambridge=
cambridge= new City("Cambridge", today);
System.out.println
System.out.println("Boston:
println("Boston: " + boston
boston. getWeather());
.getWeather());
System.out.println
System.out.println("Cambridge:
println("Cambridge: "+ cambridge.
cambridge.getWeather());
getWeather());
}
}
5
Objects As Arguments
Weather today= new Weather(40.0, 0.0); today=
City boston=
boston= new City("Boston", today);
City cambridge=
cambridge= new City("Cambridge", today);
40.0 0.0
Weather
Objects As Arguments
Weather today= new Weather(40.0, 0.0); today=
City boston=
boston= new City("Boston", today); boston=
City cambridge=
cambridge= new City("Cambridge", today);
City Weather
6
Objects As Arguments
Weather today= new Weather(40.0, 0.0); today=
City boston=
boston= new City("Boston", today); boston=
City cambridge=
cambridge= new City("Cambridge", today);
cambridge=
// Now revise the Boston weather, which was corrected
Weather bostonToday=
bostonToday= boston.
boston.getWeather();
getWeather();
bostonToday.setAvgTemp(41.0);
Objects As Arguments
Weather today= new Weather(40.0, 0.0); today=
City boston=
boston= new City("Boston", today); boston=
City cambridge=
cambridge= new City("Cambridge", today);
cambridge=
// Now revise the Boston weather, which was corrected
Weather bostonToday=
bostonToday= boston.getWeather(); bostonToday=
boston.getWeather();
bostonToday.setAvgTemp(41.0);
7
Objects As Arguments
Weather today= new Weather(40.0, 0.0); today=
City boston=
boston= new City("Boston", today); boston=
City cambridge=
cambridge= new City("Cambridge", today);
cambridge=
// Now revise the Boston weather, which was corrected
Weather bostonToday=
bostonToday= boston.getWeather(); bostonToday=
boston.getWeather();
bostonToday.setAvgTemp(41.0);
41.0
“Boston” 40.0 0.0 “Cambridge”
8
If you don’t like this…
• When you pass an object reference as an
argument to a method, the method may make its
own local copy of the object:
public class SaferCity {
private String name;
private Weather cityWeather;
cityWeather;
public SaferCity(String
SaferCity(String n, Weather c) {
name= n;
double temp= c.getAvgTemperature
c.getAvgTemperature();
getAvgTemperature();
double prec=
prec= c.getPrecipAmt
c.getPrecipAmt();
getPrecipAmt();
cityWeather=
cityWeather= new Weather(temp, prec);
prec);
}
public String getName()
getName() {
return name;
}
public Weather getWeather()
getWeather() {
return cityWeather;
cityWeather;
}
} // Weather must have methods getAvgTemperature(),
getAvgTemperature(), getPrecipAmt()
getPrecipAmt()
9
Packages in Eclipse
In Eclipse:
File -> New -> Package. Type ‘weather’
Use lower case names by convention
Create a new class Observation in weather
(File -> New -> Class … )
Class Observation
package weather; // Eclipse wrote this for you
10
Add Observation to City
• In your default package in Lecture 11:
– Modify your City class to also have an
Observation object:
• Add import weather.*; on 1st line of City.java
• Add a private Observation obj
• Modify your constructor
• Add a getObservation method
• We’ll show the solution on the next slide,
and then go on to modify WeatherTest to
use your new City and Observation
Modify WeatherTest
• Change WeatherTest, still in the default
package, to:
– Create a new Observation
– Place it in Boston and Cambridge
– Output it (System.out.println) for Boston
– Remember to import weather.*; on line 1
11
Package access
• If we added another class AdvancedObservation
to package weather
• And we made humidity and cloudCover package
access variables by removing the private
keyword (in an Observation2 class)
– We also remove the getXXX methods as unneeded
• Then AdvancedObservation can use Observation
data members, such as humidity and cloudCover
directly. It can simply say, for an Observation2
object obs:
– obs.humidity, or obs.cloudCover
as if they were in the AdvancedObservation class
12
Class AdvancedObservation
package weather;
public class AdvancedObservation {
double dewpoint;
dewpoint; // Package access
Observation2 obs;
obs; // Package access
public AdvancedObservation(double
AdvancedObservation(double d, Observation2 o) {
dewpoint=
dewpoint= d;
obs=
obs= o;
}
public String toString(){
toString(){
return ("Humidity: "+obs
"+obs.humidity+"
obs.humidity+" ; Cloud cover: " +
obs.
obs.cloudCover+
cloudCover+ " ; Dewpoint:
Dewpoint: "+ dewpoint);
dewpoint);
// We can use obs.XXX
obs.XXX directly
// Observation2 could use dewpoint directly also
}
}
13