Write Secret class with the following Instance variables: secret1 (a String): 1s
ID: 3763635 • Letter: W
Question
Write Secret class with the following
Instance variables:
secret1 (a String): 1st secret word
secret2 (a String): 2nd secret word
secret3 (a String): 3rd secret word
password (an integer): unlocks secret
Methods:
constructor:
invokes the method “decipherSecret1()” to initialize “secret1.”
It invokes the method ”decipherSecret2()” to initialize “secret2.”
It invokes the method “decipherSecret3()” to initialize “secret3.”
It invokes the method “createPassword()” to initialize “password.”
public String decipherSecret1(): reads the second token of the file “input1.txt” and returns it.
public String decipherSecret2(): reads the third token of the file “input2.txt,” reverses it (e.g., drow --> word), and returns it.
public String decipherSecret3(): reads the last token of the file “input3.txt,” removes the first and last characters, and returns it.
public void createPassword(): creates a random number between 0 to 9.
public String getTheSecret(int password): It checks the password using the method “isPasswordCorrect.” If the password is correct, it returns a string after concatenating secret2 + secret3 + secret1. Otherwise, it returns a warning message.
public boolean isPasswordCorrect(int password): returns true if this.password == password. Otherwise, it returns false.
Public String toString(): returns a random string.
Explanation / Answer
import java.util.*;
import java.io.*;
public class Secret
{
String secret1;
String secret2;
String secret3;
int password;
Secret() throws IOException
{
secret1= decipherSecret1();
secret2= decipherSecret2();
secret3= decipherSecret3();
password=createPassword();
}
public String decipherSecret1() throws IOException
{
FileReader file = new FileReader("input1.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
String[] words =line.split("\s+");
reader.close();
return words[1].toString();
}
public String decipherSecret2() throws IOException
{
FileReader file = new FileReader("input2.txt");
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
String[] words =line.split("\s+");
reader.close();
int len = words[2].length(),i;
char rWord[] = new char[20];
for(i=0;i<len;i++)
{
rWord[i] = words[2].charAt(len-i-1);
}
rWord[i] = '';
reader.close();
return rWord.toString();
}
public String decipherSecret3() throws IOException
{
FileReader file = new FileReader("input3.txt");
Scanner reader = new Scanner(file);
String line="";
while(reader.hasNext())
{
line=reader.next();
}
line = line.substring(1, line.length()-2);
reader.close();
return line.toString();
}
public int createPassword()
{
return (int) Math.random()%10;
}
public String getTheSecret(int password)
{
if (isPasswordCorrect(password))
return secret2 + secret3 + secret1;
else
return "Password is wrong.";
}
public boolean isPasswordCorrect(int password)
{
if (this.password == password)
return true;
else
return false;
}
public String toString()
{
return "Encryption Program";
}
}
import java.io.IOException;
public class SecretDemo
{
public static void main(String[] args) throws IOException
{
Secret msg =new Secret();
System.out.println(msg.getTheSecret(msg.createPassword()));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.