Type: Individual Assignment

Due: by 9:30 PM on Tuesday, September 18, 2018

Goals

The goal of this assignment is to help you some practice writing your first Java programs. In particular, you will get more practice with retrieving and using input from a user, loops, printing output to the screen, and using some of the provided Java classes (Scanner, StringBuilder, StringJoiner). Getting comfortable with the basics of Java will set you up well for future assignments in this class.

Setup

There are no outside files that you will need for this assignment. However, this is a good time to get yourself setup for future assignments.

Choosing an Editor

One big part of that will be deciding what editor or program you will use when writing code. There are many good options to choose from. In CS 111 you likely used either Brackets or TextWrangler (both of which are installed on the lab machines). Either of those are good options to continue using this course. To see all the editors installed on the lab machines, go to the /Applications/CarletonApps/ directory. If you want to try something different, here are a few more options.

  • Emacs. This is an incredibly powerful editor that has been around forever. Beware, there is some amount of start-up cost when you first start using Emacs, but here is a tour that might be helpful. Like Vim or Vi, you can usually rely on it being installed on most UNIX machines (just type emacs into a terminal prompt).
  • Aquamacs. This is a Mac specific version of Emacs that is also installed on the lab machines.
  • Vim or Vi. This is another incredibly powerful editor that has been around forever (though it also has quite a start-up cost to get through). It has the advantage that essentially any UNIX system (Mac or Linux) always has it installed, and it is very lightweight and starts up quickly. You can start it in a terminal window in any Mac or Linux system by typing vim or vi at a terminal prompt.

A different type of option is using what’s called an integrated development environment (IDE) which is a program that is designed to help you code. IDEs often make it easier to organize large projects and have some features to make coding easier (those features vary based on the IDE, but may include a debugger to step through your code line by line, code completion to help you remember the names of methods, or compiling in the background and showing you the line an error occurred on). However, they can also add extra overhead in terms of having to learn how to use the IDE and learn how to use Java. For that reason, I’m not going to talk about IDEs in class. If you take more CS courses such as software design, you’ll learn lots about IDEs. If you want to explore IDEs on your own, you’re welcome to. There are a number of options out there, but I would recommend IntelliJ as a particularly strong one.

Installing Java

Java is installed in the computer labs on campus (both in the CMC and elsewhere). But if you have your own machine and would like to use it for this class, you are welcome to install Java on your own computer as well using these instructions on the CS department website. Note that the only officially supported environment is the lab computers, but Mike Tie (mtie@carleton.edu, CMC 305) can help you troubleshoot if you run into problems installing Java on your home computer; I’m also happy to try to help you during office hours if you run into problems.

Your Assignment

This a solo assignment, and you should not work with a partner. The reason for this is to make sure that everyone feels comfortable writing a short program on their own. All of your code files should include your name at the top (in comments). You can ask questions on Piazza or talk to me, the lab assistants, or the prefect if you’re having trouble. The appendix of your book is also very helpful for Java syntax. The Javadocs for Java are incredibly useful as a reference and are linked on the Resources page.

For this assignment you’ll be creating a small Welcome to Data Structures App. You should put all code for this assignment in a file called Welcome.java. All of the functionality of your App will be contained in the main method. In particular, your program should complete the following tasks in the specified order:

a. Print to the screen “Welcome to CS 201: Data Structures!”

b. Ask the user for their name and use the Scanner class to retrieve the user input.

c. Ask the user to enter an integer (…, -2, -1, 0, 1, 2, …) and use the Scanner class to retrieve the user input.

d. Print to the screen “Welcome NAME” where NAME is the name the user typed in.

e. Print to the screen “Your name backwards is BACKWARDS” where BACKWARDS is the name the user typed in displayed backwards. You may find the StringBuilder class from the reading useful here.

f. If the the number n entered by the user was positive, print off a triangle of numbers like the following example where n=3. (I suggest using the StringJoiner class from the reading.)

1
2,2
3,3,3

g. Otherwise, if the the number n entered by the user was negative, print off a triangle of numbers like the following example where n=-4. (I suggest using the StringJoiner class from the reading.)

-1
-2,-2
-3,-3,-3
-4,-4,-4,-4

h. Otherwise, the number was 0, and just print “Cannot print a triangle of height 0.”

For example, here are a few possible runs of the program and what the output might look like:

$ javac Welcome.java
$ java Welcome
Welcome to CS 201: Data Structures!
What is your name: Yoda
Enter an integer: 5

Welcome Yoda
Your name backwards is adoY
1
2,2
3,3,3
4,4,4,4
5,5,5,5,5
$ javac Welcome.java
$ java Welcome
Welcome to CS 201: Data Structures!
What is your name: Schiller
Enter an integer: 0

Welcome Schiller
Your name backwards is rellihcS
Cannot print a triangle of height 0

Last Required Challenge: Modify your code so that if a user does not enter a valid integer, the program prints out the warning “You must enter an integer” and then quits.

Optional Challenge: Modify your code so that instead of quitting when a user does not enter an integer when prompted, the program just keeps re-prompting them to enter an integer until valid input is obtained.

Final Hints

  • Make sure to fully test your code. Try out positive and negative numbers as well as integers and real numbers. The documentation for the Scanner class may be very helpful to look at as you try to figure out how to do all of this.
  • Look back at examples from class or your book if you get stuck. It’s totally okay to experiment and try out methods to see what will happen—that is part of the learning process.
  • Make sure to save your work regularly as you go along!
  • If you get stuck, don’t hesitate to ask for help from a lab assistant, prefect, or me!

How to Submit

To submit your assignment, create a directory named assignment2 in your Hand-in directory and place your file Welcome.java within it. The directory structure relative to your course directory on the lab Macs should be Hand-in/USERNAME/assignment2/Welcome.java.

Grading

Below is a partial list of the things that we’ll look for when evaluating your work.

  • Does the program compile? It is essential that you check whether your submitted work compiles. If you submit a program that doesn’t compile, you will automatically receive at least a 25% deduction for the assignment, even if the problem was relatively minor.
  • Does the program run correctly on test input?
  • Does the program use Scanner, StringJoiner, StringBuilder correctly?
  • Does the program handle positive/negative numbers and non-integer values correctly?
  • Are comments used throughout the program to describe logic decisions?
  • Is the program named Welcome.java? Please make sure to always use any names specified in an assignment.
  • Does the program use Javadoc style comments at the top that include your name as the author of the code?

Start early, ask lots of questions, and have fun! Your instructor, the lab assistants, and the prefect are all here to help you succeed. Don’t hesitate to ask for help if you’re struggling!

Acknowledgments
This assignment was originally written by Eric Alexander. Later modifications were added by Titus Klinge.