Docsity

Appunti teorici su programmazione Java con codice e esempi, Appunti di Programmazione Java

Anteprima parziale del testo

Scarica Appunti teorici su programmazione Java con codice e esempi e più Appunti in PDF di Programmazione Java solo su Docsity!

// Use a return value class Vehicle { int passengers; // number of passengers int fuelcap; int mpg; // fuel capacity in gallons// fuel consumption in miles per gallon //Display the range. int range() { return fuelcap * mpg; // here I don't have to specify what fuelcap, mpg are // they are defined within the class and are available!! }^ } public class RetMeth { public static void main(String[] args) { // TODO Auto-generated method stub /* * Remember that first we define an address to an object only after

  • with new we e */ ffectively create a "physical" object Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); int range1, range2; // assign values to fields in minivan minivan.passengers = 7; minivan.fuelcap = 16; minivan.mpg = 21; //assign values to fields in sportscar sportscar.passengers = 2; sportscar.fuelcap = 14; sportscar.mpg = 12; range1 = minivan.range(); range2 = sportscar.range(); System.out.println("Minivan can carry " + minivan.passengers + " with range of " + range1 + " miles"); System.out.println("Minivan can carry " + sportscar.passengers + " with range of " + range2 + " miles"); // alternative version without two int defined System.out.println("Minivan can carry " + minivan.passengers + " with range of " + minivan.range() + " miles"); }^ System.out.println("Minivan can carry " + sportscar.passengers + " with range of " + sportscar.range() + " miles"); }