Primitives Notebook
a primitives demo
- Listing the types of primitives:
- Division with ints and doubles:
- Celsius to Fahrenheit Converter
- Inputting primitive data:
public class ListPrimitives {
public static void main(String[] args) {
// assigning variable values
int anInt = 505;
double aDouble = 99.9;
boolean aBoolean = true;
char aChar='\u0021';
// not a primitive but essential
String aString = "Look over there!";
System.out.println("Types of Primitives:");
System.out.println("--> Integer: " + anInt);
System.out.println("--> Double: " + aDouble);
System.out.println("--> Character: " + aChar);
System.out.println("--> Boolean: " + aBoolean);
System.out.println("--> String: " + aString);
}
}
ListPrimitives.main(null)
public class Division {
public static void main(String[] args) {
// setting the variables
int divided = 17 / 24;
double doubleDivided = 17.0 / 24.0;
// printing out the division in int and double form
System.out.println("Integer division with 17/24 is: " + divided);
// %.2f limits the # of decimal values displayed
System.out.println("Double division with 17.0/24.0 is: " + String.format("%.2f", doubleDivided));
}
}
// makes it so you can print the code's output
Division.main(null)
public class CtoF {
public static void main(String[] args) {
System.out.println("Celsius to Fahrenheit Converter:");
System.out.println("Degrees Celsius: 55 -->");
int celsius = 55;
int fahrenheit = ((celsius*9)/5)+32;
System.out.println("\tDegrees Fahrenheit: " + fahrenheit);
System.out.println("Degrees Celsius: 78 -->");
int celsius1 = 78;
int fahrenheit1 = ((celsius1*9)/5)+32;
System.out.println("\tDegrees Fahrenheit: " + fahrenheit1);
System.out.println("Degrees Celsius: 11 -->");
int celsius2 = 11;
int fahrenheit2 = ((celsius2*9)/5)+32;
System.out.println("\tDegrees Fahrenheit: " + fahrenheit2);
}
}
// makes it so you can print the code's output
CtoF.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);