Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the below code import java.util.*; /** * Requirements: * 1. Replace LastName in file and class names with your own name. * 2. Finish

Complete the below code import java.util.*;

/** * Requirements: * 1. Replace LastName in file and class names with your own name. * 2. Finish all TODOs. Test your code before submitting it. * 3. Do NOT modify ANY provided code. */ public class PROG24178_Assignment1_Lastname { public static void main(String[] args) { int pass = 0, total = 15; Person spiderman = new Person("Spiderman", 18); if (spiderman.getNumOfFriends() == 0 && !spiderman.removeFriend(null) && !spiderman.removeFriend("Spiderman") && spiderman.getAllFriendNames() == null) System.out.println("Pass #1: " + (++pass)); if (!spiderman.addFriend(null) && !spiderman.addFriend(new Person(null, 23))) System.out.println("Pass #2: " + (++pass)); if (spiderman.addFriend(new Person("MJ", 18)) && spiderman.addFriend(new Person("Ned", 18)) && spiderman.addFriend(new Person("Hulk", 27))) System.out.println("Pass #3: " + (++pass)); if (spiderman.getNumOfFriends() == 3 && spiderman.getAllFriendNames().length == 3) System.out.println("Pass #4: " + (++pass)); if (spiderman.getAllFriendNames()[0].equals("MJ") && spiderman.getAllFriendNames()[1].equals("Ned") && spiderman.getAllFriendNames()[2].equals("Hulk")) System.out.println("Pass #5: " + (++pass)); if (!spiderman.removeFriend("Thanos") && spiderman.removeFriend("Ned") && !spiderman.removeFriend("Ned")) System.out.println("Pass #6: " + (++pass)); if (spiderman.getNumOfFriends() == 2 && spiderman.getAllFriendNames().length == 2) System.out.println("Pass #7: " + (++pass)); if (!spiderman.addFriend(new Person("Hulk", 27))) System.out.println("Pass #8: " + (++pass)); if (spiderman.addFriend(new Person("Thor", 28)) && spiderman.addFriend(new Person("Ironman", 35)) && spiderman.addFriend(new Person("Dr. Strange", 32))) System.out.println("Pass #9: " + (++pass)); if (spiderman.getNumOfFriends() == 5 && spiderman.getAllFriendNames().length == 5) System.out.println("Pass #10: " + (++pass)); if (!spiderman.addFriend(new Person("Superman", 33))) System.out.println("Pass #11: " + (++pass)); if (spiderman.removeFriend("Thor") && spiderman.removeFriend("Ironman")) System.out.println("Pass #12: " + (++pass)); if (spiderman.getNumOfFriends() == 3 && spiderman.getAllFriendNames().length == 3) System.out.println("Pass #13: " + (++pass)); if (spiderman.removeFriend("Hulk") && spiderman.removeFriend("MJ") && !spiderman.removeFriend("Hulk")) System.out.println("Pass #14: " + (++pass)); if (spiderman.getNumOfFriends() == 1 && spiderman.getAllFriendNames().length == 1 && spiderman.getAllFriendNames()[0].equals("Dr. Strange")) System.out.println("Pass #15: " + (++pass)); System.out.println(" Pass/Total = " + pass + "/" + total); System.out.println(spiderman); } }

class Person { private static int numOfPerson; private String name; private int age; private int max private Person[] friends;

public Person(String name, int age) { this.name = name; this.age = age; friends = new Person[5]; //assume maximum 5 possible friends }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

public Person[] getFriends() { return friends; }

@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", friends=" + Arrays.toString(friends) + '}'; }

/* ==== Do NOT modify any code above this line ==== */

/*

* * TODO #3 - write a public method named removeFriend that * - accepts a string as the argument * - removes the friend of the specified name from the friends array * - returns true if the friend is successfully removed, otherwise returns false */

/** * TODO #4 - write a public method named getAllFriendNames that * - accepts no argument * - returns an array of strings containing all friends' names, or returns null if no name to return * - Note: the length of the returned array must be exactly the same as the number of friend names */

} //End of Person class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Population

Answered: 1 week ago

Question

6. Describe why communication is vital to everyone

Answered: 1 week ago