Assignment Java Programming 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

STIA1123 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.

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

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.

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

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

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

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.

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

STIA1123 | Programming 2
6

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 !");
}
}

STIA1123 | Programming 2
7

STIA1123 | Programming 2
8

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");
}
}
}

1. Dice

STIA1123 | Programming 2
9

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 : %10d\n", i + 1, count[i]);
}
}
}

STIA1123 | Programming 2
10

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;
}
}

STIA1123 | Programming 2
11

3a. Part-time and Fulltime Object

package Inheritance;

package Inheritance; public class PartTime extends


Employee {
private double hourlyWage;
public class Customer extends
Person {

public PartTime()
String address; }

public Customer(){ public PartTime(String


name, int id) {
}
super();
}
public void
setAddress(String address) {
this.address = address; public void display() {
} super.dispay();
}
public String getAddress()
{
public double
return address; getHourlyWage() {
} return hourlyWage;
} }
}
Customer Class

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;
}
}

public int getId() {


return id; public double getSalary() {

} return salary;
}

public Employee(String
name, int id) {
public void
super(name); setSalary(double salary) {
this.id = id; this.salary = salary;
} }

public void dispay() { public void display() {


System.out.println("The
super.dispay();
name of the employee is: " +
getName());
System.out.println("The System.out.println("Salary: RM
id of the employee is: " + " + getSalary());
getId()); }
}
}
}

FullTime Class without method comparing


Employee Class without method comparing

STIA1123 | Programming 2
13

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

STIA1123 | Programming 2
14

3b. Comparing Object

STIA1123 | Programming 2
15

STIA1123 | Programming 2
16

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("");

STIA1123 | Programming 2
17

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 !");

STIA1123 | Programming 2
18

STIA1123 | Programming 2
19

package Inheritance; package Inheritance;

public class FullTime extends public class Employee extends Person {


Employee {
private double salary;
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; }
}

public int getId() {


public double getSalary() {
return id;
return salary;
}
}

public Employee(String name, int id) {


public void
setSalary(double salary) { super(name);

this.salary = salary; this.id = id;

} }

public void display() { public void dispay() {

super.dispay(); System.out.println("The name of the


employee is: " + getName());

System.out.println("Salary: RM System.out.println("The id of the


" + getSalary()); employee is: " + getId());

} }

public boolean public boolean equals(Employee emp) {


equals(FullTime emp) {
return (id == emp.getId()) &&
return (getName().equals(emp.getName()));
(super.equals(emp) == true) &&
(salary == emp.getSalary()); }

} }

} STIA1123 | Programming 2
20

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();

STIA1123 | Programming 2
21

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("\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;

public class Current extends Account {

public Current(int accNum, double balance) {

super(accNum, balance);

public Current() {

@Override

public void display() {

System.out.println("Current Account Information");

System.out.println(" Account number: " + getAccountNumber());

System.out.println(" Balance: RM " + getAccountBalance());

System.out.println("");

STIA1123 | Programming 2
24
package Bank;
Account Class

public abstract class Account {

private int numAcc;

private double balance;

public Account() {

public Account(int numAcc, double balance) {

this.numAcc = numAcc;

this.balance = balance;

public int getAccountNumber() {

return numAcc;

public void setAccountNumber(int numAcc) {

this.numAcc = numAcc;

public double getAccountBalance() {

return balance;

public void setAccountBalance(double balance) {

this.balance = balance;

public abstract void display();

STIA1123 | Programming 2
25

Saving Class

package Bank;

public class Saving extends Account {

private double interest;

public Saving(int numAcc, double balance, double interest) {

super(numAcc, balance);

this.interest = interest;

public Saving() {

public void setInterest(double interest) {

this.interest = interest;

public double getInterest() {

return interest;

public void display() {

System.out.println("Saving Account Information");

System.out.println(" Account number: " + getAccountNumber());

STIA1123 | Programming 2
26

System.out.println(" Balance: RM " + getAccountBalance());

System.out.println(" Interest: " + getInterest() + " %");

System.out.println("");

AccountArray Class

package Bank;

import java.util.Scanner;

public class AccountArray {

public static void main(String[] args) {

STIA1123 | Programming 2
27

Scanner in = new Scanner(System.in);

int n, i = 0, numAcc;

double balance, rate;

Account[] bank = new Account[10];

do {

System.out.print("\n[1]Current || [2]Saving >> ");

n = in.nextInt();

System.out.println("------------");

if (n == 1) {

bank[i] = new Current();

System.out.println("#Current#");

System.out.println("------------");

System.out.print("Please enter account number: ");

numAcc = in.nextInt();

bank[i].setAccountNumber(numAcc);

System.out.print("Enter balance: ");

balance = in.nextDouble();

bank[i].setAccountBalance(balance);

i++;

} else if (n == 2) {

bank[i] = new Saving();

System.out.println("#Saving#");

System.out.println("------------");

System.out.print("Please enter account number: ");

numAcc = in.nextInt();

STIA1123 | Programming 2
28

bank[i].setAccountNumber(numAcc);

System.out.print("Enter balance: ");

balance = in.nextDouble();

bank[i].setAccountBalance(balance);

System.out.print("Interest rate: ");

rate = in.nextDouble();

((Saving) bank[i]).setInterest(rate);

System.out.println("---------------------------------------");

i++; bank1.display();

} else if (bank1 instanceof Saving) {


}
((Saving) bank1).display();
} while (i < bank.length);
}

}
//Output }

System.out.println("\n### Data Output ###"); }

for (Account bank1 : bank) {

if (bank1 instanceof Current) {

STIA1123 | Programming 2

You might also like