By GokiSoft.com| 15:35 21/06/2023|
Java Basic

Java Basic- OOP - Interface - Hình chữ nhật trong java

Bài tập

------------------ Bài tập kế thừa - interface -------------------------

Câu 1 . Xây dựng một interface có tên là HCNInterface (được đặt trong gói HCNPackage) chứa một số phương thức sau:

- Phương thức dientichHCN() dùng để tính diện tích hình chữ nhật.

- Phương thức getChieuDai() và getChieuRong() dùng để lấy giá trị chiều dài và chiều rộng của của hình chữ nhật.

- Phương thức setDaiRong(cd, cr) dùng để cập nhật giá trị cd, cr cho hai cạnh của hình chữ nhật.

Câu 2. Sử dụng interface HCNInterface trên để xây dựng lớp Hinhchunhat chứa hai thuộc tính là: chieudai, chieurong và các phương thức dientichHCN(), getChieuDai(), getChieuRong(), setDaiRong(cd,cr) đã khai báo trong Interface HCNInterface trên? 

Câu 3. Xây dựng lớp HCNTest chứa phương thức main thực hiện các công việc sau:

a. Khai báo một mảng chứa n hình chữ nhật (với n là số nguyên dương bất kỳ được nhập từ bàn phím). Sau đó nhập chiều dài và chiều rộng cho n hình chữ nhật đó.

b. In ra màn hình thông tin: chiều dài, chiều rộng và diện tích của n hình chữ nhật trên.

c. In ra màn hình thông tin về hình chữ nhật có diện tích lớn nhất.

-----------Ends.

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

5

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

vuong huu phu [T2008A]
vuong huu phu

2021-02-24 12:17:48



/*
 * 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 hcn;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class HCN_test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList<IHCN> hcn = new ArrayList<>();
        int n, t = 0;
        double max = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap so luong hinh chu nhat: ");
        n = scan.nextInt();
        for (int i = 0; i < n; i++) {
            hcn1 hcnt = new hcn1();
            System.out.println("Hinh chu nhat thu: " + i + 1);
            System.out.println("Nhap chieu dai: ");
            hcnt.Chieu_dai = scan.nextDouble();
            System.out.println("nhap chieu rong: ");
            hcnt.Chieu_rong = scan.nextDouble();
            hcn.add(hcnt);
        }
        for (int i = 0; i < hcn.size(); i++) {
            System.out.println("Hinh chu nhat thu: " + i + 1);
            System.out.println("Chieu dai: " + hcn.get(i).get_chieu_dai() + ", Chieu rong: " + hcn.get(i).get_chieu_rong() + ", Dirn tich: " + hcn.get(i).Dien_tich());
        }
        for (int i = t; i < hcn.size(); i++) {
            if (hcn.get(i).Dien_tich() > max) {
                max = hcn.get(i).Dien_tich();
            }
        }
        System.out.println("Hinh thu: " + t + 1 + ", chieu dai: " + hcn.get(t).get_chieu_dai() + ", chieu rong: " + hcn.get(t).get_chieu_rong() + ", Dien tich " + hcn.get(t).Dien_tich());
    }

}



vuong huu phu [T2008A]
vuong huu phu

2021-02-24 12:17:35



/*
 * 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 hcn;

/**
 *
 * @author Admin
 */
public class hcn1 implements IHCN{
    private double Chieu_dai;
    private double Chieu_rong;

    @Override
    public double Dien_tich() {
       return Chieu_dai * Chieu_rong;
    }

    @Override
    public double get_chieu_dai() {
        return Chieu_dai;
    }

    @Override
    public double get_chieu_rong() {
        return Chieu_rong;
    }

    @Override
    public void set_dai_rong(double cd, double cr) {
        Chieu_dai = cd;
        Chieu_rong = cr;      
    }

}



Đỗ Mạc Nam [T2008A]
Đỗ Mạc Nam

2021-02-24 09:31:12

HCNinterface


/*
 * 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 HCNinterface;

/**
 *
 * @author mac
 */
public class HCNinterface {
    
package HCNInterface;
public interface HCNInterface	
{
	public abstract int ChuviHCN();
	public abstract int DientichHCN();			  	
	public abstract int getChieuDai();
	public abstract int getChieuRong();			
	public abstract void setDaiRong(int cd, int cr);
}
}        



Đỗ Mạc Nam [T2008A]
Đỗ Mạc Nam

2021-02-24 09:29:40

HCNtest


/*
 * 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 HCNtest;

/**
 *
 * @author mac
 */
public class  test {
    import java.util.Scanner;
public class HCNtest extends Hinhchunhat						
{	
	/*public int d,r;
	public HCNtest(int d,int r)
	{
		this.d=d;
		this.r=r;
	}*/
	public static void main(String[] args) 
	{
				
	
	   		int i,j,cd,cr;
        	System.out.print("Nhap so luong hinh chu nhat:");
        	Scanner q=new Scanner(System.in);				
        	int n = q.nextInt();
        	//khai bao mang doi tuong khoang 20 phan tu;
        	//HCNtest cn[]=new HCNtest[20];
  			//khai bao doi tuong hcn de lay ket qua tu Hinhchunhat
        	Hinhchunhat[] hcn = new Hinhchunhat[n];
        	for(i=1;i<=n;i++)						
        	{
			System.out.println("Nhap thong tin cho HCN thu "+i);
			System.out.print("Nhap chieu dai:");
			cd= q.nextInt();
			System.out.print("Nhap chieu rong:");
			cr= q.nextInt();
			 hcn[i].setDaiRong(cd,cr);
			//cn[i]=new HCNtest(cd,cr);
        	}
        	
 	    	System.out.println("Stt \t Chieu Dai \t Chieu Rong \t Chu vi \t Dien tich");
 	    	for(i =1;i<=n;i++)
 	    	{
 	    	//hcn[i].setDaiRong(cn[i].d,cn[i].r);
			System.out.println(i+"  \t\t " +hcn[i].getChieuDai()+"   \t\t "+hcn[i].getChieuRong()+"   \t\t\t "+hcn[i].ChuviHCN()+"     \t\t "+hcn[i].DientichHCN());
 	    	}
 	    	
 	    	int Max=hcn[1].DientichHCN();	
        	for(i=1;i<=n;i++)
        	{
        		if(hcn[i].DientichHCN()>Max)	
        		Max=hcn[i].DientichHCN();
        	}
        System.out.println("Hinh chu nhat co dien tich lon nhat la: "+Max);
        									
	}
}
    
}



Nguyễn Tiến Đạt [T2008A]
Nguyễn Tiến Đạt

2021-02-24 09:29:31



/*
 * 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 lesson6.HCNPackage;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author MyPC
 */
public class HCNTest extends Hinhchunhat{
    public static void main(String[] args) {
        ArrayList<HCNInterface> hcnList = new ArrayList<>();
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N mang hinh chu nhat:");
        int n = scan.nextInt();
        for (int i = 0; i < n; i++) {
            Hinhchunhat hcn = new Hinhchunhat();
            System.out.println("Nhap chieu dai hinh chu nhat thu " + (i+1) +" :");
            hcn.chieudai = scan.nextFloat();
            System.out.println("Nhap chieu rong hinh chu nhat thu " + (i+1) +" :");
            hcn.chieurong = scan.nextFloat();
            hcnList.add(hcn);
        }
        int k = 0;
        float max = 0;
        for (int i = 0; i < n; i++) {
            System.out.println("Chieu dai hinh chu nhat thu " + (i+1) + " :" + hcnList.get(i).getChieuDai() );
            
            System.out.println("Chieu rong hinh chu nhat thu " + (i+1) + " :" + hcnList.get(i).getChieuRong() );
            
            System.out.println("Dien tich hinh chu nhat thu " + (i+1) + " :" + hcnList.get(i).dientichHCN() );
            
            if( i == 0 ){
                max = hcnList.get(i).dientichHCN();
                k = i;
                continue;
            }
            if( hcnList.get(i).dientichHCN() > max ){
                max = hcnList.get(i).dientichHCN();
                k = i;
            }
        }
        System.out.println("Thong tin ve hinh chu nhat co dien tich lon nhat la:");
        System.out.println("Chieu dai: " + hcnList.get(k).getChieuDai());
        System.out.println("Chieu rong: " + hcnList.get(k).getChieuRong());
        System.out.println("Dien tich: " + hcnList.get(k).dientichHCN());
    }
}



/*
 * 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 lesson6.HCNPackage;

/**
 *
 * @author MyPC
 */
public class Hinhchunhat implements HCNInterface{
    float chieudai , chieurong;

    public Hinhchunhat() {
    }

    public Hinhchunhat(float chieudai, float chieurong) {
        this.chieudai = chieudai;
        this.chieurong = chieurong;
    }
    
    

    @Override
    public float dientichHCN() {
        return chieudai*chieurong;
    }

    @Override
    public float getChieuDai() {
        return chieudai;
    }

    @Override
    public float getChieuRong() {
        return chieurong;
    }

    @Override
    public void setDaiRong(float cd, float cr) {
        chieudai = cd;
        chieurong = cr;
    }

    
}



/*
 * 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 lesson6.HCNPackage;

/**
 *
 * @author MyPC
 */
public interface HCNInterface {
    float dientichHCN();
    float getChieuDai();
    float getChieuRong();
    void setDaiRong( float cd, float cr);
}



Đỗ Mạc Nam [T2008A]
Đỗ Mạc Nam

2021-02-24 09:28:39

Hinhchunhat


/*
 * 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 Hinhchunhat;

/**
 *
 * @author mac
 */
public class Hinhchunhat {
    

import HCNPackage.HCNInterface;						
import java.util.Scanner;
public class Hinhchunhat implements HCNInterface			
{
	private int chieudai;
	private int chieurong;
	public int ChuviHCN()
	{
		return (chieudai + chieurong) /2;
	}
	public int DientichHCN()						
	{
		return chieudai*chieurong;
	}
	public int getChieuDai(	)						
	{
		return chieudai;							
	}
	public int getChieuRong()					
	{
		return chieurong;							
	}
	public void setDaiRong(int cd, int cr)			
	{
		this.chieudai=cd;
        this.chieurong=cr;							
	}
}
}



Nguyên Phấn Đông [T2008A]
Nguyên Phấn Đông

2021-02-24 09:11:00



/*
 * 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 HCNPackage;

import java.util.Scanner;

/**
 *
 * @author dong
 */
public class HCNtest extends Hinhchunhat{
     public static void main(String[] args) {
        System.out.print("Nhap so luong hinh chu nhat:");
        Scanner Scan=new Scanner(System.in);
        int n = Scan.nextInt();
        int i,j;
        Hinhchunhat[] HCN;
        HCN = new Hinhchunhat[n];
        int cd,cr;
        for(i=1;i<=n;i++)						
        	{
			System.out.println("Nhap thong tin cho HCN thu "+i);
			System.out.print("Nhap chieu dai:");
			cd= Scan.nextInt();
			System.out.print("Nhap chieu rong:");
			cr= Scan.nextInt();
			 HCN[i].setDaiRong(cd,cr);
        	}
        System.out.println("Stt \t Chieu Dai \t Chieu Rong  \t Dien tich");
 	    	for(i =1;i<=n;i++)
 	    	{
 	    	//hcn[i].setDaiRong(cn[i
			System.out.println(i+"  \t\t " +HCN[i].getChieuDai()+"   \t\t "+HCN[i].getChieuRong()+"     \t\t "+HCN[i].DientichHCN());
 	    	}
 	    	
 	    	int Max=HCN[1].DientichHCN();	
        	for(i=1;i<=n;i++)
        	{
        		if(HCN[i].DientichHCN()>Max)	
        		Max=HCN[i].DientichHCN();
        	}
        System.out.println("HCN co dien tich lon nhat la: "+Max);
    }
}



/*
 * 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 HCNPackage;

/**
 *
 * @author dong
 */
public class Hinhchunhat implements HCNInterface{
    private int chieudai;
    private int chieurong;
    @Override
    public int DientichHCN() {
        return chieudai*chieurong;
    }

    @Override
    public int getChieuDai() {
       return chieudai;
    }

    @Override
    public int getChieuRong() {
        return chieurong;
    }

    @Override
    public void setDaiRong(int cd, int cr) {
       this.chieudai=cd;
       this.chieurong=cr;	
    }
    
}



/*
 * 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 HCNPackage;

/**
 *
 * @author dong
 */
public interface HCNInterface {
    public abstract int DientichHCN();			  	
    public abstract int getChieuDai();
    public abstract int getChieuRong();			
    public abstract void setDaiRong(int cd, int cr);
}



Thành Lâm [T1907A]
Thành Lâm

2020-04-27 14:23:04



/*
 * 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 OOP;

/**
 *
 * @author Thannh Lam
 */
public class HinhChuNhat extends HinhHoc{
     int a;
     int b;

    public HinhChuNhat() {
    }

     
    public HinhChuNhat(int a, int b) {
        this.a = a;
        this.b = b;
    }
    
    public void tinhdientich() {
    
    }
    
    public void tinhchuvi(){
    }
    
    
     @Override
     public void nhap(){
     }
     
     public void inKQ(){
     }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public String getTenHinh() {
        return tenHinh;
    }

    public void setTenHinh(String tenHinh) {
        this.tenHinh = tenHinh;
    }

    public int getSocanh() {
        return socanh;
    }

    public void setSocanh(int socanh) {
        this.socanh = socanh;
    }
     
}



/*
 * 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 OOP;

/**
 *
 * @author Thannh Lam
 */
public abstract class HinhHoc {
    String tenHinh;
    int socanh;
    
    public abstract void nhap();
    
    public abstract void inKQ();

    public String getTenHinh() {
        return tenHinh;
    }

    public void setTenHinh(String tenHinh) {
        this.tenHinh = tenHinh;
    }

    public int getSocanh() {
        return socanh;
    }

    public void setSocanh(int socanh) {
        this.socanh = socanh;
    }
    
    
}



/*
 * 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 OOP;

/**
 *
 * @author Thannh Lam
 */
public class TamGiac extends HinhHoc{
    int a;
    int b;
    int c;

    public TamGiac() {
    }

    public TamGiac(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    

    @Override
    public void nhap() {
    }

    @Override
    public void inKQ() {
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }
    
}



/*
 * 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 OOP;

/**
 *
 * @author Thannh Lam
 */
public class Gokisoft {
    public static void main(String[] args) {
        HinhChuNhat hcn = new HinhChuNhat();
        hcn.socanh = 4;
        hcn.tenHinh = "HCN";
        hcn.a = 5;
        hcn.b = 6;
        
        System.out.println(hcn.getSocanh());
        
        
        HinhHoc hh = new TamGiac();
    }
}
Thành Lâm OOP