0% found this document useful (0 votes)
41 views5 pages

Classes: - One Class Usually Represents One Type of Object

This document discusses classes in Java and provides examples of using classes. It defines a Vehicle class with fields for passengers, fuel capacity, and fuel consumption. It then shows examples of creating Vehicle objects, assigning values to fields, and calculating ranges. It adds a range method to the Vehicle class to display the range. The key points are: 1) A class represents an object type and can contain fields and methods. 2) Examples create Vehicle objects and assign field values to calculate vehicle ranges. 3) A range method is added to the Vehicle class to display each vehicle's range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

Classes: - One Class Usually Represents One Type of Object

This document discusses classes in Java and provides examples of using classes. It defines a Vehicle class with fields for passengers, fuel capacity, and fuel consumption. It then shows examples of creating Vehicle objects, assigning values to fields, and calculating ranges. It adds a range method to the Vehicle class to display the range. The key points are: 1) A class represents an object type and can contain fields and methods. 2) Examples create Vehicle objects and assign field values to calculate vehicle ranges. 3) A range method is added to the Vehicle class to display each vehicle's range.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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
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
}
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");
}
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);
}
}
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.
}
}

You might also like