Lecture 10: IntroductionToClassesII

Download Report

Transcript Lecture 10: IntroductionToClassesII

1
Introduction to Classes

Default Constructor

this Keyword

Car Example

Exercises
2
Default Constructor


Question: What happens if a constructor is not defined in a class?
Answer: A default constructor is created automatically.
• A default constructor:
1. Has no parameter
2. Initializes instance variables with
default values
3. Is not provided if a constructor is
already defined
class Student{
private int id;
private String name;
private double gpa;
// no constructor defined
public String getName(){
return name;
}
Instance variable of type:
default value
boolean
false
byte
(byte) 0
short
(short) 0
int
0
long
0L
float
0F
double
0D
char
\u0000
object reference
null
}
3
Overloading Constructors
class Student{
 Java allows more than one
Constructor
private int id;
private String name;
 Problem: All constructors
will have the same name!
private double gpa;
public Student(int theID, String theName, double theGPA){
id = theID;
 Solution: Overloading
name = theName;
 Each constructor will
have different parameters
gpa = theGPA;
}
public Student(int theID, String theName){
id = theID;
name = theName;
// gpa is initialized to 0.0
}
// . . .
}
4
this Keyword
class Student{
 Reference to the current
object
private int id;
private String name;
private double gpa;
 Two usage of this keyword:
public Student(int id, String name, double gpa){
1. To refer to the fields and
methods of this object
Purpose: resolve conflict
in naming.
2. From a constructor of a
class
call another constructor of
the
same class.
this.id = id;
this.name = name;
this.gpa = gpa;
}
public Student(int id, String name){
this(id, name, 0.0);
}
// . . .
}
5
Example: Car Class
• Suppose we want to design a class that calculates the fuel consumption of a car
in kilometers per liter.
• This calculation is possible if we know three
things:
1. The initial reading of the odometer
2. The final reading of the odometer
3. The number of liters used.
Knowing these information, the consumption (in km per liters) =
(finalOdometerReading - initialOdometerReading)/litersUsed
6
Car class (cont’d)
class Car{
int initialOdometerReading;
int finalOdometerReading;
double litersUsed;
public Car(int initReading, int finalReading, double liters){
initialOdometerReading = initReading;
finalOdometerReading = finalReading;
litersUsed = liters;
}
public Car(int finalReading, double liters){ //used to create new car objects
finalOdometerReading = finalReading;
litersUsed = liters;
}
public double getKilometersPerLiter(){
return (finalOdometerReading – initialOdometerReading) / litersUsed; // assumes litersUsed is not zero
}
}
7
Car class: Test program
public class TestCar{
public static void main( String[ ] args){
Car car1 = new Car( 32456, 32776, 40.0);
System.out.println(“Fuel consumption for car1 is ” + car1. getKilometersPerLiter( ) + “ km/liter”);
Car car2 = new Car( 365, 30.0);
System.out.println(“Fuel consumption for car2 is ” + car2. getKilometersPerLiter( ) + “ km/liter”);
}
8
Exercises
Add a boolean method called isEconomyCar to the Car class. This method will
return true if the fuel consumption is less that 5 kilometers per liter.
Add a boolean method called isFuelGuttler to the Car class.
This method will return true if the fuel consumption is more that 15 kilometers
per liter.
Implement a class Product. A product has a name and a price. Supply
methods printProduct( ) , getPrice( ) , and setPrice().
Write a test program that makes two products,
prints them, reduces their prices by 5 Riyals, and then prints them again.
Implement a class Circle that has methods getArea ( ) and
getCircumference (). In the constructor, supply the radius
of the circle.
9