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

Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
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 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...
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.
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and...
java NetBeans Class Entry: Implement the class Entry that has a name (String), phoneNumber (String), and address (String). Implement the initialization constructor . Implement the setters and getters for all attributes. Implement the toString() method to display all attributes. Implement the equals (Entry other) to determine if two entries are equal to each other. Two entries are considered equal if they have the same name, phoneNumber, and address. Implement the compareTo (Entry other) method that returns 0 if the two...
IN Java please 1. Define a class called ListExercise containing one data field: a list of...
IN Java please 1. Define a class called ListExercise containing one data field: a list of integer. Create two constructor methods, one accessor(getter) method and one mutator(setter) method for the ListExercise class. 2. Implement a reverse(int[] a) method to reverse the elements in the list. For example, reverse(Arrays.asList(new int[ ]{1,2,3})) should return {3,2,1}. 3. Implement a swapValue(List a, int i, int j) to swap the values in index i and j. 4. Implement a pickMost(int[] a) method to pick the...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT