Vous êtes sur la page 1sur 37

Bai 1:

public class Rectangle {


private double width;
private double height;
private String color;


//constructor 0-arg
public Rectangle(){

}

//constructor full-args
public Rectangle(double width, double height, String color){
this.width=width;
this.height=height;
this.color=color;
}
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 String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

//Tinh dien tich hinh chu nhat
public double findArea(){
return width*height;
}

//Tinh va tra ve chu vi HCN
public double findPerimeter(){
return (width+height)*2;
}
}


import java.util.Scanner;
public class TestBai1 {
public static void main (String [] args){
Scanner input= new Scanner(System.in);

//Khoi tao hinh chu nhat

//1) Nhap cac gia tri khoi tao
System.out.print("Nhap mau sac: ");
String tempColor=input.nextLine();
System.out.print("Nhap chieu rong: ");
double tempWidth=input.nextDouble();
System.out.print("Nhap chieu dai: ");
double tempHeight=input.nextDouble();

//2) Tao doi tuong Rectangle bang ham khoi tao full
Rectangle rec= new Rectangle(tempWidth,tempHeight,tempColor);


//Dua ra cac thong tin hinh chu nhat vua tao

System.out.println("Hinh chu nhat co :\n" +
"Mau :"+rec.getColor()+" "+
"Rong ="+rec.getWidth()+" "+
"Dai ="+rec.getHeight()+"\n"+
"Dien tich = "+rec.findArea()+"\n"+
"Chu vi ="+rec.findPerimeter());
}
}

BAi 2:
public class StackOfIntegers {
private int size;
private int[] elements;

//constructor 0-arg
public StackOfIntegers(){

elements = new int[16];
}

//constructor 1-arg
public StackOfIntegers(int capacity){
elements= new int[capacity];
}

//test empty
public boolean isEmpty(){
if(size==0) return true;
return false;
}

//test full
public boolean isFull(){
if(size==elements.length) return true;
return false;
}

//Return the number at the top of the stack
public int peak(){
if(isEmpty()) return -99999;
return elements[size];
}

//Push a number into the stack
public void push(int value){
if(isFull()){
System.out.println("The stack is FULL!");
return;
}
elements[++size]=value;
}

//Pop and return the number at the top of the stack
public int pop(){
if(isEmpty()){
System.out.println("The stack is EMPTY");
return -99999;
}
size--;
return elements[size+1];
}

//Get the size of the stack
public int getSize(){
return size;
}
}

