Description
You have to follow the instructions, write in Eclipse Java https://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html
Unformatted Attachment Preview
HW08-1 CODE: Use the Builder design
pattern. Implement in a test application.
This lab is based on the article, Builder Pattern
In Practice”:
https://www.javacodegeeks.com/2013/01/the
-builder-pattern-in-practice.html
As the author mentions, this is NOT a
traditional usage of the pattern. The key idea
here is that when some fields are required and
some optional, the XXXBuilder makes it easy to
build a number of instances.
Note that in the example class at the right, the
inner class constructor takes the required
fields, but all the other methods are optional.
So, an example usage might be:
……………………………………….
List users
= new ArrayList();
users.add(
new User.UserBuilder(“J”, Smith”)
.age(21)
.phone(“555-1212”)
public class User {
private final String firstName; // required
private final String lastName; // required
private final int age; // optional
private final String phone; // optional
private final String address; // optional
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
//omitted getters for lastName, age, phone, address
public static class UserBuilder { //inner class
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
.build()
//inner class constructor takes the required fields
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
);
//test – invokes toString() added to User
System.out.println(users.get(0));
…………………………………………………
Your assignment is to design and code
CSCourse to represent characteristics of a
Rowan CS course, using the Builder pattern.
➔Every course has a department (e.g. “CS”)
and a number (e.g. 04.114), and a course name
(e.g. “OOPDA”). Let’s say the other fields may
or may not be initialized upon creation, such as
number of credits, whether the course is
required or optional within the CS major, and
perhaps a few more characteristics.
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
Design class CSCourse and use the Builder
pattern. Then use a test application to
populate an ArrayList of, say, five CSCourse
instances using the technique shown above.
Iterate through your List and invoke toString()
to display the data.
Deliverable is zip of your Eclipse project, plus a
screen shot of a test run.
public UserBuilder address(String address) {
this.address = address;
return this;
}
public User build() {
return new User(this);
}
} //end UserBuilder
} //end User
Purchase answer to see full
attachment