SlideShare a Scribd company logo
1 of 29
Download to read offline
STIA1123 PROGRAMMING 2
Achmad Zulkarnain
230753
Assignment 1
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
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
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.
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
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
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 !");
}
}
7
STIA1123 | 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");
}
}
}
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]);
}
}
}
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;
}
}
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
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
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
14
STIA1123 | Programming 2
3b. Comparing Object
15
STIA1123 | 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("");
}
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 !");
}
18
STIA1123 | 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()));
}
}
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();
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("");
22
STIA1123 | Programming 2
System.out.println("nnSaving Output");
sav.display();
}
}
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("");
}
}
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();
}
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());
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) {
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();
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();
}
}
}
}

More Related Content

Similar to Assignment Java Programming 2

Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diplomamustkeem khan
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make YASHU40
 
SE-IT JAVA LAB SYLLABUS
SE-IT JAVA LAB SYLLABUSSE-IT JAVA LAB SYLLABUS
SE-IT JAVA LAB SYLLABUSnikshaikh786
 
Java Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdfJava Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdfAditya Kumar
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfARORACOCKERY2111
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfoliomwillmer
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docxaryan532920
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docxrafbolet0
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfayush616992
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 

Similar to Assignment Java Programming 2 (20)

Java Practical File Diploma
Java Practical File DiplomaJava Practical File Diploma
Java Practical File Diploma
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make Question 1 briefly respond to all the following questions. make
Question 1 briefly respond to all the following questions. make
 
SE-IT JAVA LAB SYLLABUS
SE-IT JAVA LAB SYLLABUSSE-IT JAVA LAB SYLLABUS
SE-IT JAVA LAB SYLLABUS
 
Java Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdfJava Question-Bank-Class-8.pdf
Java Question-Bank-Class-8.pdf
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdfSummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
SummaryHW6 Account ManagementIn HW4, you kept track of multiple.pdf
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
C++ Lab Maual.pdf
C++ Lab Maual.pdfC++ Lab Maual.pdf
C++ Lab Maual.pdf
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docx
 
DIWE - Advanced PHP Concepts
DIWE - Advanced PHP ConceptsDIWE - Advanced PHP Concepts
DIWE - Advanced PHP Concepts
 
C#.net
C#.netC#.net
C#.net
 
Capstone ms2
Capstone ms2Capstone ms2
Capstone ms2
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docxSpring 2014 CSCI 111 Final exam   of 1 61. (2 points) Fl.docx
Spring 2014 CSCI 111 Final exam of 1 61. (2 points) Fl.docx
 
Muzzammilrashid
MuzzammilrashidMuzzammilrashid
Muzzammilrashid
 
c++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdfc++ exp 1 Suraj...pdf
c++ exp 1 Suraj...pdf
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 

More from Kaela Johnson

250 Word Essay On My Life Experience
250 Word Essay On My Life Experience250 Word Essay On My Life Experience
250 Word Essay On My Life ExperienceKaela Johnson
 
3G Communication Essay
3G Communication Essay3G Communication Essay
3G Communication EssayKaela Johnson
 
13 Reasons Why Essay Topics
13 Reasons Why Essay Topics13 Reasons Why Essay Topics
13 Reasons Why Essay TopicsKaela Johnson
 
A Modest Proposal 50 Essays Questions
A Modest Proposal 50 Essays QuestionsA Modest Proposal 50 Essays Questions
A Modest Proposal 50 Essays QuestionsKaela Johnson
 
5 Paragraph Essay On Iwo Jima
5 Paragraph Essay On Iwo Jima5 Paragraph Essay On Iwo Jima
5 Paragraph Essay On Iwo JimaKaela Johnson
 
A Good Thesis Statement For A Descriptive Essay
A Good Thesis Statement For A Descriptive EssayA Good Thesis Statement For A Descriptive Essay
A Good Thesis Statement For A Descriptive EssayKaela Johnson
 
6Th Grade Argumentative Essay Ideas
6Th Grade Argumentative Essay Ideas6Th Grade Argumentative Essay Ideas
6Th Grade Argumentative Essay IdeasKaela Johnson
 
A Good Place To Visit Essay
A Good Place To Visit EssayA Good Place To Visit Essay
A Good Place To Visit EssayKaela Johnson
 
5 Paragraph Essay Topics For 8Th Grade
5 Paragraph Essay Topics For 8Th Grade5 Paragraph Essay Topics For 8Th Grade
5 Paragraph Essay Topics For 8Th GradeKaela Johnson
 
A Streetcar Named Desire Critical Lens Essay
A Streetcar Named Desire Critical Lens EssayA Streetcar Named Desire Critical Lens Essay
A Streetcar Named Desire Critical Lens EssayKaela Johnson
 
2009 Ap English Language Synthesis Essay
2009 Ap English Language Synthesis Essay2009 Ap English Language Synthesis Essay
2009 Ap English Language Synthesis EssayKaela Johnson
 
