Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Stack Implementation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a Java Program that can decipher a secret message from a text file. Each of the first letters are a command followed by a slash and a letter or number(for indexing) that will build the phrase. The commands are attached as well as the text file I created!

I am trying to get started on it, and I am stumped on how to begin and which direction to go towards building it. Does anyone have any ideas? I am new to Java, and learn better when hearing others perspectives!
Commands.jpg
[Thumbnail for Commands.jpg]
 
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Elise!

The first step is to parse the text file into a sequence of commands. You should start by writing a separate class for each of the possible command types, and another class that will take a sequence of text lines and convert each line to an instance of one of your command types.

Don't let the commands do anything yet. The first step is only to convert the text file.
 
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between these two
Hello
p

One is text the other is a command. According to your initial statement a command should be followed by a slash. Like
p/
This would differentiate text from a command.
 
Elise Ray
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p will print the word with the commands that have occurred so far
 
Elise Ray
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've gotten a lot done, and the program seems to be running well and displaying the correct output! There is also a Driver with the main method that calls the Decoder class and passes the textfile. Do you have any suggestions on how to improve it? I would appreciate some feedback!


 
Carey Brown
Saloon Keeper
Posts: 7393
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Elise Ray wrote:p will print the word with the commands that have occurred so far


I think it's text with only the letter 'p'.

You can use str.split("/") to get the arguments for a command.
 
Stephan van Hulst
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Elise Ray wrote:Do you have any suggestions on how to improve it? I would appreciate some feedback!


  • Make classes package private unless you intend to use them outside of their package. Unlearn the habit of using the public keyword.
  • Make classes final unless you have put considerable effort in making them extensible. This is not a trivial issue. Learn the habit of making all your classes final.
  • Close resources when you're done with them. A scanner wrapped around a file should be used in a try-with-resources statement.
  • The Stack class is more or less deprecated. Use ArrayDeque instead.
  • Use String only for names and identifiers. Otherwise, parse them to strongly typed instances. Your queue of instructions should be typed Deque<Instruction>.
  • Use the Path class for file paths. Not String and not File.
  • Validate your constructor and method arguments before you use them. Throw an exception if they're not valid.
  • Use a StringBuilder to build strings. Don't use String if it doesn't represent a complete, finished message.
  • Don't decode and execute instructions in the same method. Write a method that decodes a single instruction. An instruction can contain a method that executes itself.
  • Use switch statements instead of else-if ladders.


  • An instruction interface could look like this:

    Here's an example of an implementation:
    It's probably overkill for this exercise, but you might also create a separate interface for preprocessing instructions:

    'Undo' could implement it like this:

    I would write a InstructionsParser class that has only one job: Convert a sequence of lines into a sequence of instructions:

    Now all that's left to do is iterate over the list of instructions and execute each one on a shared message builder and printer.
     
    Elise Ray
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the help! I am going to keep working on it and keep in mind the suggestions you gave!
     
    We can walk to school together. And we can both read this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic