Question

This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and TweetManager. You will load a set of tweets from a local file into a List collection. You will perform some simple queries on this collection.

The Tweet and the TweetManager classes must be in separate files and must not be in the Program.cs file.

The Tweet Class

The Tweet class consist of nine members that include two static ones (the members decorated with the $ symbol). You will implement this and all of the classes in Visual Studio. A short description of the class members is given below:

Tweet Class Fields - $ CURRENT_ID : int Properties + From : string «set absent» + To : string «set absent» + Body : string «s

The $ symbol is used to denote that this member belongs to the type rather than a specific object.

Fields:

1. CURRENT_ID – this private field is a class variable, it represents the number to be used in setting the id of this tweet.

Properties:

1. All of the properties are readonly and and are self-explanatory.

From – this property is a string representing the originator of this tweet. The getter is public and the setter is absent.

2. To – this property is a string representing the intended recipient of this tweet. The getter is public and the setter is absent.

3. Body – this property is a string representing the actual message body of this tweet. The getter is public and the setter is absent.

4. Tag – this property is a string representing the hash tag of this tweet. The getter is public and the setter is absent.

5. Id – this property is a string representing the id of this tweet. The getter is public and the setter is absent. This is used to uniquely identify a tweet.

Methods:

1. public Tweet(string from, string to, string body, string tag) – This public constructor takes four string parameters. This constructor does the following:

  1. Assigns the arguments to the appropriate properties.

  2. Sets the Id property using the class variable CURRENT_ID.

  3. After the Id property is set, the CURRENT_ID is then incremented so that the next assignment will be unique. (see description of Id above)

2. public override string ToString() – This method overrides the same method of the Object class. It does not take any parameter but return a string representation of itself. You decide on the format for the output. You should also consider outputting only part of the Body. Use the Substring() method of the string class to do this.

3. public static Tweet Parse(string line) – This is a public class method that takes a string argument and returns a Tweet object. It is used to create a Tweet object when loading the tweets from a file. The argument represents a single line of input read from the file. This method does the following:

a. Uses the method of the string class is to chunk the input into four strings. The default delimiter for the Split() method is a space, however in this case the delimiter should be a tab. To specify an argument use the following code: Split(new char[]{' '});

b. Invokes the constructor with the four arguments Return the

c. result of the above invocation

The TweetManager Class

This static class consist of five static members. You will also implement this in Visual Studio. A short description of the class members is given below:

TweetManager Static Class Fields -$ TWEETS : List<Tweet> -$ FILENAME : string Methods $ TweetManager() +$ Initialize() : stri

Fields:

1. TWEETS – this private field is a class variable, it is a collection of all the tweets in the system. It is initialized in the static constructor. It is populated in the static constructor.

2. FILENAME – this private field is a class variable, it represents the name of the file that contains all the tweets. It is used in the static constructor to read in the tweets. You will have to set this to the name of file that has the information about the tweets.

Methods:

1. static TweetManager() – This is the static constructor. It does not require any parameter. This constructor does the following:

a. Initialize the tweets field to a new list of tweets
b. Opens the file specified by the filename field for reading

c. Using a looping structure it does the following:

  1. Reads one line from the file

  2. Passes this line to the static Parse() method of the Tweet class to create a

    tweet object

  3. The resulting object is added to the tweet collection

    1. This is repeated until the input from the file is empty (null).

      2. public static void Initialize() – This class method it used to facilitate the development of this project. It will not be used in the production code, just while developing. This method does the following:

      a. Creates about 5 tweets objects and add them to the tweet collection.

      3. public static void ShowAll() – This is a public class method that does not take any argument that does not return a value.

      4. public static void ShowAll(string tag) – This is a public class method that takes a string argument that does not return a value. It display all the tweets with tag matching the argument.

      Testing

      In your test harness (the Main() method in the Program Class), write the code to test all the methods of the TweetManager class except the Initialize() method

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code to copy

main.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TweetApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //TweetManager.ShowAll();
            TweetManager.ShowAll("taxes");
            TweetManager.ShowAll("Ford");
        }
    }
}


===================================================================================

Tweet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TweetApp
{
    class Tweet
    {
        //constant field
        private static int CURRENT_ID;
      
        //properties
        public string From {get; private set;}
        public string To {get; private set;}
        public string Body {get; private set;}
        public string Tag {get; private set;}
        public string Id {get; private set; }

        /*
            This public constructor takes four string parameters. This constructor does the following:
            a.   Assigns the arguments to the appropriate properties.
            b.   Sets the Id property using the class variable CURRENT_ID.
            c.   After the Id property is set
         */

        public Tweet(string from, string to, string body, string tag)
        {
            From = from;
            To = to;
            Body = body;
            Tag = tag;
            Id = ""+ CURRENT_ID;
            CURRENT_ID++;
        }

        /*
        This method overrides the same method of the Object class.
       */
        public override string ToString()
        {
            return string.Format("ID: {0}\nFrom: {1}\nTo: {2}\nBody: {3}...\nTag: {4}",
                Id, From, To, Body.Substring(0, Body.Length), Tag);
        }

        /*
         * This is a public class method that takes a string argument and returns a Tweet object.
         * It is used to create a Tweet object when loading the tweets from a file.
        */
        public static Tweet Parse(string line)
        {
            string[] tweetParam = line.Split('\t');
            Tweet tweetLine = new Tweet(tweetParam[1], tweetParam[2], tweetParam[3], tweetParam[0]);
            return tweetLine;
        }

    }
}

===================================================================================

TweetManager.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TweetApp
{
    static class TweetManager
    {
        /*
         * Private fields to stop List of Tweets and filename
         */
        private static List<Tweet> tweets;
        private static string filename = @"TweetFile.txt";

        /*
         * This is the static constructor. It does not require any parameter.
         * This constructor does the following:
         * a.   Initialize the tweets field to a new list of tweets
         * b.   Opens the file specified by the filename field for reading
         * c.   Using a looping structure it does the following:
         * i.   Reads one line from the file
         * ii.   Passes this line to the static Parse() method of the Tweet class to create a tweet object
         * iii.The resulting object is added to the tweet collection
         * iv.   This is repeated until the input from the file is empty (null).
         */
        static TweetManager()
        {
            tweets = new List<Tweet>();
            using (TextReader reader = new StreamReader(filename))
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    tweets.Add(Tweet.Parse(line));
                    line = reader.ReadLine();
                }
            }
        }

        /*
         * This class method it used to facilitate the development of this project.
         * It will not be used in the production code. This method does the following:
         * a.   Creates about 5 tweets objects and add them to the tweet collection.
         */
        public static void Initialize(string line)
        {
            tweets.Add(Tweet.Parse("Ford   Trudeau   Bieber   Go Raptors! Go!"));
            tweets.Add(Tweet.Parse("WeTheNorth   Drake   Obama   Go Raptors! Go!"));
            tweets.Add(Tweet.Parse("Raptors   Tory   Drake   Yes Toronto will get them!"));
            tweets.Add(Tweet.Parse("Ford   Trudeau   Obama   Toronto joins cities around the world to celebrate International Day Against Homophobia & Transphobia"));
            tweets.Add(Tweet.Parse("Ford   Drake   Bieber   Go Raptors! Go!"));
        }

        /*
         * This is a public class method that does not take any argument that
         * does not return a value. It display all the tweets matching this tag.
         */
        public static void ShowAll()
        {
            foreach (Tweet tweet in tweets)
            {
                Console.WriteLine("{0}\n\n", tweet);
            }
        }

        /*
         * This is a public class method that takes a string argument
         * that does not return a value. It display all the tweets matching this tag.
         */
        public static void ShowAll(string tag)
        {
            Console.WriteLine("Tweet with {0} tag", tag);
            Console.WriteLine("---------------------");
            foreach (Tweet tweet in tweets)
            {
                if (tweet.Tag.ToLower() == tag.ToLower())
                {
                    Console.WriteLine ("{0}\n",tweet);
                }
            }
        }
    }
}

===================================================================================

TweetFile.txt

