Question

In: Computer Science

In JAVA please! The log class will allow to store multiple string messages but one at...

In JAVA please!

The log class will allow to store multiple string messages but one at a time (via record(..)), and also get back the whole sequence of messages (via read()).

assume and gurantee that each message is a single line of text.

the fields can be of my/your choice

four different methods in this class include;

public Logger() . establishes a new and empty log.

public Log duplicate() . this is not a constructor, but creates a no-aliases copy of this log and returns it. This becomes useful for when sharing the information without allowing modification.

public String[] read() . this will return an array of strings where each item in the array is the next line of the recorded content.

public void record(String msg) . this will record the given message as the next line in the log file.

here you will assume there are no newline characters in the message (the behavior is not defined if they are present; this means they must not be accounted for but if you want you can)

Solutions

Expert Solution

// Online URL for testing: http://ideone.com/BikSrN
import java.util.*;
class Log{
   private ArrayList<String> msgs;
   public Log(){
       msgs=new ArrayList<String>();
   }

   public Log duplicate(){
       Log dup=new Log();
       dup.msgs.addAll(this.msgs);
       return dup;
   }
  
   public String[] read() {
       return msgs.toArray(new String[msgs.size()]);
   }

   public void record(String msg) {
       msgs.add(msg);
   }
}

class TestMain{
   public static void main(String args[]){
       Log t=new Log();
       t.record("Log1 : Message Record 1");
       t.record("Log1 : Message Record 2");
       t.record("Log1 : Message Record 3");  
       for(String s:t.read()){
            System.out.println(s);
        }
        System.out.println("Duplicating Log");
       Log d=t.duplicate();
       d.record("Log2 : Message Record 4");
       d.record("Log2 : Message Record 5");
       for(String s:d.read()){
            System.out.println(s);
        }
   }
}


Related Solutions

Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String (String1) (must be sorted) 2. ask user for second String (String2) (must be sorted) 3. Then prints all the characters of two previous Strings, in such a way that they are sorted (see examples below). 4. consider the possibility that strings are of different sizes, one or both can be empty, etc. 5. strings can contain any kind of characters as long as they...
Using java, create a class called MyString that has one String called word as its attribute...
Using java, create a class called MyString that has one String called word as its attribute and the following methods: Constructor that accepts a String argument and sets the attribute. Method permute that returns a permuted version of word. For this method, exchange random pairs of letters in the String. To get a good permutation, if the length of the String is n, then perform 2n swaps. Use this in an application called Jumble that prompts the user for a...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /**...
In the following Java class, what would the following createFromString(String string) and saveToString() methods return? /** * This class represents a DVD player to be rented. * * @author Franklin University * @version 2.0 */ public class DVDPlayer extends AbstractItem { /** * Constructor for objects of class DVDPlayer. */ public DVDPlayer() { // No code needed } /** * Creates a DVDPlayer from a string in the format * id:desc:weeklyRate:rented * @param string The string * @return the new...
WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket...
WRITE IN JAVA PLEASE~ A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters. A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT