Week 1 - Java Primitives Post
Blog Post Demonstrating Java Primitives for AP CSA
Java Primitives
import java.util.Scanner;
public class primitives {
public static void main(String[] args) {
// int is for integers
int x = 7;
System.out.println(x);
// double is for numbers with decimals
double pi = 3.14159265358979;
System.out.println(pi);
// boolean has two return values, which are usually true and false
boolean y = true;
System.out.println(y);
// String is for text values
String z = "Hello, World!";
System.out.println(z);
for (int a = 0; a < 5; a++) {
Scanner userInput1 = new Scanner(System.in);
System.out.println("Type your first integer:");
int first = userInput1.nextInt();
System.out.println("Type your second integer:");
int second = userInput1.nextInt();
Scanner userInput2 = new Scanner(System.in);
System.out.println("Type the desired mathematical operator:");
String operator = userInput2.nextLine();
String operatorNew = operator.toLowerCase();
if (operatorNew.equals("addition")) {
int sum = first + second;
System.out.println(first + " plus " + second + " equals " + sum);
} else if (operatorNew.equals("subtraction")) {
int difference = first - second;
System.out.println(first + " subtracted by " + second + " equals " + difference);
} else if (operatorNew.equals("multiplication")) {
int product = first * second;
System.out.println(first + " multiplied by " + second + " equals " + product);
} else if (operatorNew.equals("division")) {
double quotient = (double) first / (double) second;
System.out.println(first + " divided by " + second + " equals " + quotient);
} else if (operatorNew.equals("modulation")) {
int modResult = first % second;
System.out.println(first + " modulus " + second + " equals " + modResult);
}
}
}
}
primitives.main(null);
Java Compound Operators
public class javaCompound {
public static void main(String[] args) {
// Initial Values
int x = 7;
int y = 8;
double a = 3.14;
double b = 6.28;
// The compound operators modify values
x += 7;
y += 8;
a *= 2;
b /= 2;
System.out.println(x);
System.out.println(y);
System.out.println(a);
System.out.println(b);
}
}
javaCompound.main(null);
GPA Calculator
import java.util.Scanner;
public class GPACalculator {
public static void main(String[] args) {
System.out.println("Trimester GPA Calculator");
Scanner userInput = new Scanner(System.in);
int sum = 0;
System.out.println("Enter the number of classes this trimester or the most recent trimester with final grades:");
int classes = userInput.nextInt();
System.out.println(classes + " classes");
System.out.println("Enter Class Grades:");
for (int i = 0; i <= classes; i++) {
int gpa = 0;
String grade = userInput.nextLine();
System.out.println(grade);
if (grade.equals("A")) {
gpa = 4;
} else if (grade.equals("B")) {
gpa = 3;
} else if (grade.equals("C")) {
gpa = 2;
} else if (grade.equals("D")) {
gpa = 1;
} else if (grade.equals("F")) {
gpa = 0;
}
sum = sum + gpa;
}
float gpaWhole = (float) sum/(float) classes;
System.out.println("Your Overall GPA: " + gpaWhole);
}
}
GPACalculator.main(null);