0% found this document useful (0 votes)
33 views

Nội dung: - Truyền tham số - Đa năng hóa phương thức - Phương thức khởi tạo - Kế thừa. Các ví dụ. Bài 1 Truyền tham số

Copyright
© Attribution Non-Commercial (BY-NC)
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)
33 views

Nội dung: - Truyền tham số - Đa năng hóa phương thức - Phương thức khởi tạo - Kế thừa. Các ví dụ. Bài 1 Truyền tham số

Copyright
© Attribution Non-Commercial (BY-NC)
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/ 9

Generated by Foxit PDF Creator © Foxit Software

http://www.foxitsoftware.com For evaluation only.

BÀI THỰC HÀNH SỐ 3.

Nội dung:

- Truyền tham số
- Đa năng hóa phương thức
- Phương thức khởi tạo
- Kế thừa.

Các ví dụ.

Bài 1 Truyền tham số.

public class TestPassByValue

// Main method

public static void main(String[] args)

int times=3;

System.out.println(“Before the call, variable time is:” + times);

nPrintln(“Welcome to Java!”,times);

System.out.println(“After the call, variable time is:” +times);

//in n lan message

static void nPrintln(String message, int n)

while(n>0)

System.out.println(“n=”+n);

System.out.println(message);

n--;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

Bài 2 Đa năng hóa phương thức

public class TestMethodOverloading

public static void main(String[] args)

System.out.println(“The maximum between 3 and 4 is:” + max(3,4));

System.out.println(‘The maximum between 3.0 and 5.4 is:”+ max(3.0,5.4));

// Tim max cua 2 so double

static double max(double num1,double num2)

if(num1>num2)

return num1;

else

return num2;

static int max(int num1, int num2)

if(num1>num2)

return num1;

else
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

return num2;

Bài 3 Phương thức khởi tạo


import java.io.*;

import java.util.*;

public class PhanSo implements Comparable

private int tu,mau;

public PhanSo() {

tu=0; mau=1;

public PhanSo(int t, int m) throws Exception{

if(m==0) throw new Exception("mau so bang 0!");

tu=t; mau=m;

public PhanSo(int n) {

tu=n;mau=1;

public PhanSo cong(PhanSo x) {

int t=this.tu*x.mau+this.mau*x.tu;

int m=this.mau*x.mau;

try{

return new PhanSo(t,m);

}catch(Exception e){

return null;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

public String toString() {

return (tu+"/"+mau);

public int getTu() {

return tu;

public double giaTri() {

return tu*1.0/mau;

public int getMau() {

return mau;

public int compareTo(Object o){

PhanSo p=(PhanSo)o;

if(this.giaTri()<p.giaTri()) return 1;

else if(this.giaTri()>p.giaTri()) return -1;

else return 0;

public static void main(String[] args)

try{

PhanSo p=new PhanSo(2,7);

PhanSo q=new PhanSo(2,5);


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

System.out.println(p.cong(q));

}catch(Exception e){

System.out.println(e.getMessage());

}finally{

System.out.println("Xong.");

};

Bài 4 Kế thừa

a. Xây dựng lớp Rectangle kế thừa lớp Square


Chương trình chính sử dụng lớp Square.

class Point {

private double x, y;

Point (double x, double y) {

this.x = x;

this.y = y;

double getX () {

return x;

double getY () {

return y;

}
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

class Square {

private double width;

Square (double width) {

this.width = width;

double getWidth () {

return width;

class Rectangle extends Square {

private double length;

Rectangle (double width, double length) {

// Truyền giá trị tham số width parameter vào lớp Square để khởi tạo đối tượng

super (width);

this.length = length;

double getLength () {

return length;

class Shapes {

public static void main (String [] args) {

Square s = new Square (100);

System.out.println ("s.width = " + s.getWidth ());

Rectangle r = new Rectangle (50, 25);

System.out.println ("r.width = " + r.getWidth ());


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

System.out.println ("r.length = " + r.getLength ());

b. Minh họa tính kế thừa của Java


class Box {

int width, height, depth;

Box () {

width = 0;

height = 0;

depth = 0;

Box (int width, int height, int depth) {

this.width = width;

this.height = height;

this.depth = depth;

public int volumeBox() {

return width * height * depth;

}//end of class

class SubBox extends Box {

//SubBox ke thua cac dac tinh cua Box va co them weight

//SubBox khong can phai tao lai cac dac diem da co trong Box

//Tinh ke thua cho phep co the tao cac lop con rieng biet tu lop Box

int weight;

SubBox (int width, int height, int depth, int weight) {


Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

/* Cach 1

this.width = width;

this.height = height;

this.depth = depth;

this.weight = weight;

*/

/* Cach 2 */

super(width, height, depth);

this.weight = weight;

public int volumeBox() {

return width * height * depth;

}//end of class

class JavaExample03 {

public static void main (String args[]) {

SubBox obj1 = new SubBox(2,3,4,5);

System.out.println(">> The tich 1 = " + obj1.volumeBox());

System.out.println(">> Trong luong = " + obj1.weight);

BÀI TẬP

1. Viết chương trình hoán vị 2 số

2. Viết chương trình cộng, trừ 2 thời gian(giờ, phút, giây), sử dụng các phương thức khởi tạo.

3. Viết Chương trình tạo ra lớp Nhân viên gồm

- Họ tên
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.

- Ngày sinh

- Lương

- Lương căn bản

Lớp NV Văn Phòng, Nv Sản xuất kế thừa từ lớp Nhân viên

Xuất ra danh sách Nhân viên kèm theo lương.

Biết rằng lương của NV Văn phòng bằng

Luong=Lgcanban+(songaylam*100000)+trcap;

Lương của NV sản xuất bằng

Luong=Lgcanban+(sosanpham*200000);

You might also like