Starting from:

$30

Basic "structure" of a Java program


This first assignment is different from future assignments in two very important ways:
1.    You will not normally be given Java code and told to copy it.  
2.    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 CSE174 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 (10 points)     You entered the code as given, and it runs as expected (user input/output, game, triangle).     You entered the code, and it compiles and runs, but it contains multiple errors. 
Format and comment source code (6 points)     You followed all of the given formatting requirements (indentation, comments, upper/lowercase, etc.).     You did not follow some or all of the formatting requirements as specified in the requirements. 
Make program 
modifications (9 points)     You successfully made all of the required modifications to the assignment.     You did not make one or more of the 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.  
•    If you are working in one of the Benton computer 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.):

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


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.
 

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 green 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 John 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:
1. Select all your text.  (Ctrl-A in Windows, Command-A on a Mac)
2. 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.
 

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

Continued on the next page…



 


Here's what a sample run of the program should look like if you run it.
 
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.

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. 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: 
1.    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…".

2.    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 10 guesses (rather than 5).  Modify any comments in your code to reflect this change.

3.    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.

4.    Instead of displaying the dollar symbol ($) in the triangle of money, display the character associated with the ASCII code 64. You could search the Internet for how to convert a numerical value into an ASCII character.
 
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 (.java file).

More products