Question: In this Assignment you will create a class that represents a Social Media profile (like a Facebook profile many of you have). The name of

In this Assignment you will create a class that represents a Social Media profile (like a Facebook profile many of you have). The name of this class will be SocialMediaProfile. There are many things we could represent about a Social Media Profile, we'll pick just a few to refresh our class writing skills. A SocialMediaProfile will have a name, an age, a country, a numberOfPosts, and numberOfLikes. Each of these will be private variables (these private variables must have the same exact names as the bolded names above) with the types int, double, or String (it's up to you to figure out which type for which private variable, not all types might be used). In addition to private variables you will have the following public methods

A constructor that will take in parameters to initialize the private variables in the order they were presented above. The constructor should ensure that anything with a number is not set to a value less than 0, if it is, then it initializes it to 0.

Getters and setters for each private variable must follow the naming convention used in the example Student class in Lecture 1 (i.e. for age we would have setage and getage). The setters should ensure that the private variables are never set to an illogical value (i.e. if the client passes in a negative age to setage, age remains unchanged).

A member function, entitled averageLikesPerPost, that returns the average likes per post (divide numberOfLikes by number of posts). If there are no posts, simply return the value 0 (i.e. make a condition to ensure you do not divide by 0). This takes in no parameters but it does have a return value (you must determine the return type).

A toString method that overrides Object's toString, and prints out the person in the following format (the only spaces are after commas)

name:Ashok, age:37, country:US, numberOfPosts:42, numberOfLikes:122. averageLikesPerPost:2.904

An equals method that overrides Object's equals. It should ensure that the Object being passed in is a SocialMediaProfile before casting it. Two Social Media Profiles are the same if all the private variables are equal. Remember to use .equals when comparing two Strings. You can use == to compare two ints.

We won't overload hashcode for now but it is something to ponder

There are Test Cases To this assignment, passing all the test cases is a good indication you did the assignment correctly but does not mean full credit-- you must hit all the requirements too

import org.junit.*;

import org.junit.runner.*;

import junit.framework.TestSuite;

import static org.junit.Assert.*;

public class Main {

public static void main(String[] args) {

//System.out.println("Look at the README!");

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

//You can write your test code here

//System.out.println(x);

//

//*******UNCOMMENT THE FOLLOWING LINE AND THE TEST CASES BELOW WHEN YOU ARE READY TO TEST******//

// org.junit.runner.JUnitCore.main("Main");

}

//***UNCOMMENT THESE TESTS WHEN YOU ARE READY TO TEST!****/

/*

@Test

public void Constructor_and_getname() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then ensures that the getname returns Sandy

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertTrue(myProfile.getname().equals("Sandy"));

}

@Test

public void Constructor_and_getcountry() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then ensures that the getcountry returns USA

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertEquals(myProfile.getcountry(), "USA");

}

@Test

public void Constructor_and_getNumberOfPosts() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then ensures that the getnumberOfPosts returns 50

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertEquals(myProfile.getnumberOfPosts(), 50);

}

@Test

public void toString_Test() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then runs toString and checks if the string returned is correct

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertEquals(myProfile.toString(),"name:Sandy, age:23, country:USA, numberOfPosts:50, numberOfLikes:110, averageLikesPerPost:2.2");

}

@Test

public void setname_and_setage() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then changes the name to Sandy Blake using setname and then sets age to 40, and finally sets age to a negative number (should stay 40).

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

myProfile.setname("Sandy Blake");

assertEquals(myProfile.getname(),"Sandy Blake");

myProfile.setage(40);

assertEquals(myProfile.getage(),40);

myProfile.setage(-3);

assertEquals(myProfile.getage(),40);

}

@Test

public void averageLikesPerPost_Test() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then calls averageLikesPerPost and then changes number of Posts to 0 and calls averageLikesPerPost. Beware of divide by zero errors!

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertEquals(myProfile.averageLikesPerPost(),2.2,.01);

myProfile.setnumberOfPosts(0);

assertEquals(myProfile.averageLikesPerPost(),0,.01);

}

@Test

public void setCountry_setPosts_setLikes() {

// Failure message:

// The test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110 and then sets the country to Mexico, sets the Posts to 23, then sets the posts to -3 (should stay 23), then sets the the number of likes to 44, then sets the number of likes to -3 (should stay unchanged

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

myProfile.setcountry("Mexico");

assertEquals(myProfile.getcountry(),"Mexico");

myProfile.setnumberOfPosts(23);

assertEquals(myProfile.getnumberOfPosts(),23);

myProfile.setnumberOfPosts(-3);

assertEquals(myProfile.getnumberOfPosts(),23);

myProfile.setnumberOfLikes(44);

assertEquals(myProfile.getnumberOfLikes(),44);

myProfile.setnumberOfLikes(-3);

assertEquals(myProfile.getnumberOfLikes(),44);

}

@Test

public void equals_Test() {

// Failure message:

// This test creates a SocialMediaProfile object with name Sandy, age 23, country USA, numberOfPosts 50, and numberOfLikes 110. It then creates another identical object and checks if they are equal. Then it sets one of the objects names to randy and checks if thats equal to the first object (it shouldn't be). Finally it checks equality with a different class altogether (this should also not be equal).

SocialMediaProfile myProfile= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

SocialMediaProfile myProfile2= new SocialMediaProfile("Sandy", 23, "USA", 50, 110);

assertEquals(myProfile,myProfile2);

myProfile2.setname("Randy");

assertFalse(myProfile.equals(myProfile2));

assertFalse(myProfile.equals("this is a random string"));

}

*/

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!