By GokiSoft.com| 20:59 26/04/2024|
Java Basic

Java basic- Overview - viết chương trình điều khiển TIVI bằng java

1.    Tạo lớp cTivi 

Khai báo trường private

string[] channelList : chứa danh sách các kênh có của Tivi

bool isOn: Trạng thái xác định Tivi đang bật?

string currentChannel: Kênh hiện tại đang bật

Có Phương thức khởi tạo:

-       Không có tham số  thiết lập: isOn=false; khởi tạo mảng channelList có 3 kênh mặc định là “VTV1”,”VTV2”, “VTV3”, currentChannel=”VTV1”

-       Có tham số là chuỗi string[] channelList  thiết lập: gán trường channelList theo tham số cung cấp vào, isOn=false; currentChannel lấy là kênh đầu tiên trong channelList

Phương thức private

-       string seachNextChannel có 1 tham số isForward kiểu bool : Trả về kết quả Nếu isForward là true thì tìm kênh kế tiếp trong channelList (đến cuối thì về kênh đầu tiên). Ngược lại thì tìm kênh trước trong channelList (về đầu thì quay lại kênh cuối),

-       void On() : Thiết lập isOn là true và hiển thị ---Tivi ON---

-       void Off(): thiết lập isOn là false và hiển thị ---Tivi OFF---

-       void Switch() : chuyển trạng thái isOn từ true -> false và ngược lại và hiển thị kết quả tương ứng ---Tivi ON --- hay ---Tivi OFF---

-       void Switch(string channel): Chuyển trạng thái giống Switch trên song nếu Tivi bật thì chọn luôn kênh theo tham số cung cấp vào là channel đồng thời hiển thị thông tin có tìm thấy kênh đó hay không.

-       void nextChannel() : Chỉ khi isOn là true thì thực hiện chuyển kênh (gán currentChannel theo kênh kế tiếp) đồng thời hiển thị tên kênh mới

-       void previousChannel() : chỉ khi isOn là true thì thực hiện chuyển kênh (gán currentChannel theo kênh trước đó ) đồng thời hiển thị tên kênh mới

-       void Show() : Khi tivi bật hiển thị trạng thái ---Tivi is On at channel : ### ---, khi tivi tắt thì hiển thị trạng thái ---- Tivi now OFF! ----

2.    Trong phương thức Main của lớp Program hãy viết chương trình test lớp trên:

-       Cho phép người dùng chọn 1 trong 2 cách khởi tạo tivi: Mặc định (chọn 1), Tự chọn kênh (chọn 2).

-       Căn cứ trên từng cách  chọn hãy gọi phương thức khởi tạo tương ứng (Chú ý: cách 2 thì cần cho người dùng nhập vào số kênh, và tên lần lượt từng kênh)

-       Tạo menu chức năng:

1.    Chuyen trang thai (switch)

2.    Chuyen trang thai kem theo kenh

3.    Bat tivi

4.    Tat tivi

5.    Chuyen kenh tiep

6.    Chuyen kenh truoc

7.    Xem thong tin Tivi

8.    Ket thuc

Từng chức năng sẽ gọi phương thức của đối tượng cTivi trên, chú ý với chức năng 2: cần cho người dùng nhập vào tên kênh cụ thể trước làm tham số để gọi phương thức Switch(string channel)

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Hoang Ngo [T1907A]
Hoang Ngo

