Top Banner
STIA1123 PROGRAMMING 2 Achmad Zulkarnain 230753 Assignment 1
29

Assignment Java Programming 2

Apr 06, 2023

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Assignment Java Programming 2

STIA1123 PROGRAMMING 2

Achmad Zulkarnain

230753

Assignment 1

Page 2: Assignment Java Programming 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. [email protected]

[email protected]

[email protected]

[email protected]

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 [email protected], the output will be:

Name: Lokman

School: School of Computing

If the address is [email protected], 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

Page 3: Assignment Java Programming 2

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

Page 4: Assignment Java Programming 2

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.

Page 5: Assignment Java Programming 2

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

Page 6: Assignment Java Programming 2

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

Page 7: Assignment Java Programming 2

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

}

}

Page 8: Assignment Java Programming 2

7

STIA1123 | Programming 2

Page 9: Assignment Java Programming 2

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

}

}

}

Page 10: Assignment Java Programming 2

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

}

}

}

Page 11: Assignment Java Programming 2

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;

}

}

Page 12: Assignment Java Programming 2

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

Page 13: Assignment Java Programming 2

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

Page 14: Assignment Java Programming 2

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

Page 15: Assignment Java Programming 2

14

STIA1123 | Programming 2

3b. Comparing Object

Page 16: Assignment Java Programming 2

15

STIA1123 | Programming 2

Page 17: Assignment Java Programming 2

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

}

Page 18: Assignment Java Programming 2

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

}

Page 19: Assignment Java Programming 2

18

STIA1123 | Programming 2

}

}

Page 20: Assignment Java Programming 2

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

}

}

Page 21: Assignment Java Programming 2

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

Page 22: Assignment Java Programming 2

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("\n\n#Current Output#");

cur.display();

System.out.println("");

Page 23: Assignment Java Programming 2

22

STIA1123 | Programming 2

System.out.println("\n\nSaving Output");

sav.display();

}

}

Page 24: Assignment Java Programming 2

23

STIA1123 | Programming 2

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

}

}

Page 25: Assignment Java Programming 2

24

STIA1123 | Programming 2

Account Class

package Bank;

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

}

Page 26: Assignment Java Programming 2

25

STIA1123 | Programming 2

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

Page 27: Assignment Java Programming 2

26

STIA1123 | Programming 2

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) {

Page 28: Assignment Java Programming 2

27

STIA1123 | Programming 2

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

Page 29: Assignment Java Programming 2

28

STIA1123 | Programming 2

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

}

} while (i < bank.length);

//Output

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

for (Account bank1 : bank) {

if (bank1 instanceof Current) {

bank1.display();

} else if (bank1 instanceof Saving) {

((Saving) bank1).display();

}

}

}

}