KT 15 phút


package bai1;

import java.util.Random;
import java.util.Scanner;

public class Cau1 {
	public static void main(String[] args) {
		int[] mang_ngau_nhien = new int[20];
		for(int i = 0; i<mang_ngau_nhien.length; i++){
			mang_ngau_nhien[i] = (new Random().nextInt(100)+1);
			System.out.print(mang_ngau_nhien[i] + " ,");
		}
		System.out.println("\n---------Cac so nguyen to co trong mang-------------------------------");
		printPrimeNumbers(mang_ngau_nhien);

		//===============Xoa 1 phan tu nhap vao ban phim ne=============================
		Scanner sc = new Scanner(System.in);
		System.out.print("\n Nhap So Can Xoa : ");
		int value_xoa = sc.nextInt();
		System.out.println("\n Mang Sau Khi Xoa");
		removeByValue(mang_ngau_nhien, value_xoa);

	}

	public static void printPrimeNumbers(int[] a){
		for(int i = 0; i<a.length; i++){
			if(isPrime(a[i]))
				System.out.print(a[i] + ", ");
		}
	}

	public static boolean isPrime(int val){
		for(int i = 2; i < val/2; i++){
			if(val % i == 0)
				return false;
		}
		return true;
	}

	public static void removeByValue(int[] a, int x){
		int b[] = new int[a.length];
		int j = 0;
		for(int i = 0; i < a.length; i++){
			if(a[i] != x){
				b[j] = a[i];
				j++;
			}

		}
		for(int k = 0; k < j ; k++){
			System.out.print(b[k] + ", ");
		}
	}
}

Bài 2

Cấu trúc thư mục:

d1

Bước 1: Tạo 1 interface nha có tên Place như sau :

package bai2;
// Xem ki cai nay` la inter face nha
public interface Place {
	public double perimeter();
	public double area();
}

Bước 2 Tạo 1 Class có tên House.java

package bai2;

public class House implements Place{
	private double wide;
	private double length;

	public House(double wide, double length) {
		super();
		this.wide = wide;
		this.length = length;
	}

	// tinh chu vi
	@Override
	public double perimeter() {
		return (wide + length) * 2;
	}

	// tinh dien tich
	@Override
	public double area() {
		return wide * length;
	}

	@Override
	public String toString() {
		return "House [wide=" + wide + ", length=" + length + "]";
	}

}

Bước 3 : Tạo Class WoodenHouse.java extend thằng House

package bai2;

public class WoodenHouse extends House{
	private double height;

	public WoodenHouse(double wide, double length, double height) {
		super(wide, length);
		this.height = height;
	}

	public double getHeight() {
		return height;
	}

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

	@Override
	public String toString() {
		return "WoodenHouse Piremeter : " + super.perimeter() + " Area : " + super.area() +"\n"+
			   "";
	}

}

Bước 4 tạo class Building.java extend House

package bai2;

public class Building extends House{
	private int numberOfFloors;
	private int buildingYear;
	public Building(double wide, double length, int numberOfFloors,
			int buildingYear) {
		super(wide, length);
		this.numberOfFloors = numberOfFloors;
		this.buildingYear = buildingYear;
	}
	public int getNumberOfFloors() {
		return numberOfFloors;
	}
	public void setNumberOfFloors(int numberOfFloors) {
		this.numberOfFloors = numberOfFloors;
	}
	public int getBuildingYear() {
		return buildingYear;
	}
	public void setBuildingYear(int buildingYear) {
		this.buildingYear = buildingYear;
	}
	@Override
	public String toString() {
		return "Building Piremeter : " + super.perimeter() + " Area : " + super.area()*numberOfFloors +" \n"+
			   "Building Year " +buildingYear ;
	}

}</pre>
&nbsp;
<pre>

Móa hàm main thì bó tay. Nó chả nói gì muh kiu test là test sao tao củng không hiểu. Tụi bay hỏi ổng đi rồi nt tao

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: