Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes One class usually represents one type of object

Similar presentations


Presentation on theme: "Classes One class usually represents one type of object"— Presentation transcript:

1 Classes One class usually represents one type of object
May contain its own member variables May contain its own methods to operate on the member variables Usually one class is defined in one Java file In the entire program exactly one class should contain a main method

2 Vehicle class /* A program that uses the Vehicle class.
Call this file VehicleDemo.java */ class Vehicle { int passengers; // number of passengers int fuelCapacity; // fuel capacity in litres int kmperlitre; // fuel consumption in kilometer per litre }

3 // This class declares an object of type Vehicle. class VehicleDemo {
Contd. // This class declares an object of type Vehicle. class VehicleDemo { public static void main(String args[]) { Vehicle minivan = new Vehicle(); double range; // assign values to fields in minivan minivan.passengers = 7; minivan.fuelCapacity = 16; minivan.kmperliter = 21; // compute the range assuming a full tank of gas range = minivan.fuelCapacity * minivan.kmperliter; System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range+" kms"); }

4 Contd. // This program creates two Vehicle objects. class Vehicle {
int passengers; // number of passengers double fuelCapacity; // fuel capacity double kmperliter; // fuel consumption } // This class declares an object of type Vehicle. class TwoVehicles { public static void main(String args[]) { Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); double range1, range2; // assign values to fields in minivan minivan.passengers = 7; minivan.fuelCapacity = 16; minivan.kmperliter = 21; // assign values to fields in sportscar sportscar.passengers = 2; sportscar.fuelCapacity = 14; sportscar.kmperliter = 12; // compute the ranges assuming a full tank of gas range1 = minivan.fuelCapacity * minivan.kmperliter; range2 = sportscar.fuelCapacity * sportscar.kmperliter; System.out.println("Minivan can carry " + minivan.passengers + " with a range of " + range1); System.out.println("Sportscar can carry " + sportscar.passengers + " with a range of " + range2);

5 Add method // Add range to Vehicle. class Vehicle {
int passengers; // number of passengers double FuelCapacity; // fuel capacity double kmperliter; // fuel consumption // Display the range. void range() { System.out.println("Range is " + FuelCapacity * kmperliter); } class AddMeth { public static void main(String args[]) { Vehicle minivan = new Vehicle(); Vehicle sportscar = new Vehicle(); // assign values to fields in minivan minivan.passengers = 7; minivan.FuelCapacity = 16; minivan.kmperliter = 21; // assign values to fields in sportscar sportscar.passengers = 2; sportscar.FuelCapacity = 14; sportscar.kmperliter = 12; System.out.print("Minivan can carry " + minivan.passengers + ". "); minivan.range(); // display range of minivan System.out.print("Sportscar can carry " + sportscar.passengers + ". "); sportscar.range(); // display range of sportscar.


Download ppt "Classes One class usually represents one type of object"

Similar presentations


Ads by Google