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
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-11 15:12:29
package ASSIGNMENT.assignment_15p; |
package ASSIGNMENT.assignment_15p; |
package ASSIGNMENT.assignment_15p; |
package ASSIGNMENT.assignment_15p; |
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
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]](https://www.gravatar.com/avatar/33675cc9fc3762fd323389a179aa047f.jpg?s=80&d=mm&r=g)
hoangduyminh
2020-03-11 07:11:37
zdasdasda