Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

package facebook; import java.awt.*; public class Facebook{ private String name;

ID: 3599103 • Letter: P

Question

package facebook;
import java.awt.*;
public class Facebook{
private String name; private String content; DrawingPanel panel; private Graphics g;
public Facebook(String nm){ content = "undefined"; name = nm;
// Create the drawing panel panel = new DrawingPanel(200, 150); g = panel.getGraphics();
// display name g.drawString(name+"'s mood is undefined.", 20, 75); }
public void setContent(String newContent){ content = newContent; if(content.equals("happy")){ g.setColor(Color.red); g.fillRect(0, 0, 200, 150); g.setColor(Color.black); // display mood g.drawString(name+"'s mood is:"+ "happy", 20, 75); } else if(content.equals("sad")){ g.setColor(Color.green); g.fillRect(0, 0, 200, 150); g.setColor(Color.black); // display mood g.drawString(name+"'s mood is:"+ "sad", 20, 75); } else{ g.setColor(Color.white); g.fillRect(0, 0, 200, 150); g.setColor(Color.black); g.drawString(name+"'s mood is:"+ content, 20, 75); } }
public String getContent(){ return content; }


} package facebook;
public class simple_test{
public static void main (String[] args){ Facebook fb = new Facebook("Dr. Hu"); fb.setContent("happy");
} // end main
} package facebook;
public class simple_test{
public static void main (String[] args){ Facebook fb = new Facebook("Dr. Hu"); fb.setContent("happy");
} // end main
} ---///----------------//------//
THE MOST IMPORTANT: how to DRAW GRAPHICS for this program to run by testFacebook_Graphics class belows... I don't know h to do graphics...thanks... This is the testFacebook_Graphics class

package Facebook;
import java.util.*;
public class testFacebook{
public static void main (String[] args){
// Prompt user to enter the number of facebookpresons Scanner userInput = new Scanner(System.in); System.out.println("Enter the number of facebookpresons to be created: "); int numP=0; while(true){ try{ String inputString = userInput.nextLine(); numP = Integer.parseInt(inputString); if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5. break; else System.out.println("the number is out of range [1, 5]! enter again"); } catch (NumberFormatException e){ System.out.println("invalid input. Enter an integer number!"); } }
FacebookPerson[] fbp = new FacebookPerson[numP];
//Ask the user to enter the name for each person, and create the persons for(int i=0; i< numP; i++){ System.out.println("Enter the name for person "+ (i+1)+ ":"); String name = userInput.nextLine(); fbp[i] = new FacebookPerson(name); } System.out.println("-------select a person and type the mood below--------");
//Ask the user to set the mood for a person, and update the mood, enter "####" to exit while(true){ System.out.println("Enter the name for a person (enter #### to exit):"); String name = userInput.nextLine(); if(name.equals("####")) System.exit(0); int personID = -1; for(int i=0; i< numP; i++){ if(fbp[i].getName().equals(name)){ personID = i; break; // break the for loop } } if(personID!=-1){ // found the person, otherwise personID should still be -1 System.out.println("Enter the mood for the person:"); String mood = userInput.nextLine(); fbp[personID].setMood(mood); } else System.out.println("unrecognized name!"); } // end while
} // end main
} This is the testFacebook_Graphics class

package Facebook;
import java.util.*;
public class testFacebook{
public static void main (String[] args){
// Prompt user to enter the number of facebookpresons Scanner userInput = new Scanner(System.in); System.out.println("Enter the number of facebookpresons to be created: "); int numP=0; while(true){ try{ String inputString = userInput.nextLine(); numP = Integer.parseInt(inputString); if(numP>0 && numP<=5) // accept the number if it is within range. Here we define the range to be from 1 to 5. break; else System.out.println("the number is out of range [1, 5]! enter again"); } catch (NumberFormatException e){ System.out.println("invalid input. Enter an integer number!"); } }
FacebookPerson[] fbp = new FacebookPerson[numP];
//Ask the user to enter the name for each person, and create the persons for(int i=0; i< numP; i++){ System.out.println("Enter the name for person "+ (i+1)+ ":"); String name = userInput.nextLine(); fbp[i] = new FacebookPerson(name); } System.out.println("-------select a person and type the mood below--------");
//Ask the user to set the mood for a person, and update the mood, enter "####" to exit while(true){ System.out.println("Enter the name for a person (enter #### to exit):"); String name = userInput.nextLine(); if(name.equals("####")) System.exit(0); int personID = -1; for(int i=0; i< numP; i++){ if(fbp[i].getName().equals(name)){ personID = i; break; // break the for loop } } if(personID!=-1){ // found the person, otherwise personID should still be -1 System.out.println("Enter the mood for the person:"); String mood = userInput.nextLine(); fbp[personID].setMood(mood); } else System.out.println("unrecognized name!"); } // end while
} // end main
} Source code of the discussed Facebook example is available at: https://grid.cs.gsu edu/xhu/ CSC1302/Content/Facebook.rar (note: use a file compression software such as winzip or winrar to open the rar file). You can run the testFac how the program works. ebook class to see Your HWS is to modify the Facebook example so that instead of each person has its own Facebook window, all persons are displayed in the same Facebook window. Different persons are shown at different regions of the window as illustrated by the figure at the end of this document. The size of the Facebook window is 700 x 550 and the size of each person's region is 200 x 150 The program will ask the user to enter the number of facebookpresons to be created. The maximum number of person that can be created is 9 (i.c. maximum three rows in the window). If the entered number is greater than 9 or smaller than 1, the program will ask the user to enter a new number until the entered number is between 1 and 9 (inclusive). Then the program will ask the user to enter the person names and create their corresponding regions in the window with their default mood displayed (see the illustrative figure at the end of this document). The user can then select a person (the program will prompt an "unrecognized name!" message if the typed name is not recognizable). After the person is selected, the user can do the following two things: 1) type in the mood for that person; or 2) type "SS$" to delete the person. In the first case (i.e. the user's input is not "SSS"), the selected person's region is updated in the following way When the mood is happy, the region has a red background and the mood is displayed. When the mood is sad, the region has a green background and the mood is displayed. Otherwise, the region has a white background and the mood is displayed. In the second case (i.e., the user's input is "SSS"), the person is deleted and the person's region (including the black boundary of the region) in the window is erased. An illustration of how the program runs and the corresponding window display are given at the end of this document. What to submit: Package all the java files in a "Facebook_HW5" package and submit the entire Facebook HW5 package. Your package should include a "testFacebook.java", which we will use to run your programs

Explanation / Answer

The graphics is handled by the class DrawingPanel.class

For each FacebookPerson object there is a Fasebook objerct as specified in the constructor:

public FacebookPerson(String name){
      myName = name;
      myfacebook = new Facebook(myName);
      //System.out.println("FacebookPerson's constructor");
}

Similarly, for each Fasebook object, there is a DrawingPanel object as specified in its constructor:

public Facebook(String nm){
       content = "undefined";
       name = nm;

       // Create the drawing panel
       panel = new DrawingPanel(200, 150);
       g = panel.getGraphics();

       // display name
       g.drawString(name+"'s mood is undefined.", 20, 75);
   }

The DrawingPanel calss provides the graphics for each person in a seperate frame.

Whenever, we set the mood of a person with fbp[personID].setMood(mood); in testFacebook class, it calls back the setContent() method of Facebook class which changes the graphics of that person according to the mood.