Chapter 5. Extra-Strength Methods: Writing A Program
Letâs put some muscle in our methods. We dabbled with variables, played with a few objects, and wrote a little code. But we were weak. We need more tools. Like operators. We need more operators so we can do something a little more interesting than, say, bark. And loops. We need loops, but whatâs with the wimpy while loops? We need for loops if weâre really serious. Might be useful to generate random numbers. Better learn that too. And why donât we learn it all by building something real, to see what itâs like to write (and test) a program from scratch. Maybe a game, like Battleships. Thatâs a heavy-lifting task, so itâll take two chapters to finish. Weâll build a simple version in this chapter and then build a more powerful deluxe version in Chapter 6, Using the Java Library.
Letâs build a Battleship-style game: âSink a Startupâ
Itâs you against the computer, but unlike the real Battleship game, in this one you donât place any ships of your own. Instead, your job is to sink the computerâs ships in the fewest number of guesses.
Oh, and we arenât sinking ships. Weâre killing ill-advised, Silicon Valley Startups (thus establishing business relevancy so you can expense the cost of this book).
Goal: Sink all of the computerâs Startups in the fewest number of guesses. Youâre given a rating or ...
Get Head First Java, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.