Skip to content

Commit 352f470

Browse files
committed
first commit
0 parents  commit 352f470

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3876
-0
lines changed

Diff for: 1. Exercises on Classes/Account.java

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

Diff for: 1. Exercises on Classes/Ball.java

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

Diff for: 1. Exercises on Classes/Circle.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+

Diff for: 1. Exercises on Classes/Date.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

0 commit comments

Comments
 (0)