The UML diagram below shows the design of 2 classes. The Employee class was prev
ID: 3824496 • Letter: T
Question
The UML diagram below shows the design of 2 classes. The Employee class was previously implemented. If you don’t have it, them implement it. You’re to use an Java ArrayList to implement the stack class (MyStack) to store Employee objects.
Be sure to test your implementation with a TestStack class containing the main method. You can print the list of Employee by calling the toString() method on an instance of MyStack.
Employee
-name: String
-salary: double
+ Employee(name:String,
Salary:double)
+ raiseSalary(byPercent: double):void
MyStack
-list: ArrayList<Employee>
A list to employees.
+ isEmpty(): boolean
+ getSize(): int
+ peek(): Object
+ pop(): Object
+ push(o: Object): void
Returns true if this stack is empty.
Returns the number of elements in this stack.
Returns the top element in this stack without removing it.
Returns and removes the top element in this stack.
Adds a new element to the top of this stack.
Employee
-name: String
-salary: double
+ Employee(name:String,
Salary:double)
+ raiseSalary(byPercent: double):void
Explanation / Answer
import java.util.*;
class Employee
{
String name;
Double salary;
Employee(String n, Double s)
{
name=n;
salary=s;
}
public void raiseSalary(double byPercent)
{
double inc= salary*(byPercent/100);
salary+=inc;
}
}
class MyStack
{
ArrayList<Employee> list=new ArrayList<Employee>();//Creating arraylist
public boolean isEmpty()
{
return list.isEmpty();
}
public int getSize()
{
return list.size();
}
Employee peek()
{
return list.get(list.size()-1);
}
public Employee pop()
{
Employee last;
last = list.remove((list.size()- 1));
return(last);
}
public void push(Employee e)
{
list.add(e);
}
public String toString()
{
String s="";
for(int i=0; i<list.size();i++)
{
s+=list.get(i).name+" ";
}
return s;
}
}
public class MyStackTest
{
public static void main(String a[])
{
MyStack s= new MyStack();
s.push(new Employee("Asif Khan",100000.0));
s.push(new Employee("Sheikh Junaid",150000.0));
s.push(new Employee("Junaid Khan",100000.0));
s.push(new Employee("AAtif Khan",100000.0));
System.out.println(s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.