import java.util.Scanner;
public class TestBai2 {
public static boolean isNT(int number){
if(number<2) return false;
if(number==2) return true;
for(int i=2;i<=Math.sqrt(number);i++)
if(number%i==0) return false;

return true;
}

public static void main (String [] args){
//Nhap so n
System.out.print("Nhap n = ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();

//Dua cac so nguyen to <n vao stack theo thu tu tang dan
StackOfIntegers stack= new StackOfIntegers(1000);
for(int i=2;i<n;i++)
if(isNT(i)) stack.push(i);
while(!stack.isEmpty())
System.out.println(stack.pop());

}
}

import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class TestBai3 {
public static boolean isNT(int number){
if(number<2) return false;
if(number==2) return true;
for(int i=2;i<=Math.sqrt(number);i++)
if(number%i==0) return false;

return true;
}

public static void main (String [] args){
//Nhap so n
System.out.print("Nhap n = ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();

//Dua cac so nguyen to <n vao stack theo thu tu tang dan
StackOfIntegers stack= new StackOfIntegers(1000);
for(int i=2;i<=n;i++)
while(isNT(i)&& n%i==0){
stack.push(i);
n/=i;
}
while(!stack.isEmpty())
System.out.println(stack.pop());

}
}

Bai45
public class StackOfChars {
private int size;
private char[] elements;

//constructor 0-arg
public StackOfChars(){

elements = new char[16];
}

//constructor 1-arg
public StackOfChars(int capacity){
elements= new char[capacity];
}

//test empty
public boolean isEmpty(){
if(size==0) return true;
return false;
}

//test full
public boolean isFull(){
if(size==elements.length) return true;
return false;
}

//Return the number at the top of the stack
public char peak(){
if(isEmpty()) return '@';
return elements[size];
}

//Push a number into the stack
public void push(char ch){
if(isFull()){
System.out.println("The stack is FULL!");
return;
}
elements[++size]=ch;
}

//Pop and return the number at the top of the stack
public char pop(){
if(isEmpty()){
System.out.println("The stack is EMPTY");
return '@';
}
size--;
return elements[size+1];
}

//Get the size of the stack
public int getSize(){
return size;
}
}

import BaiLam.Bai23.StackOfIntegers;
import java.util.Scanner;
public class TestBai45 {
public static void inFixToPostFix( char [] sourceChars, char [] targetChars){
int count=0;
StackOfChars stack = new StackOfChars(1000);
for(int i=0;i<=sourceChars.length-1;i++){
//Stack chi chua toan tu +-*/


switch(sourceChars[i]){
//Neu la dau '(' thi bo qua
case '(':{
break;
}
//Neu la cac toan tu + - * / thi dua vao stack
case '+':{
stack.push(sourceChars[i]);
break;
}
case '-':{
stack.push(sourceChars[i]);
break;
}
case 'x':{
stack.push(sourceChars[i]);
break;
}
case '/':{
stack.push(sourceChars[i]);
break;
}
//Neu la dau ')' thi lay (pop) toan tu + - * / ra khoi Stack va
// dua vao bieu thuc moi
case ')':{

targetChars[count++]=stack.pop();
break;
}
//Neu la toan hang thi dua vao bieu thuc moi'
default:{
targetChars[count++]=sourceChars[i];
}
}
/**
* CACH 2 : DUNG IF-ELSE
//Neu la dau '(' thi bo qua
if(sourceChars[i]=='('){
continue;
}else
if(sourceChars[i]=='+' ||sourceChars[i]=='-' ||
sourceChars[i]=='x' ||sourceChars[i]=='/'){
stack.push(sourceChars[i]);//Neu la cac toan tu + - * / thi dua vao stack
}else
if(sourceChars[i]==')'){
//Neu la dau ')' thi lay (pop) toan tu + - * / ra khoi Stack va
// dua vao bieu thuc moi
targetChars[count++]=stack.pop();
}else{
//Neu la toan hang thi dua vao bieu thuc moi'
targetChars[count++]=sourceChars[i];
}
*/

}
}

public static int calculate(char [] postFixChars){
StackOfIntegers stack = new StackOfIntegers(1000);

for(int i=0;i<=postFixChars.length-1;i++){
switch (postFixChars[i]){
case '+':{
int temp= stack.pop()+stack.pop();
stack.push(temp);
break;
}
case '-':{
int temp= -stack.pop()+stack.pop(); // So dung sau se tru truoc
stack.push(temp);
break;

}
case 'x':{
int temp= stack.pop()*stack.pop();
stack.push(temp);
break;
}
case '/':{
int temp= stack.pop()/stack.pop();
stack.push(temp);
break;
}

default:{
String str= Character.toString(postFixChars[i]);
stack.push(Integer.parseInt(str));
}
}
}
return stack.peak();
}
public static void main (String [] args){


//Nhap bieu thuc dang trung to (infix)
//System.out.print("Nhap bieu thuc dang trung to = ");
//Scanner input = new Scanner(System.in);
//String inputString =input.nextLine();
String inputString ="((3+7)x(9-(6-2)))";
String inputString1 ="(3x(((5-2)x(7+1))-6))";
char [] source = inputString1.toCharArray();
int count=0;
for(int i=0;i<source.length;i++){
if(source[i]=='(' || source[i]==')')
count++;
}

char [] target = new char[source.length-count];
inFixToPostFix(source, target);
System.out.print("Bieu thuc hau to la: ");
for(char index : target){
System.out.print(index);
}
System.out.println("\nGia tri = "+calculate(target));
}
}

Bai 6:
public class MyPoint {
private double x;
private double y;

//constructor 0-arg
public MyPoint(){

}

//constructor 2-args
public MyPoint(double x, double y){
this.x=x;
this.y=y;
}

//copying constructor
public MyPoint(MyPoint p){
this(p.x,p.y);
}

//Getters
public double getX(){
return x;
}

public double getY(){
return y;
}

public double distance (MyPoint secondPoint){
return Math.sqrt(Math.pow(secondPoint.x-this.x, 2)+Math.pow(secondPoint.y-this.y, 2));
}

public static double distance (MyPoint p1, MyPoint p2){
return Math.sqrt(Math.pow(p2.x-p1.x, 2)+Math.pow(p2.y-p1.y, 2));
}
}

import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class TestBai6 {
public static int n;
public static Scanner input;
public static double maxDistance=0;
public static int vt1,vt2;
public static void nhapN(){
input = new Scanner(System.in);
System.out.println("Nhap n= ");
n= input.nextInt();

}

public static MyPoint nhapDiem( int count){

System.out.println("Nhap diem thu "+count+":");
System.out.print("Nhap x= ");
double tempX=input.nextDouble();
System.out.print("Nhap y= ");
double tempY=input.nextDouble();
return new MyPoint(tempX,tempY);
}

public static void main (String [] args){
nhapN();

MyPoint [] arrayPoint = new MyPoint [n];
for(int i=0;i<arrayPoint.length;i++){
arrayPoint[i]= new MyPoint();
arrayPoint[i]= nhapDiem(i);
for(int j=0;j<i;j++){
double tempDistance=arrayPoint[i].distance(arrayPoint[j]);
if(tempDistance >maxDistance){
vt1=j;
vt2=i;
maxDistance=tempDistance;
}
}

}

//In ra ket qua
System.out.println("Toa do hai diem co khoang cach lon nhat la:");
System.out.printf("Diem thu nhat :(%4.2f %4.2f) \n",arrayPoint[vt1].getX(),arrayPoint[vt1].getY());
System.out.printf("Diem thu hai :(%4.2f %4.2f) \n",arrayPoint[vt2].getX(),arrayPoint[vt2].getY());
System.out.printf("Khoang cach = %4.2f \n", maxDistance);
}
}

Bai 7:

import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class Matrix {
private float [][]a;

//constructor 0-arg
public Matrix(){

}

//constructor 2-args
public Matrix(int rows, int cols){
a= new float [rows][cols];
}

// Cong hai ma tran
public Matrix add(Matrix m){

if(this.a.length != m.a.length || this.a[0].length !=m.a[0].length){
return null;
}else{
Matrix res = new Matrix (a.length,a[0].length);
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
res.a[i][j]=this.a[i][j]+m.a[i][j];
}
}
return res;
}
}

// Tru hai ma tran
public Matrix sub(Matrix m){

if(this.a.length != m.a.length || this.a[0].length !=m.a[0].length){
return null;
}else{
Matrix res = new Matrix (a.length,a[0].length);
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
res.a[i][j]=this.a[i][j]-m.a[i][j];
}
}
return res;
}
}


public Matrix neg(){
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
a[i][j]=-a[i][j];
}
}
return this;
}

public Matrix transpose(){
Matrix res = new Matrix (a[0].length,a.length);
for(int i=0;i<a[0].length;i++){
for(int j=0;j<a.length;j++){
res.a[i][j]=this.a[j][i];
}
}
return res;
}

public Matrix mul(Matrix m){
Matrix res = new Matrix (this.a.length,m.a[0].length); // Hang cua ma tran dau va cot ma tran thu 2
for(int i=0;i<this.a.length;i++){
for(int j=0;j<m.a[0].length;j++){
res.a[i][j]=0;
for(int k=0;k<this.a[0].length;k++){

res.a[i][j]+=this.a[i][k]*m.a[k][j];
}
}
}
return res;
}

public void print(){
System.out.println("\nXuat ma tran: ");
for(int i=0;i<a.length;i++){
for (int j=0;j<a[0].length;j++){
System.out.printf("%7.2f",a[i][j]);
}
System.out.println();
}
}

public void input(Scanner input){
System.out.println("\nNhap vao ma tran");
System.out.println(a.length+" "+a[0].length);
for(int i=0;i<a.length;i++){
for(int j=0;j<a[0].length;j++){
System.out.printf("Nhap a[%d][%d] = ",i,j );
a[i][j]=input.nextFloat();
}
}
}



}

import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class TestBai7 {
public static void main (String [] args){
Matrix a= new Matrix (2,3);
Matrix b= new Matrix (3,2);
Scanner in = new Scanner(System.in);
a.input(in);
b.input(in);



if(a.add(b) != null){ // Kiem tra 2 ma tran hop le hay khong
a.add(b).print();
a.sub(b).print();
}
else{
System.out.println("Hai ma tran khong hop le de cong /tru");
}

a.mul(b).print();

}
}

Bai 8:
public class PhanSo {
private int ts;
private int ms;

//constructor 0-arg
public PhanSo(){

}

//constructor 2-args
public PhanSo(int ts, int ms){
this.ts=ts;
this.ms=ms;
}


//Cong 2 phan so
public PhanSo cong(PhanSo sp2){
return new PhanSo(this.ts*sp2.ms+sp2.ts*this.ms,this.ms*sp2.ms).toiGian();
}

//Tru 2 phan so
public PhanSo tru(PhanSo ps2){
return new PhanSo(this.ts*ps2.ms-ps2.ts*this.ms,this.ms*ps2.ms).toiGian();
}

//Nhan 2 phan so
public PhanSo nhan(PhanSo ps2){
return new PhanSo(this.ts*ps2.ts,this.ms*ps2.ms).toiGian();
}

//Chia 2 phan so
public PhanSo chia(PhanSo ps2){
return new PhanSo(this.ts*ps2.ms,this.ms*ps2.ts).toiGian();
}

//Nghich dao
public PhanSo nghichDao(){
return new PhanSo(this.ms, this.ts);
}

//Doi dau
public PhanSo doiDau(){
return new PhanSo(-this.ts,this.ms);
}


//Toi gian phan so
public PhanSo toiGian(){
int max=1;
for(int i=2;i<=ts;i++){
if(ts%i==0 && ms%i==0)
max=i;
}
if(ms>0){
return new PhanSo(ts/max,ms/max);
}else{
// Mau so <0 suy ra ca ts va ms se doi dau de trinh bay
// vi du -a/-b = a/b
// vi du a/-b = -a/b
return new PhanSo(-this.ts/max,-this.ms/max);
}

}

//So sanh bang
public boolean bangNhau(PhanSo ps2){
PhanSo kq= this.chia(ps2).toiGian();
if(kq.ts==1 && kq.ms==1){
return true;
}
return false;
}

//So sanh lon hon
public boolean lonHon(PhanSo ps2){
if(this.bangNhau(ps2)==false && this.nhoHon(ps2)==false)
return true;
return false;
}

//So sanh nho hon
public boolean nhoHon(PhanSo ps2){
int tsThis=this.toiGian().ts* ps2.toiGian().ms; // Quy dong phan so This
int tsPs2 =ps2.toiGian().ts*this.toiGian().ms; //Quy dong phan so Ps2
if(tsThis<tsPs2)
return true;
return false;

}
public void hien(){
this.toiGian();
//Do ham toiGian da loai bo cac dau (-) du thua`
//nen ham hien chi con` lai :

if(ts!=0)
System.out.print(ts+"/"+ms);
else
System.out.print(0);
}

public void nhap(String str){ //str dang ts/ms
String ts="0";
String ms="0";
for(int i=0 ;i<str.length();i++){
if(str.charAt(i)=='/'){
ts=str.substring(0, i);
ms=str.substring(i+1);
}
}

this.ts=Integer.parseInt(ts);
this.ms=Integer.parseInt(ms);
}
}


import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class TestBai8 {
public static void main (String [] args){
//Nhap 2 phan so
Scanner input = new Scanner(System.in);
System.out.print("Nhap phan so thu 1 dang (ts/ms): ");
String str1=input.nextLine();
PhanSo ps1= new PhanSo();
ps1.nhap(str1);
System.out.print("Nhap phan so thu 2 dang (ts/ms): ");
String str2=input.nextLine();
PhanSo ps2= new PhanSo();
ps2.nhap(str2);

//Tong hieu tich thuong
System.out.print("Tong = ");
ps1.cong(ps2).hien();
System.out.print("\nHieu = ");
ps1.tru(ps2).hien();
System.out.print("\nThuong = ");
ps1.nhan(ps2).hien();
System.out.print("\nThuong = ");
ps1.chia(ps2).hien();

//So sanh
if(ps1.bangNhau(ps2))
System.out.println("\nHai phan so bang nhau.");
else if(ps1.nhoHon(ps2))
System.out.println("\nPhan so 1 nho hon .");
else
System.out.println("\nPhan so 1 lon hon .");

}
}

Bai 9:
public class SoPhuc {
private float a;
private float b;

//constructor 0-arg

public SoPhuc(){

}
//constructor 2-args
public SoPhuc(float thuc, float ao){
a=thuc;
b=ao;
}

//Cong 2 so phuc
public SoPhuc cong(SoPhuc sp2){
return new SoPhuc(this.a+sp2.a,this.b+sp2.b);
}

//Tru 2 so phuc
public SoPhuc tru(SoPhuc sp2){
return new SoPhuc(this.a-sp2.a,this.b-sp2.b);
}

//Nhan 2 so phuc
public SoPhuc nhan(SoPhuc sp2){
float thuc= this.a*sp2.a - this.b*sp2.b;
float ao=this.a*sp2.b+this.b*sp2.a;
return new SoPhuc(thuc,ao);
}

//Chia 2 so phuc
public SoPhuc chia(SoPhuc sp2){
float mauSo=sp2.a*sp2.a+sp2.b*sp2.b; //a^2 +b^2
float thuc = (float)(this.a*sp2.a+this.b*sp2.b)/mauSo;
float ao = (float)(this.b*sp2.a-this.a*sp2.b)/mauSo;

return new SoPhuc(thuc,ao);
}

//Nghich dao
public SoPhuc nghichDao(){
float mauSo=this.a*this.a+this.b*this.b;
float thuc=(float)a/mauSo;
float ao=(float)-b/mauSo;

return new SoPhuc(thuc,ao);
}

//So sanh bang nhau
public boolean bangNhau (SoPhuc sp2){
if(this.a==sp2.a &&this.b==sp2.b)
return true;
return false;
}
public void hien(){
if(b<0)
System.out.printf("%6.2f -j %6.2f\n",a,(-b));
else
System.out.printf("%6.2f +j %6.2f\n",a,b);
}
public void nhap(String str){
String thuc="0";
String ao="0";
for(int i=0 ;i<str.length();i++){
if(str.charAt(i)=='j'){
thuc=str.substring(0, i-1);
if(str.charAt(i-1)=='-')
ao="-"+str.substring(i+1);
}
}

this.a=Float.parseFloat(thuc);
this.b=Float.parseFloat(ao);
}
}

import java.util.Scanner;

/**
*
* @author NGOCCHIEN2
*/
public class TestBai9 {
public static void main (String [] args){

Scanner input = new Scanner(System.in);
//Nhap 2 so phuc
System.out.print("Nhap so phuc 1 dang (a+jb)");
String str1= input.nextLine();
SoPhuc sp1=new SoPhuc();
sp1.nhap(str1);

System.out.print("Nhap so phuc 2 dang (a+jb)");
String str2= input.nextLine();
SoPhuc sp2=new SoPhuc();
sp2.nhap(str2);

//Tong- hieu - tich- thuong -nghich dao -So sanh
System.out.print("Tong = ");
sp1.cong(sp2).hien();
System.out.print("Hieu = ");
sp1.tru(sp2).hien();
System.out.print("Tich = ");
sp1.nhan(sp2).hien();
System.out.print("Thuong = ");
sp1.chia(sp2).hien();
System.out.print("Nghich dao = ");
sp1.nghichDao().hien();

if(sp1.bangNhau(sp2))
System.out.println("Hai so phuc bang nhau");
else
System.out.println("Hai so phuc khac nhau");

}
}

Vous aimerez peut-être aussi