100 Best College Essay Topics
100 Best College Essay Topics100 Best College Essay Topics
100 Best College Essay TopicsKaela Johnson
 
411 Sat Essay Prompts And Writing Questions Pdf
411 Sat Essay Prompts And Writing Questions Pdf411 Sat Essay Prompts And Writing Questions Pdf
411 Sat Essay Prompts And Writing Questions PdfKaela Johnson
 
12 Years A Slave Review Essay
12 Years A Slave Review Essay12 Years A Slave Review Essay
12 Years A Slave Review EssayKaela Johnson
 
A Level Drama Essay Example
A Level Drama Essay ExampleA Level Drama Essay Example
A Level Drama Essay ExampleKaela Johnson
 
A Level French Essay Planning
A Level French Essay PlanningA Level French Essay Planning
A Level French Essay PlanningKaela Johnson
 
2014 Essay Scholarships
2014 Essay Scholarships2014 Essay Scholarships
2014 Essay ScholarshipsKaela Johnson
 
5 Paragraph Essay Sample 6Th Grade
5 Paragraph Essay Sample 6Th Grade5 Paragraph Essay Sample 6Th Grade
5 Paragraph Essay Sample 6Th GradeKaela Johnson
 

More from Kaela Johnson (20)

250 Word Essay On My Life Experience
250 Word Essay On My Life Experience250 Word Essay On My Life Experience
250 Word Essay On My Life Experience
 
3G Communication Essay
3G Communication Essay3G Communication Essay
3G Communication Essay
 
13 Reasons Why Essay Topics
13 Reasons Why Essay Topics13 Reasons Why Essay Topics
13 Reasons Why Essay Topics
 
A Modest Proposal 50 Essays Questions
A Modest Proposal 50 Essays QuestionsA Modest Proposal 50 Essays Questions
A Modest Proposal 50 Essays Questions
 
5 Paragraph Essay On Iwo Jima
5 Paragraph Essay On Iwo Jima5 Paragraph Essay On Iwo Jima
5 Paragraph Essay On Iwo Jima
 
A Good Thesis Statement For A Descriptive Essay
A Good Thesis Statement For A Descriptive EssayA Good Thesis Statement For A Descriptive Essay
A Good Thesis Statement For A Descriptive Essay
 
6Th Grade Argumentative Essay Ideas
6Th Grade Argumentative Essay Ideas6Th Grade Argumentative Essay Ideas
6Th Grade Argumentative Essay Ideas
 
10000 Bc Essay
10000 Bc Essay10000 Bc Essay
10000 Bc Essay
 
A Good Place To Visit Essay
A Good Place To Visit EssayA Good Place To Visit Essay
A Good Place To Visit Essay
 
5 Paragraph Essay Topics For 8Th Grade
5 Paragraph Essay Topics For 8Th Grade5 Paragraph Essay Topics For 8Th Grade
5 Paragraph Essay Topics For 8Th Grade
 
A Streetcar Named Desire Critical Lens Essay
A Streetcar Named Desire Critical Lens EssayA Streetcar Named Desire Critical Lens Essay
A Streetcar Named Desire Critical Lens Essay
 
2009 Ap English Language Synthesis Essay
2009 Ap English Language Synthesis Essay2009 Ap English Language Synthesis Essay
2009 Ap English Language Synthesis Essay
 
100 Best College Essay Topics
100 Best College Essay Topics100 Best College Essay Topics
100 Best College Essay Topics
 
411 Sat Essay Prompts And Writing Questions Pdf
411 Sat Essay Prompts And Writing Questions Pdf411 Sat Essay Prompts And Writing Questions Pdf
411 Sat Essay Prompts And Writing Questions Pdf
 
12 Years A Slave Review Essay
12 Years A Slave Review Essay12 Years A Slave Review Essay
12 Years A Slave Review Essay
 
A Level Drama Essay Example
A Level Drama Essay ExampleA Level Drama Essay Example
A Level Drama Essay Example
 
A Level French Essay Planning
A Level French Essay PlanningA Level French Essay Planning
A Level French Essay Planning
 
2014 Essay Scholarships
2014 Essay Scholarships2014 Essay Scholarships
2014 Essay Scholarships
 
5 Paragraph Essay Sample 6Th Grade
5 Paragraph Essay Sample 6Th Grade5 Paragraph Essay Sample 6Th Grade
5 Paragraph Essay Sample 6Th Grade
 
1 Page Essay Topics
1 Page Essay Topics1 Page Essay Topics
1 Page Essay Topics
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Assignment Java Programming 2

  • 1. STIA1123 PROGRAMMING 2 Achmad Zulkarnain 230753 Assignment 1
  • 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
  • 15. 14 STIA1123 | Programming 2 3b. Comparing Object
  • 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("");
  • 23. 22 STIA1123 | Programming 2 System.out.println("nnSaving Output"); sav.display(); } }
  • 24. 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(""); } }
  • 25. 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(); }
  • 26. 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());
  • 27. 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) {
  • 28. 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();
  • 29. 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(); } } } }