Question: I'm give a model and the controller of a application, I'm only allowed to write code in unscramble method inside the model? Here's a youtube
I'm give a model and the controller of a application, I'm only allowed to write code in unscramble method inside the model?
Here's a youtube video of what the app should do: https://www.youtube.com/watch?v=-Yxck6IupbM
Here's the controller
package com.example.billnasir.jumbleapp2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.List; public class Controller extends AppCompatActivity { private Dictionary dictionary; /** * Initializes this controller. * * @param savedInstanceState information needed for re-initialization. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_controller); // get the dictionary file File path = Environment.getExternalStorageDirectory(); File file = new File(path, "dictionary.txt"); // create a dictionary this.dictionary = new Dictionary(file); } /** * Unscrambles the entered jumble. * * @param view not applicable. */ public void jumble(View view) { // get the input as text View inputView = findViewById(R.id.input); EditText inputText = (EditText) inputView; String input = inputText.getText().toString(); // get the unscramblings List unscramblings = this.dictionary.getUnscramblings(input); // output View outputView = findViewById(R.id.output); TableLayout output = (TableLayout) outputView; for (String word : unscramblings) { TableRow row = new TableRow(this); TextView cell = new TextView(this); cell.setText(word); row.addView(cell); output.addView(row); } } }
Here's the model
package com.example.billnasir.jumbleapp2; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; /** * The dictionary, which is the model of this app. * * @author Franck van Breugel */ public class Dictionary { ArrayList words = new ArrayList(); private HashMap dictionaryList; /** * Initializes this dictionary from the given file. Each line of the file contains a * single word. * * @param file file containing the words of this dictionary. */ public Dictionary(File file) { try { Scanner input = new Scanner(file); while (input.hasNextLine()) { String word = input.nextLine(); } input.close(); } catch (FileNotFoundException e) { // do nothing } } /** * Returns the list of words that are unscramblings of the given word. * * @param word word to be unscrambled. * @return list of words that are unscramblings of the given word. */ public List getUnscramblings(String word) { words = new ArrayList<>(); for(int i = 0; i < word.length();i++) { Set temp = new HashSet();// make a temp Set here char c = word.charAt(i); for(String str: words)// iterate strList temp.add(str + c); // it will union strList with c words.add(c+""); // add c to strList words.addAll(temp); // add temp with strList } // System.out.println(strList); return words; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
