Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In JAVA please! The log class will allow to store multiple string messages but o

ID: 3793177 • Letter: I

Question

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)

PLEASE DO NOT USE ANY built in functions such as ARRAYLIST. thanks

Explanation / Answer

//This is log class as you mention to store multiple string

package com.logger;

public class Log {
String[] str;
}

//This is Logger class having four method as below

package com.logger;

public class Logger {
   String msg;
   Log log;
   static int i=0;
   String[] str= new String[5];
  
   public Logger(){
       System.out.println("welcome");
   }
  
   public Log duplicate(){
      
       log=new Log();
         
      
       System.out.println("i m in duplicate: "+msg);
       str[i]=msg;
      
       System.out.println("in duplicate assing value: "+str[i]);
       i++;
       return log;
   }
  
   public String[] read(){
      
       return str;
   }
  
  
  
   public void record(String msg){
       this.msg=msg;
       System.out.println("You entered msg is: "+msg);
   }
  
}

//This is UserTest class to test above class run this code

package com.logger;

import java.util.Scanner;

public class UserTest {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
Logger logger=new Logger();
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to continue and 2 for exit");
int b=sc.nextInt();
int a=1;
if(b==1){
while(a<=5){
   a++;
   System.out.println("Enter a message");
  
String msg=sc.next();
logger.record(msg);
logger.duplicate();

}
}else{
  
   System.out.println("You are exit");
}
  
String[] stt=logger.read();
for(String sss:stt){
   System.out.println("You entered msg is: "+sss);
  
}
   }

}

//This gives output as below

welcome
Enter 1 to continue and 2 for exit
1
Enter a message
aaa
You entered msg is: aaa
i m in duplicate: aaa
in duplicate assing value: aaa
Enter a message
bbb
You entered msg is: bbb
i m in duplicate: bbb
in duplicate assing value: bbb
Enter a message
ccc
You entered msg is: ccc
i m in duplicate: ccc
in duplicate assing value: ccc
Enter a message
ddd
You entered msg is: ddd
i m in duplicate: ddd
in duplicate assing value: ddd
Enter a message
eee
You entered msg is: eee
i m in duplicate: eee
in duplicate assing value: eee
You entered msg is: aaa
You entered msg is: bbb
You entered msg is: ccc
You entered msg is: ddd
You entered msg is: eee

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote