03-Classes Objects Methods
03-Classes Objects Methods
JAVA PROGRAMMING
Plan 2
1. Class fundamentals
2. Methods
3. Constructors
4. The new operator revisited
5. Garbage collection
6. The this keyword
Java Programming
1
12/10/2022
Plan 3
1. Class fundamentals
2. Methods
3. Constructors
4. The new operator revisited
5. Garbage collection
6. The this keyword
Java Programming
2
12/10/2022
Java Programming
6
1. public class VehicleDemo {
2. public static void main(String[] args) {
3. Vehicle minivan = new Vehicle();
4. int range;
5. // assign values to fields in minivan
6. minivan.passengers = 7;
7. minivan.fuelcap = 16; minivan.mpg = 21;
8. // compute the range assuming a full tank of gas
9. range = minivan.fuelcap * minivan.mpg;
10. System.out.println("Minivan can carry " +
11.
minivan.passengers +
12.
" with a range of " + range);
13.
}
14.
}
Java Programming
3
12/10/2022
1. ...
2. Vehicle minivan = new Vehicle();
7
3. Vehicle sportscar = new Vehicle();
4. int range1, range2;
5. // assign values to fields in minivan
6. minivan.passengers = 7;
7. minivan.fuelcap = 16; minivan.mpg = 21;
8. // assign values to fields in sportscar
9. sportscar.passengers = 2;
10. sportscar.fuelcap = 14; sportscar.mpg = 12;
11. // compute the range assuming a full tank of gas
12. range1 = minivan.fuelcap * minivan.mpg;
13. range2 = sportscar.fuelcap * sportscar.mpg;
14. System.out.println("Minivan can carry " + minivan.passengers
15. + " with a range of " + range1);
16. System.out.println("Sportscar can carry " + sportscar.passengers
17. + " with a range of " + range2);
Java Programming
Java Programming
4
12/10/2022
Result:
Java Programming
10
1. Vehicle car1 = new Vehicle();
2. Vehicle car2 = car1;
3. Vehicle car3 = new Vehicle();
4. car2 = car3 ;
5. // now car2 and car3 refer to the same object
Java Programming
10
5
12/10/2022
Plan 11
1. Class fundamentals
2. Methods
3. Constructors
4. The new operator revisited
5. Garbage collection
6. The this keyword
Java Programming
11
Method 12
ret-type name(parameter-list) {
// body of method
}
• ret-type specifies the type of data returned by the
method.
• This can be any valid type, including class types that you create.
• If the method does not return a value, its return type must be void.
• The name of the method is specified by name.
• The parameter-list is a sequence of type and identifier pairs
separated by commas.
Java Programming
12
6
12/10/2022
Java Programming
13
14
1. public class AddMethod {
2. public static void main(String[] args) {
3. Vehicle1 minivan = new Vehicle1();
4. Vehicle1 sportscar = new Vehicle1();
5. ...
6. System.out.print("Minivan can carry " +
7. minivan.passengers + ". ");
8. minivan.range();
9. System.out.print("Sportscar can carry " +
10. sportscar.passengers + ". ");
11. sportscar.range();
12. }
13. }
Java Programming
14
7
12/10/2022
Java Programming
15
Returning a value 16
1. // Use a return value.
2. class Vehicle2 {
3. int passengers; // number of passengers
4. int fuelcap; // fuel capacity in gallons
5. int mpg; // fuel consumption in miles per gallon
6. // Display the range
7. int range() {
8. return fuelcap * mpg;
9. }
10. }
Java Programming
16
8
12/10/2022
Java Programming
17
Using parameters 18
Java Programming
18
9
12/10/2022
19
1. class ChkNum{
2. // return true if x is even
3. boolean isEven(int x) {
4. if ((x%2) == 0) return true;
5. else return false;
6. }
7. }
8. public class ParamDemo {
9. public static void main(String[] args) {
10. ChkNum e = new ChkNum();
11. if(e.isEven(10)) System.out.println("10 is even.");
12. if(e.isEven(9)) System.out.println("9 is even.");
13. if(e.isEven(8)) System.out.println("8 is even.");
14. }
15. }
Java Programming
19
20
1. class Factor{
2. boolean isFactor(int a, int b) {
3. if((b%a) == 0) return true;
4. return false;
5. }
6. }
7. public class IsFact {
8. public static void main(String[] args) {
9. Factor x = new Factor();
10. if(x.isFactor(2, 20)) System.out.println(
11. "2 is factor");
12. if(x.isFactor(3, 20)) System.out.println(
13. "This won't be displayed");
14. }
15. }
Java Programming
20
10
12/10/2022
Example: Adding a
Parameterized Method 21
1. class Vehicle3 {
2. int passengers; // number of passengers
3. int fuelcap; // fuel capacity in gallons
4. int mpg; // fuel consumption in miles per gallon
5. // Return the range
6. int getRange() {
7. return mpg * fuelcap;
8. }
9. // Compute fuel needed for a given distance.
10. double getFuelNeeded(int miles) {
11. return (double) miles / mpg;
12. }
13. }
Java Programming
21
22
11
12/10/2022
Java Programming
23
24
1. public class HelpClassDemo {
2. public static void main(String args[])
3. throws java.io.IOException{
4. // Create an instance of Help class
5. /* Invoke all the methods in that instance in order to:
6. * - Display a menu,
7. * - Input the user’s choice, check for a valid
8. * response, and display information about the item
9. * selected.
10. * The program also loops until the letter q is pressed.
11. */
12. }
13. }
Java Programming
24
12
12/10/2022
Plan 25
1. Class fundamentals
2. Methods
3. Constructors
4. The new operator revisited
5. Garbage collection
6. The this keyword
Java Programming
25
Constructor 26
Java Programming
26
13
12/10/2022
Example 27
1. class MyClass{
2. int x;
3. MyClass(){
4. x = 10;
5. }
6. }
7. public class ConsDemo {
8. public static void main(String[] args) {
9. MyClass t1 = new MyClass();
10. MyClass t2 = new MyClass();
11. System.out.println(t1.x + " " + t2.x);
12. }
13. }
Java Programming
27
Parameterized constructors 28
Java Programming
28
14
12/10/2022
Java Programming
29
Java Programming
30
15
12/10/2022
Java Programming
31
Java Programming
32
16
12/10/2022
Java Programming
33
1. class Pwr2{
2.
3.
double base;
int e;
34
4. double val;
5. Pwr2(double base, int exp){
6. this.base = base;
7. this.e = exp;
8. this.val = 1;
9. if(exp == 0) return;
10. for(; exp > 0; exp--) this.val = this.val * base;
11. }
12. double getPwr() {
13. return this.val;
14. }
15. }
Java Programming
34
17
12/10/2022
35
QUESTION ?
Java Programming
35
18