Question

In: Computer Science

Java Define a class called BlogEntry that could be used to store an entry for a...

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

Solutions

Expert Solution

//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


Related Solutions

Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
1. Define a class called Odometer that will be used to track fuel and mileage for...
1. Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer’s total, and an accessor method...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type T passed in during construction, then returns their sum through a non-static method.
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Define a JAVA class ISP. The class consists of the name of the subscription plan and...
Define a JAVA class ISP. The class consists of the name of the subscription plan and a method that display the plan. Derive a class DPlan from ISP. The DPlan will charge RM10 per Mbps subscribe and RM0.20 per GB used. Derive a class MPlan from ISP. The MPlan will charge RM5 per Mbps subscribe and RM0.80 per GB used. Display the plan and select the best plan.
JAVA How to make a graph class that uses a linked list class to store nodes...
JAVA How to make a graph class that uses a linked list class to store nodes and linked list within each node to store adjacency list The linked list class has been made already.   import java.util.*; public class LinkedList implements Iterable { private int size = 0; private Node head; private Node tail; private class Node { private T data; private Node prev; private Node next; public Node(T data) { this.data = data; } }    public Iterator iterator() {...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT