JAVA For Beginners: Using The Vehicle Class
JAVA For Beginners: Using The Vehicle Class
class VehicleDemo {
int range;
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
Till now we have created an instance of Vehicle called ‘minivan’ and assigned values to passengers,
fuel capacity and fuel consumption. Let us add some statements to work out the distance that this
vehicle can travel with a tank full of fuel:
class TwoVehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
Riccardo Flask 73 | P a g e
JAVA for Beginners
Creating Objects
In the previous code, an object was created from a class. Hence ‘minivan’ was an object which was
created at run time from the ‘Vehicle’ class – vehicle minivan = new Vehicle( ) ; This statement
allocates a space in memory for the object and it also creates a reference. We can create a
reference first and then create an object:
We have created a new instance of type Vehicle named car1. However note that car2 is NOT
another instance of type Vehicle. car2 is the same object as car1 and has been assigned the same
properties,
System.out.println(car1.mpg);
System.out.println(car2.mpg);
Riccardo Flask 74 | P a g e
JAVA for Beginners
car1 and car2 are not linked. car2 can be re-assigned to another data type:
car2 = car3; // now car2 and car3 refer to the same object.
Methods
Methods are the functions which a particular class possesses. These functions usually use the data
defined by the class itself.
class Vehicle {
void range() {
Note that ‘fuelcap’ and ‘mpg’ are called directly without the dot (.) operator. Methods take the
following general form:
// body of method
‘ret-type’ specifies the type of data returned by the method. If it does not return any value we write
void. ‘name’ is the method name while the ‘parameter-list’ would be the values assigned to the
variables of a particular method (empty if no arguments are passed).
class AddMeth {
Riccardo Flask 75 | P a g e
JAVA for Beginners
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
}
}
void myMeth() {
int i;
System.out.println();
}
}
Riccardo Flask 76 | P a g e
JAVA for Beginners
Hence the method will exit when it encounters the return statement or the closing curly bracket
There can be more than one exit point in a method depending on particular conditions, but one
must pay attention as too many exit points could render the code unstructured and the program will
not function as desired (plan well your work).
void myMeth() {
// ...
if(done) return;
// ...
if(error) return;
Returning a Value
Most methods return a value. You should be familiar with the sqrt( ) method which returns the
square root of a number. Let us modify our range method to make it return a value:
class Vehicle {
int range() {
Please note that now our method is no longer void but has int since it returns a value of type
integer. It is important to know what type of variable a method is expected to return in order to set
the parameter type correctly.
Riccardo Flask 77 | P a g e
JAVA for Beginners
Main program:
class RetMeth {
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
range1 = minivan.range();
range2 = sportscar.range();
Study the last two statements, can you think of a way to make them more efficient, eliminating the
use of the two statements located just above them?
Riccardo Flask 78 | P a g e
JAVA for Beginners
// Using Parameters.
The method accepts a
class ChkNum { value of type integer, the
parameter is x, while the
// return true if x is even argument would be any
value passed to it
boolean isEven(int x) {
class ParmDemo {
Predicted Output:
10 is even.
8 is even.
A method can accept more than one parameter. The method would be declared as follows:
// ...
Riccardo Flask 79 | P a g e
JAVA for Beginners
class Factor {
class IsFact {
Predicted Output:
2 is a factor.
If we refer back to our ‘vehicle’ example, we can now add a method which works out the fuel
needed by a particular vehicle to cover a particular distance. One has to note here that the result of
this method, even if it takes integers as parameters, might not be a whole number.
Therefore one has to specify that the value that the method returns, in this case we can use
‘double’:
Riccardo Flask 80 | P a g e
JAVA for Beginners
class Vehicle {
int range() {
Main Program:
class CompFuel {
double gallons;
minivan.passengers = 7;
Riccardo Flask 81 | P a g e
JAVA for Beginners
minivan.fuelcap = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
gallons = minivan.fuelneeded(dist);
Predicted Output:
Riccardo Flask 82 | P a g e
JAVA for Beginners
In order to carry out this task we must examine the Help3.java and identifies ways how we can break
down the code into classes and methods. The code can be broken down as follows:
Method helpon( )
void helpon(int what) {
switch(what) {
case '1':
System.out.println("The if:\n");
System.out.println("if(condition) statement;");
System.out.println("else statement;");
break;
case '2':
System.out.println("The switch:\n");
System.out.println("switch(expression) {");
System.out.println(" break;");
System.out.println(" // ...");
System.out.println("}");
break;
case '3':
System.out.println("The for:\n");
System.out.println(" statement;");
break;
Riccardo Flask 83 | P a g e
JAVA for Beginners
case '4':
System.out.println("The while:\n");
System.out.println("while(condition) statement;");
break;
case '5':
System.out.println("The do-while:\n");
System.out.println("do {");
System.out.println(" statement;");
break;
case '6':
System.out.println("The break:\n");
break;
case '7':
System.out.println("The continue:\n");
break;
System.out.println();
Method showmenu( )
void showmenu() {
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. for");
Riccardo Flask 84 | P a g e