Getting Started with Java

Table of Contents

Note that in the evenings in CMC 102 and in CMC 306, we have lab assistants on duty who are happy to help. If you're having trouble getting started, feel free to pop in to get help. There should be a schedule on the wall in or near the lab indicating what hours lab assistants are working. Of course, you're also welcome to see me in office hours, as well as attend prefect sessions.

Start up jGRASP

On the department computers, jGRASP can be found in the Applications folder; start it up.

A first Java program

Look at the program Hello.java. Save this code into a directory within your account, then open the file up in jGRASP by selecting File, Open from the menu in jGRASP. Look at each line of the program, and think about what each word and symbol might mean.

Before we can run your program, we need to compile it (translate it to a language the computer can understand). To compile it, go to the Build menu in jGRASP, and choose Compile. Hopefully, the program should compile without returning any error messages. If you do get errors, check to see if you got the code correctly from the website, or ask for help.

Once your program is compiled, you can run it by going to the Build menu in jGRASP and selecting Run. Do so. If all goes well, your program should print out "Hello, world!" in the Run I/O tab.

Here's a shortcut: you don't really need to select Compile before you select Run; jGRASP will automatically compile first if you just Run it every time.

EXERCISE: Modify the program so that it prints out "Welcome to Data Structures!" You don't need to turn in the results of doing this, but you should achieve this before going on.

CodeStepByStep

Overview

Throughout this course, we'll be using jGRASP to do the bulk of our Java coding, which is why I wanted you to get started with it right away. For the short term, though, we'll be using CodeStepByStep to do a series of short targeted exercises in Java. Visit CodeStepByStep, and log in using the account you created in the last assignment.

I'll be asking you to do a series of exercises in CodeStepByStep. First, here are some notes and caveats.

CodeStepByStep has a button associated with each problem labeled "Show Solution(s)." You should only use this button as a last resort. In particular, CodeStepByStep will not give you credit for solving the problem if you do show the solution. Use it sparingly.

It may very well be possible to find solutions to these problems elsewhere online. You may not find solutions to these problems on other websites and copy them in, even if you cite them. If you're stuck, ask me, a prefect, or a lab assistant for help, or post a question on the course Q&A Forum. All of these places will help you get unstuck without handing you the entire solution, so you can continue to learn by working your way through to the answer.

You should solve these problems individually. You may talk to each other about ideas if you want help, but the code that you ultimately write should be your own. Again, please ask for help if you need it! You may use resources to help you when you're stuck, but these problems should not be viewed as pair-programming problems. I want to make sure that everyone in the course has a solid baseline knowledge of Java before we get too far in.

Some CodeStepByStep problems make use of some special libraries that we're not going to be using in this class. I've chosen problems from CodeStepByStep to avoid these, but you may occasionally see them if you attempt problems I haven't assigned, which is great if you do. Some CodeStepByStep problems make use of a class called ConsoleProgram with a method called run instead of main. Ignore that if you see it. Also, CodeStepByStep has a convenient shortcut that we won't have in jGRASP, which is that instead of typing System.out.println, you can type println. You'll see that occasionally in CodeStepByStep problems. You can use either form of println in CodeStepByStep, but my recommendation is to get in the habit of typing System.out.println since that's what you'll need to do for standard Java and for jGRASP.

Problems to work online

Work on the following problems. If you have connected your CodeStepByStep account to this course, I will automatically receive a report indicating all of the problems that you have gotten credit for.

HelloWorld. Note that the note below the solution box says you should submit it with a run method. Ignore that and use a main method as we will typically be doing. That still works in CodeStepByStep: I tried it.

expressionsNumbers1

expressionsStrings1

percentageGrade

starsPrint

numberLoops1

numberLoops2

numberLoops3

computeSumOfDigits One detail to note is that the program expects you to prompt the user with the string Type an integer: before prompting for the integer, and it also expects you to preface your output with the string Digit sum is. If you don't get this exactly right, CodeStepByStep will indicate that you've done the problem incorrectly. CodeStepByStep does a good job of showing you your output and the expected output, so if you get it wrong you can see what doesn't match. The problem just forgets to specify in the actual instructions that you're supposed to use these strings. You might find this program useful as a reference, which shows how to get input from a user.