AP Computer Science A is both a college-prep course for potential computer science majors and a foundation course for students planning to study in other technical fields such as engineering, physics, chemistry, and geology. The course emphasizes methodology, procedural abstraction, and in-depth study of algorithms, data structures, and data abstractions, as well as a detailed examination of a large case study program. Instruction includes preparation for the AP Computer Science A exam. In this course, students apply the programming tools they have learned to real-life examples on their own. Computer science is more than just programming; students who successfully complete the course will have an understanding of Java and the ability to adapt to any new programming language that they are taught in college.
import java.util.*;
public class GPACalculator {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int input; //grade (0 to 100)
double totalWeight = 0.0;
double courseWeight;
double sum = 0.0;
//Record numerical grade and weight for course 1
System.out.println("Enter the credits (double) for course 1: ");
courseWeight = kb.nextDouble();
totalWeight = totalWeight + courseWeight;
System.out.println("Enter a grade (0 to 100) for course 1:");
input = kb.nextInt();
if (input >=93) {
sum = sum + courseWeight*4.0;
} else if (input >=90) {
sum = sum + courseWeight*3.7;
} else if (input >=87) {
sum = sum + courseWeight*3.3;
} else if (input >=83) {
sum = sum + courseWeight*3.0;
} else if (input >=80) {
sum = sum + courseWeight*2.7;
} else if (input >=77) {
sum = sum + courseWeight*2.3;
} else if (input >=73) {
sum = sum + courseWeight*2.0;
} else if (input >=70) {
sum = sum + courseWeight*1.7;
} else if (input >=67) {
sum = sum + courseWeight*1.3;
} else if (input >=63) {
sum = sum + courseWeight;
} else if (input >=60) {
sum = sum + courseWeight*0.7;
} else {
sum = sum + courseWeight*0.0;
}
//reset courseWeight
courseWeight = 0.0;
//Record numerical grade and weight for course 2
System.out.println("Enter the credits (double) for course 2: ");
courseWeight = kb.nextDouble();
totalWeight = totalWeight + courseWeight;
System.out.println("Enter a grade (0 to 100) for course 2:");
input = kb.nextInt();
if (input >=93) {
sum = sum + courseWeight*4.0;
} else if (input >=90) {
sum = sum + courseWeight*3.7;
} else if (input >=87) {
sum = sum + courseWeight*3.3;
} else if (input >=83) {
sum = sum + courseWeight*3.0;
} else if (input >=80) {
sum = sum + courseWeight*2.7;
} else if (input >=77) {
sum = sum + courseWeight*2.3;
} else if (input >=73) {
sum = sum + courseWeight*2.0;
} else if (input >=70) {
sum = sum + courseWeight*1.7;
} else if (input >=67) {
sum = sum + courseWeight*1.3;
} else if (input >=63) {
sum = sum + courseWeight;
} else if (input >=60) {
sum = sum + courseWeight*0.7;
} else {
sum = sum + courseWeight*0.0;
}
//reset courseWeight
courseWeight = 0.0;
//Record numerical grade and weight for course 3
System.out.println("Enter the credits (double) for course 3: ");
courseWeight = kb.nextDouble();
totalWeight = totalWeight + courseWeight;
System.out.println("Enter a grade (0 to 100) for course 3:");
input = kb.nextInt();
if (input >=93) {
sum = sum + courseWeight*4.0;
} else if (input >=90) {
sum = sum + courseWeight*3.7;
} else if (input >=87) {
sum = sum + courseWeight*3.3;
} else if (input >=83) {
sum = sum + courseWeight*3.0;
} else if (input >=80) {
sum = sum + courseWeight*2.7;
} else if (input >=77) {
sum = sum + courseWeight*2.3;
} else if (input >=73) {
sum = sum + courseWeight*2.0;
} else if (input >=70) {
sum = sum + courseWeight*1.7;
} else if (input >=67) {
sum = sum + courseWeight*1.3;
} else if (input >=63) {
sum = sum + courseWeight;
} else if (input >=60) {
sum = sum + courseWeight*0.7;
} else {
sum = sum + courseWeight*0.0;
}
//reset courseWeight
courseWeight = 0.0;
//Record numerical grade and weight for course 4
System.out.println("Enter the credits (double) for course 4: ");
courseWeight = kb.nextDouble();
totalWeight = totalWeight + courseWeight;
System.out.println("Enter a grade (0 to 100) for course 4:");
input = kb.nextInt();
if (input >=93) {
sum = sum + courseWeight*4.0;
} else if (input >=90) {
sum = sum + courseWeight*3.7;
} else if (input >=87) {
sum = sum + courseWeight*3.3;
} else if (input >=83) {
sum = sum + courseWeight*3.0;
} else if (input >=80) {
sum = sum + courseWeight*2.7;
} else if (input >=77) {
sum = sum + courseWeight*2.3;
} else if (input >=73) {
sum = sum + courseWeight*2.0;
} else if (input >=70) {
sum = sum + courseWeight*1.7;
} else if (input >=67) {
sum = sum + courseWeight*1.3;
} else if (input >=63) {
sum = sum + courseWeight;
} else if (input >=60) {
sum = sum + courseWeight*0.7;
} else {
sum = sum + courseWeight*0.0;
}
//reset courseWeight
courseWeight = 0.0;
//Record numerical grade and weight for course 5
System.out.println("Enter the credits (double) for course 5: ");
courseWeight = kb.nextDouble();
totalWeight = totalWeight + courseWeight;
System.out.println("Enter a grade (0 to 100) for course 5:");
input = kb.nextInt();
if (input >=93) {
sum = sum + courseWeight*4.0;
} else if (input >=90) {
sum = sum + courseWeight*3.7;
} else if (input >=87) {
sum = sum + courseWeight*3.3;
} else if (input >=83) {
sum = sum + courseWeight*3.0;
} else if (input >=80) {
sum = sum + courseWeight*2.7;
} else if (input >=77) {
sum = sum + courseWeight*2.3;
} else if (input >=73) {
sum = sum + courseWeight*2.0;
} else if (input >=70) {
sum = sum + courseWeight*1.7;
} else if (input >=67) {
sum = sum + courseWeight*1.3;
} else if (input >=63) {
sum = sum + courseWeight;
} else if (input >=60) {
sum = sum + courseWeight*0.7;
} else {
sum = sum + courseWeight*0.0;
}
//reset courseWeight
courseWeight = 0.0;
System.out.println("The GPA is: " + sum /totalWeight);
}
}
/*
Working with Scanner, String to build a message for the user.
*/
import java.util.*;
public class Interaction {
public static void main(String[] args) {
//variables
String firstName, lastName, country, favMovie, output;
int age, siblings;
Scanner kb = new Scanner(System.in);
System.out.println("Hi there. What's your first name?");
firstName = kb.nextLine();
System.out.println("And your last name?");
lastName = kb.nextLine();
System.out.println("Next, enter your country of origin.");
country = kb.nextLine();
System.out.println("Next, enter your favorite movie.");
favMovie = kb.nextLine();
System.out.println("Just two more questions. Next, enter your age.");
age = kb.nextInt();
System.out.println("And finally, how many siblings do you have?");
siblings = kb.nextInt();
output = "Hello " + firstName +" "+ lastName +". Nice to meet you.\nYou are from "
+ country +" and your favorite movie is " + favMovie +".\nYou are "+ age +
" years old, and you indicated that you have " + siblings +" siblings.";
System.out.println(output);
System.out.println();
System.out.println("The message I printed for you in the preceding step contains "
+ output.length() +" characters.");
}//end main
} //end class