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

Project Outcomes - Develop a Java program that: reads input from the keyboard us

ID: 675535 • Letter: P

Question

Project Outcomes - Develop a Java program that:

reads input from the keyboard using a Scanner Object and its methods

uses selection (if and if else) statements

uses String comparison methods.

follows standard acceptable programming practices.

validates user input to limit security risk of harmful input.

Prep Readings:

Absolute Java Chapter 1 – 4 and Security Injection Labs (Integer Error and Input Validation)

Project Requirements:

1.This program defines a class for BlogEntry, which can be used to store an entry for a web log. The class tracks the poster's username, text of the entry, and date posted using the Date class defined in this chapter Display 4.13(Date.java from the book examples).

2.To test the BlogEntry class the project will define another class called BlogEntryTester that will create BlogEntry objects using data read from a file.

3.BlogEntry Class

a.Stores Blog Entry information.

b.Instance Variables

i.String username – name of the blogger

ii.Date dateOfBlog – date of the blog

iii.String blog – the text of the blog

iv.All instance variables are private.

c.Constructors

i.No-Argument or default constructor (see Pet class Display 4.15, Pet( ) as an example) .

ii.Parameterized constructor, (see Pet class Display 4.15, Pet(String initialName, int initialAge, double initialWeight) as an example)

1.Parameters include user’s name, date of blog and blog text.

2.Uses the parameters to set the appropriate instance variables.

d.Methods

i.Accessor methods (See Pet class for examples.)

1.String getUsername( ).

2.Date getDateOfBlog( ).

3.String getBlog( ).

ii.Mutator methods (See Pet class for examples.)

1.void setUsername(String … )

2.void setDateOfBlog(Date …)

3.void setBlog(String …)

iii.String getSummary() method

1.Returns up to the first ten words of the entry as a summary of the entry. If the entry has 10 words or less, the method returns the entire entry.

2.Possible logic - The String classes' indexOf method can find the position of a space. Use this along with a looping construct to find the first 10 words.

iv.String toString() method

1.Returns a nicely formatted String of all the data in object.

2.Use the accessor method to populate the returned String, do not just access the instance variables directly

3.Example String:

Author: Joey

Date posted: April 16, 2013

Text body: I went to The Masters last week, it was fantastic.

v.Remember all methods are public.

4.BlogEntryTester

a.This class will be used to test the BlogEntry Class. It will include the main method that starts the Java application

b.Class Requirements

i.Create at least two BlogEntry objects, one using the default constructor and the other using the parameterized constructor.

ii.Directly or indirectly test all methods of the BlogEntry class.

1.Indirect testing would include testing the accessor method by calling the toString method, since the toString subsequently calls the accessor methods.

2.Use the object created by the default constructor to set the instance variables using the mutator method.

3.Call the getSummary method at least twice, once with a BlogEntry object with 10 words or greater and once with a BlogEntry object with more than 10 words.

iii.Read the BlogEntry objects data from a file and pass that to the BlogEntry object either when creating the objects or via the mutator methods.

1.See Book Section2.3 and Display 2.12 for information on how to read from a file.

2.The file structure would be: Name, month, day, year, blogtext. Such as Joey Smith, 4, 16, 2013, I went to The Masters last week, it was fantastic

a.Use the Date classes dateOK() method to ensure date information is valid.

b.Test the length of the name variable to ensure it is within not larger than 20 characters.

c.See sample file test.dat on elearning.

3.Since the file field are separated (delimited) by comma, you need to inform the Scanner object of this. Note that you also need to delimit by a new line as well as by a comma. See the Scanner classes’ useDelimiter method in the Java API for details on how to handle this.

5.Include javadoc class comment following Project Comment Template on the content page.

6.The project requires two files to be turned in – BlogEntry.java and BlogEntryTester.java. Please note that during grading we will use our own data file and the Date class defined in this chapter Display 4.13(Date.java from the book examples).

7.Be sure to follow the Programming Project Submission Instructions posted by your instructor on the elearning site. The submission requirements are part of the grading for this assignment.

Explanation / Answer

Answer:

/***BlogEntry.java****/

import java.io.*;

import java.util.*;

// class BlogEntry

public class BlogEntry

{

private String username;

private Date dateOfBlog;

private String blog;

public BlogEntry()

{

username="";

dateOfBlog=new Date();

blog="";

}

public BlogEntry(String uName, Date bDate, String textBlog)

{

username=uName;

dateOfBlog=bDate;

blog=textBlog;

}

//ACCESSOR METHODS

public String getUsername( )

{

return username;

}

public Date getDateOfBlog( )

{

return dateOfBlog;

}

public String getBlog( )

{

return blog;

}

//MUTATOR METHODS

public void setUsername(String uName )

{

username=uName;

}

public void setDateOfBlog(Date bDate)

{

dateOfBlog=bDate;

}

public void setBlog(String textBlog)

{

blog=textBlog;

}

//getSummary()

public String getSummary()

{

String blogSummary="";

int countBlogWord=0;

int bStart=0;

int bpos;

bpos=blog.indexOf(" ");

while(bpos!=-1&&countBlogWord<10)

{

blogSummary+=blog.substring(bStart,bpos);

blogSummary+=" ";

bStart=bpos+1;

bpos=blog.indexOf(" ",bpos+1);

countBlogWord+=1;

}

return blogSummary;

}

//toString() return the blog details

public String toString()

{

String nSt=null;

nst= "AUTHOR: " + this.getUsername() + " " + "Date posted: " + this.getDateOfBlog() + " " + "Text body: " + this.getBlog();

return nSt;

}

}

// BlogEntryTester.java

public class BlogEntryTester

{

public static void main(String[] args)

{

BlogEntry obj1=new BlogEntry();

BlogEntry obj2=new BlogEntry("John",new Date(4,15,2013),"I went to The Masters last week, it was fantastic");

System.out.println("Blog Object 1:");

System.out.println(obj1.toString());

System.out.println("Blog Object 2:");

System.out.println(obj2.toString());

System.out.println(obj1.getSummary());

System.out.println(obj2.getSummary());

//READING FROMFILE

File blogFile=new File("test.dat");

try

{

Scanner rFile=new Scanner(blogFile);

while(rFile.hasNextLine())

{

rFile.useDelimiter(",");

String aName=rFile.next();

int m=Integer.parseInt(rFile.next());

int d=Integer.parseInt(rFile.next());

int y=Integer.parseInt(rFile.next());

String textBlog=rFile.next();

BlogEntry obj3;

//testing if Date is valid

if(dateOK(m,d,y)

{

Obj3=new BlogEntry(aName,new Date(m,d,y),textBlog);

}

else

Obj3=new BlogEntry(aName,new Date(),textBlog);

//printing the blogObject created fromfile

System.out.println(obj3.toString());

}

}

catch(Exception exp)

{

exp.printStackTrace();

}

}//main end

}//driver class ends