By GokiSoft.com| 20:35 24/04/2024|
Java Basic

Java basic- Test 30 phút - Lập trình hướng đối tượng java

- Tạo một lớp Shape gồm 1 hàm tạo và các phương thức trừu tượng tinhChuVi, tinhDienTich

- Tạo lớp HinhTron kế thừa từ lớp Shape gồm có tham số radius

--> Viết 2 hàm tạo và getter/setter tương ứng

--> Viết code cho các hàm tinhChuVi và tinhDienTich của hình tròn

- Tạo lớp HinhChuNhat kế thừa từ lớp Shape gồm 2 tham số width và height

--> Viết 2 hàm tạo và getter/setter tương ứng

-> Viết code cho các hàm tinhChuVi và tinhDienTich của hình chữ nhật tương ứng

Yêu cầu :

Tạo lớp Main thực hiện test chương trình.

Tạo 1 phương thức public static float tinhTongDienTich(Shape[] list) để tính tổng diện tích của tất cả các Shape trong mảng list

Thực hiện khai báo 1 đối tượng HinhTron và HinhChuNhat -> Sử dụng hàm tinhTongDienTich trên để tính tổng diện tích của HinhTron và HinhChuNhat trên. Hiển thị kết quả tính



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

5

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

Minh Nghia [T1907A]
Minh Nghia

2020-03-19 06:55:19



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

/**
 *
 * @author Administrator
 */
public class HinhChuNhat extends Shape{
    double width;
    double height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double tinhChuVi(){
        return (width + height) * 2;
    }
    
    @Override
    public double tinhDienTich(){
        return width * height;
    }
}



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

/**
 *
 * @author Administrator
 */
public class HinhTron extends Shape{
    final float PI = 3.14f;
    double radius;

    public HinhTron() {
    }

    public HinhTron(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    @Override
    public double tinhChuVi(){
        return 2 * Math.PI * radius;
    }
    @Override
    public double tinhDienTich(){
        return Math.PI * radius * radius;
    }
    
    
}



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


/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        HinhTron hinhTron = new HinhTron(5);
        HinhChuNhat hinhChuNhat = new HinhChuNhat(5,7);
        Shape[] list = new Shape[2];
        
        list[0] = hinhTron;
        list[1] = hinhChuNhat;
        
        double tong = tinhTongDienTich(list);
        System.out.println("Dien tinh hinh tron:" + hinhTron.tinhDienTich());
        System.out.println("Dien tich HCN:" + hinhChuNhat.tinhDienTich());
        System.out.println("Tong dien tich:" + tong);
    }
    
    public static double tinhTongDienTich(Shape[] list){
        double tongDienTich = 0;
        
        for (Shape shape : list) {
            tongDienTich += shape.tinhDienTich();
        }
        
        return tongDienTich;
            
    }    
}



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

/**
 *
 * @author Administrator
 */
public class Shape {

    public Shape() {
    }
    public double tinhChuVi(){
        return 0;
    }
    public double tinhDienTich(){
        return 0;
    }
}



Trần Mạnh Dũng [T1907A]
Trần Mạnh Dũng

2020-03-16 13:24:02



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

/**
 *
 * @author admin
 */
public class HinhChuNhat extends Shape{
    double width, height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double tinhChuVi() {
             return (width + height) * 2;
    }

    @Override
    public double tinhDienTich() {
          return width * height;
    }
    
}



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

/**
 *
 * @author admin
 */
public class HinhTron extends Shape{
    float radius;

    public HinhTron() {
    }

    public HinhTron(float radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    @Override
    public double tinhChuVi() {
         return 2 * Math.PI * radius;    
    }

    @Override
    public double tinhDienTich() {
          return Math.PI * radius * radius;  
    }
     
    
}



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

/**
 *
 * @author admin
 */
public class Shape {
    public Shape(){
        
    }
    
    public double tinhChuVi(){
        return 0;
    }
    
    public double tinhDienTich(){
        return 0;
    }
}



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

import java.util.Scanner;

/**
 *
 * @author FATE STORE
 */
public class main {

 
    public static void main(String[] args) {
        // TODO code application logic here
       HinhTron hinhtron = new HinhTron(4);
       HinhChuNhat hinhchunhat = new HinhChuNhat(6,4);
       Shape [] list = new Shape[3];
       list[0] = hinhchunhat;
       list[1] = hinhtron;
       list[2] = hinhtron;
       System.out.println("dien tich hinh chu nhat: "+ hinhchunhat.tinhDienTich());
       System.out.println("chu vi hinh chu nhat: "+ hinhchunhat.tinhChuVi());
       System.out.println("dien tich hinh tron: "+hinhtron.tinhDienTich());
        System.out.println("chu vi hinh tron: "+hinhtron.tinhChuVi());
        System.out.println("tong dien tich: "+tinhTongDienTich(list));
    }
    public static float tinhTongDienTich(Shape[] list){
        float tongDienTich = 0;
        for(Shape shape: list){
            tongDienTich += shape.tinhDienTich();
        }
        return tongDienTich;
    }
}




Lê Minh Bắc [T1907A]
Lê Minh Bắc

2020-03-16 12:42:10

Shape:

package KT15p;
public abstract class Shape {

    public Shape() {
    }
    
    abstract float tinhChuVi();
    abstract float tinhDienTich();  
}

HinhChuNhat:
package KT15p;

public class HinhChuNhat extends Shape{
    float width;
    float height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(float width, float height) {
        this.width = width;
        this.height = height;
    }
    
    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }
    
    
     @Override
    public float tinhChuVi(){
       return (float) (2*(width+height));
    }
    
    @Override
    public float tinhDienTich(){
        return (float) (width*height);
    }
}

HinhTron:
package KT15p;
import java.lang.Math;
public class HinhTron extends Shape {
    float radius;
    double pi = Math.PI;
    
    public HinhTron() {
    }

    public HinhTron(float radius) {
        this.radius = radius;
    }

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }
    
    @Override
    public float tinhChuVi(){
       return (float) (2*pi*radius);
    }
    
    @Override
    public float tinhDienTich(){
        return (float) (pi*radius*radius);
    }
}

Main:
package KT15p;

public class Main {
    public static void main(String[] agrs) {
        HinhTron hinhtron = new HinhTron(10);
        HinhChuNhat HCN = new HinhChuNhat(20,20);
        Shape[] list = new Shape[2];
        
        list[0] = hinhtron;
        list[1] = HCN;
        
        float tong = tongDienTich(list);
        
        System.out.println("Tong dien tich = " + tong);
    }
    
    public static float tongDienTich(Shape[] list) {
        float tongDienTich = 0;
        for(Shape shape : list) {
            tongDienTich += shape.tinhDienTich();
        }
        
        return tongDienTich;
}
    }



hoangduyminh [T1907A]
hoangduyminh

2020-03-13 07:15:19



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

/**
 *
 * @author Minh
 */
public class Shape {
    public Shape() {
      
    }
    public double tinhChuVi() {
      return 0;
    }
    public double tinhDienTich() {
      return 0;
    }
}

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

/**
 *
 * @author Minh
 */
public class HinhChuNhat extends Shape{
    double width,height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double tinhChuVi() {
        return (width + height) * 2;
    }

    @Override
    public double tinhDienTich() {
        return width * height;
    }
    
    

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
    
    
    
}

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

/**
 *
 * @author Minh
 */
public class HinhTron extends Shape{
    float radius;

    public HinhTron() {
    }

    public HinhTron(float radius) {
        this.radius = radius;
    }

    @Override
    public double tinhChuVi() {
        return 2 * Math.PI * radius;
    }

    @Override
    public double tinhDienTich() {
        return Math.PI * radius * radius;
    }
     

    public double getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }
    
    
}

/*
 * 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 Bt519;
/**
 *
 * @author Minh
 */
public class main {
     public static void main(String[] args) {
      HinhTron hinhTron = new HinhTron(5);
	       HinhChuNhat hinhChuNhat = new HinhChuNhat(5,6);
	       
	       Shape[] list = new Shape[2];
	       
	       list [0] = hinhTron;
	       list [1] = hinhChuNhat;
	       
	       double tong = tinhTongDienTich(list);
	       
	        System.out.println("Dien tich hifnh tron: " + hinhTron.tinhDienTich());
	        System.out.println("Doem tich hinh chu nhat la : " + hinhChuNhat.tinhDienTich());
	        System.out.println("Tong dieen tich laf : " + tong);
	       
	    }
	    public static double tinhTongDienTich(Shape[] list){
	        double tongDienTich = 0;
	        
	        for (Shape shape : list){
	            tongDienTich += shape.tinhDienTich();
	        }
	        return tongDienTich;
	    }
	            
	}



hoangduyminh [T1907A]
hoangduyminh

2020-03-13 07:15:08



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

/**
 *
 * @author Minh
 */
public class Shape {
    public Shape() {
      
    }
    public double tinhChuVi() {
      return 0;
    }
    public double tinhDienTich() {
      return 0;
    }
}

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

/**
 *
 * @author Minh
 */
public class HinhChuNhat extends Shape{
    double width,height;

    public HinhChuNhat() {
    }

    public HinhChuNhat(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double tinhChuVi() {
        return (width + height) * 2;
    }

    @Override
    public double tinhDienTich() {
        return width * height;
    }
    
    

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
    
    
    
}

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

/**
 *
 * @author Minh
 */
public class HinhTron extends Shape{
    float radius;

    public HinhTron() {
    }

    public HinhTron(float radius) {
        this.radius = radius;
    }

    @Override
    public double tinhChuVi() {
        return 2 * Math.PI * radius;
    }

    @Override
    public double tinhDienTich() {
        return Math.PI * radius * radius;
    }
     

    public double getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }
    
    
}

/*
 * 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 Bt519;
/**
 *
 * @author Minh
 */
public class main {
     public static void main(String[] args) {
      HinhTron hinhTron = new HinhTron(5);
	       HinhChuNhat hinhChuNhat = new HinhChuNhat(5,6);
	       
	       Shape[] list = new Shape[2];
	       
	       list [0] = hinhTron;
	       list [1] = hinhChuNhat;
	       
	       double tong = tinhTongDienTich(list);
	       
	        System.out.println("Dien tich hifnh tron: " + hinhTron.tinhDienTich());
	        System.out.println("Doem tich hinh chu nhat la : " + hinhChuNhat.tinhDienTich());
	        System.out.println("Tong dieen tich laf : " + tong);
	       
	    }
	    public static double tinhTongDienTich(Shape[] list){
	        double tongDienTich = 0;
	        
	        for (Shape shape : list){
	            tongDienTich += shape.tinhDienTich();
	        }
	        return tongDienTich;
	    }
	            
	}



Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-03-11 15:12:29


package ASSIGNMENT.assignment_15p;

import java.util.Scanner;

public class HinhChuNhat extends Shape{
float width;
float height;

public HinhChuNhat() {
}

public HinhChuNhat(float width, float height) {
this.width = width;
this.height = height;
}

@Override
public float tinhChuVi(){
return (float) (2*(width+height));
}

@Override
public float tinhDienTich(){
return (float) (width*height);
}

public float getWidth() {
return width;
}

public void setWidth(float width) {
this.width = width;
}

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}



}
package ASSIGNMENT.assignment_15p;


public class Main {
public static void main(String[] agrs) {
HinhTRon hinhtron = new HinhTRon(2);
HinhChuNhat HCN = new HinhChuNhat(3, 4);
Shape[] list = new Shape[2];

list[0] = hinhtron;
list[1] = HCN;

float tong = tongDienTich(list);

System.out.println("Tong dien tich = " + tong);
}

public static float tongDienTich(Shape[] list) {
float tongDienTich = 0;
for (int i = 0; i < list.length; i++) {
tongDienTich += list[i].tinhDienTich();
}
return tongDienTich;
}
}

package ASSIGNMENT.assignment_15p;

public abstract class Shape {

public Shape() {
}

abstract float tinhChuVi();
abstract float tinhDienTich();
}

package ASSIGNMENT.assignment_15p;



public class HinhTRon extends Shape {
float radius;
double pi = Math.PI;

public HinhTRon() {
}

public HinhTRon(float radius) {
this.radius = radius;
}

public float getRadius() {
return radius;
}

public void setRadius(float radius) {
this.radius = radius;
}

@Override
public float tinhChuVi(){
return (float) (2*pi*radius);
}

@Override
public float tinhDienTich(){
return (float) (pi*radius*radius);
}
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-03-11 10:12:17



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

/**
 *
 * @author DELL
 */
public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        HinhTron ht0 = new HinhTron();
        ht0.input();
        HinhTron ht = new HinhTron(ht0.getR());
        System.out.println("dien tich hinh tron: " + ht.tinhdientich());

        hinhchunhat hcn0 = new hinhchunhat();
        hcn0.input();
        hinhchunhat hcn = new hinhchunhat(hcn0.getWidth(), hcn0.getHeight());
        System.out.println("dien tich hinh chu nhat: " + hcn.tinhdientich());

        Shape[] list = new Shape[2];
        list[0] = hcn;
        list[1] = ht;

        System.out.println("tong dien tich: " + tinhTongDienTich(list));
    }

    public static float tinhTongDienTich(Shape[] list) {
        float tongDienTich = 0;
        for (Shape shape : list) {
            tongDienTich += shape.tinhdientich();
        }
        return tongDienTich;
    }

}



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

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class hinhchunhat extends Shape {

    Double width, height;
    Scanner scan = new Scanner(System.in);

    public hinhchunhat() {
    }

    public hinhchunhat(Double width, Double height) {
        this.width = width;
        this.height = height;
    }

    public Double getWidth() {
        return width;
    }

    public void setWidth(Double width) {
        this.width = width;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }

    public void input() {
          System.out.println("Height : ");
        height = Double.parseDouble(scan.nextLine());
        System.out.println("Width : ");
        width = Double.parseDouble(scan.nextLine());

    }

    /**
     *
     * @return
     */
    @Override
    public float tinhchuvi() {
        return (float) (2 * (width + height));

    }

    @Override
    public float tinhdientich() {
        return (float) (width * height);

    }
}



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

import java.util.Scanner;

/**
 *
 * @author DELL
 */
public class HinhTron extends Shape {
    
    Double r;

    public HinhTron() {
    }

    public HinhTron(Double r) {
        this.r = r;
    }

    public Double getR() {
        return r;
    }

    public void setR(Double r) {
        this.r = r;
    }
    public void input(){
        Scanner scan = new Scanner(System.in);
        System.out.println("Radius : ");
         r = Double.parseDouble(scan.nextLine());
        
         
    }
    @Override
    public float tinhchuvi() {
        return (float)(2 * Math.PI * r);

    }

    @Override
    public float tinhdientich() {
        return (float)(Math.PI * r * r);
    }

}



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

/**
 *
 * @author DELL
 */
public abstract class Shape {

    public Shape() {
    }
    public abstract float tinhchuvi();

    /**
     *
     * @return
     */
    public abstract float tinhdientich();
}



hoangduyminh [T1907A]
hoangduyminh

2020-03-11 07:11:37



zdasdasda



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

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