lec9 - 1.00 Lecture 9 Methods and Objects Access Reading...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.00 Lecture 9 Methods and Objects Access Reading for next time: Big Java: sections 7.6, 7.7 Storing weather for a city • We’ll first show an example of storing temperature and precipitation data for cities. – Our classes have some methods with arguments – We’ll examine the methods and arguments: they’ll do what you expect • We’ll then extend our weather example to have each city store a weather information object that collects the weather data in one place – These classes also have methods with arguments – We’ll examine these methods too. They also do what you expect (but your expectations will have to be a bit more sophisticated!) 1 SimpleCity public class SimpleCity { private String name; avgTemperature; private double avgTemperature; precipAmt; private double precipAmt; SimpleCity(String public SimpleCity(String n, double a, double p) { name= n; avgTemperature= avgTemperature= a; precipAmt= precipAmt= p; } getName() public String getName() { return name; } getAvgTemperature() public double getAvgTemperature() { avgTemperature; return avgTemperature; } setAvgTemperature(double public void setAvgTemperature(double t) { avgTemperature= avgTemperature= t; } getPrecipAmt() public double getPrecipAmt() { precipAmt; return precipAmt; }} SimpleWeatherTest public class SimpleWeatherTest { args) public static void main(String args) { boston= SimpleCity("Boston", SimpleCity boston= new SimpleCity("Boston", 40.0, 0.0); cambridge= SimpleCity("Cambridge", SimpleCity cambridge= new SimpleCity("Cambridge", 40.0, 0.0); // Now revise the Boston weather, which was corrected boston.setAvgTemperature(41.0); System.out.println("Boston: boston.getAvgTemperature()); System.out.println("Boston: " + boston.getAvgTemperature()); println System.out.println println("Cambridge: System.out.println("Cambridge: "+ cambridge.getAvgTemperature()); cambridge.getAvgTemperature()); } } // What is the output of this program? 2 Passing Arguments SimpleWeatherTest main(…){… City boston= … boston.setAvgTemp(41.0); … Communication only via arg list, return value Argument 1 SimpleCity boston public void setAvgTemp(double t) { // Method makes its own copy // of argument t avgTemperature= t; } Setting the Cambridge temperature would be the same 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; precipAmt; private double precipAmt; public Weather(double a, double p) { avgTemperature= avgTemperature= a; precipAmt= precipAmt= p; } setAvgTemp(double public void setAvgTemp(double t) { avgTemperature= avgTemperature= t; } setPrecipAmt(double public void setPrecipAmt(double pr) { precipAmt= precipAmt= pr; } toString() public String toString() { "+avgTemperature avgTemperature+ return ("Temperature: "+avgTemperature+ "+precipAmt precipAmt); " ; Precipitation: "+precipAmt); } } City class public class City { private String name; cityWeather; private Weather cityWeather; public City(String n, Weather c) { name= n; cityWeather= cityWeather= c; } getName() public String getName() { return name; } getWeather() public Weather getWeather() { cityWeather; return cityWeather; } } 4 WeatherTest public class WeatherTest { public static void main(String args) { args) Weather today= new Weather(40.0, 0.0); boston= City boston= new City("Boston", today); cambridge= City cambridge= new City("Cambridge", today); // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday.setAvgTemp(41.0); System.out.println println("Boston: boston.getWeather()); System.out.println("Boston: " + boston.getWeather()); System.out.println("Cambridge: cambridge.getWeather()); System.out.println("Cambridge: "+ cambridge.getWeather()); println } } Exercise- Weather classes • Download Weather, City, WeatherTest • Import them into Eclipse • Before running them, think about what the output will be • Compile and run them • Is the output what you expected? 5 Objects As Arguments today= Weather today= new Weather(40.0, 0.0); boston= City boston= new City("Boston", today); cambridge= City cambridge= new City("Cambridge", today); // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday.setAvgTemp(41.0); 40.0 0.0 Weather Objects As Arguments today= Weather today= new Weather(40.0, 0.0); City boston= new City("Boston", today); boston= boston= cambridge= City cambridge= new City("Cambridge", today); // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday.setAvgTemp(41.0); “Boston” City 40.0 0.0 Weather 6 Objects As Arguments today= Weather today= new Weather(40.0, 0.0); boston= City boston= new City("Boston", today); boston= cambridge= City cambridge= new City("Cambridge", today); cambridge= // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday.setAvgTemp(41.0); “Boston” City 40.0 0.0 Weather “Cambridge” City Objects As Arguments today= Weather today= new Weather(40.0, 0.0); City boston= new City("Boston", today); boston= boston= cambridge= City cambridge= new City("Cambridge", today); cambridge= // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday= bostonToday.setAvgTemp(41.0); “Boston” City 40.0 0.0 Weather “Cambridge” City 7 Objects As Arguments today= Weather today= new Weather(40.0, 0.0); boston= City boston= new City("Boston", today); boston= cambridge= City cambridge= new City("Cambridge", today); cambridge= // Now revise the Boston weather, which was corrected bostonToday= boston.getWeather(); Weather bostonToday= boston.getWeather(); bostonToday= bostonToday.setAvgTemp(41.0); “Boston” City 41.0 40.0 0.0 Weather “Cambridge” City When objects are passed as arguments to methods, the method makes a copy of the reference to the object, not a copy of the object! Why? Method Calls With Objects • When passing object references as arguments to a method: – The method makes its own copy of the references – It makes changes to the objects through its local copies of the references – No changes can be made to the references (arguments) • The method can’t change the reference to another object, for example – Results are returned through the return value, which may be an object • When passing built-in data types as arguments to a method: – – – – The method makes its own copy of the built-in variables It makes changes to its local copies only No changes are made to the arguments Results are returned through the return value 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; cityWeather; private Weather cityWeather; } SaferCity(String public SaferCity(String n, Weather c) { name= n; c.getAvgTemperature getAvgTemperature(); double temp= c.getAvgTemperature(); prec= c.getPrecipAmt getPrecipAmt(); double prec= c.getPrecipAmt(); cityWeather= prec); cityWeather= new Weather(temp, prec); } getName() public String getName() { return name; } getWeather() public Weather getWeather() { cityWeather; return cityWeather; } getAvgTemperature(), getPrecipAmt() // Weather must have methods getAvgTemperature(), getPrecipAmt() Access: Variables, Methods • Instance and static variables and methods have 4 access modifiers: – Private: Access only to own class’ methods • Data fields should be private, almost always • Other objects of same class can access private variables – Public: Access to all methods, all classes • Methods intended for other class’ use are public • Methods for internal use only are private – Package: Access to methods of classes in same package (a package is a group of classes) • This is the default, alas. Always specify scope explicitly • No ‘package’ keyword; it’s the default with no keyword – Protected: Used with inheritance (covered later) • Like a private variable, except it’s visible to derived or subclasses (and, in Java, to other classes in package) 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 package weather; // Eclipse wrote this for you // Cut and paste this from the download, or import it public class Observation { private double humidity; cloudCover; private double cloudCover; public Observation(double h, double c) { humidity= h; cloudCover= cloudCover= c; } getHumidity() public double getHumidity() { return humidity; } getCloudCover() public double getCloudCover() { cloudCover; return cloudCover; } toString() public String toString() { return ("Humidity: "+ humidity+ "+cloudCover cloudCover); " ; Cloud cover: "+cloudCover); } } 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 import Add a private Observation obj private Modify your constructor Add a getObservation method getObservation • 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 Modified Class Observation package package weather; public class Observation2 { double humidity; // No keyword means package access cloudCover; double cloudCover; // No keyword means package access public Observation2(double h, double c) { humidity= h; cloudCover= cloudCover= c; } } 12 Class AdvancedObservation package package weather; public class AdvancedObservation { dewpoint; double dewpoint; // Package access obs; Observation2 obs; // Package access AdvancedObservation(double public AdvancedObservation(double d, Observation2 o) { dewpoint= dewpoint= d; obs= obs= o; } toString(){ public String toString(){ "+obs obs.humidity+" return ("Humidity: "+obs.humidity+" ; Cloud cover: " + obs.cloudCover+ Dewpoint: dewpoint); obs.cloudCover+ " ; Dewpoint: "+ dewpoint); obs.XXX // We can use obs.XXX directly // Observation2 could use dewpoint directly also } } 13 ...
View Full Document

Ask a homework question - tutors are online