├── Check
└── Inheritance
│ └── ItemManager
│ ├── build
│ └── classes
│ │ ├── .netbeans_automatic_build
│ │ ├── .netbeans_update_resources
│ │ ├── DTO
│ │ ├── Item.class
│ │ ├── Statue.class
│ │ ├── Vase.class
│ │ └── Painting.class
│ │ └── GUI
│ │ └── AntiqueShop.class
│ ├── manifest.mf
│ ├── nbproject
│ ├── private
│ │ └── private.properties
│ ├── genfiles.properties
│ ├── project.xml
│ └── project.properties
│ ├── out
│ └── production
│ │ └── ItemManager
│ │ ├── DTO
│ │ ├── Item.class
│ │ ├── Vase.class
│ │ ├── Painting.class
│ │ └── Statue.class
│ │ └── GUI
│ │ └── AntiqueShop.class
│ ├── .idea
│ ├── ant.xml
│ ├── misc.xml
│ ├── .gitignore
│ └── modules.xml
│ ├── ItemManager.iml
│ ├── src
│ ├── DTO
│ │ ├── Item.java
│ │ ├── Statue.java
│ │ ├── Vase.java
│ │ └── Painting.java
│ └── GUI
│ │ └── AntiqueShop.java
│ └── build.xml
├── BAI TAP
├── ws6
│ ├── Dictionary
│ │ ├── en-vi.txt
│ │ ├── Main.java
│ │ └── Dictionary.java
│ └── Exchange_money
│ │ ├── Main.java
│ │ ├── Rate.java
│ │ └── Exchange.java
├── player
│ ├── Tax.java
│ ├── GradeBonus.java
│ ├── Player.java
│ ├── PlayerIncome.java
│ ├── TournamentIncome.java
│ └── PlayerTest.java
├── Workshop5
│ └── OrganizationManager
│ │ ├── DTO
│ │ ├── Role.java
│ │ ├── Organization.java
│ │ ├── FPTUniversity.java
│ │ ├── BeeColony.java
│ │ ├── Colony.java
│ │ └── University.java
│ │ └── GUI
│ │ └── Tester.java
├── Workshop3
│ ├── CarManager
│ │ ├── Tester.java
│ │ └── Car.java
│ └── Guitar
│ │ ├── Tester.java
│ │ └── Guitar.java
├── ws5
│ ├── Person
│ │ ├── Person.java
│ │ ├── Officer.java
│ │ ├── Worker.java
│ │ └── TestMain.java
│ └── PhoneNumber
│ │ ├── IntPhoneNumber.java
│ │ ├── PhoneNumber.java
│ │ └── TestMain.java
├── Workshop2
│ ├── Part1.java
│ ├── Part2.java
│ └── Part2_2.java
├── Exchange_money
│ ├── Main.java
│ ├── Rate.java
│ └── Exchange.java
├── workshop6
│ ├── Menu.java
│ ├── Student.java
│ ├── Inputter.java
│ └── StudentManager.java
├── WordCount
│ └── WordCount.java
└── Workshop1
│ ├── Part1.java
│ ├── Part2.java
│ └── Part3.java
├── 6. Exercises on Polymorphism, Abstract Classes and Interfaces
├── Resizable
│ ├── Resizable.java
│ ├── GeometricObject.java
│ ├── ResizableCircle.java
│ ├── TestMain.java
│ └── Circle.java
├── MovablePoint
│ ├── TestMain.java
│ ├── Movable.java
│ └── MovablePoint.java
├── AnimalAbstract
│ ├── Animal.java
│ ├── Cat.java
│ ├── Dog.java
│ ├── BigDog.java
│ └── TestMain.java
├── GeometricObject
│ ├── GeometricObject.java
│ ├── TestMain.java
│ ├── Circle.java
│ └── Rectangle.java
├── MovableRectangle
│ ├── TestMain.java
│ ├── Movable.java
│ ├── MovablePoint.java
│ └── MovableRectangle.java
├── MovableCircle
│ ├── Movable.java
│ ├── TestMain.java
│ ├── MovableCircle.java
│ └── MovablePoint.java
├── Animal
│ ├── Animal.java
│ ├── Cat.java
│ ├── Dog.java
│ ├── BigDog.java
│ └── TestMain.java
└── Shape
│ ├── Square.java
│ ├── Circle.java
│ ├── Shape.java
│ ├── Rectangle.java
│ └── TestMain.java
├── 4. Exercises on Inheritance
├── Animal
│ ├── Mammal.java
│ ├── Animal.java
│ ├── Cat.java
│ ├── TestMain.java
│ └── Dog.java
├── MovablePoint
│ ├── TestMovablePoint.java
│ ├── Point.java
│ └── MovablePoint.java
├── Point3D
│ ├── TestPoint3D.java
│ ├── Point2D.java
│ └── Point3D.java
├── Subclasses
│ ├── Person.java
│ ├── Staff.java
│ ├── TestMain.java
│ └── Student.java
├── Shape
│ ├── Shape.java
│ ├── Square.java
│ ├── Circle.java
│ ├── Rectangle.java
│ └── TestMain.java
└── Cylinder
│ ├── Circle.java
│ ├── Cylinder.java
│ └── TestCylinder.java
├── 3. More Exercises on Classes
├── BigInteger
│ └── TestBigInteger.java
├── Player
│ └── Player.java
├── MyTime
│ └── MyTime.java
├── MyComplex
│ └── MyComplex.java
└── MyPolynomial
│ └── MyPolynomial.java
├── THUAT TOAN
└── 1 2 2 3 3 3 4 4 4 4
│ └── nthterm
│ └── Nthterm.java
├── 5. Exercises on Composition vs Inheritance
├── LineSub
│ ├── Point.java
│ └── LineSub.java
└── Cylinder
│ ├── Circle.java
│ └── Cylinder.java
├── 1. Exercises on Classes
├── Circle.java
├── Rectangle.java
├── InvoiceItem.java
├── Date.java
├── Employee.java
├── Account.java
├── Time.java
└── Ball.java
├── 2. Exercises on Composition
├── MyPoint
│ └── MyPoint.java
├── Book1
│ └── Book.java
├── Invoice
│ └── Invoice.java
└── MyCircle
│ └── MyCircle.java
└── README.md
/Check/Inheritance/ItemManager/build/classes/.netbeans_automatic_build:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/.netbeans_update_resources:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/manifest.mf:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | X-COMMENT: Main-Class will be added automatically by build
3 |
4 |
--------------------------------------------------------------------------------
/BAI TAP/ws6/Dictionary/en-vi.txt:
--------------------------------------------------------------------------------
1 | apple: qua tao
2 | ball: qua bong
3 | cat: con meo
4 | dog: con cho
5 | elephant: con voi
6 | fish: con ca
7 | gift: mon qua
8 | home: nha
9 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/nbproject/private/private.properties:
--------------------------------------------------------------------------------
1 | compile.on.save=true
2 | user.properties.file=C:\\Users\\ADMIN\\AppData\\Roaming\\NetBeans\\8.2\\build.properties
3 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/DTO/Item.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/build/classes/DTO/Item.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/DTO/Statue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/build/classes/DTO/Statue.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/DTO/Vase.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/build/classes/DTO/Vase.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/DTO/Painting.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/build/classes/DTO/Painting.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build/classes/GUI/AntiqueShop.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/build/classes/GUI/AntiqueShop.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Item.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Item.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Vase.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Vase.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Painting.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Painting.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Statue.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/out/production/ItemManager/DTO/Statue.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/out/production/ItemManager/GUI/AntiqueShop.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/giakinh0823/java-programming-tutorial/HEAD/Check/Inheritance/ItemManager/out/production/ItemManager/GUI/AntiqueShop.class
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/.idea/ant.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Datasource local storage ignored files
5 | /../../../../../../:\Users\ADMIN\Downloads\ItemManager\.idea/dataSources/
6 | /dataSources.local.xml
7 | # Editor-based HTTP Client requests
8 | /httpRequests/
9 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/BAI TAP/player/Tax.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public interface Tax {
13 | public abstract double calculateTax();
14 | }
15 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/Role.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public interface Role {
13 | public void createWorker();
14 | }
15 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Resizable/Resizable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Resizable;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public interface Resizable {
13 | public abstract void resize(int percent);
14 | }
15 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovablePoint/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovablePoint;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 | public static void main(String[] args) {
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/AnimalAbstract/Animal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package AnimalAbstract;
7 |
8 | import Animal.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | abstract public class Animal {
15 | public abstract void greeting();
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/GeometricObject/GeometricObject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package GeometricObject;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public interface GeometricObject {
13 | public double getArea();
14 | public double getPerimeter();
15 | }
16 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/ItemManager.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovableRectangle/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovableRectangle;
7 |
8 | import MovableCircle.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class TestMain {
15 |
16 | public static void main(String[] args) {
17 |
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Resizable/GeometricObject.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Resizable;
7 |
8 | import GeometricObject.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public interface GeometricObject {
15 | public double getArea();
16 | public double getPerimeter();
17 | }
18 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/nbproject/genfiles.properties:
--------------------------------------------------------------------------------
1 | build.xml.data.CRC32=c701f951
2 | build.xml.script.CRC32=a26d04e2
3 | build.xml.stylesheet.CRC32=8064a381@1.80.1.48
4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6 | nbproject/build-impl.xml.data.CRC32=c701f951
7 | nbproject/build-impl.xml.script.CRC32=86fe409a
8 | nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
9 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovablePoint/Movable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovablePoint;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public interface Movable {
13 | public abstract void moveUp();
14 | public abstract void moveDown();
15 | public abstract void moveLeft();
16 | public abstract void moveRight();
17 | }
18 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovableCircle/Movable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovableCircle;
7 |
8 | import MovablePoint.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public interface Movable {
15 | public abstract void moveUp();
16 | public abstract void moveDown();
17 | public abstract void moveLeft();
18 | public abstract void moveRight();
19 | }
20 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/nbproject/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | org.netbeans.modules.java.j2seproject
4 |
5 |
6 | ItemManager
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Animal/Mammal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Mammal extends Animal{
13 |
14 | public Mammal() {
15 | }
16 |
17 | public Mammal(String name) {
18 | super(name);
19 | }
20 |
21 |
22 | @Override
23 | public String toString() {
24 | return "Mammal["+super.toString() + ']';
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Animal/Animal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public abstract class Animal {
13 | private String name;
14 |
15 | public Animal(String name) {
16 | this.name = name;
17 | }
18 |
19 | public Animal() {
20 | }
21 |
22 |
23 |
24 | public abstract void greets();
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovableRectangle/Movable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovableRectangle;
7 |
8 | import MovableCircle.*;
9 | import MovablePoint.*;
10 |
11 | /**
12 | *
13 | * @author GIA KINH
14 | */
15 | public interface Movable {
16 | public abstract void moveUp();
17 | public abstract void moveDown();
18 | public abstract void moveLeft();
19 | public abstract void moveRight();
20 | }
21 |
--------------------------------------------------------------------------------
/BAI TAP/player/GradeBonus.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public interface GradeBonus {
13 | public static final int GRADE_A_BONUS_PERCENT = 100;
14 | public static final int GRADE_B_BONUS_PERCENT = 50;
15 | public static final int GRADE_C_BONUS_PERCENT = 30;
16 | public static final int GRADE_D_BONUS_PERCENT = 10;
17 | public abstract double calculateGradeBonus();
18 | }
19 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Animal/Animal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Animal {
13 | private String name;
14 |
15 | public Animal() {
16 | }
17 |
18 |
19 | public Animal(String name) {
20 | this.name = name;
21 | }
22 |
23 | @Override
24 | public String toString() {
25 | return "Animal[" + "name=" + name + ']';
26 | }
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Animal/Cat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Cat extends Mammal{
13 |
14 | public Cat() {
15 | }
16 |
17 | public Cat(String name) {
18 | super(name);
19 | }
20 |
21 | public void greets(){
22 | System.out.println("Meow");
23 | }
24 |
25 | @Override
26 | public String toString() {
27 | return "Cat[" + super.toString() + ']';
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/AnimalAbstract/Cat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package AnimalAbstract;
7 |
8 | import Animal.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Cat extends Animal {
15 |
16 | public Cat() {
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return "Cat[" + super.toString() + ']';
22 | }
23 |
24 | @Override
25 | public void greeting() {
26 | System.out.println("Meow!");
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/3. More Exercises on Classes/BigInteger/TestBigInteger.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package BigInteger;
7 |
8 | import java.math.BigInteger;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class TestBigInteger {
15 | public static void main (String [] args) {
16 | BigInteger i1 = new BigInteger("11111111111111111111111111111111111111111111111111111111111111");
17 | BigInteger i2 = new BigInteger("2222222222222343242222222234234234234212222222222222222222");
18 | System.out.println (i1.add(i2));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Animal/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 |
14 | public static void main(String[] args) {
15 | Cat c1 = new Cat("Gia Kinh");
16 | System.out.println(c1);
17 | c1.greets();
18 | Dog d1 = new Dog("Lan Anh");
19 | Dog d2 = new Dog("Noki");
20 | System.out.println(d1);
21 | System.out.println(d2);
22 | d1.greets();
23 | d1.greets(d2);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Animal/Cat.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Cat extends Animal{
13 |
14 | public Cat(String name) {
15 | super(name);
16 | }
17 |
18 | public Cat() {
19 | }
20 |
21 | @Override
22 | public String toString() {
23 | return "Cat[" + super.toString() +']';
24 | }
25 |
26 |
27 |
28 | @Override
29 | public void greets() {
30 | System.out.println("Meow");
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop3/CarManager/Tester.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop3.CarManager;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class Tester {
13 |
14 | public static void main(String[] args) {
15 | Car c = new Car();
16 | c.pressStartButton();
17 | c.pressAcceleratorButton();
18 | c.output();
19 |
20 | Car c2 = new Car("red", 100, true, true);
21 | c2.pressAcceleratorButton();
22 | c2.setColour("black");
23 | System.out.println("Colour of c3:" + c2.getColour());
24 | c2.output();
25 |
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Animal/Dog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Dog extends Mammal{
13 |
14 | public Dog() {
15 | }
16 |
17 | public Dog(String name) {
18 | super(name);
19 | }
20 |
21 | public void greets(){
22 | System.out.println("Woof");
23 | }
24 |
25 | public void greets(Dog another){
26 | System.out.println("Woooof");
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "Dog[" + super.toString() + ']';
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovableCircle/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovableCircle;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 |
14 | public static void main(String[] args) {
15 | Movable m1 = new MovablePoint(5, 6, 10, 15); // upcast
16 | System.out.println(m1);
17 | m1.moveLeft();
18 | System.out.println(m1);
19 |
20 | Movable m2 = new MovableCircle(1, 2, 3, 4, 20); // upcast
21 | System.out.println(m2);
22 | m2.moveRight();
23 | System.out.println(m2);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/AnimalAbstract/Dog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package AnimalAbstract;
7 |
8 | import Animal.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Dog extends Animal {
15 |
16 | public Dog() {
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return "Dog[" + super.toString() + ']';
22 | }
23 |
24 | @Override
25 | public void greeting() {
26 | System.out.println("Woof!");
27 | }
28 |
29 | public void greeting(Dog another) {
30 | System.out.println("Woooooooooof!");
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/BAI TAP/player/Player.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public abstract class Player {
13 | private String name;
14 |
15 | public Player() {
16 | }
17 |
18 | public Player(String name) {
19 | this.name = name;
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | public void setName(String name) {
27 | this.name = name;
28 | }
29 |
30 | @Override
31 | public String toString() {
32 | return "Player[" + "name=" + name + ']';
33 | }
34 |
35 | abstract public void displayDetails();
36 | }
37 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/AnimalAbstract/BigDog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package AnimalAbstract;
7 |
8 | import Animal.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class BigDog extends Dog {
15 |
16 | public BigDog() {
17 | }
18 |
19 | @Override
20 | public String toString() {
21 | return "BigDog[" + super.toString() + ']';
22 | }
23 |
24 | @Override
25 | public void greeting() {
26 | System.out.println("Woow!");
27 | }
28 |
29 | @Override
30 | public void greeting(Dog another) {
31 | System.out.println("Woooooowwwww!");
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Animal/Dog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Dog extends Animal{
13 |
14 | public Dog() {
15 | }
16 |
17 | public Dog(String name) {
18 | super(name);
19 | }
20 |
21 | @Override
22 | public String toString() {
23 | return "Dog[" + super.toString() +']';
24 | }
25 |
26 | @Override
27 | public void greets() {
28 | System.out.println("Woow");
29 | }
30 |
31 | public void greets(Dog another) {
32 | System.out.println("Woooooow");
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Resizable/ResizableCircle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Resizable;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class ResizableCircle extends Circle implements Resizable{
13 |
14 | public ResizableCircle(double radius) {
15 | super(radius);
16 | }
17 |
18 | public ResizableCircle() {
19 | }
20 |
21 | @Override
22 | public String toString() {
23 | return "ResizableCircle[" + super.toString() +']';
24 | }
25 |
26 |
27 |
28 | @Override
29 | public void resize(int percent) {
30 | radius *= percent/100;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/THUAT TOAN/1 2 2 3 3 3 4 4 4 4/nthterm/Nthterm.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package nthterm;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Nthterm {
15 |
16 | /**
17 | * @param args the command line arguments
18 | */
19 |
20 | public static int nthterm(int n){
21 | return (int)Math.floor((1+Math.sqrt(1+8*(n-1)))/2);
22 | }
23 |
24 | public static void main(String[] args) {
25 | Scanner scanner = new Scanner(System.in);
26 | System.out.print("Enter n: ");
27 | int n = scanner.nextInt();
28 | System.out.println("Value: " + nthterm(n));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Animal/BigDog.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class BigDog extends Dog{
13 |
14 | public BigDog() {
15 | }
16 |
17 | public BigDog(String name) {
18 | super(name);
19 | }
20 |
21 | @Override
22 | public String toString() {
23 | return "BigDog[" + super.toString() + ']';
24 | }
25 |
26 | @Override
27 | public void greets(Dog another) {
28 | System.out.println("Woooooow");
29 | }
30 |
31 | public void greets(BigDog another) {
32 | System.out.println("Wooooooooow");
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/BAI TAP/ws6/Dictionary/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Dictionary;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Main {
15 | public static void main(String[] args) {
16 | Scanner scanner = new Scanner(System.in);
17 | Dictionary dictionary = new Dictionary();
18 | String word;
19 | do{
20 | System.out.print("Enter the word: ");
21 | word = scanner.nextLine();
22 | if (word.isEmpty()) {
23 | break;
24 | }else{
25 | System.out.println("Meaning: " + dictionary.lookup(word));
26 | }
27 | }while(true);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/MovablePoint/TestMovablePoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovablePoint;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMovablePoint{
13 | public static void main(String[] args) {
14 | MovablePoint m1 = new MovablePoint();
15 | System.out.println(m1);
16 | MovablePoint m2 = new MovablePoint(1, 2);
17 | System.out.println(m2);
18 | MovablePoint m3 = new MovablePoint(3, 4, 5, 6);
19 | System.out.println(m3);
20 |
21 | m1.setX(7);
22 | m1.setY(8);
23 | m1.setxSpeed(9);
24 | m1.setySpeed(10);
25 | System.out.println(m1);
26 | System.out.println("Move: " + m1.move());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/Organization.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public abstract class Organization {
13 | private int size;
14 |
15 | public Organization() {
16 | }
17 |
18 | public Organization(int size) {
19 | this.size = size;
20 | }
21 |
22 | public int getSize() {
23 | return size;
24 | }
25 |
26 | public void setSize(int size) {
27 | this.size = size;
28 | }
29 |
30 | public abstract void communicateByTool();
31 |
32 | @Override
33 | public String toString() {
34 | return "The organization’s size is " + size;
35 | }
36 |
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/Person/Person.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Person;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public abstract class Person {
13 | private String name;
14 |
15 | public Person(String name) {
16 | this.name = name;
17 | }
18 |
19 | public Person() {
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | public void setName(String name) {
27 | this.name = name;
28 | }
29 |
30 | public void display(){
31 | System.out.println("Name: "+ this.name);
32 | }
33 |
34 | @Override
35 | public String toString() {
36 | return "Person{" + "name=" + name + '}';
37 | }
38 |
39 | public abstract double getSalary();
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/Person/Officer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Person;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Officer extends Person{
13 |
14 | private double bSalary;
15 |
16 | public Officer(String name, double bSalary) {
17 | super(name);
18 | this.bSalary = bSalary;
19 | }
20 |
21 | public Officer(double bSalary) {
22 | this.bSalary = bSalary;
23 | }
24 |
25 | public Officer() {
26 | }
27 |
28 | @Override
29 | public void display() {
30 | super.display(); //To change body of generated methods, choose Tools | Templates.
31 | System.out.println("Salary: "+this.bSalary);
32 | }
33 |
34 | @Override
35 | public double getSalary() {
36 | return this.bSalary;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Point3D/TestPoint3D.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Point3D;
7 |
8 | import java.util.Arrays;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | class TestPoint3D {
15 |
16 | public static void main(String[] args) {
17 | Point3D p1 = new Point3D(1, 2, 3);
18 | System.out.println(p1);
19 |
20 | p1.setX(4);
21 | p1.setY(5);
22 | p1.setZ(6);
23 | System.out.println(p1);
24 | System.out.println("point X : " + p1.getX());
25 | System.out.println("point Y : " + p1.getY());
26 | System.out.println("point Z : " + p1.getZ());
27 |
28 | p1.setXY(7, 8);
29 | System.out.println(p1);
30 | p1.setXYZ(9, 10, 11);
31 | System.out.println("new point: " + Arrays.toString(p1.getXYZ()));
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop2/Part1.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop2;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author giaki
13 | */
14 | public class Part1 {
15 |
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | int number=0;
19 | do {
20 | System.out.print("Enter the number: ");
21 | try {
22 | number = Integer.parseInt(scanner.nextLine());
23 | if (number < 1) {
24 | throw new Exception();
25 | }
26 | System.out.println("The number is " + number);
27 | break;
28 | } catch (Exception e) {
29 | System.out.println("The number is invaild");
30 | }
31 | } while (true);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Resizable/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Resizable;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 | public static void main(String[] args) {
14 | GeometricObject g1 = new Circle(1.2);
15 | System.out.println(g1);
16 | System.out.println("Perimeter = "+g1.getPerimeter());
17 | System.out.println("Area = "+ g1.getArea());
18 |
19 | Resizable g2 = new ResizableCircle(3.4);
20 | System.out.println(g2);
21 | g2.resize(56);
22 | System.out.println(g2);
23 |
24 |
25 | GeometricObject g3 = (GeometricObject) g2;
26 | System.out.println(g3);
27 | System.out.println("Perimeter = "+g3.getPerimeter());
28 | System.out.println("Area = "+g3.getArea());
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop2/Part2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop2;
7 |
8 | import java.util.Scanner;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | *
13 | * @author giaki
14 | */
15 | public class Part2 {
16 |
17 | public static void main(String[] args) {
18 | Scanner scanner = new Scanner(System.in);
19 | do {
20 | try {
21 | System.out.print("Enter the string: ");
22 | String str = scanner.nextLine();
23 | if (!Pattern.matches("SE\\d+", str)) {
24 | throw new Exception();
25 | }
26 | System.out.println("The string is " + str);
27 | break;
28 | } catch (Exception e) {
29 | System.out.println("The string is invaild");
30 | }
31 | } while (true);
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/PhoneNumber/IntPhoneNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package PhoneNumber;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class IntPhoneNumber extends PhoneNumber{
13 | protected String countryCode;
14 |
15 | public IntPhoneNumber() {
16 | }
17 |
18 | public IntPhoneNumber(String countryCode, int area, String number) {
19 | super(area, number);
20 | this.countryCode = countryCode;
21 | }
22 |
23 | @Override
24 | public void display(){
25 | if (!this.countryCode.isEmpty()) {
26 | System.out.print(this.countryCode + "-");
27 | }
28 | if (this.area!=0) {
29 | System.out.print(this.area + "-");
30 | }
31 | if(!this.number.isEmpty()){
32 | System.out.print(this.number);
33 | }
34 | System.out.println();
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Subclasses/Person.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Subclasses;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Person {
13 | private String name;
14 | private String address;
15 |
16 | public Person() {
17 | }
18 |
19 | public Person(String name, String address) {
20 | this.name = name;
21 | this.address = address;
22 | }
23 |
24 | public String getName() {
25 | return name;
26 | }
27 |
28 | public void setName(String name) {
29 | this.name = name;
30 | }
31 |
32 | public String getAddress() {
33 | return address;
34 | }
35 |
36 | public void setAddress(String address) {
37 | this.address = address;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "Person[" + "name=" + name + ",address=" + address + ']';
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/BAI TAP/Exchange_money/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Scanner;
10 | import java.util.StringTokenizer;
11 |
12 | /**
13 | *
14 | * @author GIA KINH
15 | */
16 | public class Main {
17 | public static void main(String[] args) {
18 | Scanner scanner = new Scanner(System.in);
19 | System.out.print("Enter the first currency code: ");
20 | String code1 = scanner.nextLine();
21 | System.out.print("Enter the second currency code: ");
22 | String code2 = scanner.nextLine();
23 | System.out.print("Enter the amount of first currency: ");
24 | double amount = scanner.nextDouble();
25 | Rate rate = new Rate(code1, code2, amount);
26 | Exchange exchange = new Exchange();
27 | System.out.println("The amount after converted: " + exchange.convert(rate));
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Shape/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Shape {
13 | private String color="red";
14 | private boolean filled=true;
15 |
16 | public Shape() {
17 | }
18 |
19 | public Shape(String color, boolean filled) {
20 | this.color = color;
21 | this.filled = filled;
22 | }
23 |
24 | public String getColor() {
25 | return color;
26 | }
27 |
28 | public void setColor(String color) {
29 | this.color = color;
30 | }
31 |
32 | public boolean isFilled() {
33 | return filled;
34 | }
35 |
36 | public void setFilled(boolean filled) {
37 | this.filled = filled;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "Shape[" + "color=" + color + ",filled=" + filled + ']';
43 | }
44 | }
45 |
46 |
47 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Shape/Square.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Square extends Rectangle{
13 |
14 | public Square() {
15 | }
16 |
17 | public Square(double side) {
18 | super(side, side);
19 | }
20 |
21 | public Square(double side, String color, boolean filled) {
22 | super(side, side, color, filled);
23 | }
24 |
25 | public double getSide(){
26 | return this.getLength();
27 | }
28 |
29 | public void setWidth(double side){
30 | super.setWidth(side);
31 | super.setLength(side);
32 | }
33 |
34 | public void setLength(double side){
35 | super.setWidth(side);
36 | super.setLength(side);
37 | }
38 |
39 | @Override
40 | public String toString(){
41 | return "Square["+super.toString()+"]";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/Person/Worker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Person;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Worker extends Person{
13 |
14 | private double hrs;
15 | final double RATE=5.5;
16 |
17 | public Worker(String name, double hrs) {
18 | super(name);
19 | this.hrs = hrs;
20 | }
21 |
22 | public Worker(double hrs) {
23 | this.hrs = hrs;
24 | }
25 |
26 | public Worker() {
27 | }
28 |
29 | @Override
30 | public void display() {
31 | super.display(); //To change body of generated methods, choose Tools | Templates.
32 | System.out.println("Hourse: " + this.hrs);
33 | System.out.println("RATE: "+this.RATE);
34 | System.out.println("Salary: "+ this.getSalary());
35 | }
36 |
37 | @Override
38 | public double getSalary() {
39 | return hrs*RATE;
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/5. Exercises on Composition vs Inheritance/LineSub/Point.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package LineSub;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Point {
13 |
14 | // Các biến
15 | private int x; // x co phan tu
16 | private int y; // y tọa độ
17 |
18 | // Constructor
19 | public Point(int x, int y) {
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | // Phương thức công khai
25 | public String toString() {
26 | return "Point:(" + x + "," + y + ")";
27 | }
28 |
29 | public int getX() {
30 | return x;
31 | }
32 |
33 | public int getY() {
34 | return y;
35 | }
36 |
37 | public void setX(int x) {
38 | this.x = x;
39 | }
40 |
41 | public void setY(int y) {
42 | this.y = y;
43 | }
44 |
45 | public void setXY(int x, int y) {
46 | this.x = x;
47 | this.y = y;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Subclasses/Staff.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Subclasses;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Staff extends Person{
13 | private String school;
14 | private double pay;
15 |
16 | public Staff(String name, String address, String school, double pay) {
17 | super(name, address);
18 | this.school = school;
19 | this.pay = pay;
20 | }
21 |
22 | public String getSchool() {
23 | return school;
24 | }
25 |
26 | public void setSchool(String school) {
27 | this.school = school;
28 | }
29 |
30 | public double getPay() {
31 | return pay;
32 | }
33 |
34 | public void setPay(double pay) {
35 | this.pay = pay;
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return "Staff[" + super.toString() + "school=" + school + ", pay=" + pay + ']';
41 | }
42 |
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Shape/Square.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Square extends Rectangle{
13 |
14 | public Square() {
15 | }
16 |
17 | public Square(double side) {
18 | super(side, side);
19 | }
20 |
21 | public Square(double side, String color, boolean filled) {
22 | super(side, side, color, filled);
23 | }
24 |
25 | public double getSide(){
26 | return this.getLength();
27 | }
28 |
29 | public void setWidth(double side){
30 | super.setWidth(side);
31 | super.setLength(side);
32 | }
33 |
34 | public void setLength(double side){
35 | super.setWidth(side);
36 | super.setLength(side);
37 | }
38 |
39 | @Override
40 | public String toString(){
41 | return "Square["+super.toString()+"]";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/GeometricObject/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package GeometricObject;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 | public static void main(String[] args) {
14 | GeometricObject c1;
15 | c1 = new Circle(1);
16 | System.out.println(c1);
17 |
18 | GeometricObject c2;
19 | c2 = new Circle(2);
20 | System.out.println(c2);
21 | System.out.println("Area: " + c2.getArea());
22 | System.out.println("Perimeter: " + c2.getPerimeter());
23 |
24 | GeometricObject r1;
25 | r1 = new Rectangle(1, 2);
26 | System.out.println(r1);
27 | GeometricObject r2;
28 | r2 = new Rectangle(3, 4);
29 | System.out.println(r2);
30 | System.out.println("Area: " + r2.getArea());
31 | System.out.println("Perimeter: " + r2.getPerimeter());
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/BAI TAP/ws6/Exchange_money/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Scanner;
10 | import java.util.StringTokenizer;
11 |
12 | /**
13 | *
14 | * @author GIA KINH
15 | */
16 | public class Main {
17 |
18 | public static void main(String[] args) {
19 | Scanner scanner = new Scanner(System.in);
20 | System.out.print("Enter the first currency code: ");
21 | String code1 = scanner.nextLine();
22 | System.out.print("Enter the second currency code: ");
23 | String code2 = scanner.nextLine();
24 | System.out.print("Enter the amount of first currency: ");
25 | double amount = scanner.nextDouble();
26 | Rate rate = new Rate(code1, code2, amount);
27 | Exchange exchange = new Exchange();
28 | System.out.println("The amount after converted: " + exchange.convert(rate.getCODE1(), rate.getCODE2(), rate.getRate()));
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Animal/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Animal;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 | public static void main(String[] args) {
14 | Cat c1 = new Cat("Niko");
15 | c1.greets();
16 | Dog d1 = new Dog("Rubi");
17 | d1.greets();
18 | BigDog bD1 = new BigDog("Laiso");
19 |
20 | Animal a1 = new Cat("Niko");
21 | a1.greets();
22 | Animal a2 = new Dog("Top");
23 | a2.greets();
24 | Animal a3 = new BigDog("Ky");
25 | a3.greets();
26 |
27 | Dog d2 = (Dog) a2;
28 | BigDog bD2 = (BigDog) a3;
29 | Dog d3 = (Dog) a3;
30 |
31 | d2.greets(d3);
32 | d3.greets(d2);
33 |
34 | d2.greets(bD2);
35 | bD2.greets(d2);
36 |
37 | bD2.greets(bD1);
38 | bD1.greets(bD2);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/GeometricObject/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package GeometricObject;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Circle implements GeometricObject{
13 |
14 | private double radius;
15 |
16 | public Circle(double radius) {
17 | this.radius = radius;
18 | }
19 |
20 | public Circle() {
21 | }
22 |
23 | public double getRadius() {
24 | return radius;
25 | }
26 |
27 | public void setRadius(double radius) {
28 | this.radius = radius;
29 | }
30 |
31 | @Override
32 | public String toString() {
33 | return "Circle[" + "radius=" + radius + ']';
34 | }
35 |
36 |
37 |
38 | @Override
39 | public double getArea() {
40 | return radius*radius*Math.PI;
41 | }
42 |
43 | @Override
44 | public double getPerimeter() {
45 | return radius*2*Math.PI;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Point3D/Point2D.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Point3D;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Point2D {
13 | private float x;
14 | private float y;
15 |
16 | public Point2D() {
17 | }
18 |
19 | public Point2D(float x, float y) {
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | public float getX() {
25 | return x;
26 | }
27 |
28 | public void setX(float x) {
29 | this.x = x;
30 | }
31 |
32 | public float getY() {
33 | return y;
34 | }
35 |
36 | public void setY(float y) {
37 | this.y = y;
38 | }
39 |
40 | public void setXY(float x, float y){
41 | this.x = x;
42 | this.y = y;
43 | }
44 |
45 | public float[] getXY(){
46 | return new float[]{this.x,this.y};
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "(" + x + "," + y + ')';
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/MovablePoint/Point.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovablePoint;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Point {
13 | private float x;
14 | private float y;
15 |
16 | public Point() {
17 | }
18 |
19 | public Point(float x, float y) {
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | public float getX() {
25 | return x;
26 | }
27 |
28 | public void setX(float x) {
29 | this.x = x;
30 | }
31 |
32 | public float getY() {
33 | return y;
34 | }
35 |
36 | public void setY(float y) {
37 | this.y = y;
38 | }
39 |
40 | public void setXY(float x, float y){
41 | this.x = x;
42 | this.y = y;
43 | }
44 |
45 | public float[] getXY(){
46 | return new float[]{this.x,this.y};
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "(" + x + "," + y + ')';
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Resizable/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Resizable;
7 |
8 | import GeometricObject.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Circle implements GeometricObject{
15 |
16 | protected double radius;
17 |
18 | public Circle(double radius) {
19 | this.radius = radius;
20 | }
21 |
22 | public Circle() {
23 | }
24 |
25 | public double getRadius() {
26 | return radius;
27 | }
28 |
29 | public void setRadius(double radius) {
30 | this.radius = radius;
31 | }
32 |
33 | @Override
34 | public String toString() {
35 | return "Circle[" + "radius=" + radius + ']';
36 | }
37 |
38 |
39 |
40 | @Override
41 | public double getArea() {
42 | return radius*radius*Math.PI;
43 | }
44 |
45 | @Override
46 | public double getPerimeter() {
47 | return radius*2*Math.PI;
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Point3D/Point3D.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Point3D;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Arrays;
10 |
11 | /**
12 | *
13 | * @author GIA KINH
14 | */
15 | public class Point3D extends Point2D{
16 | private float z;
17 |
18 | public Point3D() {
19 | }
20 |
21 | public Point3D(float x, float y, float z) {
22 | super(x, y);
23 | this.z = z;
24 | }
25 |
26 | public float getZ() {
27 | return z;
28 | }
29 |
30 | public void setZ(float z) {
31 | this.z = z;
32 | }
33 |
34 | public void setXYZ(float x, float y, float z){
35 | this.setXY(x, y);
36 | this.z=z;
37 | }
38 |
39 | public float[] getXYZ(){
40 | float[] list = new float[]{this.getX(),this.getY(),this.z};
41 | return list;
42 | }
43 |
44 | @Override
45 | public String toString() {
46 | return "(" + this.getX() + "," + this.getY() + "," + z + '}';
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Shape/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Circle extends Shape{
13 | private double radius=1.0;
14 |
15 | public Circle() {
16 | }
17 |
18 | public Circle(double radius) {
19 | this.radius = radius;
20 | }
21 |
22 | public Circle(double radius, String color, boolean filled) {
23 | super(color, filled);
24 | this.radius = radius;
25 | }
26 |
27 | public double getArea(){
28 | return this.radius*this.radius*Math.PI;
29 | }
30 |
31 | public double getPerimeter(){
32 | return this.radius*2*Math.PI;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "Circle["+ super.toString() + ",radius=" + radius + ']';
38 | }
39 |
40 | public void setRadius(int radius) {
41 | this.radius = radius;//To change body of generated methods, choose Tools | Templates.
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop2/Part2_2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop2;
7 |
8 | import java.util.Scanner;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | *
13 | * @author giaki
14 | */
15 | public class Part2_2 {
16 |
17 | public String inputString() throws Exception {
18 | Scanner scanner = new Scanner(System.in);
19 | System.out.print("Enter the string: ");
20 | String str = scanner.nextLine();
21 | if (!Pattern.matches("SE\\d+", str)) {
22 | throw new Exception();
23 | }
24 | return str;
25 | }
26 |
27 | public static void main(String[] args) {
28 | do {
29 | try {
30 | Part2_2 part2 = new Part2_2();
31 | String str = part2.inputString();
32 | System.out.println("The string is " + str);
33 | break;
34 | } catch (Exception e) {
35 | System.out.println("The string is invaild");
36 | }
37 | } while (true);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/BAI TAP/workshop6/Menu.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package workshop6;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Scanner;
10 |
11 | /**
12 | *
13 | * @author giaki
14 | */
15 | public class Menu {
16 | public static int getChoice(ArrayList options){
17 | int i=1;
18 | for (String option : options) {
19 | System.out.println(i+"-"+option);
20 | i++;
21 | }
22 | System.out.print("Choose 1.." + options.size()+": ");
23 | Scanner scanner = new Scanner(System.in);
24 | return Integer.parseInt(scanner.nextLine());
25 | }
26 |
27 | public static int getChoice(Object[] options){
28 | int i=1;
29 | for (Object option : options) {
30 | System.out.println(i+"-"+option.toString());
31 | i++;
32 | }
33 | System.out.print("Choose 1.." + options.length+": ");
34 | Scanner scanner = new Scanner(System.in);
35 | return Integer.parseInt(scanner.nextLine());
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Shape/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Circle extends Shape{
13 | protected double radius=1.0;
14 |
15 | public Circle() {
16 | }
17 |
18 | public Circle(double radius) {
19 | this.radius = radius;
20 | }
21 |
22 | public Circle(double radius, String color, boolean filled) {
23 | super(color, filled);
24 | this.radius = radius;
25 | }
26 |
27 | public double getArea(){
28 | return this.radius*this.radius*Math.PI;
29 | }
30 |
31 | public double getPerimeter(){
32 | return this.radius*2*Math.PI;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "Circle["+ super.toString() + ",radius=" + radius + ']';
38 | }
39 |
40 | public void setRadius(int radius) {
41 | this.radius = radius;//To change body of generated methods, choose Tools | Templates.
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Shape/Shape.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public abstract class Shape {
13 |
14 | protected String color = "red";
15 | protected boolean filled = true;
16 |
17 | public Shape() {
18 | }
19 |
20 | public Shape(String color, boolean filled) {
21 | this.color = color;
22 | this.filled = filled;
23 | }
24 |
25 | public String getColor() {
26 | return color;
27 | }
28 |
29 | public void setColor(String color) {
30 | this.color = color;
31 | }
32 |
33 | public boolean isFilled() {
34 | return filled;
35 | }
36 |
37 | public void setFilled(boolean filled) {
38 | this.filled = filled;
39 | }
40 |
41 | public abstract double getArea();
42 |
43 | public abstract double getPerimeter();
44 |
45 |
46 | @Override
47 | public String toString() {
48 | return "Shape[" + "color=" + color + ",filled=" + filled + ']';
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Cylinder/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Cylinder;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Circle {
13 | private double radius=1.0;
14 | private String color="red";
15 |
16 | public Circle() {
17 | }
18 |
19 | public Circle(double radius) {
20 | this.radius = radius;
21 | }
22 |
23 | public Circle(double radius, String color) {
24 | this.radius = radius;
25 | this.color = color;
26 | }
27 |
28 | public double getRadius() {
29 | return radius;
30 | }
31 |
32 | public void setRadius(double radius) {
33 | this.radius = radius;
34 | }
35 |
36 | public String getColor() {
37 | return color;
38 | }
39 |
40 | public void setColor(String color) {
41 | this.color = color;
42 | }
43 |
44 | public double getArea(){
45 | return this.radius*this.radius*Math.PI;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return "Circle[" + "radius=" + radius + ",color=" + color + ']';
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/5. Exercises on Composition vs Inheritance/Cylinder/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Cylinder;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Circle {
13 | private double radius=1.0;
14 | private String color="red";
15 |
16 | public Circle() {
17 | }
18 |
19 | public Circle(double radius) {
20 | this.radius = radius;
21 | }
22 |
23 | public Circle(double radius, String color) {
24 | this.radius = radius;
25 | this.color = color;
26 | }
27 |
28 | public double getRadius() {
29 | return radius;
30 | }
31 |
32 | public void setRadius(double radius) {
33 | this.radius = radius;
34 | }
35 |
36 | public String getColor() {
37 | return color;
38 | }
39 |
40 | public void setColor(String color) {
41 | this.color = color;
42 | }
43 |
44 | public double getArea(){
45 | return this.radius*this.radius*Math.PI;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return "Circle[" + "radius=" + radius + ",color=" + color + ']';
51 | }
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/BAI TAP/Exchange_money/Rate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Rate {
13 | private String CODE1;
14 | private String CODE2;
15 | private double rate;
16 |
17 | public Rate(String CODE1, String CODE2, double rate) {
18 | this.CODE1 = CODE1;
19 | this.CODE2 = CODE2;
20 | this.rate = rate;
21 | }
22 |
23 | public Rate() {
24 | }
25 |
26 | public String getCODE1() {
27 | return CODE1;
28 | }
29 |
30 | public void setCODE1(String CODE1) {
31 | this.CODE1 = CODE1;
32 | }
33 |
34 | public String getCODE2() {
35 | return CODE2;
36 | }
37 |
38 | public void setCODE2(String CODE2) {
39 | this.CODE2 = CODE2;
40 | }
41 |
42 | public double getRate() {
43 | return rate;
44 | }
45 |
46 | public void setRate(double rate) {
47 | this.rate = rate;
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "Rate{" + "CODE1=" + CODE1 + ", CODE2=" + CODE2 + ", rate=" + rate + '}';
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/GUI/Tester.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.GUI;
7 |
8 | import Workshop5.OrganizationManager.DTO.BeeColony;
9 | import Workshop5.OrganizationManager.DTO.Colony;
10 | import Workshop5.OrganizationManager.DTO.FPTUniversity;
11 | import Workshop5.OrganizationManager.DTO.Role;
12 | import Workshop5.OrganizationManager.DTO.University;
13 |
14 | /**
15 | *
16 | * @author giaki
17 | */
18 | public class Tester {
19 |
20 | public static void main(String[] args) {
21 | Colony obj1 = new BeeColony(2000, "honey", "land");
22 | System.out.println(obj1);
23 | obj1.grow();
24 | obj1.reproduce();
25 |
26 | University obj2 = new FPTUniversity(100000, "FPT", "Cantho");
27 | System.out.println(obj2);
28 | obj2.enroll();
29 | obj2.educate();
30 |
31 | Role df = new BeeColony(3000, "wasp", "land");
32 | System.out.println(df);
33 | df.createWorker();
34 |
35 | df = new FPTUniversity(100000, "FPT", "Hanoi");
36 | System.out.println(df);
37 | df.createWorker();
38 |
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/FPTUniversity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class FPTUniversity extends University implements Role{
13 |
14 | private String address;
15 |
16 | public FPTUniversity(String address) {
17 | this.address = address;
18 | }
19 |
20 | public FPTUniversity(String name,String address) {
21 | super(name);
22 | this.address = address;
23 | }
24 |
25 | public FPTUniversity(int size, String name,String address) {
26 | super(size, name);
27 | this.address = address;
28 | }
29 |
30 | public String getAddress() {
31 | return address;
32 | }
33 |
34 | public void setAddress(String address) {
35 | this.address = address;
36 | }
37 |
38 | @Override
39 | public void createWorker() {
40 | System.out.println("Providing human resources");
41 | }
42 |
43 | @Override
44 | public String toString() {
45 | return "FPTU has four campuses Hanoi, HCM, DaNang, CanTho, QuyNhon";
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/BAI TAP/ws6/Exchange_money/Rate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Rate {
13 | private String CODE1;
14 | private String CODE2;
15 | private double rate;
16 |
17 | public Rate(String CODE1, String CODE2, double rate) {
18 | this.CODE1 = CODE1;
19 | this.CODE2 = CODE2;
20 | this.rate = rate;
21 | }
22 |
23 | public Rate() {
24 | }
25 |
26 | public String getCODE1() {
27 | return CODE1;
28 | }
29 |
30 | public void setCODE1(String CODE1) {
31 | this.CODE1 = CODE1;
32 | }
33 |
34 | public String getCODE2() {
35 | return CODE2;
36 | }
37 |
38 | public void setCODE2(String CODE2) {
39 | this.CODE2 = CODE2;
40 | }
41 |
42 | public double getRate() {
43 | return rate;
44 | }
45 |
46 | public void setRate(double rate) {
47 | this.rate = rate;
48 | }
49 |
50 | @Override
51 | public String toString() {
52 | return "Rate{" + "CODE1=" + CODE1 + ", CODE2=" + CODE2 + ", rate=" + rate + '}';
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/BeeColony.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class BeeColony extends Colony implements Role{
13 |
14 | private String type;
15 |
16 | public BeeColony() {
17 | }
18 |
19 | public BeeColony(String type) {
20 | this.type = type;
21 | }
22 |
23 | public BeeColony(String place,String type) {
24 | super(place);
25 | this.type = type;
26 | }
27 |
28 | public BeeColony(int size,String place, String type) {
29 | super(size,place);
30 | this.type = type;
31 | }
32 |
33 | public String getType() {
34 | return type;
35 | }
36 |
37 | public void setType(String type) {
38 | this.type = type;
39 | }
40 |
41 | @Override
42 | public void createWorker() {
43 | System.out.println("Worker bees perform all the work of the bees");
44 | }
45 |
46 | @Override
47 | public String toString() {
48 | return "The colony’s type is " + type + ", size is about "+super.getSize()+", and the place is "+super.getPlace();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Cylinder/Cylinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Cylinder;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Cylinder extends Circle {
13 |
14 | private double height = 1.0;
15 |
16 | public Cylinder() {
17 | }
18 |
19 | public Cylinder(double radius) {
20 | super(radius);
21 | }
22 |
23 | public Cylinder(double radius, String color) {
24 | super(radius, color);
25 | }
26 |
27 | public Cylinder(double radius, double height) {
28 | super(radius);
29 | this.height = height;
30 | }
31 |
32 | public Cylinder(double radius, double height, String color) {
33 | super(radius, color);
34 | this.height = height;
35 | }
36 |
37 | public double getHeight() {
38 | return height;
39 | }
40 |
41 | public void setHeight(double height) {
42 | this.height = height;
43 | }
44 |
45 | public double getVolume() {
46 | return this.getArea() * this.height;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "Cylinder: subclass of " + super.toString()
52 | + " height=" + height;
53 | }
54 | }
55 |
56 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/PhoneNumber/PhoneNumber.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package PhoneNumber;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class PhoneNumber {
13 | protected int area;
14 | protected String number;
15 |
16 | public PhoneNumber(int area, String number) {
17 | this.area = area;
18 | this.number = number;
19 | }
20 |
21 | public PhoneNumber() {
22 | }
23 |
24 | public void display(){
25 | if (this.area!=0) {
26 | System.out.print(this.area + "-");
27 | }
28 | if(!this.number.isEmpty()){
29 | System.out.print(this.number);
30 | }
31 | System.out.println();
32 | }
33 |
34 | public int getArea() {
35 | return area;
36 | }
37 |
38 | public void setArea(int area) {
39 | this.area = area;
40 | }
41 |
42 | public String getNumber() {
43 | return number;
44 | }
45 |
46 | public void setNumber(String number) {
47 | this.number = number;
48 | }
49 |
50 |
51 |
52 | @Override
53 | public String toString() {
54 | return "PhoneNumber{" + "area=" + area + ", number=" + number + '}';
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/GeometricObject/Rectangle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package GeometricObject;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Rectangle implements GeometricObject{
13 | private double width;
14 | private double length;
15 |
16 | public Rectangle() {
17 | }
18 |
19 | public Rectangle(double width, double length) {
20 | this.width = width;
21 | this.length = length;
22 | }
23 |
24 | public double getWidth() {
25 | return width;
26 | }
27 |
28 | public void setWidth(double width) {
29 | this.width = width;
30 | }
31 |
32 | public double getLength() {
33 | return length;
34 | }
35 |
36 | public void setLength(double length) {
37 | this.length = length;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "Rectangle[" + "width=" + width + ",length=" + length + ']';
43 | }
44 |
45 | @Override
46 | public double getArea() {
47 | return length*width;
48 | }
49 |
50 | @Override
51 | public double getPerimeter() {
52 | return (width+length)*2;
53 | }
54 |
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/Colony.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class Colony extends Organization{
13 | private String place;
14 |
15 | public Colony() {
16 | }
17 |
18 | public Colony(String place) {
19 | this.place = place;
20 | }
21 |
22 | public Colony(int size, String place) {
23 | super(size);
24 | this.place = place;
25 | }
26 |
27 | public String getPlace() {
28 | return place;
29 | }
30 |
31 | public void setPlace(String place) {
32 | this.place = place;
33 | }
34 |
35 | public void grow(){
36 | System.out.println("An annual cycle of growth that begins in spring");
37 | }
38 |
39 | public void reproduce(){
40 | System.out.println("Colony can reproduce itself through a process");
41 | }
42 | @Override
43 | public void communicateByTool() {
44 | System.out.println("The colony communicate by sound");
45 | }
46 |
47 | @Override
48 | public String toString() {
49 | return "The colony size is " + super.getSize() + ", the colony’s place is " + place;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/BAI TAP/Exchange_money/Exchange.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | import java.util.ArrayList;
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | *
13 | * @author GIA KINH
14 | */
15 | public class Exchange extends ArrayList{
16 | public Exchange() {
17 | this.add("USD;VND;17000");
18 | this.add("EUR;USD;1.2");
19 | this.add("USD;IDN;15789");
20 | this.add("USD;EUR;0.83");
21 | this.add("CAD;IDN;16869");
22 | }
23 |
24 | public double convert(Rate rate) {
25 | for (String rateString : this) {
26 | StringTokenizer tokenizer = new StringTokenizer(rateString, ";");
27 | String tokenizer_code1 = tokenizer.nextToken();
28 | String tokenizer_code2 = tokenizer.nextToken();
29 | Double tokenizer_amount = Double.parseDouble(tokenizer.nextToken());
30 | if (rate.getCODE1().toLowerCase().equals(tokenizer_code1.toLowerCase()) && rate.getCODE2().toLowerCase().equals(tokenizer_code2.toLowerCase())) {
31 | return tokenizer_amount * rate.getRate();
32 | }
33 | }
34 | System.out.println("Can not found code!!!!");
35 | return rate.getRate();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/BAI TAP/WordCount/WordCount.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package WordCount;
7 |
8 | import java.util.Enumeration;
9 | import java.util.HashSet;
10 | import java.util.Hashtable;
11 | import java.util.Scanner;
12 | import java.util.StringTokenizer;
13 |
14 | /**
15 | *
16 | * @author giaki
17 | */
18 | public class WordCount {
19 | public static void main(String[] args) {
20 | Scanner scanner = new Scanner(System.in);
21 | System.out.print("Enter a string: ");
22 | String str = scanner.nextLine().trim();
23 | StringTokenizer token = new StringTokenizer(str," ");
24 | Hashtable hashWords = new Hashtable(100,0.2f);
25 | while(token.hasMoreTokens()){
26 | String word = token.nextToken().toLowerCase();
27 | hashWords.put(word, hashWords.containsKey(word.toLowerCase())? hashWords.get(word)+1 :1);
28 | }
29 | // Enumeration enumeration = hashWords.keys();
30 | // while (enumeration.hasMoreElements()) {
31 | // String key = enumeration.nextElement();
32 | // System.out.println(key + " " + hashWords.get(key));
33 | // }
34 | hashWords.forEach((key,value)-> System.out.println(key + " "+ value));
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop1/Part1.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop1;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author giaki
13 | */
14 | public class Part1 {
15 |
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | System.out.print("Enter number of rows: ");
19 | int rows = scanner.nextInt();
20 | System.out.print("Enter number of cols: ");
21 | int cols = scanner.nextInt();
22 | int matrix[][] = new int[rows][cols];
23 | System.out.println("Enter the matrix");
24 | for (int i = 0; i < rows; i++) {
25 | for (int j = 0; j < cols; j++) {
26 | System.out.print("matrix[" + i + "][" + j + "]= ");
27 | matrix[i][j] = scanner.nextInt();
28 | }
29 | }
30 | System.out.println("Matrix inputted: ");
31 | int sum=0;
32 | for (int i = 0; i < rows; i++) {
33 | for (int j = 0; j < cols; j++) {
34 | System.out.print(matrix[i][j] + " ");
35 | sum+=matrix[i][j];
36 | }
37 | System.out.println();
38 | }
39 | System.out.println("Sum: "+sum);
40 | System.out.println("Average: " +(float)sum/(rows*cols));
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop5/OrganizationManager/DTO/University.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop5.OrganizationManager.DTO;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class University extends Organization{
13 | private String name;
14 |
15 | public University() {
16 | }
17 |
18 | public University(String name) {
19 | this.name = name;
20 | }
21 |
22 | public University(int size, String name) {
23 | super(size);
24 | this.name = name;
25 | }
26 |
27 | public String getName() {
28 | return name;
29 | }
30 |
31 | public void setName(String name) {
32 | this.name = name;
33 | }
34 |
35 | public void enroll(){
36 | System.out.println("The registration for enrollment is only valid when the University has received all enrollment documents and enrollment fees");
37 | }
38 |
39 | public void educate(){
40 | System.out.println("Provide education at university standard");
41 | }
42 |
43 | @Override
44 | public void communicateByTool() {
45 | System.out.println("In the university, people communicate by voice");
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return "Encourage the advancement and development of knowledge";
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/BAI TAP/player/PlayerIncome.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class PlayerIncome extends Player implements Tax{
13 |
14 | private double income;
15 |
16 | public PlayerIncome(double income) {
17 | this.income = income;
18 | }
19 |
20 | public PlayerIncome(double income, String name) {
21 | super(name);
22 | this.income = income;
23 | }
24 |
25 | public PlayerIncome() {
26 | }
27 |
28 | public double getIncome() {
29 | return income;
30 | }
31 |
32 | public void setIncome(double income) {
33 | this.income = income;
34 | }
35 |
36 |
37 |
38 |
39 | @Override
40 | public void displayDetails() {
41 | System.out.println("Name player: "+super.getName());
42 | System.out.println("Salary: " + (this.income - this.calculateTax())+ "$");
43 | System.out.println("Tax: " + this.calculateTax()+"$");
44 | }
45 |
46 | @Override
47 | public double calculateTax() {
48 | if(this.income<=10000){
49 | return this.income*15/100;
50 | }else if(this.income <=20000){
51 | return this.income * 20 / 100;
52 | }else{
53 | return this.income * 30 / 100;
54 | }
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Subclasses/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Subclasses;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestMain {
13 |
14 | public static void main(String[] args) {
15 | Student s1 = new Student("PRO192", 2021, 300, "Ha Gia Kinh", "Ha Noi");
16 | System.out.println(s1);
17 | s1.setAddress("Quang Binh");
18 | System.out.println(s1);
19 | s1.setFee(300);
20 | s1.setProgram("MAD101");
21 | s1.setYear(2022);
22 | System.out.println("Name is " + s1.getName());
23 | System.out.println("Adress is " + s1.getAddress());
24 | System.out.println("Fee is " + s1.getFee());
25 | System.out.println("Program is " + s1.getProgram());
26 | System.out.println("Year is " + s1.getYear());
27 |
28 | Staff sf1 = new Staff("Dieu Linh", "Viet Nam", "Ba Vi", 250);
29 | System.out.println(sf1);
30 |
31 | sf1.setAddress("Japan");
32 | System.out.println(sf1);
33 | sf1.setPay(300);
34 | sf1.setSchool("Tokyo Universe");
35 | System.out.println("Name is " + sf1.getName());
36 | System.out.println("Adress is " + sf1.getAddress());
37 | System.out.println("Pay is " + sf1.getPay());
38 | System.out.println("School is " + sf1.getSchool());
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Shape/Rectangle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Rectangle extends Shape{
13 | private double width;
14 | private double length;
15 |
16 | public Rectangle() {
17 | }
18 |
19 | public Rectangle(double width, double length) {
20 | this.width = width;
21 | this.length = length;
22 | }
23 |
24 | public Rectangle(double width, double length, String color, boolean filled) {
25 | super(color, filled);
26 | this.width = width;
27 | this.length = length;
28 | }
29 |
30 | public double getWidth() {
31 | return width;
32 | }
33 |
34 | public void setWidth(double width) {
35 | this.width = width;
36 | }
37 |
38 | public double getLength() {
39 | return length;
40 | }
41 |
42 | public void setLength(double length) {
43 | this.length = length;
44 | }
45 |
46 | public double getArea(){
47 | return this.length*this.width;
48 | }
49 |
50 | public double getPerimeter(){
51 | return (this.length+this.width)*2;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "Rectangle["+ super.toString() + ",width=" + width + ", length=" + length + ']';
57 | }
58 |
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/BAI TAP/ws6/Exchange_money/Exchange.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Exchange_money;
7 |
8 | import java.util.ArrayList;
9 | import java.util.StringTokenizer;
10 |
11 | /**
12 | *
13 | * @author GIA KINH
14 | */
15 | public class Exchange {
16 |
17 | public static ArrayList listRates = new ArrayList();
18 |
19 | public static void createRete() {
20 | listRates.add("USD;VND;17000");
21 | listRates.add("EUR;USD;1.2");
22 | listRates.add("USD;IDN;15789");
23 | listRates.add("USD;EUR;0.83");
24 | listRates.add("CAD;IDN;16869");
25 | }
26 |
27 | public double convert(String code1, String code2, double amount) {
28 | createRete();
29 | for (String rateString : listRates) {
30 | StringTokenizer tokenizer = new StringTokenizer(rateString, ";");
31 | String tokenizer_code1 = tokenizer.nextToken();
32 | String tokenizer_code2 = tokenizer.nextToken();
33 | Double tokenizer_amount = Double.parseDouble(tokenizer.nextToken());
34 | if (code1.toLowerCase().equals(tokenizer_code1.toLowerCase()) && code2.toLowerCase().equals(tokenizer_code2.toLowerCase())) {
35 | return tokenizer_amount * amount;
36 | }
37 | }
38 | System.out.println("Can not found code!!!!");
39 | return amount;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/AnimalAbstract/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package AnimalAbstract;
7 |
8 | import Animal.*;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class TestMain {
15 |
16 | public static void main(String[] args) {
17 | // Using the subclasses
18 | Cat cat1 = new Cat();
19 | cat1.greeting();
20 | Dog dog1 = new Dog();
21 | dog1.greeting();
22 | BigDog bigDog1 = new BigDog();
23 | bigDog1.greeting();
24 |
25 | // Using Polymorphism
26 | Animal animal1 = new Cat();
27 | animal1.greeting();
28 | Animal animal2 = new Dog();
29 | animal2.greeting();
30 | Animal animal3 = new BigDog();
31 | animal3.greeting();
32 | // Animal animal4 = new Animal(); // Error!!! Animal is abstract; cannot be instantiated !
33 |
34 | // Downcast
35 | Dog dog2 = (Dog)animal2;
36 | BigDog bigDog2 = (BigDog)animal3;
37 | Dog dog3 = (Dog)animal3;
38 | // Cat cat2 = (Cat)animal2; // Error!!! Dog cannot be cast to Cat !
39 |
40 | dog2.greeting(dog3);
41 | dog3.greeting(dog2);
42 | dog2.greeting(bigDog2);
43 | bigDog2.greeting(dog2);
44 | bigDog2.greeting(bigDog1);
45 |
46 | bigDog1.greeting(bigDog2);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Cylinder/TestCylinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Cylinder;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TestCylinder {
13 | public static void main(String[] args) {
14 | Cylinder c1 = new Cylinder();
15 | System.out.println("Cylinder:"
16 | + " radius=" + c1.getRadius()
17 | + " height=" + c1.getHeight()
18 | + " base area=" + c1.getArea()
19 | + " volume=" + c1.getVolume());
20 |
21 | // Declare and allocate a new instance of cylinder
22 | // specifying height, with default color and radius
23 | Cylinder c2 = new Cylinder(10.0);
24 | System.out.println("Cylinder:"
25 | + " radius=" + c2.getRadius()
26 | + " height=" + c2.getHeight()
27 | + " base area=" + c2.getArea()
28 | + " volume=" + c2.getVolume());
29 |
30 | // Declare and allocate a new instance of cylinder
31 | // specifying radius and height, with default color
32 | Cylinder c3 = new Cylinder(2.0, 10.0);
33 | System.out.println("Cylinder:"
34 | + " radius=" + c3.getRadius()
35 | + " height=" + c3.getHeight()
36 | + " base area=" + c3.getArea()
37 | + " volume=" + c3.getVolume());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/Shape/Rectangle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Shape;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Rectangle extends Shape{
13 | protected double width;
14 | protected double length;
15 |
16 | public Rectangle() {
17 | }
18 |
19 | public Rectangle(double width, double length) {
20 | this.width = width;
21 | this.length = length;
22 | }
23 |
24 | public Rectangle(double width, double length, String color, boolean filled) {
25 | super(color, filled);
26 | this.width = width;
27 | this.length = length;
28 | }
29 |
30 | public double getWidth() {
31 | return width;
32 | }
33 |
34 | public void setWidth(double width) {
35 | this.width = width;
36 | }
37 |
38 | public double getLength() {
39 | return length;
40 | }
41 |
42 | public void setLength(double length) {
43 | this.length = length;
44 | }
45 |
46 | public double getArea(){
47 | return this.length*this.width;
48 | }
49 |
50 | public double getPerimeter(){
51 | return (this.length+this.width)*2;
52 | }
53 |
54 | @Override
55 | public String toString() {
56 | return "Rectangle["+ super.toString() + ",width=" + width + ", length=" + length + ']';
57 | }
58 |
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/BAI TAP/workshop6/Student.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package workshop6;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class Student implements Comparable{
13 | String code;
14 | String name;
15 | int mark;
16 |
17 | public Student() {
18 | code="";
19 | name="";
20 | mark=0;
21 | }
22 |
23 |
24 | public Student(String code, String name, int mark) {
25 | this.code = code.toUpperCase();
26 | this.name = name.toUpperCase();
27 | this.mark = (mark>=0 && mark<=10)? mark : 0;
28 | }
29 |
30 | public String getCode() {
31 | return code;
32 | }
33 |
34 | public void setCode(String code) {
35 | this.code = code;
36 | }
37 |
38 | public String getName() {
39 | return name;
40 | }
41 |
42 | public void setName(String name) {
43 | if(name.length()>0){
44 | this.name = name.trim().toUpperCase();
45 | }
46 | }
47 |
48 | public int getMark() {
49 | return mark;
50 | }
51 |
52 | public void setMark(int mark) {
53 | this.mark = (mark>=0 && mark<=10)? mark : 0;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return code +", "+name+", "+mark;
59 | }
60 |
61 | @Override
62 | public int compareTo(Student t) {
63 | return -(this.getMark() - t.getMark());
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/4. Exercises on Inheritance/Subclasses/Student.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Subclasses;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Student extends Person{
13 | private String program;
14 | private int year;
15 | private double fee;
16 |
17 | public Student(String name, String address, String program, int year, double fee) {
18 | super(name, address);
19 | this.program = program;
20 | this.year = year;
21 | this.fee = fee;
22 | }
23 |
24 | public Student(String program, int year, double fee, String name, String address) {
25 | super(name, address);
26 | this.program = program;
27 | this.year = year;
28 | this.fee = fee;
29 | }
30 |
31 |
32 | public String getProgram() {
33 | return program;
34 | }
35 |
36 | public void setProgram(String program) {
37 | this.program = program;
38 | }
39 |
40 | public int getYear() {
41 | return year;
42 | }
43 |
44 | public void setYear(int year) {
45 | this.year = year;
46 | }
47 |
48 | public double getFee() {
49 | return fee;
50 | }
51 |
52 | public void setFee(double fee) {
53 | this.fee = fee;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return "Student[" + super.toString() + ",program=" + program + ",year=" + year + ",fee=" + fee + ']';
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop1/Part2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop1;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author giaki
13 | */
14 | public class Part2 {
15 |
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | System.out.print("Input number 1: ");
19 | double number1 = scanner.nextDouble();
20 | System.out.print("Input number 2: ");
21 | double number2 = scanner.nextDouble();
22 | scanner.nextLine();
23 | System.out.print("Enter the operator(+-*/): ");
24 | char op = scanner.nextLine().charAt(0);
25 | switch (op) {
26 | case '+':
27 | System.out.println("The result of: " + number1 + op + number2 + "=" + (number1 + number2));
28 | break;
29 | case '-':
30 | System.out.println("The result of: " + number1 + op + number2 + "=" + (number1 - number2));
31 | break;
32 | case '*':
33 | System.out.println("The result of: " + number1 + op + number2 + "=" + (number1 * number2));
34 | break;
35 | case '/':
36 | System.out.println("The result of: " + number1 + op + number2 + "=" + (number1 / number2));
37 | break;
38 | default:
39 | System.out.println("Please enter operator");
40 | break;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/BAI TAP/workshop6/Inputter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package workshop6;
7 |
8 | import java.util.Scanner;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | *
13 | * @author giaki
14 | */
15 | public class Inputter {
16 |
17 | public static Scanner scanner = new Scanner(System.in);
18 |
19 | public static int inputInt(String msg, int min, int max) {
20 | if (min > max) {
21 | int z = min;
22 | min = max;
23 | max = z;
24 | }
25 | int data;
26 | do {
27 | System.out.print(msg);
28 | data = Integer.parseInt(scanner.nextLine());
29 | } while (data < min || data > max);
30 | return data;
31 | }
32 |
33 | public static String inputStr(String msg) {
34 | System.out.print(msg);
35 | String data = scanner.nextLine().trim();
36 | return data;
37 | }
38 |
39 | public static String inputNonBlankStr(String msg) {
40 | String data = null;
41 | do {
42 | System.out.print(msg);
43 | data = scanner.nextLine().trim();
44 | } while (data.length() == 0 || data == null);
45 | return data;
46 | }
47 |
48 | public static String inputPattern(String msg, String pattern) {
49 | String data = null;
50 | do {
51 | System.out.print(msg);
52 | data = scanner.nextLine().trim();
53 | } while (!data.matches(pattern));
54 | return data;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Circle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | /**
8 | *
9 | * @author GIA KINH
10 | */
11 | public class Circle {
12 | private double radius;
13 |
14 | public Circle() {
15 | radius = 1.0;
16 | }
17 |
18 |
19 | public Circle(double radius) {
20 | this.radius = radius;
21 | }
22 |
23 | public double getRadius() {
24 | return radius;
25 | }
26 |
27 | public void setRadius(double radius) {
28 | this.radius = radius;
29 | }
30 |
31 | public double getArea() {
32 | return radius*radius*Math.PI;
33 | }
34 |
35 | public double getCircumference(){
36 | return radius*2*Math.PI;
37 | }
38 |
39 | @Override
40 | public String toString() {
41 | return "Circle[" + "radius=" + radius + ']';
42 | }
43 | }
44 |
45 | class TestCircle{
46 | public static void main(String[] args) {
47 | // Test Constructors and toString()
48 | Circle c1 = new Circle(1.1);
49 | System.out.println(c1); // toString()
50 | Circle c2 = new Circle(); // default constructor
51 | System.out.println(c2);
52 |
53 | // Test setter and getter
54 | c1.setRadius(2.2);
55 | System.out.println(c1); // toString()
56 | System.out.println("radius is: " + c1.getRadius());
57 |
58 | // Test getArea() and getCircumference()
59 | System.out.printf("area is: %.2f%n", c1.getArea());
60 | System.out.printf("circumference is: %.2f%n", c1.getCircumference());
61 | }
62 | }
63 |
64 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/Person/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Person;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class TestMain {
15 | public static void main(String[] args) {
16 | Person[] e = new Person[10];
17 | int n = 0, c = 0;
18 | do {
19 | System.out.println("Worker (1); Officer(2): ");
20 | Scanner in = new Scanner(System.in);
21 | c = in.nextInt();
22 | if (c == 1) {
23 | //accept information of worker
24 | System.out.print("Enter worker name: ");
25 | String name = in.next();
26 | System.out.print("Enter worker working hours: ");
27 | int hrs = in.nextInt();
28 | e[n] = new Worker(name, hrs);
29 | n++;
30 | } else if (c == 2) {
31 | //accept information of Officer
32 | System.out.print("Enter Officer name: ");
33 | String name = in.next();
34 | System.out.print("Enter officer salary: ");
35 | double salary = in.nextDouble();
36 | e[n] = new Officer(name, salary);
37 | n++;
38 | }
39 | } while (c != 0);
40 | //print all objects of e
41 | for (int i = 0; i listGuitars = new ArrayList();
21 | Random random = new Random();
22 | for (int i = 0; i < 10; i++) {
23 | Guitar guitar = new Guitar("G12" + random.nextInt(10 + 1), random.nextInt((9000 - 1000) + 1) + 1000, "Sony" + i, "Mode" + i, "hardWood" + i, "softWood" + i);
24 | listGuitars.add(guitar);
25 | }
26 | Collections.sort(listGuitars, new Comparator() {
27 | @Override
28 | public int compare(Guitar t, Guitar t1) {
29 | return t.getSerialNumber().compareTo(t1.getSerialNumber());
30 | }
31 | });
32 | for (Guitar guitar : listGuitars) {
33 | System.out.println(guitar.toString());
34 | }
35 |
36 | Guitar obj1 = new Guitar();
37 | Guitar obj2 = new Guitar("G123", 2000, "Sony", "Model123", "hardWood", "softWood");
38 | System.out.println("State of obj1:");
39 | obj1.createSound();
40 | System.out.println("State of obj2:");
41 | obj2.createSound();
42 |
43 | System.out.println("set price = 3000 of obj1");
44 | obj1.setPrice(3000);
45 | System.out.println("get price of obj1:" + obj1.getPrice());
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/BAI TAP/workshop6/StudentManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package workshop6;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class StudentManager {
13 | public static void main(String[] args) {
14 | String[] options= {"Adđ new student", "Search a student", "Update name and mark", "Remove a student", "List all","Sort All Student by mark (descending) and display using Compable","Sort All Student by mark (Ascending), if mark equals sort by Name and display using Comprator", "Quit"};
15 | int choice = 0;
16 | StudentList list = new StudentList();
17 | do{
18 | System.out.println("Student managing program.");
19 | choice = Menu.getChoice(options);
20 | switch(choice){
21 | case 1:
22 | list.addStudent();
23 | break;
24 | case 2:
25 | list.searchStudent();
26 | break;
27 | case 3:
28 | list.updateStudent();
29 | break;
30 | case 4:
31 | list.removeStudent();
32 | break;
33 | case 5:
34 | list.printAll();
35 | break;
36 | case 6:
37 | list.sortByMarkDecresing();
38 | break;
39 | case 7:
40 | list.sortByMarkInCresing();
41 | break;
42 | default:
43 | System.out.println("Bye");
44 | break;
45 | }
46 | }while(choice>0 && choice listStudents = new ArrayList();
45 | System.out.print("Enter number student: ");
46 | int n=scanner.nextInt();
47 | scanner.nextLine();
48 | for(int i=0 ; i listwords = new ArrayList();
24 |
25 | public static void readDataFormFile() {
26 | File file = new File("D:\\FPT UNIVERSITY\\STUDY\\KY 2\\PRO\\BIN\\STRING WORK SHOP\\String_workshop\\src\\Dictionary\\en-vi.txt");
27 | try {
28 | if (file.exists()) {
29 | Scanner scanner = new Scanner(file);
30 | while(scanner.hasNextLine()){
31 | String data = scanner.nextLine();
32 | listwords.add(data.trim().replaceAll("\\s+", " "));
33 | }
34 | }else{
35 | try {
36 | file.createNewFile();
37 | } catch (IOException ex) {
38 | System.out.println("Can not create file");
39 | }
40 | }
41 | } catch (FileNotFoundException ex) {
42 | System.out.println("File data not found");
43 | }
44 | }
45 |
46 | public Dictionary() {
47 | readDataFormFile();
48 | }
49 |
50 |
51 | public String lookup(String word) {
52 | for (String wordString : listwords) {
53 | StringTokenizer tokenizer = new StringTokenizer(wordString, ":");
54 | String english = tokenizer.nextToken().toLowerCase().trim();
55 | String vietname = tokenizer.nextToken().toLowerCase().trim();
56 | if (word.toLowerCase().equals(english)) {
57 | return vietname;
58 | }
59 | }
60 | return "not found";
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/src/GUI/AntiqueShop.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package GUI;
7 |
8 | import DTO.Item;
9 | import DTO.Painting;
10 | import DTO.Statue;
11 | import DTO.Vase;
12 | import java.util.Scanner;
13 |
14 | /**
15 | *
16 | * @author GIA KINH
17 | */
18 | public class AntiqueShop {
19 |
20 | public static void main(String[] args) {
21 | Item item = null;
22 | int choice = 0;
23 | Scanner scanner = new Scanner(System.in);
24 | do {
25 |
26 | System.out.println("1. Create a Vase:");
27 | System.out.println("2. Create a Statue:");
28 | System.out.println("3. Create a Painting:");
29 | System.out.println("4. Display the Item:");
30 | System.out.print("Input a choice: ");
31 | choice = scanner.nextInt();
32 | scanner.nextLine();
33 | switch (choice) {
34 | case 1:
35 | item = new Vase();
36 | ((Vase) item).inputVase();
37 | break;
38 | case 2:
39 | item = new Statue();
40 | ((Statue) item).inputStatue();
41 | break;
42 | case 3:
43 | item = new Painting();
44 | ((Painting) item).inputPainting();
45 | break;
46 | case 4:
47 | if (item != null) {
48 | if (item instanceof Vase) {
49 | ((Vase) item).outputVase();
50 | } else if (item instanceof Statue) {
51 | ((Statue) item).outputStatue();
52 | } else if (item instanceof Painting) {
53 | ((Painting) item).outputPainting();
54 | }
55 | } else {
56 | System.out.println("you need to create an object");
57 | }
58 | break;
59 | }
60 |
61 | } while (choice <= 4);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/6. Exercises on Polymorphism, Abstract Classes and Interfaces/MovableRectangle/MovableRectangle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MovableRectangle;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class MovableRectangle implements Movable {
13 |
14 | private MovablePoint topLeft;
15 | private MovablePoint bottomRight;
16 |
17 | public MovableRectangle() {
18 | }
19 |
20 | public MovableRectangle(MovablePoint topLeft, MovablePoint bottomRight) {
21 | this.topLeft = topLeft;
22 | this.bottomRight = bottomRight;
23 | }
24 |
25 | public MovablePoint getTopLeft() {
26 | return topLeft;
27 | }
28 |
29 | public void setTopLeft(MovablePoint topLeft) {
30 | this.topLeft = topLeft;
31 | }
32 |
33 | public MovablePoint getBottomRight() {
34 | return bottomRight;
35 | }
36 |
37 | public void setBottomRight(MovablePoint bottomRight) {
38 | this.bottomRight = bottomRight;
39 | }
40 |
41 | @Override
42 | public String toString() {
43 | return "MovableRectangle[" + "topLeft=" + topLeft + ",bottomRight=" + bottomRight + ']';
44 | }
45 |
46 | @Override
47 | public void moveUp() {
48 | this.topLeft.setY(this.topLeft.getY() - this.topLeft.getySpeed());
49 | this.bottomRight.setY(this.bottomRight.getY() - this.bottomRight.getySpeed());
50 | }
51 |
52 | @Override
53 | public void moveDown() {
54 | this.topLeft.setY(this.topLeft.getY() + this.topLeft.getySpeed());
55 | this.bottomRight.setY(this.bottomRight.getY() + this.bottomRight.getySpeed());
56 | }
57 |
58 | @Override
59 | public void moveLeft() {
60 | this.topLeft.setX(this.topLeft.getX() - this.topLeft.getxSpeed());
61 | this.bottomRight.setX(this.bottomRight.getX() - this.bottomRight.getxSpeed());
62 | }
63 |
64 | @Override
65 | public void moveRight() {
66 | this.topLeft.setX(this.topLeft.getX() + this.topLeft.getxSpeed());
67 | this.bottomRight.setX(this.bottomRight.getX() + this.bottomRight.getxSpeed());
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop3/CarManager/Car.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop3.CarManager;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class Car {
13 | private String colour;
14 | private int enginePower;
15 | private boolean convertible;
16 | private boolean parkingBrake;
17 |
18 | public Car() {
19 | }
20 |
21 | public Car(String colour, int enginePower, boolean convertible, boolean parkingBrake) {
22 | this.colour = colour;
23 | this.enginePower = enginePower;
24 | this.convertible = convertible;
25 | this.parkingBrake = parkingBrake;
26 | }
27 |
28 | public String getColour() {
29 | return colour;
30 | }
31 |
32 | public void setColour(String colour) {
33 | this.colour = colour;
34 | }
35 |
36 | public int getEnginePower() {
37 | return enginePower;
38 | }
39 |
40 | public void setEnginePower(int enginePower) {
41 | this.enginePower = enginePower;
42 | }
43 |
44 | public boolean isConvertible() {
45 | return convertible;
46 | }
47 |
48 | public void setConvertible(boolean convertible) {
49 | this.convertible = convertible;
50 | }
51 |
52 | public boolean isParkingBrake() {
53 | return parkingBrake;
54 | }
55 |
56 | public void setParkingBrake(boolean parkingBrake) {
57 | this.parkingBrake = parkingBrake;
58 | }
59 |
60 | public void pressStartButton(){
61 | System.out.println("You have pressed the start button");
62 | }
63 |
64 | public void pressAcceleratorButton(){
65 | System.out.println("You have pressed the accelerator button");
66 | }
67 |
68 | public void output(){
69 | System.out.println("Colour: "+ colour);
70 | System.out.println("EnginePower: "+ enginePower);
71 | System.out.println("Convertible: "+ enginePower);
72 | System.out.println("ParkingBrake: "+ parkingBrake);
73 | }
74 |
75 | @Override
76 | public String toString() {
77 | return "Car{" + "colour=" + colour + ", enginePower=" + enginePower + ", convertible=" + convertible + ", parkingBrake=" + parkingBrake + '}';
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/InvoiceItem.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | /**
8 | *
9 | * @author GIA KINH
10 | */
11 | public class InvoiceItem {
12 | private String id;
13 | private String desc;
14 | private int qty;
15 | private double unitPrice;
16 |
17 | public InvoiceItem(String id, String desc, int qty, double unitPrice) {
18 | this.id = id;
19 | this.desc = desc;
20 | this.qty = qty;
21 | this.unitPrice = unitPrice;
22 | }
23 |
24 | public String getID() {
25 | return id;
26 | }
27 |
28 | public void setID(String id) {
29 | this.id = id;
30 | }
31 |
32 | public String getDesc() {
33 | return desc;
34 | }
35 |
36 | public void setDesc(String desc) {
37 | this.desc = desc;
38 | }
39 |
40 | public int getQty() {
41 | return qty;
42 | }
43 |
44 | public void setQty(int qty) {
45 | this.qty = qty;
46 | }
47 |
48 | public double getUnitPrice() {
49 | return unitPrice;
50 | }
51 |
52 | public void setUnitPrice(double unitPrice) {
53 | this.unitPrice = unitPrice;
54 | }
55 |
56 | public double getTotal(){
57 | return this.unitPrice * this.qty;
58 | }
59 |
60 | @Override
61 | public String toString() {
62 | return "InvoiceItem[" + "id=" + id + ",desc=" + desc + ",qty=" + qty + ",unitPrice=" + unitPrice + ']';
63 | }
64 | }
65 |
66 |
67 | class TestInvoiceItem {
68 | public static void main(String[] args) {
69 | // Test constructor and toString()
70 | InvoiceItem inv1 = new InvoiceItem("A101", "Pen Red", 888, 0.08);
71 | System.out.println(inv1); // toString();
72 |
73 | // Test Setters and Getters
74 | inv1.setQty(999);
75 | inv1.setUnitPrice(0.99);
76 | System.out.println(inv1); // toString();
77 | System.out.println("id is: " + inv1.getID());
78 | System.out.println("desc is: " + inv1.getDesc());
79 | System.out.println("qty is: " + inv1.getQty());
80 | System.out.println("unitPrice is: " + inv1.getUnitPrice());
81 |
82 | // Test getTotal()
83 | System.out.println("The total is: " + inv1.getTotal());
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/BAI TAP/ws5/PhoneNumber/TestMain.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package PhoneNumber;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Scanner;
10 |
11 | /**
12 | *
13 | * @author GIA KINH
14 | */
15 | public class TestMain {
16 | public static void main(String[] args) {
17 | Scanner scanner = new Scanner(System.in);
18 | System.out.println("Enter list of phone numbers");
19 | System.out.println("-----------------------------------------");
20 | ArrayList phoneNumbers = new ArrayList();
21 | int choose = 0;
22 | int area;
23 | String number;
24 | String codeCountry;
25 | do{
26 | System.out.print("Type of phone number ? (1 – local phone, 2 – Inter phone number, 0 - exit): ");
27 | choose = scanner.nextInt();
28 | switch(choose){
29 | case 1:
30 | System.out.print("Enter area code: ");
31 | area = scanner.nextInt();
32 | scanner.nextLine();
33 | System.out.print("Enter number: ");
34 | number = scanner.next();
35 | scanner.nextLine();
36 | phoneNumbers.add(new PhoneNumber(area, number));
37 | break;
38 | case 2:
39 | System.out.print("Enter country code: ");
40 | codeCountry = scanner.next();
41 | scanner.nextLine();
42 | System.out.print("Enter area code: ");
43 | area = scanner.nextInt();
44 | scanner.nextLine();
45 | System.out.print("Enter number: ");
46 | number = scanner.next();
47 | scanner.nextLine();
48 | phoneNumbers.add(new IntPhoneNumber(codeCountry,area, number));
49 | break;
50 | case 0:
51 | break;
52 | default:
53 | System.out.println("Please enter [0->2]");
54 | break;
55 | }
56 | }while(choose !=0);
57 | System.out.println("List of phone number:");
58 | System.out.println("----------------------------------------");
59 | for (PhoneNumber phoneNumber: phoneNumbers) {
60 | phoneNumber.display();
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/5. Exercises on Composition vs Inheritance/Cylinder/Cylinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Cylinder;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class Cylinder extends Circle {
13 |
14 | private Circle base;
15 | private double height = 1.0;
16 |
17 | public Cylinder() {
18 | this.base = new Circle();
19 | }
20 |
21 | public Cylinder(double radius) {
22 | super(radius);
23 | this.base = new Circle();
24 | }
25 |
26 | public Cylinder(double radius, String color) {
27 | super(radius, color);
28 | this.base = new Circle();
29 | }
30 |
31 | public Cylinder(double radius, double height) {
32 | super(radius);
33 | this.base = new Circle();
34 | this.height = height;
35 | }
36 |
37 | public Cylinder(double radius, double height, String color) {
38 | super(radius, color);
39 | this.height = height;
40 | this.base = new Circle();
41 | }
42 |
43 |
44 | public Cylinder(double radius1, String color1, double radius2, String color2) {
45 | super(radius1, color1);
46 | this.base = new Circle(radius2, color2);
47 | }
48 |
49 | public Cylinder(double height, double radius1, String color1, double radius2, String color2) {
50 | super(radius1, color1);
51 | this.height = height;
52 | this.base = new Circle(radius2, color2);
53 | }
54 |
55 | public Cylinder(double radius1, String color1, double radius2, String color2, double height) {
56 | super(radius1, color1);
57 | this.height = height;
58 | this.base = new Circle(radius2, color2);
59 | }
60 |
61 | public double getHeight() {
62 | return height;
63 | }
64 |
65 | public void setHeight(double height) {
66 | this.height = height;
67 | }
68 |
69 | public double getVolume() {
70 | return this.getArea() * this.height;
71 | }
72 |
73 | public Circle getBase() {
74 | return base;
75 | }
76 |
77 | public void setBase(Circle base) {
78 | this.base = base;
79 | }
80 |
81 | @Override
82 | public String toString() {
83 | return "Cylinder["+ "circle=" + super.toString() + "" + "base=" + base + ", height=" + height + '}';
84 | }
85 | }
86 |
87 | class TestCylinder{
88 | public static void main(String[] args) {
89 |
90 | }
91 | }
--------------------------------------------------------------------------------
/3. More Exercises on Classes/Player/Player.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Player;
7 |
8 | import java.util.Random;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 |
15 | class Ball{
16 | private float x;
17 | private float y;
18 | private float z;
19 |
20 | public Ball(float x, float y, float z) {
21 | this.x = x;
22 | this.y = y;
23 | this.z = z;
24 | }
25 |
26 | public float getX() {
27 | return x;
28 | }
29 |
30 | public void setX(float x) {
31 | this.x = x;
32 | }
33 |
34 | public float getY() {
35 | return y;
36 | }
37 |
38 | public void setY(float y) {
39 | this.y = y;
40 | }
41 |
42 | public float getZ() {
43 | return z;
44 | }
45 |
46 | public void setZ(float z) {
47 | this.z = z;
48 | }
49 |
50 | public void setXYZ(float x, float y, float z){
51 | this.x = x;
52 | this.y = y;
53 | this.z = z;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return "("+ x + "," + y + "," + z + ")";
59 | }
60 | }
61 |
62 | public class Player {
63 | private int number;
64 | private float x;
65 | private float y;
66 | private float z;
67 |
68 | public Player(int number, float x, float y, float z) {
69 | this.number = number;
70 | this.x = x;
71 | this.y = y;
72 | this.z = z;
73 | }
74 |
75 | public void move(float xDisp, float yDisp){
76 | this.x +=xDisp;
77 | this.y +=yDisp;
78 | }
79 |
80 | public void jump(float zDisp){
81 | this.z+=zDisp;
82 | }
83 |
84 | public boolean near(Ball ball){
85 | if(Math.sqrt(Math.pow(ball.getX() - this.x, 2) + Math.pow(ball.getY()-this.y, 2) + Math.pow(ball.getZ()-this.z, 2)) < 8){
86 | return true;
87 | }else{
88 | return false;
89 | }
90 | }
91 |
92 | public void kick(Ball ball){
93 | Random rand = new Random();
94 | ball.setX(ball.getX()+rand.nextInt(20));
95 | ball.setY(ball.getY()+rand.nextInt(20));
96 | ball.setZ(ball.getZ()+rand.nextInt(5));
97 | }
98 |
99 | }
100 |
101 | class TestPlayer{
102 | public static void main(String[] args) {
103 |
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Date.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | import java.text.ParseException;
8 | import java.text.SimpleDateFormat;
9 | import java.util.logging.Level;
10 | import java.util.logging.Logger;
11 |
12 | /**
13 | *
14 | * @author GIA KINH
15 | */
16 | public class Date {
17 | private int day;
18 | private int month;
19 | private int year;
20 |
21 | public Date(int day, int month, int year) {
22 | this.day = day;
23 | this.month = month;
24 | this.year = year;
25 | }
26 |
27 | public int getDay() {
28 | return day;
29 | }
30 |
31 | public void setDay(int day) {
32 | this.day = day;
33 | }
34 |
35 | public int getMonth() {
36 | return month;
37 | }
38 |
39 | public void setMonth(int month) {
40 | this.month = month;
41 | }
42 |
43 | public int getYear() {
44 | return year;
45 | }
46 |
47 | public void setYear(int year) {
48 | this.year = year;
49 | }
50 |
51 | public void setDate(int day, int month, int year) {
52 | this.day = day;
53 | this.month = month;
54 | this.year = year;
55 | }
56 |
57 | @Override
58 | public String toString() {
59 | String dateString = String.format("%d-%d-%d", year, month, day);
60 | java.util.Date date = null;
61 |
62 | try {
63 | date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
64 | } catch (ParseException ex) {
65 | Logger.getLogger(Date.class.getName()).log(Level.SEVERE, null, ex);
66 | }
67 |
68 | String newstring = new SimpleDateFormat("dd/MM/yyyy").format(date);
69 | return newstring;
70 | }
71 |
72 |
73 | }
74 |
75 | class TestDate {
76 | public static void main(String[] args) {
77 | // Test constructor and toString()
78 | Date d1 = new Date(1, 2, 2014);
79 | System.out.println(d1); // toString()
80 |
81 | // Test Setters and Getters
82 | d1.setMonth(12);
83 | d1.setDay(9);
84 | d1.setYear(2099);
85 | System.out.println(d1); // toString()
86 | System.out.println("Month: " + d1.getMonth());
87 | System.out.println("Day: " + d1.getDay());
88 | System.out.println("Year: " + d1.getYear());
89 |
90 | // Test setDate()
91 | d1.setDate(3, 4, 2016);
92 | System.out.println(d1); // toString()
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/2. Exercises on Composition/MyPoint/MyPoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package myPoint;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class MyPoint {
13 |
14 | private int x;
15 | private int y;
16 |
17 | public MyPoint() {
18 | }
19 |
20 | public MyPoint(int x, int y) {
21 | this.x = x;
22 | this.y = y;
23 | }
24 |
25 | public int getX() {
26 | return x;
27 | }
28 |
29 | public void setX(int x) {
30 | this.x = x;
31 | }
32 |
33 | public int getY() {
34 | return y;
35 | }
36 |
37 | public void setY(int y) {
38 | this.y = y;
39 | }
40 |
41 | public int[] getXY() {
42 | return new int[]{this.x, this.y};
43 | }
44 |
45 | public void setXY(int x, int y) {
46 | this.x = x;
47 | this.y = y;
48 | }
49 |
50 | public double distance(int x, int y) {
51 | return (double) Math.sqrt(Math.pow((x - this.x), 2) + Math.pow((y - this.y), 2));
52 | }
53 |
54 | public double distance(MyPoint another) {
55 | return (double) Math.sqrt(Math.pow((another.x - this.x), 2) + Math.pow((another.y - this.y), 2));
56 | }
57 |
58 | public double distance() {
59 | return (double) Math.sqrt(Math.pow((0 - this.x), 2) + Math.pow((0 - this.y), 2));
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return "(" + x + "," + y + ')';
65 | }
66 | }
67 |
68 | class testMyPoint {
69 |
70 | public static void main(String[] args) {
71 | // Test program to test all constructors and public methods
72 | MyPoint p1 = new MyPoint(); // Test constructor
73 | System.out.println(p1); // Test toString()
74 | p1.setX(8); // Test setters
75 | p1.setY(6);
76 | System.out.println("x is: " + p1.getX()); // Test getters
77 | System.out.println("y is: " + p1.getY());
78 | p1.setXY(3, 0); // Test setXY()
79 | System.out.println(p1.getXY()[0]); // Test getXY()
80 | System.out.println(p1.getXY()[1]);
81 | System.out.println(p1);
82 |
83 | MyPoint p2 = new MyPoint(0, 4); // Test another constructor
84 | System.out.println(p2);
85 | // Testing the overloaded methods distance()
86 | System.out.println(p1.distance(p2)); // which version?
87 | System.out.println(p2.distance(p1)); // which version?
88 | System.out.println(p1.distance(5, 6)); // which version?
89 | System.out.println(p1.distance()); // which version?
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/nbproject/project.properties:
--------------------------------------------------------------------------------
1 | annotation.processing.enabled=true
2 | annotation.processing.enabled.in.editor=false
3 | annotation.processing.processor.options=
4 | annotation.processing.processors.list=
5 | annotation.processing.run.all.processors=true
6 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
7 | build.classes.dir=${build.dir}/classes
8 | build.classes.excludes=**/*.java,**/*.form
9 | # This directory is removed when the project is cleaned:
10 | build.dir=build
11 | build.generated.dir=${build.dir}/generated
12 | build.generated.sources.dir=${build.dir}/generated-sources
13 | # Only compile against the classpath explicitly listed here:
14 | build.sysclasspath=ignore
15 | build.test.classes.dir=${build.dir}/test/classes
16 | build.test.results.dir=${build.dir}/test/results
17 | # Uncomment to specify the preferred debugger connection transport:
18 | #debug.transport=dt_socket
19 | debug.classpath=\
20 | ${run.classpath}
21 | debug.test.classpath=\
22 | ${run.test.classpath}
23 | # Files in build.classes.dir which should be excluded from distribution jar
24 | dist.archive.excludes=
25 | # This directory is removed when the project is cleaned:
26 | dist.dir=dist
27 | dist.jar=${dist.dir}/ItemManager.jar
28 | dist.javadoc.dir=${dist.dir}/javadoc
29 | excludes=
30 | includes=**
31 | jar.compress=false
32 | javac.classpath=
33 | # Space-separated list of extra javac options
34 | javac.compilerargs=
35 | javac.deprecation=false
36 | javac.external.vm=true
37 | javac.processorpath=\
38 | ${javac.classpath}
39 | javac.source=1.8
40 | javac.target=1.8
41 | javac.test.classpath=\
42 | ${javac.classpath}:\
43 | ${build.classes.dir}
44 | javac.test.processorpath=\
45 | ${javac.test.classpath}
46 | javadoc.additionalparam=
47 | javadoc.author=false
48 | javadoc.encoding=${source.encoding}
49 | javadoc.noindex=false
50 | javadoc.nonavbar=false
51 | javadoc.notree=false
52 | javadoc.private=false
53 | javadoc.splitindex=true
54 | javadoc.use=true
55 | javadoc.version=false
56 | javadoc.windowtitle=
57 | main.class=itemmanager.ItemManager
58 | manifest.file=manifest.mf
59 | meta.inf.dir=${src.dir}/META-INF
60 | mkdist.disabled=false
61 | platform.active=default_platform
62 | run.classpath=\
63 | ${javac.classpath}:\
64 | ${build.classes.dir}
65 | # Space-separated list of JVM arguments used when running the project.
66 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
67 | # To set system properties for unit tests define test-sys-prop.name=value:
68 | run.jvmargs=
69 | run.test.classpath=\
70 | ${javac.test.classpath}:\
71 | ${build.test.classes.dir}
72 | source.encoding=UTF-8
73 | src.dir=src
74 | test.src.dir=test
75 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/src/DTO/Painting.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package DTO;
7 |
8 | import java.util.Scanner;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 | public class Painting extends Item{
15 | private int height;
16 | private int width;
17 | private boolean isWatercolour;
18 | private boolean isFramed;
19 |
20 | public Painting() {
21 | }
22 |
23 | public Painting(int value, String creator, int height, int width, boolean isWatercolour, boolean isFramed) {
24 | super(value, creator);
25 | this.height = height;
26 | this.width = width;
27 | this.isWatercolour = isWatercolour;
28 | this.isFramed = isFramed;
29 | }
30 |
31 | public int getHeight() {
32 | return height;
33 | }
34 |
35 | public void setHeight(int height) {
36 | this.height = height;
37 | }
38 |
39 | public int getWidth() {
40 | return width;
41 | }
42 |
43 | public void setWidth(int width) {
44 | this.width = width;
45 | }
46 |
47 | public boolean isIsWatercolour() {
48 | return isWatercolour;
49 | }
50 |
51 | public void setIsWatercolour(boolean isWatercolour) {
52 | this.isWatercolour = isWatercolour;
53 | }
54 |
55 | public boolean isIsFramed() {
56 | return isFramed;
57 | }
58 |
59 | public void setIsFramed(boolean isFramed) {
60 | this.isFramed = isFramed;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return "Painting[" + super.toString() + "height=" + height + ",width=" + width + ",isWatercolour=" + isWatercolour + ",isFramed=" + isFramed + ']';
66 | }
67 |
68 |
69 |
70 | public void inputPainting(){
71 | super.input();
72 | Scanner scanner = new Scanner(System.in);
73 | System.out.print("Input a height: ");
74 | this.height = scanner.nextInt();
75 | scanner.nextLine();
76 | System.out.print("Input a width: ");
77 | this.width = scanner.nextInt();
78 | scanner.nextLine();
79 | System.out.print("Is watercolour: ");
80 | this.isWatercolour = scanner.nextBoolean();
81 | scanner.nextLine();
82 | System.out.print("Is Framed: ");
83 | this.isFramed = scanner.nextBoolean();
84 | scanner.nextLine();
85 | }
86 |
87 | public void outputPainting(){
88 | super.output();
89 | System.out.println("Height: " + height);
90 | System.out.println("Width: " + width);
91 | System.out.println("Is Watercolour: " + isWatercolour);
92 | System.out.println("Is Framed: " + isFramed);
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Employee.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | /**
8 | *
9 | * @author GIA KINH
10 | */
11 | public class Employee {
12 | private int id;
13 | private String firstName;
14 | private String lastName;
15 | private int salary;
16 |
17 | public Employee() {
18 | }
19 |
20 | public Employee(int id, String firstName, String lastName, int salary) {
21 | this.id = id;
22 | this.firstName = firstName;
23 | this.lastName = lastName;
24 | this.salary = salary;
25 | }
26 |
27 | public int getID() {
28 | return id;
29 | }
30 |
31 | public void setID(int id) {
32 | this.id = id;
33 | }
34 |
35 | public String getFirstName() {
36 | return firstName;
37 | }
38 |
39 | public void setFirstName(String firstName) {
40 | this.firstName = firstName;
41 | }
42 |
43 | public String getLastName() {
44 | return lastName;
45 | }
46 |
47 | public void setLastName(String lastName) {
48 | this.lastName = lastName;
49 | }
50 |
51 | public int getSalary() {
52 | return salary;
53 | }
54 |
55 | public void setSalary(int salary) {
56 | this.salary = salary;
57 | }
58 |
59 | public String getName(){
60 | return this.firstName + " " + this.lastName;
61 | }
62 |
63 | public int getAnnualSalary(){
64 | return this.salary * 12;
65 | }
66 |
67 | public int raiseSalary(int percent){
68 | this.salary += ((this.salary*percent)/100);
69 | return this.salary;
70 | }
71 |
72 | @Override
73 | public String toString() {
74 | return "Employee[" + "id=" + id + ",name=" + firstName + " " + lastName + ",salary=" + salary + ']';
75 | }
76 | }
77 |
78 | class TestEmployee {
79 | public static void main(String[] args) {
80 | // Test constructor and toString()
81 | Employee e1 = new Employee(8, "Peter", "Tan", 2500);
82 | System.out.println(e1); // toString();
83 |
84 | // Test Setters and Getters
85 | e1.setSalary(999);
86 | System.out.println(e1); // toString();
87 | System.out.println("id is: " + e1.getID());
88 | System.out.println("firstname is: " + e1.getFirstName());
89 | System.out.println("lastname is: " + e1.getLastName());
90 | System.out.println("salary is: " + e1.getSalary());
91 |
92 | System.out.println("name is: " + e1.getName());
93 | System.out.println("annual salary is: " + e1.getAnnualSalary()); // Test method
94 |
95 | // Test raiseSalary()
96 | System.out.println(e1.raiseSalary(10));
97 | System.out.println(e1);
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/3. More Exercises on Classes/MyTime/MyTime.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MyTime;
7 |
8 | import java.text.ParseException;
9 | import java.text.SimpleDateFormat;
10 | import java.util.Date;
11 | import java.util.logging.Level;
12 | import java.util.logging.Logger;
13 |
14 | /**
15 | *
16 | * @author GIA KINH
17 | */
18 | public class MyTime {
19 | private int hour;
20 | private int minute;
21 | private int second;
22 |
23 | public MyTime(int hour, int minute, int second) {
24 | this.hour = hour;
25 | this.minute = minute;
26 | this.second = second;
27 | }
28 |
29 | public int getHour() {
30 | return hour;
31 | }
32 |
33 | public void setHour(int hour) {
34 | this.hour = hour;
35 | }
36 |
37 | public int getMinute() {
38 | return minute;
39 | }
40 |
41 | public void setMinute(int minute) {
42 | this.minute = minute;
43 | }
44 |
45 | public int getSecond() {
46 | return second;
47 | }
48 |
49 | public void setSecond(int second) {
50 | this.second = second;
51 | }
52 |
53 | public void setTime(int hour, int minute, int second){
54 | this.hour = hour;
55 | this.minute = minute;
56 | this.second = second;
57 | }
58 |
59 | @Override
60 | public String toString() {
61 | String timeString = String.format("%d:%d:%d", hour, minute, second);
62 | Date time= null;
63 | try {
64 | time = new SimpleDateFormat("HH:mm:ss").parse(timeString);
65 | } catch (ParseException ex) {
66 | Logger.getLogger(MyTime.class.getName()).log(Level.SEVERE, null, ex);
67 | }
68 | timeString = new SimpleDateFormat("HH:mm:ss").format(time);
69 | return timeString;
70 | }
71 |
72 | public MyTime nextHour(){
73 | this.hour++;
74 | return this;
75 | }
76 |
77 | public MyTime nextMinute(){
78 | this.minute++;
79 | return this;
80 | }
81 |
82 | public MyTime nextSecond(){
83 | this.second++;
84 | return this;
85 | }
86 |
87 |
88 | public MyTime previousHour(){
89 | this.hour--;
90 | return this;
91 | }
92 |
93 | public MyTime previousMinute(){
94 | this.minute--;
95 | return this;
96 | }
97 |
98 | public MyTime previousSecond(){
99 | this.second--;
100 | return this;
101 | }
102 | }
103 |
104 | class TestMyTime{
105 | public static void main(String[] args) {
106 | MyTime myTime = new MyTime(23,59,59);
107 | System.out.println(myTime);
108 | myTime.nextHour();
109 | System.out.println(myTime);
110 | myTime.nextSecond();
111 | System.out.println(myTime);
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Account.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | /**
8 | *
9 | * @author GIA KINH
10 | */
11 |
12 | public class Account {
13 | private String id;
14 | private String name;
15 | private int balance;
16 |
17 | public Account(String id, String name) {
18 | this.id = id;
19 | this.name = name;
20 | }
21 |
22 | public Account(String id, String name, int balance) {
23 | this.id = id;
24 | this.name = name;
25 | this.balance = balance;
26 | }
27 |
28 | public String getID() {
29 | return id;
30 | }
31 |
32 | public void setID(String id) {
33 | this.id = id;
34 | }
35 |
36 | public String getName() {
37 | return name;
38 | }
39 |
40 | public void setName(String name) {
41 | this.name = name;
42 | }
43 |
44 | public int getBalance() {
45 | return balance;
46 | }
47 |
48 | public void setBalance(int balance) {
49 | this.balance = balance;
50 | }
51 |
52 | public int credit(int amount){
53 | this.balance += amount;
54 | return this.balance;
55 | }
56 |
57 | public int debit(int amount){
58 | if(amount<=this.balance){
59 | this.balance-=amount;
60 | }else{
61 | System.out.println("Amount exceeded balance");
62 | }
63 | return this.balance;
64 | }
65 |
66 | public int transferTo(Account account, int amount){
67 | if(amount<=this.balance){
68 | account.balance += amount;
69 | this.balance -= amount;
70 | }else{
71 | System.out.println("Amount exceeded balance");
72 | }
73 | return this.balance;
74 | }
75 |
76 | @Override
77 | public String toString() {
78 | return "Account[" + "id=" + id + ",name=" + name + ",balance=" + balance + ']';
79 | }
80 |
81 | }
82 |
83 | class TestAccount {
84 | public static void main(String[] args) {
85 | // Test constructor and toString()
86 | Account a1 = new Account("A101", "Tan Ah Teck", 88);
87 | System.out.println(a1); // toString();
88 | Account a2 = new Account("A102", "Kumar"); // default balance
89 | System.out.println(a2);
90 |
91 | // Test Getters
92 | System.out.println("ID: " + a1.getID());
93 | System.out.println("Name: " + a1.getName());
94 | System.out.println("Balance: " + a1.getBalance());
95 |
96 | // Test credit() and debit()
97 | a1.credit(100);
98 | System.out.println(a1);
99 | a1.debit(50);
100 | System.out.println(a1);
101 | a1.debit(500); // debit() error
102 | System.out.println(a1);
103 |
104 | // Test transfer()
105 | a1.transferTo(a2, 100); // toString()
106 | System.out.println(a1);
107 | System.out.println(a2);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java Programming Tutorial OOP Exercises
2 | **FPT University**
3 |
4 | 1. Exercises on Classes
5 | 1.1 An Introduction to Classes and Instances by Example - The Circle Class
6 |
7 | 1.2 Ex: Yet Another Circle Class
8 |
9 | 1.3 Ex: The Rectangle Class
10 |
11 | 1.4 Ex: The Employee Class
12 |
13 | 1.5 Ex: The InvoiceItem Class
14 |
15 | 1.6 Ex: The Account Class
16 |
17 | 1.7 Ex: The Date Class
18 |
19 | 1.8 Ex: The Time Class
20 |
21 | 1.9 Ex: The Ball Class
22 |
23 | 2. Exercises on Composition
24 | 2.1 An Introduction to OOP Composition by Example - the Author and Book Classes
25 |
26 | 2.2 (Advanced) The Author and Book Classes Again - An Array of Objects as an Instance Variable
27 |
28 | 2.3 Ex: The Author and Book Classes - Your Turn
29 |
30 | 2.4 Ex: The Customer and Invoice classes
31 |
32 | 2.5 Ex: The Customer and Account classes
33 |
34 | 2.6 Ex: The MyPoint Class
35 |
36 | 2.7 Ex: The MyLine and MyPoint Classes
37 |
38 | 2.8 Ex: The MyCircle and MyPoint Classes
39 |
40 | 2.9 Ex: The MyTriangle and MyPoint Classes
41 |
42 | 2.10 Ex: The MyRectangle and MyPoint Classes
43 |
44 | 3. More Exercises on Classes
45 | 3.1 Ex: The MyComplex class
46 |
47 | 3.2 Ex: The MyPolynomial Class
48 |
49 | 3.3 Ex: Using JDK's BigInteger Class
50 |
51 | 3.4 Ex: The MyTime Class
52 |
53 | 3.5 Ex: The MyDate Class
54 |
55 | 3.6 Ex: Bouncing Balls - Ball and Container Classes
56 |
57 | 3.7 Ex: The Ball and Player Classes
58 | 4. Exercises on Inheritance
59 | 4.1 An Introduction to OOP Inheritance by Example - The Circle and Cylinder Classes
60 |
61 | 4.2 Ex: Superclass Person and its subclasses
62 |
63 | 4.3 Ex: Point2D and Point3D
64 |
65 | 4.4 Ex: Point and MovablePoint
66 |
67 | 4.5 Ex: Superclass Shape and its subclasses Circle, Rectangle and Square
68 |
69 | 4.6 Ex: Superclass Animal and its subclasses
70 | 5. Exercises on Composition vs Inheritance
71 | 5.1 Ex: The Point and Line Classes
72 |
73 | 5.2 Ex: The Circle and Cylinder Classes Using Composition
74 | 6. Exercises on Polymorphism, Abstract Classes and Interfaces
75 | 6.1 Ex: Abstract Superclass Shape and Its Concrete Subclasses
76 |
77 | 6.2 Ex: GeometricObject Interface and its Implementation Classes Circle and Rectangle
78 |
79 | 6.3 Ex: Movable Interface and its Implementation MovablePoint Class
80 |
81 | 6.4 Ex: Movable Interface and its Implementation Classes MovablePoint and MovableCircle
82 |
83 | 6.5 Ex: Interfaces Resizable and GeometricObject
84 |
85 | 6.6 Ex: Abstract Superclass Animal and its Implementation Subclasses
86 |
87 | 6.7 Ex: Another View of Abstract Superclass Animal and its Implementation Subclasses
88 |
89 | 6.8 Ex: Interface Movable and its implementation subclasses MovablePoint and MovableCircle
90 | 7. More Exercises on OOP
91 | 7.1 Ex: The Discount System
92 |
93 | 7.2 Ex: Polyline of Points with ArrayList
94 |
95 | 8. Exercises on Data Structures
96 | 8.1 Ex: MyIntStack
97 |
--------------------------------------------------------------------------------
/BAI TAP/Workshop3/Guitar/Guitar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Workshop3.Guitar;
7 |
8 | /**
9 | *
10 | * @author giaki
11 | */
12 | public class Guitar implements Comparable {
13 |
14 | private String serialNumber;
15 | private double price;
16 | private String builder;
17 | private String model;
18 | private String backWood;
19 | private String topWood;
20 |
21 | public Guitar() {
22 | }
23 |
24 | public Guitar(String serialNumber, double price, String builder, String model, String backWood, String topWood) {
25 | this.serialNumber = serialNumber;
26 | this.price = price;
27 | this.builder = builder;
28 | this.model = model;
29 | this.backWood = backWood;
30 | this.topWood = topWood;
31 | }
32 |
33 | public String getSerialNumber() {
34 | return serialNumber;
35 | }
36 |
37 | public void setSerialNumber(String serialNumber) {
38 | if (!serialNumber.isEmpty()) {
39 | this.serialNumber = serialNumber;
40 | }
41 | }
42 |
43 | public double getPrice() {
44 | return price;
45 | }
46 |
47 | public void setPrice(double price) {
48 | this.price = price;
49 | }
50 |
51 | public String getBuilder() {
52 | return builder;
53 | }
54 |
55 | public void setBuilder(String builder) {
56 | this.builder = builder;
57 | }
58 |
59 | public String getModel() {
60 | return model;
61 | }
62 |
63 | public void setModel(String model) {
64 | this.model = model;
65 | }
66 |
67 | public String getBackWood() {
68 | return backWood;
69 | }
70 |
71 | public void setBackWood(String backWood) {
72 | this.backWood = backWood;
73 | }
74 |
75 | public String getTopWood() {
76 | return topWood;
77 | }
78 |
79 | public void setTopWood(String topWood) {
80 | this.topWood = topWood;
81 | }
82 |
83 | public void createSound() {
84 | System.out.println("serialNumber: " + getSerialNumber());
85 | System.out.println("Price: " + getPrice());
86 | System.out.println("Builder: " + getBuilder());
87 | System.out.println("Model: " + getModel());
88 | System.out.println("BackWood: " + getBackWood());
89 | System.out.println("TopWood: " + getTopWood());
90 | }
91 |
92 | @Override
93 | public String toString() {
94 | return "Guitar{" + "serialNumber=" + serialNumber + ", price=" + price + ", builder=" + builder + ", model=" + model + ", backWood=" + backWood + ", topWood=" + topWood + '}';
95 | }
96 |
97 | @Override
98 | public int compareTo(Guitar t) {
99 | if (this.getPrice() > t.getPrice()) {
100 | return 1;
101 | } else if(this.getPrice() < t.getPrice()){
102 | return -1;
103 | }else return this.getSerialNumber().compareTo(t.getSerialNumber());
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Time.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 |
7 | import java.text.DateFormat;
8 | import java.text.ParseException;
9 | import java.text.SimpleDateFormat;
10 | import java.util.Date;
11 | import java.util.logging.Level;
12 | import java.util.logging.Logger;
13 |
14 | /**
15 | *
16 | * @author GIA KINH
17 | */
18 | public class Time {
19 | private int hour;
20 | private int minute;
21 | private int second;
22 |
23 | public Time(int hour, int minute, int second) {
24 | this.hour = hour;
25 | this.minute = minute;
26 | this.second = second;
27 | }
28 |
29 | public int getHour() {
30 | return hour;
31 | }
32 |
33 | public void setHour(int hour) {
34 | this.hour = hour;
35 | }
36 |
37 | public int getMinute() {
38 | return minute;
39 | }
40 |
41 | public void setMinute(int minute) {
42 | this.minute = minute;
43 | }
44 |
45 | public int getSecond() {
46 | return second;
47 | }
48 |
49 | public void setSecond(int second) {
50 | this.second = second;
51 | }
52 |
53 | public void setTime(int hour, int minute, int second) {
54 | this.hour = hour;
55 | this.minute = minute;
56 | this.second = second;
57 | }
58 |
59 | public Time nextSecond(){
60 | this.second+=1;
61 | return this;
62 | }
63 |
64 | public Time previousSecond(){
65 | this.second-=1;
66 | return this;
67 | }
68 |
69 | @Override
70 | public String toString() {
71 | String timeString = String.format("%d:%d:%d", hour, minute, second);
72 | Date time = null;
73 | try {
74 | time = new SimpleDateFormat("HH:mm:ss").parse(timeString);
75 | } catch (ParseException ex) {
76 | Logger.getLogger(Time.class.getName()).log(Level.SEVERE, null, ex);
77 | }
78 | timeString = new SimpleDateFormat("HH:mm:ss").format(time);
79 | return timeString;
80 | }
81 |
82 | }
83 |
84 | class TestTime {
85 | public static void main(String[] args) {
86 | // Test constructors and toString()
87 | Time t1 = new Time(1, 2, 3);
88 | System.out.println(t1); // toString()
89 |
90 | // Test Setters and Getters
91 | t1.setHour(4);
92 | t1.setMinute(5);
93 | t1.setSecond(6);
94 | System.out.println(t1); // toString()
95 | System.out.println("Hour: " + t1.getHour());
96 | System.out.println("Minute: " + t1.getMinute());
97 | System.out.println("Second: " + t1.getSecond());
98 |
99 | // Test setTime()
100 | t1.setTime(23, 59, 58);
101 | System.out.println(t1); // toString()
102 |
103 | // Test nextSecond();
104 | System.out.println(t1.nextSecond());
105 | System.out.println(t1.nextSecond().nextSecond());
106 |
107 | // Test previousSecond()
108 | System.out.println(t1.previousSecond());
109 | System.out.println(t1.previousSecond().previousSecond());
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/2. Exercises on Composition/Book1/Book.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Book1;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 |
13 | class Author {
14 | private String name;
15 | private String email;
16 | private char gender;
17 |
18 | public Author() {
19 | }
20 |
21 |
22 |
23 | public Author(String name, String email, char gender) {
24 | this.name = name;
25 | this.email = email;
26 | if (gender == 'm' || gender == 'f') {
27 | this.gender = gender;
28 | }else{
29 | System.out.println("Gender of 'm' or 'f'");
30 | }
31 | }
32 |
33 | public String getName() {
34 | return name;
35 | }
36 |
37 | public void setName(String name) {
38 | this.name = name;
39 | }
40 |
41 | public String getEmail() {
42 | return email;
43 | }
44 |
45 | public void setEmail(String email) {
46 | this.email = email;
47 | }
48 |
49 | public char getGender() {
50 | return gender;
51 | }
52 |
53 | public void setGender(char gender) {
54 | if (gender == 'm' || gender == 'f') {
55 | this.gender = gender;
56 | }else{
57 | System.out.println("Gender of 'm' or 'f'");
58 | }
59 | }
60 |
61 | @Override
62 | public String toString() {
63 | return "Author[" + "name=" + name + ",email=" + email + ",gender=" + gender + ']';
64 | }
65 | }
66 |
67 |
68 | public class Book {
69 | private String name;
70 | private Author author;
71 | private double price;
72 | private int qty;
73 |
74 | public Book(String name, Author author, double price) {
75 | this.name = name;
76 | this.author = author;
77 | this.price = price;
78 | }
79 |
80 | public Book(String name, Author author, double price, int qty) {
81 | this.name = name;
82 | this.author = author;
83 | this.price = price;
84 | this.qty = qty;
85 | }
86 |
87 | public String getName() {
88 | return name;
89 | }
90 |
91 | public void setName(String name) {
92 | this.name = name;
93 | }
94 |
95 | public Author getAuthor() {
96 | return author;
97 | }
98 |
99 | public void setAuthor(Author author) {
100 | this.author = author;
101 | }
102 |
103 | public double getPrice() {
104 | return price;
105 | }
106 |
107 | public void setPrice(double price) {
108 | this.price = price;
109 | }
110 |
111 | public int getQty() {
112 | return qty;
113 | }
114 |
115 | public void setQty(int qty) {
116 | this.qty = qty;
117 | }
118 |
119 | @Override
120 | public String toString() {
121 | return "Book[" + "name=" + name + "," + author + ",price=" + price + ",qty=" + qty + ']';
122 | }
123 | }
124 |
125 | class TestBook{
126 | public static void main(String[] args) {
127 | Book book = new Book("Python", new Author("Gia Kinh", "giakinh2000@gmail.com", 'm'), 20.4,6);
128 | System.out.println(book);
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/5. Exercises on Composition vs Inheritance/LineSub/LineSub.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package LineSub;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class LineSub extends Point {
13 |
14 | private Point end;
15 |
16 | public LineSub(Point begin, Point end) { // caller to construct the Points
17 | super(begin.getX(), begin.getY()); // need to reconstruct the begin Point
18 | this.end = end;
19 | }
20 |
21 | public LineSub(int beginX, int beginY, int endX, int endY) {
22 | super(beginX, beginY); // construct the begin Point
23 | this.end = new Point(endX, endY); // construct the end Point
24 | }
25 |
26 | @Override
27 | public String toString() {
28 | return "LineSub[" + "begin=" + super.toString() + ",end=" + end + ']';
29 | }
30 |
31 | public Point getBegin() {
32 | return new Point(super.getX(), super.getY());
33 | }
34 |
35 | public Point getEnd() {
36 | return this.end;
37 | }
38 |
39 | public void setBegin(int beginX, int beginY) {
40 | setXY(beginX, beginY);
41 | }
42 |
43 | public void setEnd(int endX, int endY) {
44 | this.end.setXY(endX, endY);
45 | }
46 |
47 | public int getBeginX() {
48 | return getX();
49 | }
50 |
51 | public int getBeginY() {
52 | return getY();
53 | }
54 |
55 | public int getEndX() {
56 | return this.end.getX();
57 | }
58 |
59 | public int getEndY() {
60 | return this.end.getY();
61 | }
62 |
63 | public void setBeginX(int beginX) {
64 | setX(beginX);
65 | }
66 |
67 | public void setBeginY(int beginY) {
68 | setY(beginY);
69 | }
70 |
71 | public void setBeginXY(int beginX, int beginY) {
72 | setXY(beginX, beginY);
73 | }
74 |
75 | public void setEndX(int endX) {
76 | this.end.setX(endX);
77 | }
78 |
79 | public void setEndY(int endY) {
80 | this.end.setY(endY);
81 | }
82 |
83 | public void setEndXY(int endX, int endY) {
84 | this.end.setXY(endX, endY);
85 | }
86 |
87 | public int getLength() {
88 | return (int)(Math.sqrt(Math.pow(this.end.getX()-super.getX(), 2)+ Math.pow(this.end.getY()-super.getY(), 2)));
89 | } // Length of the line
90 | // Math.sqrt(xDiff*xDiff + yDiff*yDiff)
91 | public double getGradient() {
92 | return (double)(Math.atan2(this.end.getY()-super.getY(), this.end.getX()-super.getX()));
93 | } // Gradient in radians
94 | // Math.atan2(yDiff, xDiff)
95 |
96 | }
97 |
98 |
99 | class TestLineSub {
100 | public static void main(String[] args) {
101 | LineSub lineSub = new LineSub(8, 9,6,7);
102 | System.out.println(lineSub.getBegin());
103 | System.out.println(lineSub.getEnd());
104 | System.out.println(lineSub.getLength());
105 | System.out.println(lineSub.getGradient());
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/3. More Exercises on Classes/MyComplex/MyComplex.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MyComplex;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class MyComplex {
13 | private double real;
14 | private double imag;
15 |
16 | public MyComplex() {
17 | }
18 |
19 | public MyComplex(double real, double imag) {
20 | this.real = real;
21 | this.imag = imag;
22 | }
23 |
24 | public double getReal() {
25 | return real;
26 | }
27 |
28 | public void setReal(double real) {
29 | this.real = real;
30 | }
31 |
32 | public double getImag() {
33 | return imag;
34 | }
35 |
36 | public void setImag(double imag) {
37 | this.imag = imag;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "(" + real + "+" + imag + "i)";
43 | }
44 |
45 | public boolean isReal(){
46 | return (real == 0);
47 | }
48 |
49 | public boolean isImaginary(){
50 | return (imag == 0);
51 | }
52 |
53 | public boolean equals(double real, double imag){
54 | return (this.real == real && this.imag == imag);
55 | }
56 |
57 | public boolean equals(MyComplex another){
58 | return (this.real == another.real && this.imag == another.imag);
59 | }
60 |
61 | public double Magnitude(){
62 | return (double)(Math.sqrt(real*real + imag*imag));
63 | }
64 |
65 | public double argument(){
66 | return Math.atan2(imag, real);
67 | }
68 |
69 | public MyComplex addInto(MyComplex right){
70 | this.real+=right.real;
71 | this.imag+=right.imag;
72 | return this;
73 | }
74 |
75 | public MyComplex add(MyComplex right){
76 | this.real+=right.real;
77 | this.imag+=right.imag;
78 | return this;
79 | }
80 |
81 | public MyComplex addNew(MyComplex right){
82 | return new MyComplex(this.real+right.real,this.imag+right.imag);
83 | }
84 |
85 | public MyComplex subtract(MyComplex right){
86 | this.real-=right.real;
87 | this.imag-=right.imag;
88 | return this;
89 | }
90 |
91 | public MyComplex subtractNew(MyComplex right){
92 | return new MyComplex(this.real-right.real,this.imag-right.imag);
93 | }
94 |
95 | public MyComplex multiply(MyComplex right){
96 | this.real=(this.real*right.real - this.imag*right.imag);
97 | this.imag=(this.real*right.imag + this.imag*right.real);
98 | return this;
99 | }
100 |
101 | public MyComplex divide(MyComplex right){
102 | this.real=(double)((this.real*right.real + this.imag*right.imag)/(Math.pow(right.real, 2) + Math.pow(right.imag, 2)));
103 | this.imag-=(double)((this.imag*right.real - this.real*right.imag)/(Math.pow(right.real, 2) + Math.pow(right.imag, 2)));
104 | return this;
105 | }
106 |
107 | public MyComplex conjugate(){
108 | this.imag *= -1;
109 | return this;
110 | }
111 |
112 | }
113 |
114 | class TestMyComplex{
115 | public static void main(String[] args) {
116 |
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/BAI TAP/player/TournamentIncome.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class TournamentIncome extends Player implements GradeBonus {
13 |
14 | private int totalMatches;
15 | private int battle;
16 | private double[] points;
17 | private double income;
18 |
19 | public TournamentIncome(int totalMatches, int battle, double[] points, double income) {
20 | this.income = income;
21 | this.totalMatches = totalMatches;
22 | this.battle = battle;
23 | this.points = points;
24 | }
25 |
26 | public TournamentIncome(int totalMatches, int battle, double[] points, double income, String name) {
27 | super(name);
28 | this.income = income;
29 | this.totalMatches = totalMatches;
30 | this.battle = battle;
31 | this.points = points;
32 | }
33 |
34 | public TournamentIncome(int totalMatches, int battle, double[] points) {
35 | this.totalMatches = totalMatches;
36 | this.battle = battle;
37 | this.points = points;
38 | }
39 |
40 | public TournamentIncome(double income, String name) {
41 | super(name);
42 | this.income = income;
43 | }
44 |
45 | public TournamentIncome() {
46 | }
47 |
48 | public int getTotalMatches() {
49 | return totalMatches;
50 | }
51 |
52 | public void setTotalMatches(int totalMatches) {
53 | this.totalMatches = totalMatches;
54 | }
55 |
56 | public int getBattle() {
57 | return battle;
58 | }
59 |
60 | public void setBattle(int battle) {
61 | this.battle = battle;
62 | }
63 |
64 | public double[] getPoints() {
65 | return points;
66 | }
67 |
68 | public void setPoints(double[] points) {
69 | this.points = points;
70 | }
71 |
72 | public double getIncome() {
73 | return income;
74 | }
75 |
76 | public void setIncome(double income) {
77 | this.income = income;
78 | }
79 |
80 | @Override
81 | public void displayDetails() {
82 | System.out.println("Name player: " + super.getName());
83 | System.out.println("Grade bonus: " + this.calculateGradeBonus() + "%");
84 | System.out.println("Income: " + this.income + "$");
85 | }
86 |
87 | @Override
88 | public double calculateGradeBonus() {
89 | double totalPoint = 0;
90 | double money = 1000000;
91 | if (points != null) {
92 | for (double i : points) {
93 | totalPoint += i;
94 | }
95 | }else{
96 | System.out.println("Please enter points");
97 | return 0;
98 | }
99 | if (totalPoint < this.totalMatches * 100 * 0.3) {
100 | return GRADE_D_BONUS_PERCENT*money/100;
101 | } else if (totalPoint < this.totalMatches * 100 * 0.4) {
102 | return GRADE_C_BONUS_PERCENT*money/100;
103 | } else if (totalPoint < this.totalMatches * 100 * 0.5) {
104 | return GRADE_B_BONUS_PERCENT*money/100;
105 | } else {
106 | return GRADE_A_BONUS_PERCENT*money/100;
107 | }
108 | }
109 |
110 | }
111 |
--------------------------------------------------------------------------------
/1. Exercises on Classes/Ball.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | /**
7 | *
8 | * @author GIA KINH
9 | */
10 | public class Ball {
11 | private float x;
12 | private float y;
13 | private int radius;
14 | private float xDelta;
15 | private float yDelta;
16 |
17 | public Ball(float x, float y, int radius, float xDelta, float yDelta) {
18 | this.x = x;
19 | this.y = y;
20 | this.radius = radius;
21 | this.xDelta = xDelta;
22 | this.yDelta = yDelta;
23 | }
24 |
25 | public float getX() {
26 | return x;
27 | }
28 |
29 | public void setX(float x) {
30 | this.x = x;
31 | }
32 |
33 | public float getY() {
34 | return y;
35 | }
36 |
37 | public void setY(float y) {
38 | this.y = y;
39 | }
40 |
41 | public int getRadius() {
42 | return radius;
43 | }
44 |
45 | public void setRadius(int radius) {
46 | this.radius = radius;
47 | }
48 |
49 | public float getXDelta() {
50 | return xDelta;
51 | }
52 |
53 | public void setXDelta(float xDelta) {
54 | this.xDelta = xDelta;
55 | }
56 |
57 | public float getYDelta() {
58 | return yDelta;
59 | }
60 |
61 | public void setYDelta(float yDelta) {
62 | this.yDelta = yDelta;
63 | }
64 |
65 | public void move(){
66 | this.x += this.xDelta;
67 | this.y += this.yDelta;
68 | }
69 |
70 | public void reflectHorizontal(){
71 | this.xDelta = -this.xDelta;
72 | }
73 |
74 | public void reflectVertical(){
75 | this.yDelta = -this.yDelta;
76 | }
77 |
78 | @Override
79 | public String toString() {
80 | return "Ball[("+ x+ "," + y + "),speed=(" + xDelta + "," + yDelta + ")]";
81 | }
82 | }
83 |
84 | class TestMain {
85 | public static void main(String[] args) {
86 | // Test constructor and toString()
87 | Ball ball = new Ball(1.1f, 2.2f, 10, 3.3f, 4.4f);
88 | System.out.println(ball); // toString()
89 |
90 | // Test Setters and Getters
91 | ball.setX(80.0f);
92 | ball.setY(35.0f);
93 | ball.setRadius(5);
94 | ball.setXDelta(4.0f);
95 | ball.setYDelta(6.0f);
96 | System.out.println(ball); // toString()
97 | System.out.println("x is: " + ball.getX());
98 | System.out.println("y is: " + ball.getY());
99 | System.out.println("radius is: " + ball.getRadius());
100 | System.out.println("xDelta is: " + ball.getXDelta());
101 | System.out.println("yDelta is: " + ball.getYDelta());
102 |
103 | // Bounce the ball within the boundary
104 | float xMin = 0.0f;
105 | float xMax = 100.0f;
106 | float yMin = 0.0f;
107 | float yMax = 50.0f;
108 | for (int i = 0; i < 15; i++) {
109 | ball.move();
110 | System.out.println(ball);
111 | float xNew = ball.getX();
112 | float yNew = ball.getY();
113 | int radius = ball.getRadius();
114 | // Check boundary value to bounce back
115 | if ((xNew + radius) > xMax || (xNew - radius) < xMin) {
116 | ball.reflectHorizontal();
117 | }
118 | if ((yNew + radius) > yMax || (yNew - radius) < yMin) {
119 | ball.reflectVertical();
120 | }
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/3. More Exercises on Classes/MyPolynomial/MyPolynomial.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MyPolynomial;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | public class MyPolynomial {
13 |
14 | private double[] coeffs;
15 |
16 | public MyPolynomial(double... coeffs) {
17 | this.coeffs = coeffs;
18 | }
19 |
20 | public double[] getCoeffs() {
21 | return coeffs;
22 | }
23 |
24 | public void setCoeffs(double[] coeffs) {
25 | this.coeffs = coeffs;
26 | }
27 |
28 | public int getDegree() {
29 | return coeffs.length - 1;
30 | }
31 |
32 | @Override
33 | public String toString() {
34 | String coeffsString = "";
35 | for (int degree = coeffs.length - 1; degree >= 0; degree--) {
36 | if (coeffs[degree] == 0) {
37 | continue;
38 | }
39 | if (degree == coeffs.length - 1) {
40 | coeffsString += Math.floor(coeffs[degree] * 100) / 100;
41 | }else{
42 | coeffsString += (coeffs[degree] > 0 ? " + " : " - ");
43 | coeffsString += Math.floor(coeffs[degree] * 100) / 100;
44 | }
45 |
46 | if (degree >= 2) {
47 | coeffsString += "x^" + degree;
48 | } else if (degree == 1) {
49 | coeffsString += "x";
50 | }
51 | }
52 | return coeffsString;
53 | }
54 |
55 | public double evaluate(double x) {
56 | double ans = 0;
57 | for (int degree = 0; degree < coeffs.length; degree++) {
58 | ans += Math.pow((coeffs[degree] * x), degree);
59 | }
60 | return ans;
61 | }
62 |
63 | public MyPolynomial add(MyPolynomial another) {
64 | int size = coeffs.length > another.getDegree() + 1 ? coeffs.length : another.getDegree() + 1;
65 |
66 | double[] ans = new double[size];
67 |
68 | for (int degree = 0; degree < ans.length; degree++) {
69 | double add = 0;
70 | if (degree <= this.getDegree()) {
71 | add += coeffs[degree];
72 | }
73 | if (degree <= another.getDegree()) {
74 | add += another.coeffs[degree];
75 | }
76 | ans[degree] = add;
77 | }
78 |
79 | return new MyPolynomial(ans);
80 | }
81 |
82 | public MyPolynomial multiply(MyPolynomial another) {
83 | int size = getDegree() + another.getDegree() + 1;
84 | double[] ans = new double[size];
85 |
86 | for (int deg1 = 0; deg1 < coeffs.length; deg1++) {
87 | for (int deg2 = 0; deg2 < another.coeffs.length; deg2++) {
88 | double num = coeffs[deg1] * another.coeffs[deg2];
89 | ans[deg1 + deg2] += num;
90 | }
91 | }
92 |
93 | return new MyPolynomial(ans);
94 | }
95 |
96 | }
97 |
98 | class TestMyPolynomial {
99 | public static void main(String[] args) {
100 | MyPolynomial p1 = new MyPolynomial(1.1, 2.2, 3.3);
101 | MyPolynomial p2 = new MyPolynomial(1.1, 2.2, 3.3, 4.4, 5.5);
102 | double[] coeffs = {1.2, 3.4, 5.6, 7.8} ;
103 | MyPolynomial p3 = new MyPolynomial(coeffs);
104 | System.out.println(p2);
105 | System.out.println(p3);
106 | System.out.println(p2.add(p3));
107 | System.out.println(p3.multiply(p1));
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/Check/Inheritance/ItemManager/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 | Builds, tests, and runs the project ItemManager.
12 |
13 |
73 |
74 |
--------------------------------------------------------------------------------
/2. Exercises on Composition/Invoice/Invoice.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package Invoice;
7 |
8 | /**
9 | *
10 | * @author GIA KINH
11 | */
12 | class Customer {
13 |
14 | private int id;
15 | private String name;
16 | private int discount;
17 |
18 | public Customer(int id, String name, int discount) {
19 | this.id = id;
20 | this.name = name;
21 | this.discount = discount;
22 | }
23 |
24 | public int getID() {
25 | return id;
26 | }
27 |
28 | public void setID(int id) {
29 | this.id = id;
30 | }
31 |
32 | public String getName() {
33 | return name;
34 | }
35 |
36 | public void setName(String name) {
37 | this.name = name;
38 | }
39 |
40 | public int getDiscount() {
41 | return discount;
42 | }
43 |
44 | public void setDiscount(int discount) {
45 | this.discount = discount;
46 | }
47 |
48 | @Override
49 | public String toString() {
50 | return name + "(" + id + ")" + "(" + discount + "%)";
51 | }
52 | }
53 |
54 | public class Invoice {
55 |
56 | private int id;
57 | private Customer customer;
58 | private double amount;
59 |
60 | public Invoice(int id, Customer customer, double amount) {
61 | this.id = id;
62 | this.customer = customer;
63 | this.amount = amount;
64 | }
65 |
66 | public int getID() {
67 | return id;
68 | }
69 |
70 | public void setID(int id) {
71 | this.id = id;
72 | }
73 |
74 | public Customer getCustomer() {
75 | return customer;
76 | }
77 |
78 | public void setCustomer(Customer customer) {
79 | this.customer = customer;
80 | }
81 |
82 | public double getAmount() {
83 | return amount;
84 | }
85 |
86 | public void setAmount(double amount) {
87 | this.amount = amount;
88 | }
89 |
90 | public int getCustomerID() {
91 | return this.customer.getID();
92 | }
93 |
94 | public String getCustomerName() {
95 | return this.customer.getName();
96 | }
97 |
98 | public int getCustomerDiscount() {
99 | return this.customer.getDiscount();
100 | }
101 |
102 | public double getAmountAfterDiscount() {
103 | return this.amount - ((this.amount * this.customer.getDiscount()) / 100);
104 | }
105 |
106 | @Override
107 | public String toString() {
108 | return "Invoice[" + "id=" + id + ",customer=" + customer + ",amount=" + amount + "]";
109 | }
110 | }
111 |
112 | class TestInvoice {
113 |
114 | public static void main(String[] args) {
115 | Customer c1 = new Customer(88, "Tan Ah Teck", 10);
116 | System.out.println(c1); // Customer's toString()
117 |
118 | c1.setDiscount(8);
119 | System.out.println(c1);
120 | System.out.println("id is: " + c1.getID());
121 | System.out.println("name is: " + c1.getName());
122 | System.out.println("discount is: " + c1.getDiscount());
123 |
124 | // Test Invoice class
125 | Invoice inv1 = new Invoice(101, c1, 888.8);
126 | System.out.println(inv1);
127 |
128 | inv1.setAmount(999.9);
129 | System.out.println(inv1);
130 | System.out.println("id is: " + inv1.getID());
131 | System.out.println("customer is: " + inv1.getCustomer()); // Customer's toString()
132 | System.out.println("amount is: " + inv1.getAmount());
133 | System.out.println("customer's id is: " + inv1.getCustomerID());
134 | System.out.println("customer's name is: " + inv1.getCustomerName());
135 | System.out.println("customer's discount is: " + inv1.getCustomerDiscount());
136 | System.out.printf("amount after discount is: %.2f%n", inv1.getAmountAfterDiscount());
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/2. Exercises on Composition/MyCircle/MyCircle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package MyCircle;
7 |
8 | import java.util.Arrays;
9 |
10 | /**
11 | *
12 | * @author GIA KINH
13 | */
14 |
15 | class MyPoint {
16 |
17 | private int x;
18 | private int y;
19 |
20 | public MyPoint() {
21 | }
22 |
23 | public MyPoint(int x, int y) {
24 | this.x = x;
25 | this.y = y;
26 | }
27 |
28 | public int getX() {
29 | return x;
30 | }
31 |
32 | public void setX(int x) {
33 | this.x = x;
34 | }
35 |
36 | public int getY() {
37 | return y;
38 | }
39 |
40 | public void setY(int y) {
41 | this.y = y;
42 | }
43 |
44 | public int[] getXY() {
45 | return new int[]{this.x, this.y};
46 | }
47 |
48 | public void setXY(int x, int y) {
49 | this.x = x;
50 | this.y = y;
51 | }
52 |
53 | public double distance(int x, int y) {
54 | return (double) Math.sqrt(Math.pow((x - this.x), 2) + Math.pow((y - this.y), 2));
55 | }
56 |
57 | public double distance(MyPoint another) {
58 | return (double) Math.sqrt(Math.pow((another.x - this.x), 2) + Math.pow((another.y - this.y), 2));
59 | }
60 |
61 | public double distance() {
62 | return (double) Math.sqrt(Math.pow((0 - this.x), 2) + Math.pow((0 - this.y), 2));
63 | }
64 |
65 | @Override
66 | public String toString() {
67 | return "(" +x + "," + y + ')';
68 | }
69 | }
70 |
71 |
72 | public class MyCircle {
73 | private MyPoint center;
74 | private int radius;
75 |
76 | public MyCircle() {
77 | }
78 |
79 | public MyCircle(MyPoint center, int radius) {
80 | this.center = center;
81 | this.radius = radius;
82 | }
83 |
84 | public MyCircle(int x, int y, int radius) {
85 | this.center = new MyPoint(x,y);
86 | this.radius = radius;
87 | }
88 |
89 | public MyPoint getCenter() {
90 | return center;
91 | }
92 |
93 | public void setCenter(MyPoint center) {
94 | this.center = center;
95 | }
96 |
97 | public int getRadius() {
98 | return radius;
99 | }
100 |
101 | public void setRadius(int radius) {
102 | this.radius = radius;
103 | }
104 |
105 | public int getCenterX(){
106 | return this.center.getX();
107 | }
108 | public int getCenterY(){
109 | return this.center.getY();
110 | }
111 |
112 | public void setCenterX(int x){
113 | this.center.setX(x);
114 | }
115 |
116 | public void setCenterY(int y){
117 | this.center.setX(y);
118 | }
119 |
120 | public int[] getCenterXY() {
121 | return this.center.getXY();
122 | }
123 |
124 | public void setCenterXY(int x, int y) {
125 | this.center.setXY(x,y);
126 | }
127 |
128 | public double getArea(){
129 | return Math.pow(this.radius, 2) * Math.PI;
130 | }
131 |
132 | public double getCircumference(){
133 | return this.radius * 2 * Math.PI;
134 | }
135 |
136 | public double distance(MyCircle another){
137 | return this.center.distance(another.center);
138 | }
139 |
140 | @Override
141 | public String toString() {
142 | return "MyCircle[" + "radius=" +radius+",center=" + this.center.toString() + ']';
143 | }
144 | }
145 |
146 | class TestMyCircle{
147 | public static void main(String[] args) {
148 | MyCircle myCircle = new MyCircle(new MyPoint(5, 8), 6);
149 | System.out.println(myCircle.distance(new MyCircle(new MyPoint(30,46),2)));
150 | System.out.println(myCircle.getArea());
151 | System.out.println(myCircle.getCircumference());
152 | System.out.println(myCircle.toString());
153 | System.out.println(Arrays.toString(myCircle.getCenterXY()));
154 | }
155 | }
--------------------------------------------------------------------------------
/BAI TAP/player/PlayerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package player;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Collections;
10 | import java.util.Scanner;
11 |
12 | /**
13 | *
14 | * @author GIA KINH
15 | */
16 | public class PlayerTest {
17 |
18 | public static Scanner scanner = new Scanner(System.in);
19 | public static int choose;
20 | public static ArrayList players = new ArrayList();
21 | public static ArrayList tournamentIncomes = new ArrayList();
22 |
23 | public static void menu() {
24 | System.out.println("1.Nhap thong tin cau thu moi");
25 | System.out.println("2.Tinh thu nhap chua co thuong");
26 | System.out.println("3.Cap nhat diem thi dau cua cau thu");
27 | System.out.println("4.Tinh tien thuong");
28 | System.out.println("5.Thoat");
29 | System.out.print("Enter number: ");
30 | choose = scanner.nextInt();
31 | scanner.nextLine();
32 | }
33 |
34 | public static void enterPlayer() {
35 | System.out.print("Enter name: ");
36 | String name = scanner.nextLine();
37 | System.out.print("Enter income: ");
38 | double income = scanner.nextDouble();
39 | PlayerIncome playerIncome = new PlayerIncome(income, name);
40 | players.add(playerIncome);
41 | TournamentIncome tournamentIncome = new TournamentIncome(playerIncome.getIncome(), playerIncome.getName());
42 | tournamentIncomes.add(tournamentIncome);
43 | }
44 |
45 | public static void cacularIncome() {
46 | for (PlayerIncome playerIncome : players) {
47 | playerIncome.displayDetails();
48 | }
49 | }
50 |
51 | public static void enterPointPlayer() {
52 | boolean isPlayer = false;
53 | String name;
54 | int vt=0;
55 | do {
56 | System.out.print("Enter name player: ");
57 | name = scanner.nextLine();
58 | int d=0;
59 | for(PlayerIncome playerIncome: players){
60 | if(playerIncome.getName().equals(name)){
61 | vt=d;
62 | isPlayer = true;
63 | break;
64 | }
65 | d++;
66 | }
67 | } while (isPlayer==false);
68 | System.out.print("Enter total matches: ");
69 | int totalMatches = scanner.nextInt();
70 | System.out.print("Enter battle: ");
71 | int battle = scanner.nextInt();
72 | double point[] = new double[battle];
73 | for (int i = 0; i < battle; i++) {
74 | System.out.print("Enter point in battle " + (i+1) +": ");
75 | point[i] = scanner.nextDouble();
76 | }
77 | TournamentIncome tournamentIncome = new TournamentIncome(totalMatches, battle, point, players.get(vt).getIncome(), players.get(vt).getName());
78 | tournamentIncomes.set(vt, tournamentIncome);
79 | }
80 |
81 | public static void cacularTournamentIncome() {
82 | for (TournamentIncome tournamentIncome : tournamentIncomes) {
83 | tournamentIncome.displayDetails();
84 | }
85 | }
86 |
87 | public static void main(String[] args) {
88 | do {
89 | menu();
90 | switch (choose) {
91 | case 1:
92 | enterPlayer();
93 | break;
94 | case 2:
95 | cacularIncome();
96 | break;
97 | case 3:
98 | enterPointPlayer();
99 | break;
100 | case 4:
101 | cacularTournamentIncome();
102 | break;
103 | case 5:
104 | break;
105 | }
106 | } while (choose != 5);
107 |
108 | }
109 | }
110 |
--------------------------------------------------------------------------------