Consider the following class: public class MergeSequence {private ArrayList valu
ID: 3938788 • Letter: C
Question
Consider the following class: public class MergeSequence {private ArrayList values; public MergeSequence () {values = new ArrayList();} public void add(int n) {values.add (n);} public String toString () {return values.toString();}} Add a method public MergeSequence append (MergeSequence other) that creates a new sequence, appending this and the other sequence, without modifying either sequence. For example, if sequence (a) is: 14 9 16 and b is the sequence 9 7 4 9 11 then the call a.append(b) returns the sequence 14916974911 without modifying a or b. Use JUnit testing framewrork to verify the functionality of your MergeSequence Class. Implement a superclass Adult. Make two classes, Worker and Manager, that inherit from Adult. An adult has a name and a year of birth. A worker has a job title, and a manager has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.Explanation / Answer
Hi, Please find my implementation.
I have not implemented JUnit functionality for first question.
1)
import java.util.ArrayList;
public class MergeSequence {
private ArrayList<Integer> values;
public MergeSequence(){
values = new ArrayList<>();
}
public void add(int n){
values.add(n);
}
public String toString(){
return values.toString();
}
//1
public MergeSequence append(MergeSequence other){
MergeSequence result = new MergeSequence();
// adding current object values
for(Integer x : this.values){
result.add(x);
}
// adding other values
for(Integer x : other.values){
result.add(x);
}
// returning result
return result;
}
}
2)
######### Adult.java ###########
public class Adult {
// instance variables
private String name;
private int year;
// constructor
public Adult(String name, int year){
this.year = year;
this.name = name;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String toString(){
return name+", "+year;
}
}
########## Worker.java ############
public class Worker extends Adult {
// instance variables
private String title;
// constructor
public Worker(String name, int year, String title){
// calling parent cinstructor
super(name, year);
this.title = title;
}
// getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String toString(){
return super.toString()+", "+title;
}
}
############ Manager.java #############
public class Manager extends Adult {
// instance variables
private double salary;
// constructor
public Manager(String name, int year, double salary){
super(name, year);
this.salary= salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String toString(){
return super.toString()+", "+salary;
}
}
############## TestAdult.java ##############
public class TestAdult {
public static void main(String[] args) {
// creating Object of Worker
Worker worker = new Worker("Alex", 1992, "Technician");
// creating Object of Manager
Manager manger = new Manager("Bob", 1993, 2314.56);
System.out.println(worker);
System.out.println(manger);
}
}
/*
Sample run:
Alex, 1992, Technician
Bob, 1993, 2314.56
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.