2020-03-16 06:55:55



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        CTivi tivi;
        Scanner input = new Scanner(System.in);
        int choose;
        
        System.out.println("Chon cach khoi tao kenh");
        System.out.println("1. Mac dinh");
        System.out.println("2. Tu chon kenh");
        System.out.println("Choose: ");
        choose = Integer.parseInt(input.nextLine());
        
        switch(choose) {
            case 1:
                tivi = new CTivi();
                break;
            default:
                System.out.println("Nhap so kenh can them : ");
                int n = Integer.parseInt(input.nextLine());
                String[] channelList = new String[n];
                
                for (int i = 0; i < n; i++) {
                    System.out.format("\nNhap kenh (%d) : ", i);
                    channelList[i] = input.nextLine();
                }
                
                tivi = new CTivi(channelList);
                break;
        }
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    tivi.Switch();
                    break;
                case 2:
                    System.out.println("Nhap kenh can tim : ");
                    String searchChannel = input.nextLine();
                    
                    tivi.Switch(searchChannel);
                    break;
                case 3:
                    tivi.On();
                    break;
                case 4:
                    tivi.Off();
                    break;
                case 5:
                    tivi.nextChannel();
                    break;
                case 6:
                    tivi.previousChannel();
                    break;
                case 7:
                    tivi.Show();
                    break;
                case 8:
                    System.out.println("Exit Program!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 8);
    }
    
    public static void showMenu() {
        System.out.println("1.    Chuyen trang thai (switch)");
        System.out.println("2.    Chuyen trang thai kem theo kenh");
        System.out.println("3.    Bat tivi");
        System.out.println("4.    Tat tivi");
        System.out.println("5.    Chuyen kenh tiep");
        System.out.println("6.    Chuyen kenh truoc");
        System.out.println("7.    Xem thong tin Tivi");
        System.out.println("8.    Ket thuc");
        System.out.println("Choose: ");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

/**
 *
 * @author Diep.Tran
 */
public class CTivi {
    private String[] channelList;
    private boolean isOn;
    private String currentChannel;
    private int currentIndex = 0;

    public CTivi() {
        isOn = false;
        channelList = new String[]{"VTV1", "VTV2", "VTV3"};
        currentChannel = "VTV1";
        currentIndex = 0;
    }

    public CTivi(String[] channelList) {
        this.channelList = channelList;
        isOn = false;
        if(channelList.length > 0) {
            currentChannel = channelList[0];
            currentIndex = 0;
        }
    }
    
    private String searchNextChannel(boolean isForward) {
        if(channelList.length == 0) {
            return null;
        }
        
        if(isForward) {
            currentIndex++;
            if(currentIndex >= channelList.length) {
                currentIndex = 0;
            }
        } else {
            currentIndex--;
            if(currentIndex < 0) {
                currentIndex = channelList.length - 1;
            }
        }
        
        currentChannel = channelList[currentIndex];
        return currentChannel;
    }
    
    public void On() {
        isOn = true;
        System.out.println("--Tivi On--");
    }
    
    public void Off() {
        isOn = false;
        System.out.println("--Tivi Off--");
    }
    
    public void Switch() {
        if(isOn) {
            Off();
        } else {
            On();
        }
    }
    
    public void Switch(String channel) {
        Switch();
        if(isOn) {
            currentIndex = -1;
            for (int i = 0; i < channelList.length; i++) {
                if(channelList[i].equalsIgnoreCase(channel)) {
                    currentChannel = channelList[i];
                    currentIndex = i;
                    break;
                }
            }
            
            if(currentIndex == -1) {
                System.out.println("Khong tim thay kenh : " + channel);
            } else {
                System.out.println("Da tim thay kenh : " + channel);
            }
        }
    }
    
    public void nextChannel() {
        if(isOn) {
            searchNextChannel(true);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void previousChannel() {
        if(isOn) {
            searchNextChannel(false);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void Show() {
        if(isOn) {
            System.out.println("---Tivi is On at channel : " + currentChannel);
        } else {
            System.out.println("---- Tivi now OFF! ----");
        }
    }
}



Hoang Ngo [T1907A]
Hoang Ngo

2020-03-16 06:55:22



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

/**
 *
 * @author Diep.Tran
 */
public class CTivi {
    private String[] channelList;
    private boolean isOn;
    private String currentChannel;
    private int currentIndex = 0;

    public CTivi() {
        isOn = false;
        channelList = new String[]{"VTV1", "VTV2", "VTV3"};
        currentChannel = "VTV1";
        currentIndex = 0;
    }

    public CTivi(String[] channelList) {
        this.channelList = channelList;
        isOn = false;
        if(channelList.length > 0) {
            currentChannel = channelList[0];
            currentIndex = 0;
        }
    }
    
    private String searchNextChannel(boolean isForward) {
        if(channelList.length == 0) {
            return null;
        }
        
        if(isForward) {
            currentIndex++;
            if(currentIndex >= channelList.length) {
                currentIndex = 0;
            }
        } else {
            currentIndex--;
            if(currentIndex < 0) {
                currentIndex = channelList.length - 1;
            }
        }
        
        currentChannel = channelList[currentIndex];
        return currentChannel;
    }
    
    public void On() {
        isOn = true;
        System.out.println("--Tivi On--");
    }
    
    public void Off() {
        isOn = false;
        System.out.println("--Tivi Off--");
    }
    
    public void Switch() {
        if(isOn) {
            Off();
        } else {
            On();
        }
    }
    
    public void Switch(String channel) {
        Switch();
        if(isOn) {
            currentIndex = -1;
            for (int i = 0; i < channelList.length; i++) {
                if(channelList[i].equalsIgnoreCase(channel)) {
                    currentChannel = channelList[i];
                    currentIndex = i;
                    break;
                }
            }
            
            if(currentIndex == -1) {
                System.out.println("Khong tim thay kenh : " + channel);
            } else {
                System.out.println("Da tim thay kenh : " + channel);
            }
        }
    }
    
    public void nextChannel() {
        if(isOn) {
            searchNextChannel(true);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void previousChannel() {
        if(isOn) {
            searchNextChannel(false);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void Show() {
        if(isOn) {
            System.out.println("---Tivi is On at channel : " + currentChannel);
        } else {
            System.out.println("---- Tivi now OFF! ----");
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        CTivi tivi;
        Scanner input = new Scanner(System.in);
        int choose;
        
        System.out.println("Chon cach khoi tao kenh");
        System.out.println("1. Mac dinh");
        System.out.println("2. Tu chon kenh");
        System.out.println("Choose: ");
        choose = Integer.parseInt(input.nextLine());
        
        switch(choose) {
            case 1:
                tivi = new CTivi();
                break;
            default:
                System.out.println("Nhap so kenh can them : ");
                int n = Integer.parseInt(input.nextLine());
                String[] channelList = new String[n];
                
                for (int i = 0; i < n; i++) {
                    System.out.format("\nNhap kenh (%d) : ", i);
                    channelList[i] = input.nextLine();
                }
                
                tivi = new CTivi(channelList);
                break;
        }
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    tivi.Switch();
                    break;
                case 2:
                    System.out.println("Nhap kenh can tim : ");
                    String searchChannel = input.nextLine();
                    
                    tivi.Switch(searchChannel);
                    break;
                case 3:
                    tivi.On();
                    break;
                case 4:
                    tivi.Off();
                    break;
                case 5:
                    tivi.nextChannel();
                    break;
                case 6:
                    tivi.previousChannel();
                    break;
                case 7:
                    tivi.Show();
                    break;
                case 8:
                    System.out.println("Exit Program!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 8);
    }
    
    public static void showMenu() {
        System.out.println("1.    Chuyen trang thai (switch)");
        System.out.println("2.    Chuyen trang thai kem theo kenh");
        System.out.println("3.    Bat tivi");
        System.out.println("4.    Tat tivi");
        System.out.println("5.    Chuyen kenh tiep");
        System.out.println("6.    Chuyen kenh truoc");
        System.out.println("7.    Xem thong tin Tivi");
        System.out.println("8.    Ket thuc");
        System.out.println("Choose: ");
    }
}




Hoang Ngo [T1907A]
Hoang Ngo

2020-03-16 06:55:22



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

/**
 *
 * @author Diep.Tran
 */
public class CTivi {
    private String[] channelList;
    private boolean isOn;
    private String currentChannel;
    private int currentIndex = 0;

    public CTivi() {
        isOn = false;
        channelList = new String[]{"VTV1", "VTV2", "VTV3"};
        currentChannel = "VTV1";
        currentIndex = 0;
    }

    public CTivi(String[] channelList) {
        this.channelList = channelList;
        isOn = false;
        if(channelList.length > 0) {
            currentChannel = channelList[0];
            currentIndex = 0;
        }
    }
    
    private String searchNextChannel(boolean isForward) {
        if(channelList.length == 0) {
            return null;
        }
        
        if(isForward) {
            currentIndex++;
            if(currentIndex >= channelList.length) {
                currentIndex = 0;
            }
        } else {
            currentIndex--;
            if(currentIndex < 0) {
                currentIndex = channelList.length - 1;
            }
        }
        
        currentChannel = channelList[currentIndex];
        return currentChannel;
    }
    
    public void On() {
        isOn = true;
        System.out.println("--Tivi On--");
    }
    
    public void Off() {
        isOn = false;
        System.out.println("--Tivi Off--");
    }
    
    public void Switch() {
        if(isOn) {
            Off();
        } else {
            On();
        }
    }
    
    public void Switch(String channel) {
        Switch();
        if(isOn) {
            currentIndex = -1;
            for (int i = 0; i < channelList.length; i++) {
                if(channelList[i].equalsIgnoreCase(channel)) {
                    currentChannel = channelList[i];
                    currentIndex = i;
                    break;
                }
            }
            
            if(currentIndex == -1) {
                System.out.println("Khong tim thay kenh : " + channel);
            } else {
                System.out.println("Da tim thay kenh : " + channel);
            }
        }
    }
    
    public void nextChannel() {
        if(isOn) {
            searchNextChannel(true);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void previousChannel() {
        if(isOn) {
            searchNextChannel(false);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void Show() {
        if(isOn) {
            System.out.println("---Tivi is On at channel : " + currentChannel);
        } else {
            System.out.println("---- Tivi now OFF! ----");
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        CTivi tivi;
        Scanner input = new Scanner(System.in);
        int choose;
        
        System.out.println("Chon cach khoi tao kenh");
        System.out.println("1. Mac dinh");
        System.out.println("2. Tu chon kenh");
        System.out.println("Choose: ");
        choose = Integer.parseInt(input.nextLine());
        
        switch(choose) {
            case 1:
                tivi = new CTivi();
                break;
            default:
                System.out.println("Nhap so kenh can them : ");
                int n = Integer.parseInt(input.nextLine());
                String[] channelList = new String[n];
                
                for (int i = 0; i < n; i++) {
                    System.out.format("\nNhap kenh (%d) : ", i);
                    channelList[i] = input.nextLine();
                }
                
                tivi = new CTivi(channelList);
                break;
        }
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    tivi.Switch();
                    break;
                case 2:
                    System.out.println("Nhap kenh can tim : ");
                    String searchChannel = input.nextLine();
                    
                    tivi.Switch(searchChannel);
                    break;
                case 3:
                    tivi.On();
                    break;
                case 4:
                    tivi.Off();
                    break;
                case 5:
                    tivi.nextChannel();
                    break;
                case 6:
                    tivi.previousChannel();
                    break;
                case 7:
                    tivi.Show();
                    break;
                case 8:
                    System.out.println("Exit Program!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 8);
    }
    
    public static void showMenu() {
        System.out.println("1.    Chuyen trang thai (switch)");
        System.out.println("2.    Chuyen trang thai kem theo kenh");
        System.out.println("3.    Bat tivi");
        System.out.println("4.    Tat tivi");
        System.out.println("5.    Chuyen kenh tiep");
        System.out.println("6.    Chuyen kenh truoc");
        System.out.println("7.    Xem thong tin Tivi");
        System.out.println("8.    Ket thuc");
        System.out.println("Choose: ");
    }
}




Trần Văn Điệp [Teacher]
Trần Văn Điệp

2020-03-13 07:31:43

Video hướng dẫn chữa bài tập


https://gokisoft.com/bt07-tao-chuong-trinh-tivi-bang-java-lap-trinh-java.html


Trần Văn Điệp [Teacher]
Trần Văn Điệp

2020-03-13 07:30:46



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

/**
 *
 * @author Diep.Tran
 */
public class CTivi {
    private String[] channelList;
    private boolean isOn;
    private String currentChannel;
    private int currentIndex = 0;

    public CTivi() {
        isOn = false;
        channelList = new String[]{"VTV1", "VTV2", "VTV3"};
        currentChannel = "VTV1";
        currentIndex = 0;
    }

    public CTivi(String[] channelList) {
        this.channelList = channelList;
        isOn = false;
        if(channelList.length > 0) {
            currentChannel = channelList[0];
            currentIndex = 0;
        }
    }
    
    private String searchNextChannel(boolean isForward) {
        if(channelList.length == 0) {
            return null;
        }
        
        if(isForward) {
            currentIndex++;
            if(currentIndex >= channelList.length) {
                currentIndex = 0;
            }
        } else {
            currentIndex--;
            if(currentIndex < 0) {
                currentIndex = channelList.length - 1;
            }
        }
        
        currentChannel = channelList[currentIndex];
        return currentChannel;
    }
    
    public void On() {
        isOn = true;
        System.out.println("--Tivi On--");
    }
    
    public void Off() {
        isOn = false;
        System.out.println("--Tivi Off--");
    }
    
    public void Switch() {
        if(isOn) {
            Off();
        } else {
            On();
        }
    }
    
    public void Switch(String channel) {
        Switch();
        if(isOn) {
            currentIndex = -1;
            for (int i = 0; i < channelList.length; i++) {
                if(channelList[i].equalsIgnoreCase(channel)) {
                    currentChannel = channelList[i];
                    currentIndex = i;
                    break;
                }
            }
            
            if(currentIndex == -1) {
                System.out.println("Khong tim thay kenh : " + channel);
            } else {
                System.out.println("Da tim thay kenh : " + channel);
            }
        }
    }
    
    public void nextChannel() {
        if(isOn) {
            searchNextChannel(true);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void previousChannel() {
        if(isOn) {
            searchNextChannel(false);
            System.out.println("Current Channel : " + currentChannel);
        }
    }
    
    public void Show() {
        if(isOn) {
            System.out.println("---Tivi is On at channel : " + currentChannel);
        } else {
            System.out.println("---- Tivi now OFF! ----");
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession9;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        CTivi tivi;
        Scanner input = new Scanner(System.in);
        int choose;
        
        System.out.println("Chon cach khoi tao kenh");
        System.out.println("1. Mac dinh");
        System.out.println("2. Tu chon kenh");
        System.out.println("Choose: ");
        choose = Integer.parseInt(input.nextLine());
        
        switch(choose) {
            case 1:
                tivi = new CTivi();
                break;
            default:
                System.out.println("Nhap so kenh can them : ");
                int n = Integer.parseInt(input.nextLine());
                String[] channelList = new String[n];
                
                for (int i = 0; i < n; i++) {
                    System.out.format("\nNhap kenh (%d) : ", i);
                    channelList[i] = input.nextLine();
                }
                
                tivi = new CTivi(channelList);
                break;
        }
        
        do {
            showMenu();
            choose = Integer.parseInt(input.nextLine());
            
            switch(choose) {
                case 1:
                    tivi.Switch();
                    break;
                case 2:
                    System.out.println("Nhap kenh can tim : ");
                    String searchChannel = input.nextLine();
                    
                    tivi.Switch(searchChannel);
                    break;
                case 3:
                    tivi.On();
                    break;
                case 4:
                    tivi.Off();
                    break;
                case 5:
                    tivi.nextChannel();
                    break;
                case 6:
                    tivi.previousChannel();
                    break;
                case 7:
                    tivi.Show();
                    break;
                case 8:
                    System.out.println("Exit Program!!!");
                    break;
                default:
                    System.out.println("Nhap sai!!!");
                    break;
            }
        } while(choose != 8);
    }
    
    public static void showMenu() {
        System.out.println("1.    Chuyen trang thai (switch)");
        System.out.println("2.    Chuyen trang thai kem theo kenh");
        System.out.println("3.    Bat tivi");
        System.out.println("4.    Tat tivi");
        System.out.println("5.    Chuyen kenh tiep");
        System.out.println("6.    Chuyen kenh truoc");
        System.out.println("7.    Xem thong tin Tivi");
        System.out.println("8.    Ket thuc");
        System.out.println("Choose: ");
    }
}



Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó