Starting from:
$35

$29

PROGRAM #1: Solution

This first assignment is different from future assignments in two very important ways:

You will not normally be given Java code and told to copy it.



You are NOT expected to understand all of this code. You will probably figure out what some of this code does as you type it, but do not worry if you do not understand all of it.



Typical assignments in CSE 174 will ask you to figure out code on your own, and will focus on programming concepts that have already been taught.




Outcomes:




Use a contemporary programming language and programming environment



Write, compile, edit, and debug simple Java programs



Format and comment source code that adheres to a given set of formatting guidelines



Use the course website



Scoring:




At a bare minimum, the program you submit must have the assigned source code, and your source code must compile and run without crashing.




If you submit source code, but it does not compile, your score for this assignment will be zero.



If you submit source code that roughly resembles the requirements and it compiles, but it crashes under normal operating conditions (nice input from the user), your score for this assignment will be 5 points.





Full credit
No credit or Partial credit






Enter and run given code
You entered the code as given, and it runs
You entered the code, and it compiles
(10 points)
as expected (user input/output, game,
and runs, but it contains multiple errors.


triangle).








Format and comment
You followed all of the given formatting
You did not follow some or all of the
source code (5 points)
requirements (indentation, comments,
formatting requirements as specified in


upper/lowercase, etc.).
the requirements.






Make program
You successfully made all of the required
You did not make one or more of the
modifications (8 points)
modifications to the assignment.
required modifications to your program.












Part 1: Get your environment set up




If you are working on your own computer… Set up the Dr. Java IDE. The Google Drive contains installation instructions.



If you are working in one of the Benton labs… Dr. Java is already installed



Under Dr. Java's Edit menu, select "Preferences" and make some changes that will make Dr.



Java easier to use…




o Under the "Display Options" section, check the box next to "Show All Line Numbers". o Under the "Miscellaneous" section, set the "Indent Level" to 3.




o Click "OK" to apply these changes and return to the programming environment.







Part 2: Type your source code




The next several pages contain the source code for a Java program. Using Dr. Java, type it in exactly as shown, including indentation, comments, blank lines, upper/lowercase, etc.




Begin by typing this much (Don’t type the line numbers. They will show up automatically.):







Modify this section with




your name, instructor,

section, and date.

Things to notice:

Indentation: If you do not make any mistakes in your typing, then Dr. Java will automatically indent each line as shown above.




Indentation is important because it helps humans (such as you and your peers and instructors) read your code.

Curly braces: Lines 11, 13, 17, 21, and 25 contain opening curly braces, and lines 15, 19, 23, 27, and 29 contain closing curly




braces. Curly braces are important because the Java compiler looks for them to know when a block of code begins and ends. Comments: Beginning a line with a double slash, //, indicates that the text is a comment for humans (the compiler ignores it). Color coding: Dr. Java will automatically color code certain words blue, comments will be green, and unrecognized words




black. This helps humans read your code.




Upper/lowercase: Note that only a few words begin with an uppercase letter (Scanner, MyFirstProgram, and String). Java is case sensitive, meaning that the Java compiler considers Scanner, scanner, SCANNER, and ScAnNeR to be different. (You will see later that choosing uppercase vs. lowercase helps humans as well.)










Get to know the basic "structure" of a Java program:







Basic structure:




One class: Represented by all the code within the yellow rectangle. Notice that the curly brace at the end of line 11 marks the beginning of that block of code, and the curly brace at the end of line 29 ends that block of code.




Four methods: Our class contains four methods, highlighted in orange. The names of these methods are: main, greet, explainGame, and printBorder. Each method has its own set of curly braces.

One of those methods is named main: When you tell the Java Virtual Machine to run a program, the JVM will look for the method named main. Without it, you would get an error message indicating that no main method was found.




In CSE 174, most of the programs we write will




consist of one class with multiple methods.




Make it easier to spot your curly braces:




One technique that programmers often use to make their code easier to read is to put a comment after the closing brace of each class and method to indicate which code has just ended. Modify your code to include the comments shown below:













Explain the purpose of your program:




Before writing the code for a class or any of its methods, write comments to summarize the purpose of that code:




The comments before a class should summarize the purpose of the class as a whole.




The comments before a method should summarize the purpose of that specific method.




Later, we may put comments within a method to explain what part of that method does. For now, add the following comments to your code.










Use the Enter key!




Unlike when using a word processor, you need to press the "Enter" key at the end of each line.

As a bonus: when you press enter at the end of a line, Dr. Java should automatically indent your code correctly.




As a general rule we will look for a way to keep every line of our program at 70 characters or fewer. Notice that Dr. Java indicates the current line number and column in the lower right hand corner . For example, this is what would be displayed if the cursor were on line number 34, column 11:




For now, just match the lines you type to what is shown to the left.







A key to writing programs is to save, compile, and test frequently.

Write a little, save it, compile it, and test it.




Write a little more, save it, compile it, and test it. Write a little more, save it, compile it, and test it.




So…save your work and compile it. If you get an error message…

Read the message.




Note the line number where the error occurred (often, the actual mistake comes before that




line number).




Try to figure out how to fix the problem on your own. If you are stuck, copy the error message, go to the discussions at the course website and paste the exact error message along with any other pertinent information, and see if anyone has a suggestion.










Don’t move on until the above compiles correctly. You may also run your compiled code at this point, but won’t see anything happen yet (because the main method contains no code).




Write the code for the greet() method:




Put your cursor inside the greet method, and type the yellow highlighted code below. We call this the body of the greet method. Notice that the non-highlighted code is what you've already typed. Notice also that println stands for "print line". The character between the t and the n is a lowercase letter l.







Modify the code so that your name is displayed, instead of Mary Smith's name.

Save and compile your code, fixing any errors.

Run your code, but you still won’t see anything happen yet.







Why does nothing happen when you run your code?




The reason is that when you run a program, the Java Runtime Environment runs the code that you've written in the body of the main() method. Right now, you have a main() method, but no code in the body of that method. Once you start putting code in the body of the main() method, then you will find that your program actually does something when you click "run".













Fixing indentation:




Dr. Java will automatically handle indentation for you as long as you press "enter" at the end of each line of text that you type. If at any time, you notice that your indentation looks incorrect, use the following Dr. Java indentation shortcut to fix it:




Select all your text. (Ctrl-A in Windows, Command-A on a Mac)
Press the "tab" key.



If Dr. Java is still not indenting correctly, there is a good chance that you've made an error in your code (such as missing parentheses, misspelling a Java command, or something similar).




Type the highlighted code for the body of the explainGame() method:










Save and compile your code, fixing any errors.




Type the highlighted code for the body of the printBorder() method:













Save and compile your code, fixing any errors. Note that one of the lines above contains a print() statement, and the other contains a println() statement. What’s the difference? When you print with println(), the cursor moves to the next line after it is done printing. When you print with print(), the cursor stays on the same line when it is done printing.




Looking back:




Recall that this class contains 4 methods. So far, you have written code for three of those methods. Your program still does nothing when it is "run", because it lacks any code in the method named main(). That's what you still need to do: type the body of the main() method. In doing so, you will be making use of all of your other methods. Pay attention and you should notice that you will eventually "call on" the greet(), explainGame() and printBorder() methods as you type the code for the main() method.







Next, write the body of the main() method.




Put your cursor inside the body of the main() method. Since this is a longer method, you should




frequently compile and run your code. Throughout the code, you will see several stars to indicate that it would be a good idea to save, compile, and run. Don’t move on until the program compiles and runs correctly at each star. Note that the first three lines below were already typed in a previous step.







Begin typing here:







(At this point, if you run your program, it should prompt the user for her first and last name, display a greeting, introduce the game, and ask for her first guess.)




Continued on the next page…







Don't type this line…it's the end of the main()

method, which you've typed already.
















CSE 174 - Program 1 Page 8Here's what a sample run of the program should look like if you run it. (Notice that Mary Smith is a very lucky guesser.)







Part 3: Submit the current version of your program




Is your program running as expected? If so, then now is a good time to submit your current working version, even though you will still be making changes. This is a very important step because soon you will be making several changes to your program. It is good to upload a version you know is working before you make those changes.




So, prior to making any of the modifications below, go to program1 on the course website and submit your source code for this assignment. When you do, the website automatically changes the name of the file. Don’t worry about that. It’s fine. Do not change the name of your file on your computer. It should be FirstProgram.java at all times, even when you make changes below.
















CSE 174 - Program 1 Page 9

Part 4: Modify the program




Now it is time to "play around" with your program. Note that you do not need to do any external "research" to solve the following (except perhaps for modification #4). Instead, look carefully at the program you already typed, and learn some techniques from the code you already typed.




Once the program is working correctly, make all four of these modifications:




Even though the program prompts the user for her first and last name, it only displays her first name in the introduction. Don't change the part of the program that asks the user for her name, but fix the program so that the greeting displays the user's first and last name, separated with a space, rather than just the first name. For example, “Hello Mary Smith,” rather than "Hello Mary,"). The game introduction should still only display the user's first name (so, it should still say, for example, "Let's play a game, Mary…".



The mystery number should be a random number from 1 to 100, rather than 1 to 50, and in order to "win" the game, the number must be guessed in fewer than 7 guesses (rather than 6). Modify any comments in your code to reflect this change.



Modify the part of the program that prints the triangle by choosing one of the following
options. Either:




Ask the user how many rows should be in the triangle, and display that many rows. For example, if the user says 7, then the triangle would display 7 rows of dollar signs, beginning




with 1, then 2, then 3, then 4, then 5, then 6, then 7. OR…




Make the number of rows in the triangle be a random number between 1 and 30. Random does NOT mean that the programmer should pick her favorite number and put it in the program. Rather, "random" means that each time your program is run, the computer will generate a different, unpredictable number of rows in the triangle, with as few as one row, or as many as thirty rows.







Instead of displaying the dollar symbol ($) in the triangle of money, display the cents symbol (¢). You could search the internet for how to display this symbol. But there are other ways.






What if something goes wrong with your file?




Whenever you upload your work to the course website, you are creating your own personal "backup copy" of your work. If something should go wrong as you make your modifications, remember that you can go back to the course website, locate the file you submitted, and download it to your computer.







Part 5: Submit your modified source code




Once you have made the required modifications to your code, and you find that your code works as expected, it's time to re-upload your source code. On the course website, go to program1 and upload your updated source code file.
















CSE 174 - Program 1 Page 10

More products