Java Jupyter Tests
System.out.println("Hello");
System.out.println("Test");
System.out.println("Test Again");
// java style to import library
import java.util.Scanner;
// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
public class ScanPrimitives {
public static void main(String[] args) {
Scanner input;
// primitive int
input = new Scanner(System.in);
System.out.print("Enter an integer: ");
try {
int sampleInputInt = input.nextInt();
System.out.println(sampleInputInt);
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
// primitive double
input = new Scanner(System.in);
System.out.print("Enter a double: ");
try {
double sampleInputDouble = input.nextDouble();
System.out.println(sampleInputDouble);
} catch (Exception e) { // if not a number
System.out.println("Not an double (form like 9.99), " + e);
}
input.close();
// primitive boolean
input = new Scanner(System.in);
System.out.print("Enter a boolean: ");
try {
boolean sampleInputBoolean = input.nextBoolean();
System.out.println(sampleInputBoolean);
} catch (Exception e) { // if not true or false
System.out.println("Not an boolean (true or false), " + e);
}
input.close();
// wrapper class String
input = new Scanner(System.in);
System.out.print("Enter a String: ");
try {
String sampleInputString = input.nextLine();
System.out.println(sampleInputString);
} catch (Exception e) { // this may never happen
System.out.println("Not an String, " + e);
}
input.close();
}
}
ScanPrimitives.main(null);
Output Primitive Data
The second key to to all programming is Output. All programming has means to format and combine data. In these examples you see descriptions of the mathematical operation combine with the result of the operation.
public class PrimitiveDivision {
public static void main(String[] args) {
int i1 = 7, i2 = 2;
System.out.println("Integer Division");
System.out.println("\tint output with concatenation: " + i1 + "/" + i2 + " = " + i1/i2);
System.out.println(String.format("\tint output with format: %d/%d = %d",i1, i2, i1/i2));
System.out.printf("\tint output with printf: %d/%d = %d\n",i1, i2, i1/i2);
double d1 = 7, d2 = 2;
System.out.println("Double Division");
System.out.println("\tdouble output with concatenation: " + d1 + "/" + d2 + " = " + d1/d2);
System.out.println(String.format("\tdouble output with format: %.2f/%.2f = %.2f",d1, d2, d1/d2));
System.out.printf("\tdouble output with printf: %.2f/%.2f = %.2f\n",d1, d2, d1/d2);
System.out.println("Casting and Remainders");
System.out.printf("\tint cast to double on division: %d/%d = %.2f\n",i1, i2, i1/(double)i2);
System.out.println("\tint using modulo for remainder: " + i1 + "/" + i2 + " = " + i1/i2 + " remainder " + i1%i2);
}
}
PrimitiveDivision.main(null);
// java style to import library
import java.util.Scanner;
// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
public class ScanPrimitives {
public static void main(String[] args) {
Scanner input;
// primitive int
input = new Scanner(System.in);
System.out.print("Enter an integer: ");
try {
int sampleInputInt = input.nextInt();
System.out.println(sampleInputInt);
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
// primitive double
input = new Scanner(System.in);
System.out.print("Enter a double: ");
try {
double sampleInputDouble = input.nextDouble();
System.out.println(sampleInputDouble);
} catch (Exception e) { // if not a number
System.out.println("Not an double (form like 9.99), " + e);
}
input.close();
// primitive boolean
input = new Scanner(System.in);
System.out.print("Enter a boolean: ");
try {
boolean sampleInputBoolean = input.nextBoolean();
System.out.println(sampleInputBoolean);
} catch (Exception e) { // if not true or false
System.out.println("Not an boolean (true or false), " + e);
}
input.close();
// wrapper class String
input = new Scanner(System.in);
System.out.print("Enter a String: ");
try {
String sampleInputString = input.nextLine();
System.out.println(sampleInputString);
} catch (Exception e) { // this may never happen
System.out.println("Not an String, " + e);
}
input.close();
}
}
ScanPrimitives.main(null);
Output Primitive Data
The second key to to all programming is Output. All programming has means to format and combine data. In these examples you see descriptions of the mathematical operation combine with the result of the operation.
public class PrimitiveDivision {
public static void main(String[] args) {
int i1 = 7, i2 = 2;
System.out.println("Integer Division");
System.out.println("\tint output with concatenation: " + i1 + "/" + i2 + " = " + i1/i2);
System.out.println(String.format("\tint output with format: %d/%d = %d",i1, i2, i1/i2));
System.out.printf("\tint output with printf: %d/%d = %d\n",i1, i2, i1/i2);
double d1 = 7, d2 = 2;
System.out.println("Double Division");
System.out.println("\tdouble output with concatenation: " + d1 + "/" + d2 + " = " + d1/d2);
System.out.println(String.format("\tdouble output with format: %.2f/%.2f = %.2f",d1, d2, d1/d2));
System.out.printf("\tdouble output with printf: %.2f/%.2f = %.2f\n",d1, d2, d1/d2);
System.out.println("Casting and Remainders");
System.out.printf("\tint cast to double on division: %d/%d = %.2f\n",i1, i2, i1/(double)i2);
System.out.println("\tint using modulo for remainder: " + i1 + "/" + i2 + " = " + i1/i2 + " remainder " + i1%i2);
}
}
PrimitiveDivision.main(null);
// imports allow you to use code already written by others. It is good to explore and learn libraries. The names around the dots often give you a hint to the originator of the code.
// these are objects
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
// java style to import library
double mass;
double accel;
double Force;
double ac;
double m;
double mod1;
double mod2;
double result;
public class Menu {
// Instance Variables
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color // final = not going to change, static means element is not changing (not part of the object)
public final String[][] COLORS = { // 2D Array of ANSI Terminal Colors
{"Default",DEFAULT},
{"Red", "\u001B[31m"},
{"Green", "\u001B[32m"},
{"Yellow", "\u001B[33m"},
{"Blue", "\u001B[34m"},
{"Purple", "\u001B[35m"},
{"Cyan", "\u001B[36m"},
{"White", "\u001B[37m"},
};
// 2D column location for data
public final int NAME = 0;
public final int ANSI = 1; // ANSI is the "standard" for terminal codes
// Constructor on this Object takes control of menu events and actions
public Menu() {
Scanner sc = new Scanner(System.in); // using Java Scanner Object
this.print(); // print Menu
boolean quit = false;
while (!quit) {
try { // scan for Input
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
// Print the menu options to Terminal
private void print() {
//System.out.println commands below is used to present a Menu to the user.
System.out.println("-------------------------\n");
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Say Hello");
System.out.println("2 - Output colors");
System.out.println("3 - Loading in color");
System.out.println("4 - F=ma calculator");
System.out.println("0 - Quit");
System.out.println("-------------------------\n");
}
// Private method to perform action and return true if action is to quit/exit
private boolean action(int selection) {
boolean quit = false;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
System.out.print("Hello, World!");
break;
case 2:
for(int i = 0; i < COLORS.length; i++) // loop through COLORS array
System.out.print(COLORS[i][ANSI] + COLORS[i][NAME]);
break;
case 3:
Scanner input;
// primitive int
input = new Scanner(System.in);
System.out.println("Modulos calculator");
System.out.print("Enter your first number:");
try {
double mod1 = input.nextDouble();
System.out.println(mod1);
// m = mod1;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
// primitive int
input = new Scanner(System.in);
System.out.print("Enter the second number: ");
try {
double mod2 = input.nextDouble();
System.out.println(mod2);
// ac = accel;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input.close();
System.out.println("Modulos Calculator");
double result = mod1 % mod2;
System.out.print(mod1 + "modulos" + mod2 + '=' + Force);
break;
case 4:
// java style to import library
// class must alway have 1st letter as uppercase, CamelCase is Java Class convention
Scanner input4;
// primitive int
input4 = new Scanner(System.in);
System.out.println("F=ma calculator");
System.out.println("Enter the mass: ");
try {
double mass = input4.nextDouble();
System.out.println(mass);
m = mass;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input4.close();
// primitive int
input4 = new Scanner(System.in);
System.out.print("Enter the accel: ");
try {
double accel = input4.nextDouble();
System.out.println(accel);
ac = accel;
} catch (Exception e) { // if not an integer
System.out.println("Not an integer (form like 159), " + e);
}
input4.close();
System.out.println("Force calculator");
double Force = m * ac;
System.out.print(m + "*" + ac + '=' + Force);
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
// Static driver/tester method
static public void main(String[] args) {
new Menu(); // starting Menu object
}
}
Menu.main(null);