Quick Java Guide
We will use Java to do a few programming tasks. In the classroom I suggest you use Netbeans as Java IDE.
Alternatively you can use this
online Java IDE
About Java:
- The name of the file needs to match the name of the class.
- Structures start with { and end with }
- Most lines need to end in a semicolon
Java input/output example
Java IDEs
NetBeans
Netbeans is on the Classroom computers. You can
install NetBeans at home
You will need to
create a project in Netbeans for your files to live in. You can have multiple projects, but multiple
.java files for different programs can live in the same project; each .java file
in a project has to have a unique name. Files in your project are listed on the left,
you can double click to open them in the editor on the right and right click them or their code in the
editor to Run File; output or errors will be shown at the bottom.
- You can add new files using File->New, making sure that the name of the file is the same as the name of the class
you want to use. For instance, if the name of the class is MainA, the file should be MainA.java
- You can paste my example code into a blank .java file you created in the editor and then start editing it.
- At top the button showing two floppys is the Save All button. If you run your code, it will save automatically
- Right-click your java file at left or your code in the editor to choose Run File each time you want it to run your
code to test changes. (Just "Run" runs whichever Netbeans has set as your "default" java file.)
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off under
Tools->Options->Editor->Code Completion, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
isn't detected til a later line.
Online IDE
When you cannot easily access Netbeans, here is an online Java IDE
-- an editor that also lets you run code right in the browser. One side of the page is an editor, at the top there
is a Run button, and the other side shows the output (or errors).
You can edit the code in the editor and run it as many times as you want. This does unfortunately have ads,
some of which might be misleading. You do not need to download any extra tools to use this page, but you do need to be
careful to save your files, it will not automatically do this for you!!
- The editor page starts off with example code in it. At the top of the editor is a "+" button. Click this to create a
new file and change the name so the name of the file is the same as as the name of the class with .java at the end.
For instance, if the name of the class is MainA, the file should be MainA.java
- You can paste my example code into a blank .java file you created in the editor and then start editing it.
You can also use the open button to open an existing .java file from your computer.
- At top under the "+New" dropdown is a Save option which lets you download a copy of the file.
Save often and don't lose your work! It WILL NOT SAVE your code if you close the browser, so you need to download your work!!!
- You have to click Run again each time you want it to run your code to test changes.
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off by right-clicking the code,
choosing Editor Options, and and unchecking Autocomplete at the bottom of the list, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
isn't detected til a later line.
About this assignment:
- The editor page starts off with example code in it. At the top of the editor is a + button. Click this to create a
new file and change the name so the name of the file is the same as as the name of the class with .java at the end.
For instance, for the first part the class is MainA, so the file should be MainA.java
- Now paste my example code into the editor and you can start editing it. You can also use the open button to open an existing .java file from your computer.
- Make a new file for each part (don't forget to save and download!)
- Attach all your .java files to a single submission in blackboard, not multiple submissions.
Part A
The following code in Java creates two variables, prints them, and then creates a third variable.
It should then swap the values of the first two variables, using the third as temporary space,
and then print the two variables again, but it is not complete. Edit this code and replace the 111, 222, and 333 in this code correctly so that the
values are swapped.
// This code sets things up for a general java program with input
import java.util.Scanner;
public class MainA
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// done with setup, main code starts here
// declare variables
// pay attention to the semicolons;
int firstNum;
int secondNum;
// print and then read in from the user
System.out.println("what is the first number?");
firstNum = scan.nextInt(); // reading in - note specific code for reading in an int
// print and then read in from the user
System.out.println("what is the second number?");
secondNum = scan.nextInt();
// print before swap
System.out.println("first is " + firstNum);
System.out.println("second is " + secondNum);
// extra variable
int temp;
// FIX THIS TO SWAP
temp = 111;
firstNum = 222;
secondNum = 333;
// print after swap
System.out.println("first is now " + firstNum);
System.out.println("second is now " + secondNum);
// main code ends (still need the below two lines!!)
}
}
Part B
The following example code shows some more variable types in java. Using this example as a model, write new
java code that:
- gets from the user what type of animal they are buying
- gets from the user the price per animal
- gets from the user how many of the animal
- prints for the user a nice summary of what they are buying, with the total cost, assuming 15% tax.
// This code sets things up for a general java program with input
import java.util.Scanner;
public class MainB
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// done with setup, main code starts here
// print and then read in a double from the user
System.out.println("what is a?");
double a = scan.nextDouble(); // example of declaring and reading in on same line - note specific code for double
// print and then read in from the user
System.out.println("what is b?");
double b = scan.nextDouble();
// print and then read in a String from the user
System.out.println("What is your name?");
// the type String is capitalized in this language
String userName = scan.next(); // note specific code for String
// this is overkill, but shows you how to do a constant in Java using keyword final
final double NUMCOUNT = 2.0;
// do math and print the result
double average = (a + b) / NUMCOUNT;
System.out.println(userName + " your average is " + average);
// main code ends (still need the below two lines!!)
}
}