Giải Đề Thi Java 2017 - 2
Bài Kham Khảo
Phần 1
Câu 1 + Câu 2
Class Hình Học
// Code by Tuấn
//Edit code by Vy Nguyễn
public abstract class HinhHoc {
private String TenHinh;
private int SoCanh;
public String getTenHinh() {
return TenHinh;
}
public void setTenHinh(String tenHinh) {
TenHinh = tenHinh;
}
public int getSoCanh() {
return SoCanh;
}
public void setSoCanh(int soCanh) {
SoCanh = soCanh;
}
public HinhHoc() {
super();
}
public HinhHoc(String ten_hinh, int so_canh) {
super();
this.setTenHinh(ten_hinh);
this.setSoCanh(so_canh);
}
public abstract float tinhDienTich();
}
Class Hình Chữ Nhật
// Code by Tuấn
//Edit code by Vy Nguyễn
public class HinhChuNhat extends HinhHoc{
private double Dai, Rong;
public double getDai() {
return Dai;
}
public void setDai(double dai) {
Dai = dai;
}
public double getRong() {
return Rong;
}
public void setRong(double rong) {
Rong = rong;
}
public HinhChuNhat() {
super();
}
public HinhChuNhat(String ten_hinh, int so_canh, double chieu_dai, double chieu_rong) {
super(ten_hinh,so_canh);
this.setDai(chieu_dai);
this.setRong(chieu_rong);
}
public float tinhDienTich() {
return (float)(this.getDai() * this.getRong());
}
}
Class Hình Tam Giác
// Code by Tuấn
//Edit code by Vy Nguyễn
public class HinhTamGiac extends HinhHoc {
private double CanhA, CanhB, CanhC;
public double getCanhA() {
return CanhA;
}
public void setCanhA(double canhA) {
CanhA = canhA;
}
public double getCanhB() {
return CanhB;
}
public void setCanhB(double canhB) {
CanhB = canhB;
}
public double getCanhC() {
return CanhC;
}
public void setCanhC(double canhC) {
CanhC = canhC;
}
public HinhTamGiac() {
super();
}
public HinhTamGiac(String ten_hinh,int so_canh, double canha, double canhb, double canhc) {
super(ten_hinh,so_canh);
this.setCanhA(canha);
this.setCanhB(canhb);
this.setCanhC(canhc);
}
public float tinhDienTich() {
float p = (float) (this.getCanhA()+this.getCanhB()+this.getCanhC()) / 2;
float DT = (float) (Math.sqrt(p*(p-this.getCanhA()) * (p - this.getCanhB()) * (p - this.getCanhC())));
return DT;
}
}
Class Test Hình Học
// Code by Tuấn
//Edit code by Vy Nguyễn
public class TesstHinhHoc {
public static void main(String[] args) {
HinhChuNhat hcn = new HinhChuNhat("Hinh Chu Nhat", 4, 6 ,4);
System.out.println("Dien Tich HCN = "+hcn.tinhDienTich());
HinhTamGiac tg = new HinhTamGiac("Hinh Tam Giac", 3, 5,6,7);
System.out.println("Dien Tich Tam Giac = "+tg.tinhDienTich());
}
}
Phần 2
Câu 1 + Câu 2 + Câu 3 + Câu 4
// Code by Tuấn
//Edit code by Vy Nguyễn
import java.util.*;
public class MangSoNguyen {
// Câu 1
public int inPut(){
Scanner sc = new Scanner(System.in);
int n;
do{
n = sc.nextInt();
}while(n < 0);
return n;
}
public void inPut_Arr(int[] a) {
for(int i=0; i<a.length; i++) {
System.out.print("a["+(i+1)+"]= ");
a[i] = inPut();
}
}
// Câu 2
public int toTal(int[] a) {
int s = 0;
for(int i=0; i<a.length; i++)
if(a[i] %2 == 0 && a[i] % 3 == 0) s+= a[i];
return s;
}
// Câu 3
public int UCLN(int a, int b) {
while(b != 0) {
if( a > b) a-= b;
else b-= a;
}
return a;
}
public void UCLN_Arr(int[] a) {
int d = UCLN(a[0], a.length);
System.out.println("UCLN = " + d);
}
public static void main(String[] args) {
MangSoNguyen h = new MangSoNguyen();
System.out.print("Nhap So Luong Phan Tu: ");
int n = h.inPut();
int[] a = new int[n];
System.out.println("Nhap Mang");
h.inPut_Arr(a);
System.out.println("Tong = " + h.toTal(a));
h.UCLN_Arr(a);
}
}
No Comment to " Giải Đề Thi Java 2017 - 2 "