Raptors   Drake   Obama   Go Raptors! Go!
Raptors   Tory   Drake   Yes Toronto will get them!
Raptors   Tory   Obama   Toronto joins cities around the world to celebrate International Day Against Homophobia & Transphobia
Taxes   Drake   Bieber   Go Raptors! Go!
Raptors   Drake   Obama   Go Raptors! Go!
Raptors   Tory   Drake   Yes Toronto will get them!
Raptors   Tory   Obama   Toronto joins cities around the world to celebrate International Day Against Homophobia & Transphobia
Ford   Trudeau   Bieber   Go Raptors! Go!
WeTheNorth   Drake   Obama   Go Raptors! Go!
Raptors   Tory   Drake   Yes Toronto will get them!
Ford   Trudeau   Obama   Toronto joins cities around the world to celebrate International Day Against Homophobia & Transphobia
Ford   Drake   Bieber   Go Raptors! Go!


==================================================================================

Program Screenshot

main.cs

main.cs Tweet.cs TweetManager.cs TweetFile.txt 3 4 inc using System; using System.Collections.Generic; using System.Linq; usi

===================================================================================

Tweet.cs

TweetFile.txt main.cs Tweet.cs TweetManager.cs 1- using System; using System.Collections.Generic; using System.Linq; 4 using

main.cs Tweet.cs TweetManager.cs TweetFile.txt This public constructor takes four string parameters. This constructor does th

==================================================================================

TweetManager.cs

TweetFile.txt main.cs Tweet.cs TweetManager.cs 1- using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 us

main.cs Tweet.cs TweetManager.cs TweetFile.txt line = reader.ReadLine(); 38 40 * This class method it used to facilitate the

main.cs Tweet.cs TweetManager.cs: TweetFile.txt tweets. Add (Tweet.Parse(Ford Trudeau Bieber Go Raptors! Go!)); tweets.Add(

================================================================================

TweetFile.txt

main.cs Tweet.cs TweetManager.cs TweetFile.txt 1 Raptors Drake Obama Go Raptors! Go! 2 Raptors Tory Drake Yes Toronto will ge

===================================================================================

Sample output

input Tweet with taxes tag --------------------- ID: 3 From: Drake To: Bieber Body: Go Raptors! Go!... Tag: Taxes Tweet with

Add a comment
Know the answer?
Add Answer to:
This assignment attempts to model the social phenomenon twitter. It involves two main classes: Tweet and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you...

    JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student...

    Write an abstract class “Student” and three concrete classes, “UnderGrad” and “Graduate” both inherit from Student and “PostGraduate” that inherits form “Graduate”. Write the class definition for the abstract class “Student”. The class definition should include private instance variables of type String to hold the student’s first name, a string for his/her major and an int to hold the number of units taken. Getter and setter methods for each of the variables should be included in the class definition. Also...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Create a C++ console program that defines a class named EvenNumber that represents an even number....

    Create a C++ console program that defines a class named EvenNumber that represents an even number. Create the class inside a separate header file (.h) and then include this header in your main source code file. The class should have one private integer data field to store the even number. The default constructor should initialize the data field to 0. Also define a 1-arg constructor that initializes the object with the specified value. Define a public getter method named getValue...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class....

    Consider the class public elass Box·xtenda 0bJeetE private String stuff』 Write two constructors for the class. The first constructor will take zero input arguments and set the stuff attibute to null. The second will take a String as input and set the shul atbute to input string Getter and Setter Write a public getter method called getsauff that returns the string stored by the sthuff attribute If stulf is null, it should return the string "no stuff in here Write...

  • Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp....

    Additional info is this will use a total of four classes Book, BookApp, TextBook, and TextBookApp. Also uses Super in the TextBook Class section. Problem 1 (10 points) Вook The left side diagram is a UML diagram for Book class. - title: String The class has two attributes, title and price, and get/set methods for the two attributes. - price: double Вook) Book(String, double) getTitle(): String setTitle(String): void + getPrice() double setPrice(double): void toString() String + The first constructor doesn't...

  • Follow program instructions carefully. spacing is important. I think It needs a static void main too....

    Follow program instructions carefully. spacing is important. I think It needs a static void main too. Need to run it! You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT