Write the following base class:
A friend on social media has the following features:
First Name
Last Name
Friendship level: values can be one of the choices best, ultra, great, good, acquaintance, the category was borrowed from PokemonGo
The friend has the following behaviors
Constructor/s: set MAX_IN_LEVEL as 2
toString: returns first, last names and friendship level as a String
improveFS: improve friendship level by one category higher
A friendGroup has the following features;
An arrayList of friends
The friendGroup has the following behaviors
Constructor/s: instantiate an ArrayList or assign value of friends to the instance variable
Display friends: use for each loop (enhanced for loop) to display all friends
findFriend(String first)
listFriends(friendship level passed as parameter): list all friends in that category
addFriend(Friend aFriend)
addFriend(int index, Friend aFriend)
removeFriend(String first)
removeAll(friendship level passed as parameter): remove all friends in that category
improvable(String first): check if the higher level of the friendship is full, if it is full, return false, if not, return true
improveFriend(String first): first check if the friendship can be improved, if it is improvable, improve the friendship
A tester class
FriendGroup homies = new FriendGroup();
homies.addFriend(new Friend("Tim", "Roberts", 4));
homies.addFriend(new Friend("Mary", "Roberts", 4));
homies.addFriend(new Friend("Tom", "Roberts", 3));
homies.addFriend(new Friend("Tam", "Roberts", 2));
homies.addFriend(3, new Friend("Terry", "Roberts", 3)); homies.addFriend(new Friend("Carrie", "Roberts", 1));
;
homies.displayFriends();
homies.listFriends(2);
sysout(homies.improveFriend(“Terry”);
homies.displayFriends();
sysout(homies.improveFriend(“Carrie”);
homies.listFriends(2);
homies.removeFriend(“Mary”);
homies.listFriends(4);
sysout(homies.improveFriend(“Terry”);
homies.removeAll(3);
homies.displayFriends();
sysout(findFriend(“Tam”);
Program:
Friend.java
public class Friend{ /* Friend class to containing the details about a friend */ // Initializing the data members static final int MAX_IN_LEVEL = 2; public String firstName; public String lastName; public int friendshipLevel; // Storing possible values of friendship public String[] friendship = {"best", "ultra", "great", "good", "acquaintance"}; // Constructor to initialize a friend Friend(String firstName, String lastName, int friendshipLevel){ this.firstName = firstName; this.lastName = lastName; this.friendshipLevel = friendshipLevel; } // Method that return the string object of the class public String toString(){ return "First Name : " + this.firstName + "\nLast Name : " + this.lastName + "\nFriendship Level : " + friendship[this.friendshipLevel]; } // Method to improve friendship public void improveFS(){ // check whether the friendshipLevel is improvable if(this.friendshipLevel > 0){ this.friendshipLevel--; } } }
FriendGroup.java
// importing Array List class import java.util.ArrayList; public class FriendGroup{ /* Class to organise a group of friends*/ // array list to store friends ArrayList<Friend> friends = new ArrayList<Friend>(); // Method to display details of all friends public void displayFriends(){ // Iterating over every object in arraylist for(Friend friend : this.friends){ System.out.println(friend.toString()); } } // Method to find whether a friend exists public boolean findFriend(String first){ // Iterating over every object in arraylist for(Friend friend : friends){ if(friend.firstName.equals(first)){ return true; } } return false; } // Method to list the friends of same level public void listFriends(int level){ // Iterating over every object in arraylist for(Friend friend : friends){ if(friend.friendshipLevel == level){ System.out.println(friend.toString()); } } } // Method to add a new friend to the list of friends public void addFriend(Friend aFriend){ this.friends.add(aFriend); } // Method to add a new friend to the list of friends with the index specified public void addFriend(int index, Friend aFriend){ this.friends.add(index, aFriend); } // Method to remove a friend from the list of friends public void removeFriend(String first){ // Iterating over every object in arraylist for(int i=0; i<friends.size(); i++){ if(friends.get(i).firstName.equals(first)){ friends.remove(i); } } } // Method to remove all friends that belong to the level specified public void removeAll(int level){ // Iterating over every object in arraylist for(int i=0; i<friends.size(); i++){ if(friends.get(i).friendshipLevel == level){ friends.remove(i); } } } // Method to verify whether friendship can be improvable public boolean improvable(String first){ // Iterating over every object in arraylist for(Friend friend : friends){ if(friend.firstName.equals(first)){ // check whether the friendshipLevel is best i.e 0 if(friend.friendshipLevel == 0){ return false; } } } return false; } // Method to improve friendship if it is improvable public boolean improveFriend(String first){ // Iterating over every object in arraylist for(Friend friend : friends){ if(friend.firstName.equals(first)){ // check whether the friendshipLevel is best i.e 0 if(friend.friendshipLevel != 0){ friend.improveFS(); return true; } } } return false; } }
TesterClass.java
class TesterClass { public static void main(String[] args) { FriendGroup homies = new FriendGroup(); homies.addFriend(new Friend("Tim", "Roberts", 4)); homies.addFriend(new Friend("Mary", "Roberts", 4)); homies.addFriend(new Friend("Tom", "Roberts", 3)); homies.addFriend(new Friend("Tam", "Roberts", 2)); homies.addFriend(3, new Friend("Terry", "Roberts", 3)); homies.addFriend(new Friend("Carrie", "Roberts", 1)); homies.displayFriends(); homies.listFriends(2); System.out.println(homies.improveFriend("Terry")); homies.displayFriends(); System.out.println(homies.improveFriend("Carrie")); homies.listFriends(2); homies.listFriends(4); System.out.println(homies.improveFriend("Terry")); homies.removeAll(3); homies.removeFriend("Mary"); homies.displayFriends(); System.out.println(homies.findFriend("Tam")); } }
output:
First Name : Tim Last Name : Roberts Friendship Level : acquaintance First Name : Mary Last Name : Roberts Friendship Level : acquaintance First Name : Tom Last Name : Roberts Friendship Level : good First Name : Terry Last Name : Roberts Friendship Level : good First Name : Tam Last Name : Roberts Friendship Level : great First Name : Carrie Last Name : Roberts Friendship Level : ultra First Name : Tam Last Name : Roberts Friendship Level : great true First Name : Tim Last Name : Roberts Friendship Level : acquaintance First Name : Mary Last Name : Roberts Friendship Level : acquaintance First Name : Tom Last Name : Roberts Friendship Level : good First Name : Terry Last Name : Roberts Friendship Level : great First Name : Tam Last Name : Roberts Friendship Level : great First Name : Carrie Last Name : Roberts Friendship Level : ultra true First Name : Terry Last Name : Roberts Friendship Level : great First Name : Tam Last Name : Roberts Friendship Level : great First Name : Tim Last Name : Roberts Friendship Level : acquaintance First Name : Mary Last Name : Roberts Friendship Level : acquaintance true First Name : Tim Last Name : Roberts Friendship Level : acquaintance First Name : Terry Last Name : Roberts Friendship Level : ultra First Name : Tam Last Name : Roberts Friendship Level : great First Name : Carrie Last Name : Roberts Friendship Level : best true
Hope this helps!
Write the following base class: A friend on social media has the following features: First Name...
Write a class called Book. Here are the relevant attributes: title author yearPublished bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...
Java: Create a class called Vehicle with the following features: a. It has three private data members (instance variables): one is the manufacturer’s name (String manufacturer), the second is the number of cylinders in the engine (int cylinder), and the third is the owner (Person owner). The class Person is described above. b. It has three constructors, a no-argument constructor, a constructor with three parameters, and a copy constructor. The no-argument constructor will set the manufacturer to “None”, cylinder to...
1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...
Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Identify clearly where in your code each of these points is being demonstrated Q 4(c) [6 Marks] Write an abstract Student class in Java which has a name, id_number, year and programme_code as member variables. Add a constructor to your class and a display()...
Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...
Write the following in Java Design a class called ReviewSystem, this class should have an attribute reviewList, which is an ArrayList type. add a constructor to initialize the attribute. add a method addReviewAndComment(). This method asks from the user to provide an integer (0 to 4 for the five ratings) and a String as a comment. If the user inputs -1, it ends the loop of asking. All valid user inputs should be saved to the reviewList attribute. For example:...
create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...
Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate accessor methods Appropriate mutator methods toString method equals method (b) Write a FruitDriver class that: Initializes an array of Fruit objects, fruitArray, with 10 objects with names: banana, apple, mango, orange,pineapple, pear, grapes, tangerine, watermelon, sweetmelon and appropriate prices per kilogram. Uses an appropriate loop to display all objects with pricePerKilogram > 5.00 Saudi Riyals, if any. Calls a linearSearch method: public static...
Course Class Create a class called Course. It will not have a main method; it is a business class. Put in your documentation comments at the top. Be sure to include your name. Course must have the following instance variables named as shown in the table: Variable name Description crn A unique number given each semester to a section (stands for Course Registration Number) subject A 4-letter abbreviation for the course (e.g., ITEC, PHED, CHEM) number A 4-digit number associated...