Introduction to Java using NetBeans

Section 1 : An Introduction to NetBeans and Java

 

 

What is Java?

JAVA is a high-level programming language developed by Sun Microsystems. Java was originally called OAK, designed for handheld devices, but was unsuccessful. In 1995 Sun changed the name to Java and modified the language to take advantage of the burgeoning World Wide Web.

Java is an object-oriented programming language. Object-oriented programming is a way of writing programs with the common theme that the program is a collection of objects that work together to perform the task at hand. Each object is a collection of data and methods that manipulate the data to perform required tasks. You will learn more about object-oriented programming as the course progresses.

 


 

Hello World

Open a New Project

Name the Project and Set the Location to your K:Drive

Java Comment Statements

1. Multiple line comments

Example Icon Example

/*
* This is an example of a multiple line comment
* that shows information over many lines. It begins with a
* slash and an asterisk and ends with an asterisk and a slash
*/

2. Single line comments

// This comment type is used for short one line comments

 

 

Using System.out.println

System.out.println ("Hello World"); is responsible for outputting words to the screen.

The function: System.out.println( );

The function System.out.println( ); takes letters (strings) or values (numbers) as a parameter and displays it on the screen (anything inside of the ( ) is displayed). You can display more than one string or numeric by joining them together using the "+" operator.

Example Icon Example

System.out.println("Hello " + "how are you today");

This code will print out the phrase "Hello how are you today" on the screen by joining the two strings together into one line of output.

System.out.println("Your age is " + 21);

This code will print out the phrase "Your age is 21" on the screen by joining the string and the numeric together into one line of output.

Printing special characters using the System.out.println(); function:

Certain ASCII characters that are difficult to represent in a string either because they are not viewable (like an enter key) or because they are part of the formatting of strings in Java (like a " or a ' or a \ ) may be displayed using an ASCII character code or control code.

Shown below is a chart listing of some common control codes:

Control Code What it means
\n new line
\r return character
\t tab
\b backspace
\" double quote
\' single quote
\\ backslash

Example Icon Example

If you wanted to print the phrase Go "SENS" Go (quotes around SENS) on the screen, you would use the code:

System.out.println("Go \" SENS \" Go");

If you wanted to print GO on one line, SENS on another line and Go on a third line you could use the code:

System.out.println("Go \n SENS \n Go");

resulting in screen output of:

Go
SENS
Go


Using Tabs (note: print and println)


String Formating

Note: %n moves to new line

Note: %s is for strings, you can also use %d and %f for numbers)

Number Formating


System.out.printf("%d %n",
461012); // --> "461012"
System.out.printf("%08d %n",
461012); // --> "00461012"
System.out.printf("%+8d %n",
461012); // --> " +461012"
System.out.printf("%,8d %n",
461012); // --> " 461,012"


double pi = Math.PI;

System.out.printf("%f %n ", pi); // --> "3.141593"
System.out.printf("%.3f %n", pi); // --> "3.142"
System.out.printf("%10.3 f%n", pi); // --> " 3.142"

 

 

 

 

Assignments

Assignment 1.1  BingoCardProgram

Create a BingoCard application that displays a traditional bingo card with five columns of five unique numbers.  The column labels are B, I, N, G, O.  Column b contains numbers from 1 to 15, column I 16 to 30, column N 31 to 45, column G 46 to 60 and column O 61 through 75.

      B                  I                      N                     G                     O

      2                20                    42                    60                    64

    14                25                    32                    55                    70

      5                18               FREE                    53                    67

    12                16                    31                    46                    75

    10                22                    39                    59                    71

Assignment 1.2  RectangleProgram

            Create a Rectangle application that displays a rectangle of asterisks (*).  The rectangle should be 15  *  wide and 7 *  high. You must use printf and not simple type a bunch of spaces.

***************
*                          *
*                          *
*                          *
*                          *
*                          *
***************

Assignment 1.3  TicTacToeProgram

            Create an application that displays a tic-tac-toe board with an X in the center.

Assignment 1.4   SmileProgram 

            Create an application that displays a smiling face made of keyboard characters. You must have a circular outline and at least eyes, nose, mouth.

Assignment 1.5  GioPizzaProgram 

            Create an application that displays the menu shown below.  (Note: You will be expanding on this assignment in a later section)

Gio's Pizza
-------------------------------------------
                   (SML)  (MED)  (LRG)
Cheese             5.00    7.50   10.00
Pepperoni          5.75    8.63   11.50
Combination        6.50    9.75   13.00
Meat Lovers        8.00   12.00   16.00

 

Posting your work

1. Upload the Java Projects to your Gravelle Folder (Share with me only) and put a link for each assignment on your website.

2. Upload your code to your Gravelle Folder and embed on your site

3. Take a screenshot of your console output and embed on your site

JAR files which are executable java files do not work on projects without a display so we will not use them in this assignment.