Company of Heroes: Eastern Front

Author Topic: Java Help  (Read 3603 times)

Offline neosdark

  • Donor
  • Major
  • *
  • Posts: 805
    • View Profile
Java Help
« on: July 09, 2012, 03:40:14 PM »
Hey dudes, in case some of you didn't know I'm currently at a summer program at Cornell University in Upstate New York. Everything here is nice and dandy, but I need a hell ton of help in Java (which to be fair I'm considering dropping because I find it to ridiculously hard for me, I'm not too much a computer language guy). I've been having massive problems with this and its really getting to me. If any of you can find it in your heart to help me I would be most grateful.

This is the question I'm working on (Which is due tonight btw)

q1. Write a Java program to read in a file of text. The text in the file will comprise multiple lines, and each line will have a pair of numbers and an operation (eg "twentyone plus fourteen"). Your program should output the answers in the format "21 + 14 = 35", thus converting the text to numerical values and interpreting the textual operator as an arithmetic operation. You should not store all possible textual versions of numbers (we will only work with input text for numbers between 0 and 999), but should write code which can parse expressions like "seventythree" into "seventy" and "three" (we will make large numbers into single words, not separated by spaces, as illustrated here, though may write 347 as either "threehundredandfortyseven" or "threhundredfortyseven").
 The only 'operators' will be "plus", "minus", "times", "multipliedby", "dividedby", "remainderondividingby", and "modulo" (which means the same as remainder on dividing by). Your program should print the answers to the screen and to a file called my_output.txt. Some comments on style: you should not put everything into the main method, and should construct such other classes as help to make the program more cleanly organised.

I've tried multiple ways of doing this and nothing works out for me
« Last Edit: July 09, 2012, 03:47:27 PM by neosdark »

Offline Rikard Blixt

  • Administrator
  • Mr. Spam
  • *
  • Posts: 1930
  • 'The Fixer'
    • View Profile
    • Eastern Front
Re: Java Help
« Reply #1 on: July 09, 2012, 06:25:30 PM »
I'll give you some helpful hints at least on how I would solve it.

0. Read file (scanner class)
1. For each line (scanner class)
   2. Split up on spaces ( str.split(" "); )
   2a. [0 ] = 1st number, [1] = operator, [2] = 2nd number
   3. Detect if first part [0 ] is {one, two, three, four ... nine} or hundred (for eg. "hundredfortyseven")
      4. if yes: it's >= 100 or < 10, detect if first or second part of the word is "hundred"
         5. if yes: it's >= 100 -> remove "and" if it's any of the next coming letters and "jump" to step  7.
         6. if no it's < 10, then it's just the [0 ] that is the result
      7: if no: then it's >= 10 or < 100. Take the first part and parse it to numbers, then "jump" to step 6 for the last part.
   8. repeat 3-7 for [2]
   9. check which operator ( [1] ) with simple if statment and then do the operation it says on the two numbers.
   10. print!


With jumps I mean have a separate function that does it obviously, not a "goto"...

I would recommend using a HashMap of <String, Integer> for the value storage. So the setup looks like this:

HashMap<String, Integer> numbers = new HashMap<String, Integer>();
     numbers.put("one", 1);
     numbers.put("two", 2);
     numbers.put("three", 3);
     ... 9
     numbers.put("ten", 10);
     numbers.put("twenty", 20);
     ... 100 (with steps of 10)

accessing would be eg.
numbers.get("one") 
would return an int of 1.
« Last Edit: July 09, 2012, 08:28:06 PM by Rizz »
Valar Morghulis
Quote
[CoH:EF-Dev]Walki: BULLSHIT
[CoH:EF-Dev]Walki: I'll eat a tree if this won't work now
[CoH:EF-Dev]Rizz: ok
[CoH:EF-Dev]Rizz: I'll hold you to it
<10 minutes later>
[CoH:EF-Dev]Walki: I think I really gotta eat a tree...
[CoH:EF-Dev]Walki: got a decent one?

Offline neosdark

  • Donor
  • Major
  • *
  • Posts: 805
    • View Profile
Re: Java Help
« Reply #2 on: July 10, 2012, 02:31:58 AM »
Thanks Rizz, some of this stuff helped out a lot. The only thing I don't recall how to do is print to a text file. Last thing I have to get done "WOOT"

Offline Walentin 'Walki' L.

  • Developer
  • Poster of the Soviet Union
  • *
  • Posts: 3262
  • Creator of Things
    • View Profile
    • Steam Profile
Re: Java Help
« Reply #3 on: July 10, 2012, 08:53:58 AM »
I even would try to put the function in different classes & files to practice OOP it's always good to learn as soon as possible. It may sound ridiclous for so a small program but it will help you a lot in the future.
I identify as a four-eyed bird man. /s

Offline jojorabbit

  • Developer
  • Commissar
  • *
  • Posts: 481
    • View Profile
Re: Java Help
« Reply #4 on: July 10, 2012, 09:20:30 AM »
Thanks Rizz, some of this stuff helped out a lot. The only thing I don't recall how to do is print to a text file. Last thing I have to get done "WOOT"

http://www.roseindia.net/java/beginners/WriteTextFileExample.shtml

Offline neosdark

  • Donor
  • Major
  • *
  • Posts: 805
    • View Profile
Re: Java Help
« Reply #5 on: July 11, 2012, 06:03:13 PM »
So umm round 2 here guys, we have to do the following:
The point of this question is to get you working on designing your own classes. This problem is fairly open-ended, so budget your time carefully to ensure that you don't spent so long on any one aspect that you have too little time to get a reasonably functional solution! The exercise is to write a Java program which can take as input a text file. This text file will actually be a bibliography from a paper or dissertation that was created according to one of the standard bibliographic formats. (If you're interested, it will be in the Oxford University Press, Harvard, APA style -- you can google that!) Your task is to read in the file and create from it a collection of Bib_Items, where a Bib_Item is a class that you'll have to build to hold titles, authors, editors, journals, publishers, pages, dates, etc.. You'll also want to build a Bib class to hold Bib_Items. The Bib class at least should also have methods to input Bib_Items and to output selected Bib_Items, eg just books, or just journal articles, or just works with a particular author, or just works between two dates (the relevant date field for this will only have the year). You should have some exception-handling in cases where you're unable to make rational decisions about how to input a particular bib entry. The text file will have blank lines (either empty, or only containing spaces and/or new line characters) separating each bib item. I will describe the details further in class. Understand also that this exercise will be enhance in later homeworks, so in order that you can save time later by being able to build on what you write now, you should think carefully about how you might design this program before you start coding.

Any hints on how we (speaking for a couple of Joes in this class here cause we are quite scared by this) would go around doing this?

Offline neosdark

  • Donor
  • Major
  • *
  • Posts: 805
    • View Profile
Re: Java Help
« Reply #6 on: August 02, 2012, 04:30:02 AM »
Hey guys, on my last project here and the idea I came up with was to create a Minesweeper Solver. I could make a Minesweeper easy enough, but I'm not even sure how to start a Solver program. Any hints on where to start?