Assignment Java Programming 2
Assignment Java Programming 2
Assignment 1
Achmad Zulkarnain
230753
1
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.
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:
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
STIA1123 | Programming 2
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.
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
Asg12.java
STIA1123 | Programming 2
3
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.
STIA1123 | Programming 2
4
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:
Two employees are equal if both have the same name and id.
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.
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
STIA1123 | Programming 2
5
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.
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.
STIA1123 | Programming 2
6
package Asg11;
import java.util.Scanner;
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 !");
}
}
STIA1123 | Programming 2
7
STIA1123 | Programming 2
8
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");
}
}
}
1. Dice
STIA1123 | Programming 2
9
package Dice;
import java.util.Scanner;
STIA1123 | Programming 2
10
Dice Class
package Dice;
import java.util.Random;
public Dice() {
number = 1;
}
STIA1123 | Programming 2
11
package Inheritance;
public PartTime()
String address; }
PartTime Class
STIA1123 | Programming 2
12
package Inheritance;
package Inheritance;
public class FullTime extends
Employee {
public class Employee extends private double salary;
Person {
private int id;
public FullTime() {
public Employee() { }
}
public FullTime(String
name, int id, double salary) {
public void setID(int id) {
super(name, id);
this.id = id;
this.salary = salary;
}
}
} return salary;
}
public Employee(String
name, int id) {
public void
super(name); setSalary(double salary) {
this.id = id; this.salary = salary;
} }
STIA1123 | Programming 2
13
package Inheritance;
public Person() {
}
Person Class
STIA1123 | Programming 2
14
STIA1123 | Programming 2
15
STIA1123 | Programming 2
16
Asg13b.
package Inheritance;
import java.util.Scanner;
String name;
int id;
//Employee Object
System.out.println("===__Employee__===");
name = in.next();
emp[i].setName(name);
id = in.nextInt();
emp[i].setID(id);
System.out.println("");
STIA1123 | Programming 2
17
if (emp[0].equals(emp[1]) == true) {
} else {
//Fulltime object
System.out.println("\n===__Fulltime__===");
name = in.next();
ft[i].setName(name);
id = in.nextInt();
ft[i].setID(id);
ft[i].setSalary(salary);
System.out.println("");
if (ft[0].equals(ft[1]) == true) {
} else {
STIA1123 | Programming 2
18
STIA1123 | Programming 2
19
public FullTime() {
public Employee() {
}
}
public FullTime(String
name, int id, double salary) { public void setID(int id) {
super(name, id); this.id = id;
this.salary = salary; }
}
} }
} }
} }
} STIA1123 | Programming 2
20
Demo Account
import java.util.Scanner;
double rate;
STIA1123 | Programming 2
21
System.out.println("#Current#");
System.out.println("------------");
nCurr = in.nextInt();
cur.setAccountNumber(nCurr);
balCurr = in.nextInt();
cur.setAccountBalance(balCurr);
System.out.println("\n#Saving#");
System.out.println("------------");
nSave = in.nextInt();
sav.setAccountNumber(nSave);
balSave = in.nextInt();
sav.setAccountBalance(balSave);
rate = in.nextDouble();
sav.setInterest(rate);
System.out.println("---------------------------------------");
//Output
System.out.println("\n\n#Current Output#");
cur.display();
System.out.println("");
STIA1123 | Programming 2
22
System.out.println("\n\nSaving Output");
sav.display();
STIA1123 | Programming 2
23
Current Class
package Bank;
super(accNum, balance);
public Current() {
@Override
System.out.println("");
STIA1123 | Programming 2
24
package Bank;
Account Class
public Account() {
this.numAcc = numAcc;
this.balance = balance;
return numAcc;
this.numAcc = numAcc;
return balance;
this.balance = balance;
STIA1123 | Programming 2
25
Saving Class
package Bank;
super(numAcc, balance);
this.interest = interest;
public Saving() {
this.interest = interest;
return interest;
STIA1123 | Programming 2
26
System.out.println("");
AccountArray Class
package Bank;
import java.util.Scanner;
STIA1123 | Programming 2
27
int n, i = 0, numAcc;
do {
n = in.nextInt();
System.out.println("------------");
if (n == 1) {
System.out.println("#Current#");
System.out.println("------------");
numAcc = in.nextInt();
bank[i].setAccountNumber(numAcc);
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
i++;
} else if (n == 2) {
System.out.println("#Saving#");
System.out.println("------------");
numAcc = in.nextInt();
STIA1123 | Programming 2
28
bank[i].setAccountNumber(numAcc);
balance = in.nextDouble();
bank[i].setAccountBalance(balance);
rate = in.nextDouble();
((Saving) bank[i]).setInterest(rate);
System.out.println("---------------------------------------");
i++; bank1.display();
}
//Output }
STIA1123 | Programming 2