update,
This commit is contained in:
33
it114105/itp3914/Lab10/Ex1/Employee.java
Normal file
33
it114105/itp3914/Lab10/Ex1/Employee.java
Normal file
@@ -0,0 +1,33 @@
|
||||
public class Employee {
|
||||
String name;
|
||||
int salary;
|
||||
|
||||
Employee(String name, int salary){
|
||||
this.name = name;
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public Employee() {}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public int getSalary() {
|
||||
return salary;
|
||||
}
|
||||
public void setSalary(int salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public void displayDetails(){
|
||||
System.out.printf("Employee 1: name=%s salary=%d\n", name, salary);
|
||||
}
|
||||
|
||||
public void raiseSalary(double perc){
|
||||
salary = (int)(salary * perc) + salary;
|
||||
}
|
||||
|
||||
}
|
24
it114105/itp3914/Lab10/Ex1/Ex1b.java
Normal file
24
it114105/itp3914/Lab10/Ex1/Ex1b.java
Normal file
@@ -0,0 +1,24 @@
|
||||
class Ex1b {
|
||||
public static void main(String[] args) {
|
||||
Employee emp1 = new Employee();
|
||||
Employee emp2 = new Employee();
|
||||
int oldSalary;
|
||||
// Part 1-2 here
|
||||
emp1.setName("Chan Tai Man");
|
||||
emp1.setSalary(12000);
|
||||
emp2.setName("Tam Pring Shing");
|
||||
emp2.setSalary(13500);
|
||||
// Part 3 below
|
||||
System.out.println("Before-");
|
||||
System.out.println("Employee 1: name=" + emp1.getName() + " salary=" + emp1.getSalary());
|
||||
System.out.println("Employee 2: name=" + emp2.getName() + " salary=" + emp2.getSalary());
|
||||
// Part 4-5 here
|
||||
emp1.setSalary((int) (emp1.getSalary()*0.1)+emp1.getSalary());
|
||||
emp2.setSalary(((int) (emp2.getSalary()*0.05) + emp2.getSalary()));
|
||||
System.out.println("After-");
|
||||
System.out.println("Employee 1: name=" + emp1.getName() + " salary=" + emp1.getSalary());
|
||||
System.out.println("Employee 2: name=" + emp2.getName() + " salary=" + emp2.getSalary());
|
||||
|
||||
|
||||
}
|
||||
}
|
15
it114105/itp3914/Lab10/Ex1/Ex1cde.java
Normal file
15
it114105/itp3914/Lab10/Ex1/Ex1cde.java
Normal file
@@ -0,0 +1,15 @@
|
||||
public class Ex1cde {
|
||||
public static void main(String[] args) {
|
||||
Employee emp1 = new Employee("Chan Tai Main", 12000);
|
||||
Employee emp2 = new Employee("Tam Ping Shing", 13500);
|
||||
System.out.println("Before-");
|
||||
emp1.displayDetails();
|
||||
emp2.displayDetails();
|
||||
|
||||
emp1.raiseSalary(0.1);
|
||||
emp2.raiseSalary(0.05);
|
||||
System.out.println("After-");
|
||||
emp1.displayDetails();
|
||||
emp2.displayDetails();
|
||||
}
|
||||
}
|
24
it114105/itp3914/Lab10/Ex2/Ex2b.java
Normal file
24
it114105/itp3914/Lab10/Ex2/Ex2b.java
Normal file
@@ -0,0 +1,24 @@
|
||||
public class Ex2b {
|
||||
public static void main(String[] args) {
|
||||
Student stud1 = new Student();
|
||||
Student stud2 = new Student();
|
||||
Student stud3 = new Student();
|
||||
|
||||
stud1.setId(310567);
|
||||
stud1.setName("Cheung Siu Ming");
|
||||
stud1.setScore(87.1);
|
||||
|
||||
stud2.setId(451267);
|
||||
stud2.setName("Ng Wau Man");
|
||||
stud2.setScore(77.5);
|
||||
|
||||
stud3.setId(789014);
|
||||
stud3.setName("Wong Sui Kai");
|
||||
stud3.setScore(83.4);
|
||||
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud1.getName(), stud1.getId(), stud1.getScore());
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud2.getName(), stud2.getId(), stud2.getScore());
|
||||
System.out.printf("Student: name=%s id=%d score=%.1f\n", stud3.getName(), stud3.getId(), stud3.getScore());
|
||||
System.out.println("Average Score =" + (stud1.getScore()+stud2.getScore()+stud3.getScore())/ 3);
|
||||
}
|
||||
}
|
15
it114105/itp3914/Lab10/Ex2/Ex2c.java
Normal file
15
it114105/itp3914/Lab10/Ex2/Ex2c.java
Normal file
@@ -0,0 +1,15 @@
|
||||
public class Ex2c {
|
||||
public static void main(String[] args) {
|
||||
Student[] students = new Student[3];
|
||||
students[0] = new Student("Cheung Siu Ming", 310567, 87.1);
|
||||
students[1] = new Student("CNg Wai Man", 451267, 77.5);
|
||||
students[2] = new Student("Wong Sui Kai", 789014, 83.4);
|
||||
double totalScore = 0.0;
|
||||
for (Student student : students) {
|
||||
student.printDetails();
|
||||
totalScore += student.getScore();
|
||||
}
|
||||
System.out.println("Average Score = " + totalScore/students.length);
|
||||
|
||||
}
|
||||
}
|
41
it114105/itp3914/Lab10/Ex2/Student.java
Normal file
41
it114105/itp3914/Lab10/Ex2/Student.java
Normal file
@@ -0,0 +1,41 @@
|
||||
public class Student {
|
||||
String name;
|
||||
int id;
|
||||
double score;
|
||||
|
||||
Student(){}
|
||||
|
||||
Student(String name, int id, double score) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setScore(double score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public double getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void printDetails(){
|
||||
System.out.println("Student: name="+name+" id=" + id + " score="+score);
|
||||
}
|
||||
}
|
35
it114105/itp3914/Lab10/Ex3/Ex3.java
Normal file
35
it114105/itp3914/Lab10/Ex3/Ex3.java
Normal file
@@ -0,0 +1,35 @@
|
||||
class AStudent {
|
||||
private String name;
|
||||
public int age;
|
||||
|
||||
public void setName(String inName) {
|
||||
name = inName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
if(age<0 || age>120){
|
||||
this.age = 18;
|
||||
System.out.println("Invalid age:" + age + ", reset it to 18");
|
||||
}else
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
|
||||
public class Ex3 {
|
||||
public static void main(String s[]) {
|
||||
AStudent stud1 = new AStudent();
|
||||
AStudent stud2 = new AStudent();
|
||||
stud1.setName("Chan Tai Man");
|
||||
stud1.setAge(19);
|
||||
stud2.setName("Ng Hing");
|
||||
stud2.setAge(-23);
|
||||
System.out.println("Student: name="+stud1.getName() +
|
||||
", age=" + stud1.age);
|
||||
System.out.println("Student: name="+stud2.getName() +
|
||||
", age=" + stud2.age);
|
||||
}
|
||||
}
|
48
it114105/itp3914/Lab10/Ex4/CurrencyConverter.java
Normal file
48
it114105/itp3914/Lab10/Ex4/CurrencyConverter.java
Normal file
@@ -0,0 +1,48 @@
|
||||
class CurrencyConverter {
|
||||
private double exchangeRate;
|
||||
private double commissionRate;
|
||||
private int largeAmount;
|
||||
|
||||
public CurrencyConverter(double er , double cr){
|
||||
exchangeRate = er;
|
||||
commissionRate = cr;
|
||||
}
|
||||
|
||||
public double fromUSDollar(double dollar){
|
||||
if (dollar >= largeAmount)
|
||||
return (dollar * exchangeRate * (1-commissionRate*0.5));
|
||||
else
|
||||
return (dollar * exchangeRate * (1-commissionRate));
|
||||
}
|
||||
|
||||
public double toUSDollar(double foreignMoney){
|
||||
if (foreignMoney/exchangeRate >= largeAmount)
|
||||
return (foreignMoney/exchangeRate*(1-commissionRate*0.5));
|
||||
else
|
||||
return (foreignMoney/exchangeRate*(1-commissionRate));
|
||||
}
|
||||
|
||||
public void setExchangeRate(double rate){
|
||||
exchangeRate = rate;
|
||||
}
|
||||
|
||||
public double getExchangeRate(){
|
||||
return exchangeRate;
|
||||
}
|
||||
|
||||
public void setCommissionRate(double rate){
|
||||
commissionRate = rate;
|
||||
}
|
||||
|
||||
public double getCommissionRate(){
|
||||
return commissionRate;
|
||||
}
|
||||
|
||||
public void setLargeAmount(int amount){
|
||||
largeAmount = amount;
|
||||
}
|
||||
|
||||
public int getLargeAmount(){
|
||||
return largeAmount;
|
||||
}
|
||||
}
|
19
it114105/itp3914/Lab10/Ex4/Ex4.java
Normal file
19
it114105/itp3914/Lab10/Ex4/Ex4.java
Normal file
@@ -0,0 +1,19 @@
|
||||
public class Ex4 {
|
||||
public static void main(String[] args) {
|
||||
CurrencyConverter yenConverter = new CurrencyConverter(115.7, 0.0005);
|
||||
CurrencyConverter euroConverter = new CurrencyConverter(0.9881, 0.0003);
|
||||
|
||||
yenConverter.setLargeAmount(50000);
|
||||
euroConverter.setLargeAmount(50000);
|
||||
int yens = 1500000;
|
||||
System.out.println(yens + " yens = US$ " + yenConverter.toUSDollar(yens));
|
||||
int usd = 20000;
|
||||
System.out.println("US$ " + usd + " = " + yenConverter.fromUSDollar(usd) + " yens");
|
||||
|
||||
int euros = 170000;
|
||||
System.out.println(euros + " euros = US$ " + euroConverter.toUSDollar(euros));
|
||||
usd = 20000;
|
||||
System.out.println("US$ " + usd + " = " + euroConverter.fromUSDollar(usd) + " euros");
|
||||
|
||||
}
|
||||
}
|
241
it114105/itp3914/Lab10/README.md.original
Normal file
241
it114105/itp3914/Lab10/README.md.original
Normal file
@@ -0,0 +1,241 @@
|
||||
# Lab 10 Objects and Classes
|
||||
## Topic 4.1 to 4.4 Part (a) Objects and Classes
|
||||
|
||||
## Exercise 1
|
||||
|
||||
(a) Create a class `Employee` that has
|
||||
|
||||
Variables:
|
||||
| Instance Variable name | Data type |
|
||||
| - | - |
|
||||
| name | String |
|
||||
| salary | int |
|
||||
|
||||
Methods:`getName()`, `setName()`, `getSalary()` and `setSalary()`
|
||||
|
||||
(b) Complete the following test program that creates two `Employee` object instances named `emp1` and `emp2`. Then perform the followings:
|
||||
1. Set the name and salary of `emp1` to *"Chan Tai Man"* and 12000, respectively.
|
||||
2. Set the name and salary of `emp2` to *"Tam Ping Shing"* and 13500, respectively.
|
||||
3. Print the current details of `emp1` and ``emp2``.
|
||||
4. Increase the salary of *"Chan Tai Man"* by 10% and the salary of *"Tam Ping Shing"* by 5%.
|
||||
5. Print the new details of `emp1` and `emp2`.
|
||||
|
||||
```java
|
||||
public class Ex1b {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Employee emp1 = new Employee();
|
||||
Employee emp2 = new Employee();
|
||||
int oldSalary;
|
||||
|
||||
// Part 1-2 here
|
||||
// Part 3 below
|
||||
System.out.println("Before-");
|
||||
System.out.println("Employee 1: name=" + emp1.getName() +" salary=" + emp1.getSalary());
|
||||
System.out.println("Employee 2: name=" + emp2.getName() +" salary=" + emp2.getSalary());
|
||||
|
||||
// Part 4-5 here
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The output of the program is shown below.
|
||||
```
|
||||
C:\> java Ex1b
|
||||
|
||||
Before-
|
||||
Employee 1: name=Chan Tai Man salary=12000
|
||||
Employee 2: name=Tam Ping Shing salary=13500
|
||||
After-
|
||||
Employee 1: name=Chan Tai Man salary=13200
|
||||
Employee 2: name=Tam Ping Shing salary=14175
|
||||
```
|
||||
|
||||
(c) Redo Part (b) by adding a Constructor to set the name and salary.
|
||||
|
||||
(d) Add a method `displayDetails()` to display the name and salary in Employee class, and modify Ex1b to use `displayDetails()`to print the details of `emp1` and `emp2`.
|
||||
|
||||
(e) Add method `raiseSalary()` that accepts a percentage increase in salary (`double`) as argument and increases the salary by the corresponding percentage. Create a new Test program `Ex1cde.java` to raise Chan’s salary by 10% and that of Tam by 5%. Print the details of `emp1` and `emp2` after the increase of salaries.
|
||||
|
||||
```
|
||||
C:\> java Ex1cde
|
||||
|
||||
Before-
|
||||
Employee: name=Chan Tai Man salary=12000
|
||||
Employee: name=Tam Ping Shing salary=13500
|
||||
After-
|
||||
Employee: name=Chan Tai Man salary=13200
|
||||
Employee: name=Tam Ping Shing salary=14175
|
||||
```
|
||||
|
||||
## Exercise 2
|
||||
(a) Create a class `Student` that has
|
||||
|
||||
Variables:
|
||||
| Attribute name | Data type |
|
||||
| - | - |
|
||||
| name | String |
|
||||
| id | int |
|
||||
| score | double |
|
||||
|
||||
Methods: *the getter and setter methods for each of the above attributes.*
|
||||
|
||||
|
||||
(b) Write a test program Ex2b.java that creates three Student object instances named `stud1`, `stud2` and `stud3`. Then perform the followings:
|
||||
1. Set the name, id and score of stud1 to *"Cheung Siu Ming"*, 310567 and 87.1.
|
||||
2. Set the name, id and score of stud2 to *"Ng Wai Man"*, 451267 and 77.5.
|
||||
3. Set the name, id and score of stud3 to *"Wong Sui Kai"*, 789014 and 83.4.
|
||||
4. Print the details of `stud1`, `stud2` and `stud3`.
|
||||
5. Find and print the average score among the three students.
|
||||
|
||||
The output of the program is shown below.
|
||||
```
|
||||
C:\> java Ex2b
|
||||
Student : name=Cheung Siu Ming id=310567 score=87.1
|
||||
Student : name=Ng Wai Man id=451267 score=77.5
|
||||
Student : name=Wong Sui Kai id=789014 score=83.4
|
||||
|
||||
Average Score = 82.66666666666667
|
||||
```
|
||||
|
||||
(c) Redo Part (b) by adding a Constructor to set the name, id and score and then create a new test program `Ex2c.java` to test it.
|
||||
|
||||
```
|
||||
C:\> java Ex2c
|
||||
Student : name=Cheung Siu Ming id=310567 score=87.1
|
||||
Student : name=Ng Wai Man id=451267 score=77.5
|
||||
Student : name=Wong Sui Kai id=789014 score=83.4
|
||||
|
||||
Average Score = 82.66666666666667
|
||||
```
|
||||
|
||||
## Exercise 2
|
||||
Consider the following classes.
|
||||
|
||||
```java
|
||||
class AStudent {
|
||||
private String name;
|
||||
public int age;
|
||||
|
||||
public void setName(String inName) {
|
||||
name = inName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class Ex3 {
|
||||
|
||||
public static void main(String s[]) {
|
||||
AStudent stud1 = new AStudent();
|
||||
AStudent stud2 = new AStudent();
|
||||
stud1.setName("Chan Tai Man");
|
||||
stud1.age = 19;
|
||||
stud2.setName("Ng Hing");
|
||||
stud2.age = -23;
|
||||
System.out.println("Student: name=" + stud1.getName() +", age=" + stud1.age);
|
||||
|
||||
System.out.println("Student: name=" + stud2.getName() +", age=" + stud2.age);
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For the object `stud2`, age is -23 which is a negative value, this is not allowed in human being.
|
||||
|
||||
Enhance the class `AStudent` by enforcing data encapsulation on the attribute `age`. If the inputted `age` is invalid, print an error message and set the age to 18.
|
||||
|
||||
```
|
||||
C:\> java Ex3
|
||||
-23 is not allowed! Age must be greater than or equal to 18!
|
||||
Student: name=Chan Tai Man, age=19
|
||||
Student: name=Ng Hing, age=18
|
||||
```
|
||||
|
||||
## Exercise 4
|
||||
Given the following CurrencyConverter class which handles the exchange between a foreign currency and US dollars.
|
||||
```java
|
||||
public class CurrencyConverter {
|
||||
private double exchangeRate;
|
||||
private double commissionRate;
|
||||
private int largeAmount;
|
||||
|
||||
public CurrencyConverter(double er, double cr) {
|
||||
exchangeRate = er;
|
||||
commissionRate = cr;
|
||||
}
|
||||
|
||||
public double fromUSDollar(double dollar) {
|
||||
|
||||
if (dollar >= largeAmount)
|
||||
return (dollar * exchangeRate * (1 - commissionRate * 0.5));
|
||||
else
|
||||
return (dollar * exchangeRate * (1 - commissionRate));
|
||||
|
||||
}
|
||||
|
||||
public double toUSDollar(double foreignMoney) {
|
||||
|
||||
if (foreignMoney / exchangeRate >= largeAmount)
|
||||
return (foreignMoney / exchangeRate * (1 - commissionRate * 0.5));
|
||||
else
|
||||
return (foreignMoney / exchangeRate * (1 - commissionRate));
|
||||
|
||||
}
|
||||
|
||||
public void setExchangeRate(double rate) {
|
||||
exchangeRate = rate;
|
||||
}
|
||||
|
||||
public double getExchangeRate() {
|
||||
return exchangeRate;
|
||||
}
|
||||
|
||||
public void setCommissionRate(double rate) {
|
||||
commissionRate = rate;
|
||||
}
|
||||
|
||||
public double getCommissionRate() {
|
||||
return commissionRate;
|
||||
}
|
||||
|
||||
public void setLargeAmount(int amount) {
|
||||
largeAmount = amount;
|
||||
}
|
||||
|
||||
public int getLargeAmount() {
|
||||
return largeAmount;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Write a test program `Ex4.java` to test the CurrencyConverter class by performing the following operations:
|
||||
|
||||
1. Instantiate two `CurrencyConverter` object with the following attributes:
|
||||
|
||||
| | exchangeRate | comissionRate |
|
||||
| - | - | - |
|
||||
| yenConverter | 115.7 | 0.0005 |
|
||||
| euroCOnverter | 0.9881 | 0.0003 |
|
||||
|
||||
2. Set `largeAmount` to 50000.
|
||||
3. Use the `yenConverter` to convert 1500000 yens to US dollars.
|
||||
4. Use the `yenConverter` to convert from US$2000 to yens.
|
||||
5. Set the exchange rate of euro to 0.9881.
|
||||
6. Use the `euroConverter` to convert 170000 euros to US$.
|
||||
7. Use the `euroConverter` to convert from US$20000 to euros.
|
||||
|
||||
Sample output
|
||||
```
|
||||
C:\> java Ex4
|
||||
1500000 yens = US$ 12958.0812445981
|
||||
US$ 2000 = 231284.30000000002 yens
|
||||
|
||||
170000 euros = US$ 172021.55652261918
|
||||
US$ 20000 = 19756.0714 euros
|
||||
```
|
Reference in New Issue
Block a user