By GokiSoft.com|
10:19 24/10/2020|
Java Basic
[Share Code] Tìm hiểu java basic - Lập trình OOP - Lập trình Java
#Test.java
/*
* 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 aptech.gokisoft;
import aptech.SinhVien;
/**
*
* @author student
*/
public class Test {
public static void main(String[] args) {
System.out.println("Test create project...");
SinhVien sv = new SinhVien();
sv.fullname = "A";
// sv.address = "Ha Noi";
// sv.email = "a@gmail.com";
// sv.rollno = "R001";
System.out.println("Ten sinh vien: " + sv.fullname);
}
}
#Student.java
/*
* 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 aptech.model;
/**
*
* @author student
*/
public class Student {
}
#Citizen.java
/*
* 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 aptech.oop;
/**
*
* @author student
*/
public class Citizen {
String fullname, address;
public Citizen() {
System.out.println("c01");
}
public Citizen(String fullname, String address) {
this.fullname = fullname;
this.address = address;
System.out.println("c02");
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void running() {
System.out.println("Cong dan " + fullname + " dang chay");
}
}
#Student.java
/*
* 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 aptech.oop;
/**
*
* @author student
*/
public class Student extends Citizen{
String rollno;
public Student() {
System.out.println("s01");
}
public Student(String rollno, String fullname, String address) {
super(fullname, address);
this.rollno = rollno;
System.out.println("s02");
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public void learning() {
System.out.println("Cong dan dang hoc");
}
}
#Test.java
/*
* 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 aptech.oop;
/**
*
* @author student
*/
public class Test {
public static void main(String[] args) {
// Citizen c = new Citizen("A", "Ha Noi");
// Student std = new Student("ABC", "OKOK", "Nam Dinh");
//
// std.fullname = "KKKKK";
//
// c.running();
// std.running();
Student std = new Student();
std.running();
}
}
#Animal.java
/*
* 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 aptech;
/**
*
* @author student
*/
public class Animal {
String name, foodType;
int age;
public Animal() {
}
public Animal(String name, String foodType) {
this.name = name;
this.foodType = foodType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age > 0) {
this.age = age;
} else {
System.err.println("Tuoi > 0");
}
}
}
#LoopTest.java
/*
* 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 aptech;
/**
*
* @author student
*/
public class LoopTest {
public static void main(String[] args) {
int i = 7;
// while(i <= 6) {
// System.out.println("i = " + i);
// i+=2;
// }
//
// i = 7;
// do {
// System.out.println("i = " + i);
// i+=2;
// } while(i<=6);
i = 2;
for (i = 2; i <= 6; i+=2) {
System.out.println("i = " + i);
}
/*//tinh tong cac so tu 1 -> 10
i = 1;
int sum = 0;
while(i<=10) {
sum += i;
i++;
}
System.out.println("sum = " + sum);
//tinh tong cac so tu 1 -> 10 -> cac so chia het cho 3
i = 1;
sum = 0;
while(i<=10) {
if(i % 3 == 0) {
sum += i;
}
i++;
}
// Cach 2:
i = 1;
sum = 0;
while(i<=10) {
if(i % 3 != 0) {
i++;
continue;
}
sum += i;
i++;
}
System.out.println("sum = " + sum);
//tinh tong cac so tu 1 -> 10 -> dung tinh khi i % 7 == 0
i = 1;
sum = 0;
while(i<=10) {
if(i % 7 == 0) {
break;
}
sum+=i;
i++;
}
System.out.println("sum = " + sum);*/
}
}
#Phan1.java
/*
* 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 aptech;
import java.util.Scanner;
/**
*
* @author student
*/
public class Phan1 {
static enum STATUS {BAT_DIEN, TAT_DIEN};
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Hello World!!!");
String name, address;
int age;
// Nhap thong tin sinh vien
// Khai bao bien nhap tu ban phim
Scanner input = new Scanner(System.in);
//Nhap thong tin
System.out.println("Nhap ten: ");
name = input.nextLine();
System.out.println("Nhap tuoi: ");
age = Integer.parseInt(input.nextLine());//float, double, ...
System.out.println("Nhap dia chi: ");
address = input.nextLine();
//msg => Thanh nien => age >= 18
//msg => Chua du 18 tuoi => age < 18
String msg = (age >= 18)?"Thanh Nien":"Chua du 18 tuoi";
if(age >= 18) {
msg = "Thanh Nien";
} else {
msg = "Chua du 18 tuoi";
}
//switch: so nguyen, ky tu, String, enum.
System.out.println("msg >> " + msg);
System.out.println("Ten: " + name + ", dia chi: " + address + ", tuoi : " + age);
//Mo phong ve bong dien
String type = "ABC";
// int status = 0;//0 -> OFF => TAT, 1 => ON => BAT & SANG
STATUS status = STATUS.BAT_DIEN;
System.out.println("status: " + status);
switch(status) {
case BAT_DIEN:
break;
case TAT_DIEN:
break;
default:
break;
}
}
}
#SinhVien.java
/*
* 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 aptech;
/**
*
* @author student
*/
public class SinhVien {
public String fullname;
protected String address;
private String email;
String rollno;
public SinhVien() {
System.out.println("Khoi tao ham tao mac dinh");
//code business logic => OK
}
public SinhVien(String fname) {
fullname = fname;
}
public SinhVien(String fullname, String address, String email, String rollno) {
this.fullname = fullname;
this.address = address;
this.email = email;
this.rollno = rollno;
}
public SinhVien(String fullname, String rollno) {
this.fullname = fullname;
this.rollno = rollno;
}
public void running() {
System.out.println("Sinh vien " + fullname + " dang chay bo");
}
public void learning() {
System.out.println("Sinh vien " + fullname + " dang hoc");
}
public void learning(String subject) {
System.out.println("Sinh vien " + fullname + " dang hoc " + subject);
}
}
#Test.java
/*
* 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 aptech;
/**
*
* @author student
*/
public class Test {
public static void main(String[] args) {
SinhVien sv = new SinhVien();
sv.fullname = "A";
sv.address = "Ha Noi";
// sv.email = "a@gmail.com";
sv.rollno = "R001";
System.out.println("Ten sinh vien: " + sv.fullname);
SinhVien sv01 = new SinhVien("ABC");
System.out.println("Ten sinh vien: " + sv01.fullname);
sv01.learning();
SinhVien sv02 = new SinhVien("TRAN VAN A", "r001");
System.out.println("Ten: " + sv02.fullname + ", ma sinh vien: " + sv02.rollno);
sv02.learning();
sv02.learning("HTML/CSS/JS");
Animal animal = new Animal("Dog", "ABC");
animal.name = "Dog";
animal.setName("Dog");
animal.setAge(-5);
System.out.println(animal.name);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)