Primitives Notebook
public class Primitives {
public static void main(String[] args) {
int sampleint = 1324;
double sampledouble = 3.14;
boolean samplebool = false;
String samplestring = "hello world";
String diffstring = new String("hllo wrld");
System.out.println("Integer: " + sampleint);
System.out.println("Double: " + sampledouble);
System.out.println("Boolean: " + samplebool);
System.out.println("String: " + samplestring);
System.out.println("Different String: " + diffstring);
}
}
Primitives.main(null)
import java.util.Scanner;
public class InpPrimitives {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
System.out.print("Type an integer: ");
try {
int UserIntInput = input.nextInt();
System.out.println(UserIntInput);
} catch (Exception e) { // if not an integer
System.out.println("That is not an integer (form like 159), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type a double: ");
try {
double UserDoubleInput = input.nextDouble();
System.out.println(UserDoubleInput);
} catch (Exception e) {
System.out.println("That is not a double (form like 9.99), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type a boolean: ");
try {
boolean UserBoolInput = input.nextBoolean();
System.out.println(UserBoolInput);
} catch (Exception e) {
System.out.println("That is not a boolean (true or false), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type a string: ");
try {
String UserStringInput = input.nextLine();
System.out.println(UserStringInput);
} catch (Exception e) {
System.out.println("That is not a string, " + e);
}
input.close();
}
}
InpPrimitives.main(null);
public class PrimitiveDivision {
public static void main(String[] args) {
int sampleint1 = 7, sampleint2 = 2;
System.out.println("Dividing integers");
System.out.println("\tConcatenation example: " + sampleint1 + "/" + sampleint2 + " = " + sampleint1/sampleint2);
System.out.println(String.format("\tUsing string formatting: %d/%d = %d",sampleint1, sampleint2, sampleint1/sampleint2));
System.out.printf("\tint output with printf: %d/%d = %d",sampleint1, sampleint2, sampleint1/sampleint2);
double doub1 = 7.1, doub2 = 2.1;
System.out.println("\nDouble Division");
System.out.println("\tConcatenation example: " + doub1 + "/" + doub2 + " = " + doub1/doub2);
System.out.println(String.format("\tString formatting: %.2f/%.2f = %.2f",doub1, doub2, doub1/doub2));
System.out.printf("\tPrintf formatting: %.2f/%.2f = %.2f\n",doub1, doub2, doub1/doub2);
System.out.println("Casting and Remainders");
System.out.printf("\tint cast to double on division: %d/%d = %.2f\n",sampleint1, sampleint2, sampleint1/(double)sampleint2);
System.out.println("\tint using modulo for remainder: " + sampleint1 + "/" + sampleint2 + " = " + sampleint1/sampleint2 + " remainder " + sampleint1%sampleint2);
}
}
PrimitiveDivision.main(null);
public class GradeCalculator {
ArrayList<Double> grades;
public GradeCalculator() {
this.grades = new ArrayList<>();
this.enterGrades();
}
private boolean isZero(double value){
double threshold = 0.001;
return value >= -threshold && value <= threshold;
}
private void enterGrades() {
Scanner input;
while (true) {
input = new Scanner(System.in);
System.out.print("Enter a double, 0 to exit: ");
try {
double sampleInputDouble = input.nextDouble();
System.out.println(sampleInputDouble);
if (isZero(sampleInputDouble)) break;
else this.grades.add(sampleInputDouble);
} catch (Exception e) {
System.out.println("Not an double (form like 9.99), " + e);
}
input.close();
}
}
public double average() {
double total = 0;
for (double num : this.grades) {
total += num;
}
return (total / this.grades.size());
}
public static void main(String[] args) {
GradeCalculator grades = new GradeCalculator();
System.out.println("Average: " + String.format("%.2f", grades.average()));
}
}
GradeCalculator.main(null);
class PrimitiveDemo {
public static void main(String[] args) {
int DemInt = 143;
double DemDoub = 22.7;
boolean DemBool = false;
String myStr = "Hello World";
System.out.println(DemInt);
System.out.println(DemDoub);
System.out.println(DemBool);
System.out.println(myStr);
}
}
PrimitiveDemo.main(null)
// Hack 1: Celsius to Farenheit Calculator
class CTF {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
System.out.print("Type in a temperature in celsius: ");
try {
int UserIntInput = input.nextInt();
double Faren = ((UserIntInput * 9)/5) + 32; // formula
System.out.println(UserIntInput + " in celsius is " + Faren + " Farenheit."); // output string
} catch (Exception e) {
System.out.println("That is not a valid temperature " + e);
}
}
}
CTF.main(null)
// Hack: Nth Fibbonacci
class Fibo {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in); // take user input
System.out.print("Type in a number ");
try {
int N = input.nextInt(); // how far to go
int i1 = 0, i2 = 1; // starting numbers
int count = 0; // iteration
while (count < N) {
System.out.print(i1 + " ");
int i3 = i2 + i1;
i1 = i2;
i2 = i3;
count = count + 1;
}
} catch (Exception e) {
System.out.println("That is not a valid temperature " + e);
}
}
}
Fibo.main(null)