In: Computer Science
Java
Define a class called BlogEntry that could be used to store an entry for a Web log.
The class should have instance variables to store :
- the poster’s username,
- text of the entry,
- and the date of the entry using the Date class
Date class is:
class Date
{
private int day, year;
private String mon;
public Date()
{
mon=" ";
day=0;
year=0;
}
public String toString()
{
return (mon+"/"+day+"/"+year);
}
public set_date(String m, int d, int y)
{
mon=m;
day=d;
year=y;
}
}
Methods:
-Add a constructor that allows the user of the class to set all instance variables.
-Also add a method, DisplayEntry()/toString(), that outputs all of the instance variables,
- method called getSummary that returns the first 10 characters from the text (or the entire text if it is less than 10 characters).
.
.
.
.
.
.
my code so far:
import java.util.Scanner;
public class BlogEntry
{
private String name;
private String entry;
public BlogEntry(String n, String e, String m, int d,
int y)
{
System.out.println("Poster's name:
"+n+"\nEntry Text: "+e);
System.out.println("Date: "+m+"
"+d+", "+y);
}
public String getName()
{
return name;
}
public void setName(String n)
{
this.name=n;
}
public String getEntry()
{
return entry;
}
public void setEntry(String e)
{
this.entry=e;
}
class date
{
private int d, y;
private String m;
public date(String m,int d, int y)
{
m= " ";
d=0;
y=0;
}
public void setdate(String m, int d, int y)
{
m=m;
d=d;
y=y;
}
}
private void String setSummary()
{
String summary=" ";
int space=0;
int script=0;
while(space<=9 &&
script<entry.length())
{
String
next=entry.substring(script,script+1);
if(next.equals(" "))
{
if(space<=9)
space++;
else
break;
}
summary+=next;
script++;
}
return summary;
}
public getSummary()
{
return summary;
}
public static void main(String [] args)
{
String name;
String entry;
String month;
int day;
int year;
Scanner input=new Scanner(System.in);
System.out.println("Please enter the Poster's name: ");
name=input.nextLine();
System.out.println("What is the text for the blog entry:
");
entry=input.nextLine();
System.out.println("What is the date of the entry?");
System.out.println("Please enter the month: ");
month=input.nextLine();
System.out.println("Please enter the day: ");
day=input.nextInt();
System.out.println("Please enter the year: ");
year=input.nextInt();
System.out.println("\nBlog Entry:");
BlogEntry entry1= new BlogEntry(name,entry,month,day,year);
BlogEntry entry2= new BlogEntry(name,entry,month,day,year);
entry2.setSummary(entry);
System.out.println("\nThe summary of this entry is
"+entry2.getSummary);
}
}
.
.
.
.
the error is occuring for my getSummary and setSummary
BlogEntry.java:47: error: '(' expected private void String setSummary() ^
BlogEntry.java:67: error: invalid method declaration; return type required public getSummary() ^
2 errors
//Try this Java code
class Date
{
private int day, year;
private String mon;
public Date()
{
mon=" ";
day=0;
year=0;
}
//overload constructor
public Date(int day, int year, String mon) {
this.day = day;
this.year = year;
this.mon = mon;
}
@Override
public String toString()
{
return (mon+"/"+day+"/"+year);
}
public void set_date(String m, int d, int y)
{
mon=m;
day=d;
year=y;
}
}
//==========================================
public class BlogEntry {
/**
* the poster’s username,
*/
private String userName;
/**
*text of the entry,
*/
private String entry;
/**
* date of the entry using the Date class
*/
private Date date;
//Constructor
public BlogEntry(String userName, String entry, Date date) {
this.userName = userName;
this.entry = entry;
this.date = date;
}
//Getters and setters
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEntry() {
return entry;
}
public void setEntry(String entry) {
this.entry = entry;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
/**
*
* @return outputs all of the instance variables,
*/
@Override
public String toString() {
return "User Name: "+userName+"\n Blog Date: "+date+"\nBlog Entry: \n"+entry;
}
/**
*
* @return the first 10 characters from the text
* (or the entire text if it is less than 10 characters).
*/
public String getSummary()
{
if(entry.length()<=10)
{
return entry;
}
else
{
return entry.substring(0,10);
}
}
}
//===========================================
import java.util.Scanner;
public class BlogEntryTest {
public static void main(String[] args)
{
String name;
String entry;
String month;
int day;
int year;
Scanner input = new Scanner(System.in);
//Prompt to user
System.out.print("Please enter the Poster's name: ");
name=input.nextLine();
System.out.print("What is the text for the blog entry: ");
entry=input.nextLine();
System.out.println("What is the date of the entry?");
System.out.println("Please enter the month: ");
month=input.nextLine();
System.out.print("Please enter the day: ");
day=input.nextInt();
System.out.print("Please enter the year: ");
year=input.nextInt();
//Create object
BlogEntry blogEntry = new BlogEntry(name,entry,new Date(day,year,month));
System.out.println("\nBlog Entry:");
System.out.println(blogEntry);
System.out.println("\nBlog Summary:");
System.out.println(blogEntry.getSummary());
}
}
//Output

//If you need any help regarding this solution .......... please leave a comment ...... thanks