public class PrimitiveExamples {
public static void main(String[] args) {
int exampleInt = 94;
double exampleDouble = 100.4;
boolean exampleBoolean = false;
String exampleString = "Argentina winning the world cup!";
String exampleStringFormal = new String("Lionel Messi along with the Argentinian National Team are the favorites for this World Cup");
System.out.println("Integer Example: " + exampleInt);
System.out.println("Double Example: " + exampleDouble);
System.out.println("Boolean Example: " + exampleBoolean);
System.out.println("String Example: " + exampleString);
System.out.println("Formal String Example: " + exampleStringFormal);
}
}
PrimitiveExamples.main(null)
import java.util.Scanner;
public class PranavPrimitivesExamples {
public static void main(String[] args) {
Scanner input;
input = new Scanner(System.in);
System.out.print("Type An Integer: ");
try {
int PersonInt = input.nextInt();
System.out.println(PersonInt);
} catch (Exception e) {
System.out.println("This Isn't An Integer (form like 159), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type A Double: ");
try {
double PersonDouble = input.nextDouble();
System.out.println(PersonDouble);
} catch (Exception e) {
System.out.println("This Isn't A Double (form like 9.99), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type A Boolean: ");
try {
boolean PersonBoolean = input.nextBoolean();
System.out.println(PersonBoolean);
} catch (Exception e) {
System.out.println("This Isn't A Boolean (true or false), " + e);
}
input.close();
input = new Scanner(System.in);
System.out.print("Type A String: ");
try {
String PersonString = input.nextLine();
System.out.println(PersonString);
} catch (Exception e) {
System.out.println("This Isn't A String, " + e);
}
input.close();
}
}
PranavPrimitivesExamples.main(null);
public class PranavPrimitiveDivision {
public static void main(String[] args) {
int b1 = 9, b2 = 3;
System.out.println("Division For Integers");
System.out.println("int output with concatenation: " + b1 + "/" + b2 + " = " + b1/b2);
System.out.println(String.format("int output with format: %d/%d = %d",b1, b2, b1/b2));
System.out.printf("\tint output with printf: %d/%d = %d\n",b1, b2, b1/b2);
double c1 = 9, c2 = 3;
System.out.println("Division For Doubles");
System.out.println("\tdouble output with concatenation: " + c1 + "/" + c2 + " = " + c1/c2);
System.out.println(String.format("\tdouble output with format: %.2f/%.2f = %.2f",c1, c2, c1/c2));
System.out.printf("\tdouble output with printf: %.2f/%.2f = %.2f\n",c1, c2, c1/c2);
System.out.println("Casting and Remainders");
System.out.printf("\tint cast to double on division: %d/%d = %.2f\n",b1, b2, b1/(double)b2);
System.out.println("\tint using modulo for remainder: " + b1 + "/" + b2 + " = " + b1/b2 + " remainder " + b1%b2);
}
}
PranavPrimitiveDivision.main(null);
public class PranavSchoolGradeCalculator {
ArrayList<Double> grades;
public PranavSchoolGradeCalculator() {
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) {
PranavSchoolGradeCalculator grades = new PranavSchoolGradeCalculator();
System.out.println("Average: " + String.format("%.2f", grades.average()));
}
}
PranavSchoolGradeCalculator.main(null);
public class PranavSchoolGradeCalculator {
ArrayList<Double> grades;
public PranavSchoolGradeCalculator() {
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) {
PranavSchoolGradeCalculator grades = new PranavSchoolGradeCalculator();
System.out.println("Average: " + String.format("%.2f", grades.average()));
}
}