You are on page 1of 7

Bài tập 1: Đọc tài liệu lập trình hướng đối tượng và viết chạy 10 ví dụ trong

sách (coppy hình chạy vào file)


Bài làm
Ví dụ 1

CODE Hình chạy


/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

package dinhquangtoan_bai1;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World");
}

Ví dụ 2

CODE Hình chạy


/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

package dinhquangtoan _bai2;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("This is what the
main method received");
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}

Ví dụ 3

CODE Hình chạy


/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/

package dinhquangtoan _bai3;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args)
{
// TODO code application logic
here
int num=10;
if(num%5==0)
System.out.println(num + "is
divisable for 5!");
else
System.out.println(num + "is
indivisable for 5!");
}

}
Ví dụ 4
CODE Hình chạy
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

package dinhquangtoan_bai4_switchdemo;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int day = 2;
switch(day)
{
case 0 :
System.out.println("Sunday");
break;
case 1 :
System.out.println("Monday");
break;
case 2 :
System.out.println("Tuesday");
break;
case 3 :
System.out.println("Wednesday");
break;
case 4 :
System.out.println("Thursday");
break;
case 5 :
System.out.println("Friday");
break;
case 6 :
System.out.println("Satuday");
break;
default:
System.out.println("Invalid
day of week");
}
}

}
Ví dụ 5

CODE Hình chạy


/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

package dinhquangtoan _bai5_whiledemo;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a = 5, sum = 1;
while(a>=1)
{
sum+=a;
a--;
}
System.out.println("the sum is " +
sum);

}
Ví dụ 6

CODE Hình chạy


/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/

package
dinhquangtoan_bai6_dowhiledemo;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a=1, sum=0;
do
{
sum+=a;
a++;
}while(a<=5);
System.out.println("sum of 1 to 5 is"
+ sum);

}
Ví dụ 7

CODE Hình chạy


/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/

package dinhquangtoan _bai7_fordemo;

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

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int sum=0;
for(int i=1; i<=5; i++)
sum+=i;
System.out.println("The sum is" +
sum);
}

Ví dụ 8

CODE Hình chạy


package
dinhquangtoan_bai8_rectangledemo;
import java.awt.*;
import java.lang.*;
public class Main {

/**
* @param args the command line
arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int x=0, y=0;
if(args.length>=2)
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
}
if(x>0 && y>0)
{
int chuvi=2*(x+y);
System.out.println("Chu vi la" +
chuvi);
int dientich=x*y;
System.out.println("Dien tich la"
+ dientich);
}
else
System.out.println("Cac tham so
khong dung");
}

You might also like