Skip to content

Instantly share code, notes, and snippets.

@williamhaley
Created December 16, 2020 20:04
Show Gist options
  • Save williamhaley/5679e20dacaf7e3afba45424faa5b4d6 to your computer and use it in GitHub Desktop.
Save williamhaley/5679e20dacaf7e3afba45424faa5b4d6 to your computer and use it in GitHub Desktop.
NCP Java Demo (Logic)

NCP Java Install Instructions

Install Java on your computer. Your computer must be a Windows, macOS, or Linux computer. Not a Chromebook. You may also need to reboot.

Open the terminal program on your computer. On macOS it is called "Terminal". On Windows it is called "Command Prompt". Run the java command. Then run the javac command. If you see a message like "command not found", then Java is not properly installed.

Click the Download Zip button in the upper right hand corner of this page. Once the download is complete copy all the Java files to your Desktop.

Basic Logic Example

Open your computer's terminal program and use the cd command to change directory to your Desktop.

Windows

cd \Users\%username%\OneDrive\Desktop

or

cd \Users\%username%\Desktop

macOS and Linux

cd $HOME/Desktop

Compile the program

javac Main.java

Run the program

java Main
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
class Main {
public static void main(String[] args) {
Demo demo = new Demo();
demo.doTheDemo();
}
}
class Demo {
public Demo() {
}
public void doTheDemo() {
// Loop while we add to a number.
int count = 0;
while (count < 10) {
System.out.println("the count is: " + count);
count++; // count += 1 or count = count + 1
}
// Create a String variable.
String name = "John";
// See what the variable is.
if (name.equals("Bob")) {
System.out.println("the name is Bob");
} else {
System.out.println("the name is NOT Bob");
}
// Create an int variable.
int x = 135;
// If x is equal to 135.
if (x == 135) {
System.out.println("x is equal to 135");
}
this.myMethod("my parameter"); // or method("my parameter");
List<String> listOfNames = new ArrayList<String>();
listOfNames.add("Bob");
listOfNames.add("Margaret");
listOfNames.add("Alice");
System.out.println("loop 1");
for (String listedName : listOfNames) {
System.out.println("Name is: " + listedName);
}
System.out.println("loop 2");
for (int index = 0; index < listOfNames.size(); index++) {
System.out.printf("Name is: %s\n", listOfNames.get(index));
}
System.out.println("loop 3");
Iterator<String> listIterator = listOfNames.iterator();
while (listIterator.hasNext()) {
System.out.printf("Name is: %s\n", listIterator.next());
}
Map<String, String> mapOfData = new HashMap<String, String>();
mapOfData.put("will", "some data");
mapOfData.put("alice", "some different data");
}
public void myMethod(String parameter) {
System.out.println("the parameter is: " + parameter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment