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 PLEASE The Palindrome class will have a single constructor that accepts a String argument...
IN JAVA PLEASE The Palindrome class will have a single constructor that accepts a String argument and checks to see if the String is a palindrome. If it is a palindrome it will store the palindrome and the original String as state values. If is not a palindrome it will throw a “NotPalindromeError” and not finish the creation of the new Palindrome object. The constructor will use a single stack to evaluate if the String is a palindrome. You must...
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...
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: 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...
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)
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!   Write a class Store which includes the attributes: store name, city. Write another class...
IN JAVA!   Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT