2. 1
STIA1123 | Programming 2
STIA1123 Assignment 1
(Due Thursday 27 April 2015)
Answer all questions below. For every questions, you must submit a hardcopy and a softcopy of your source
code (.java file). Also, in the hardcopy, print some sample outputs from the execution of your programs.
------------------------------------------------------------------------------------------------------------------------
Reminder: Each source code submitted must be the result of your own work. Copied source codes will
earned you zero (0) mark.
1. An email address of a student is given in the form:
<name>@<school abbrev>.uum.edu.my
e.g. hakim@ibs.uum.edu.my
lokman@soc.uum.edu.my
soon_kiat@sois.uum.edu.my
siva@soa.uum.edu.my
Write a Java program that can read a student email address (in the above format) and display
as output the name of the student and his/her complete school name. For example:
If the address is lokman@soc.uum.edu.my, the output will be:
Name: Lokman
School: School of Computing
If the address is soon_kiat@sois.uum.edu.my, the output will be:
Name: Soon_kiat
School: School of International Studies
Note:
Assume there are only 4 schools which are:
Abbreviation School Name
ibs Islamic Business School
soa School of Accounting
soc School of Computing
sois School of International Studies
3. 2
STIA1123 | Programming 2
For the name, just display the name from the email but with the first letter capitalized.
If the address is not given in the described format above, output an appropriate error message.
Name of source code file to be submitted: Asg11.java
2. When a six-sided dice is rolled, the number on the top face is between 1 and 6. Design and implement a
class, called Dice, to represent a die. The default constructor should initialize a dice to the number 1.
Your class must contain the method rollDice() that returns a random number between 1 and 6 (simulating
the throwing of a dice). Write a program that inputs an integer N (representing how many times to throw
the dice) and display as output how many times each of the number between 1 to 6 are generated from
the N throws of the dice.
Sample running 1:
Enter N> 10
No 1 = 2
No 2 = 1
No 3 = 1
No 4 = 1
No 5 = 2
No 6 = 3
Sample running 2:
Enter N> 10
No 1 = 1
No 2 = 0
No 3 = 2
No 4 = 3
No 5 = 2
No 6 = 2
Name of source code file to be submitted: Die.java
Asg12.java
4. 3
STIA1123 | Programming 2
3 a). Given below is the UML diagram for a Person class inheritance hierarchy:
Write the definition of all five classes in the above UML diagram. The display() method for
Employee object will print out the name and id of the Employee object. The display() method
for FullTime object will print out the name, id and salary of the FullTime object.
5. 4
STIA1123 | Programming 2
Write a test program named Asg13 that creates
i) a FullTime object and display his/her name , id and salary.
ii) a PartTime object and display his/her name and id.
In each case you must prompt the user to input all the needed values.
Name of source code file to be submitted: Person.java, Employee.java, Customer.java
FullTime.java, PartTime.java & Asg13a.java
3 b). The class Employee has another method named equals() which can be used to compare whether an
Employee object is equal to another Employee object. The header of this method is as follows:
public boolean equals(Employee emp)
Two employees are equal if both have the same name and id.
Write the complete definition of this equals() method in Employee class.
On the other hand, for two FullTime to be equal, they must have the same name, id and salary. Thus,
you also need to override the equals() method in the FullTime class.
Write a test program named Asg13b that creates two Employee objects & two FullTime objects
and test their equals() method.
Name of source code file to be submitted: Employee.java, FullTime.java & Asg13b.java
5. Create an abstract class named Account for a bank. Include an integer field for the account number and
a double field for the account balance. Both field are private. Also include a constructor that requires an
account number and a balance value and sets these values to the appropriate fields. Add two public get
6. 5
STIA1123 | Programming 2
methods for the fields – getAccountNumber() and getAccountBalance() – each of which returns the
appropriate field. Also add an abstract method named display().
Create two subclasses of Account: Current and Saving. Within the Current class, the display method
displays the string “Current Account Information”, the account number and the balance. Within the Saving
class, add a field to hold the interest rate and require the Saving constructor to accept an argument for the
value of the interest rate. The Saving display method displays the string “Saving Account Information”, the
account number, the balance and the interest rate.
a) Write an application named DemoAccount that demonstrates you can instantiate and display both a
Current and a Saving objects. Get user to input all the needed values.
Name of source code file to be submitted: Account.java, Current.java, Saving.java &
DemoAccount.java
b) Write an application named AccountArray in which you enter data for 10 Account objects and store
them in an array (use a for loop for this that repeat 10 times). For each input of the account object, first
ask the user whether to input Current or Saving object. Then ask the user to input all the appropriate data
values for either Current or Saving (depending on whether he/she chooses Current or Saving). After all
10 account objects have been created and stored in the array, use another for loop to display all the data
about all the Account objects in the array.
Name of source code file to be submitted: AccountArray.java
7. 6
STIA1123 | Programming 2
Email Address Checker
package Asg11;
import java.util.Scanner;
public class Asg11 {
//public static String name;
public static void main(String[] args) {
String name;
int index;
Scanner in = new Scanner(System.in);
String keyword = "@";
System.out.print("Please enter your email address> ");
name = in.nextLine();
index = name.indexOf(keyword);
if (name.matches("(.*)uum.edu.my(.*)")) {
System.out.println("Name: " + name.substring(0,
1).toUpperCase() + name.substring(1, index));
strMatches(name);
} else {
System.out.println("nWrong email address !");
}
}
9. 8
STIA1123 | Programming 2
1. Dice
public static void strMatches(String name) {
if (name.matches("(.*)ibs(.*)") == true) {
System.out.println("School:t Islamic Business School");
} else if (name.matches("(.*)soa(.*)") == true) {
System.out.println("School:t School of Accounting");
} else if (name.matches("(.*)soc(.*)") == true) {
System.out.println("School:t School of computing");
} else if (name.matches("(.*)sois(.*)") == true) {
System.out.println("School:t School of International
Studies");
} else {
System.out.println("You don't belong to any school");
}
}
}
10. 9
STIA1123 | Programming 2
Dice main class Asg12
package Dice;
import java.util.Scanner;
public class Asg12 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Dice dadu = new Dice();
int[] count = new int[6];
int number;
System.out.print("Enter N> ");
int N = in.nextInt();
for (int i = 0; i < N; i++) {
dadu.rollDice();
number = dadu.getNumber();
++count[number];
}
for (int i = 0; i < count.length; i++) {
System.out.printf("No%4d : %10dn", i + 1, count[i]);
}
}
}
11. 10
STIA1123 | Programming 2
Dice Class
package Dice;
import java.util.Random;
public class Dice {
private int number;
public Dice() {
number = 1;
}
public int rollDice() {
Random r = new Random();
number = r.nextInt(6);
return number;
}
public int getNumber() {
return number;
}
}
12. 11
STIA1123 | Programming 2
3a. Part-time and Fulltime Object
package Inheritance;
public class Customer extends
Person {
String address;
public Customer(){
}
public void
setAddress(String address) {
this.address = address;
}
public String getAddress()
{
return address;
}
}
Customer Class
package Inheritance;
public class PartTime extends
Employee {
private double hourlyWage;
public PartTime()
}
public PartTime(String
name, int id) {
super();
}
public void display() {
super.dispay();
}
public double
getHourlyWage() {
return hourlyWage;
}
}
PartTime Class
13. 12
STIA1123 | Programming 2
package Inheritance;
public class Employee extends
Person {
private int id;
public Employee() {
}
public void setID(int id) {
this.id = id;
}
public int getId() {
return id;
}
public Employee(String
name, int id) {
super(name);
this.id = id;
}
public void dispay() {
System.out.println("The
name of the employee is: " +
getName());
System.out.println("The
id of the employee is: " +
getId());
}
}
Employee Class without method comparing
package Inheritance;
public class FullTime extends
Employee {
private double salary;
public FullTime() {
}
public FullTime(String
name, int id, double salary) {
super(name, id);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void
setSalary(double salary) {
this.salary = salary;
}
public void display() {
super.dispay();
System.out.println("Salary: RM
" + getSalary());
}
}
FullTime Class without method comparing
14. 13
STIA1123 | Programming 2
package Inheritance;
public class Person {
private String name;
public Person() {
}
public Person(String name)
{
this.name = name;
}
public void setName(String
name) {
this.name = name;
}
public String getName() {
return name;
}
}
Person Class
17. 16
STIA1123 | Programming 2
Asg13b.
package Inheritance;
import java.util.Scanner;
public class Asg13b {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name;
int id;
Employee[] emp = new Employee[2];
FullTime[] ft = new FullTime[2];
//Employee Object
System.out.println("===__Employee__===");
for (int i = 0; i < emp.length; i++) {
emp[i] = new Employee();
System.out.print("Name " + (i + 1) + "t: ");
name = in.next();
emp[i].setName(name);
System.out.print("Enter the id: ");
id = in.nextInt();
emp[i].setID(id);
System.out.println("");
}
18. 17
STIA1123 | Programming 2
if (emp[0].equals(emp[1]) == true) {
System.out.println("****The same person !! ****");
} else {
System.out.println("***This is not the same person !***");
}
//Fulltime object
System.out.println("n===__Fulltime__===");
for (int i = 0; i < ft.length; i++) {
ft[i] = new FullTime();
System.out.print("Name " + (i + 1) + " : ");
name = in.next();
ft[i].setName(name);
System.out.print("Enter the id: ");
id = in.nextInt();
ft[i].setID(id);
System.out.print("enter salary: ");
double salary = in.nextDouble();
ft[i].setSalary(salary);
System.out.println("");
}
if (ft[0].equals(ft[1]) == true) {
System.out.println("****The same person !!****");
} else {
System.out.println("***This is not the same person !");
}
20. 19
STIA1123 | Programming 2
package Inheritance;
public class FullTime extends
Employee {
private double salary;
public FullTime() {
}
public FullTime(String
name, int id, double salary) {
super(name, id);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void
setSalary(double salary) {
this.salary = salary;
}
public void display() {
super.dispay();
System.out.println("Salary: RM
" + getSalary());
}
public boolean
equals(FullTime emp) {
return
(super.equals(emp) == true) &&
(salary == emp.getSalary());
}
}
package Inheritance;
public class Employee extends Person {
private int id;
public Employee() {
}
public void setID(int id) {
this.id = id;
}
public int getId() {
return id;
}
public Employee(String name, int id) {
super(name);
this.id = id;
}
public void dispay() {
System.out.println("The name of the
employee is: " + getName());
System.out.println("The id of the
employee is: " + getId());
}
public boolean equals(Employee emp) {
return (id == emp.getId()) &&
(getName().equals(emp.getName()));
}
}
21. 20
STIA1123 | Programming 2
Demo Account
Demo Account Class
package Bank;
import java.util.Scanner;
public class DemoAccount {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int nSave, nCurr;
double balSave, balCurr;
double rate;
Current cur = new Current();
22. 21
STIA1123 | Programming 2
Saving sav = new Saving();
System.out.println("#Current#");
System.out.println("------------");
System.out.print("Please enter account number: ");
nCurr = in.nextInt();
cur.setAccountNumber(nCurr);
System.out.print("Please enter the balance: ");
balCurr = in.nextInt();
cur.setAccountBalance(balCurr);
System.out.println("n#Saving#");
System.out.println("------------");
System.out.print("Please enter account number: ");
nSave = in.nextInt();
sav.setAccountNumber(nSave);
System.out.print("Please enter the balance: ");
balSave = in.nextInt();
sav.setAccountBalance(balSave);
System.out.print("Please enter the interest rate: ");
rate = in.nextDouble();
sav.setInterest(rate);
System.out.println("---------------------------------------");
//Output
System.out.println("nn#Current Output#");
cur.display();
System.out.println("");