Skip to content

Instantly share code, notes, and snippets.

@dzt
Last active October 20, 2017 06:33
Show Gist options
  • Select an option

  • Save dzt/3e3d07856f72a1967144d716b882ec74 to your computer and use it in GitHub Desktop.

Select an option

Save dzt/3e3d07856f72a1967144d716b882ec74 to your computer and use it in GitHub Desktop.
AP CS A - Chapter 5 Solutions (100pts)
public class FloatCheck {
float one, two;
public FloatCheck(float one, float two) {
this.one = one;
this.two = two;
}
public boolean roundToTwoDec() {
double oneVal = (double) Math.round(one * 100) / 100.0;
double twoVal = (double) Math.round(two * 100) / 100.0;
if (oneVal == twoVal) {
return true;
} else {
return false;
}
}
public boolean differ() {
double diff = (double) Math.abs(one - two);
if (diff < 0.01) {
return true;
}
return false;
}
}
import java.util.Scanner;
public class FloatCheckTester {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
float one, two;
System.out.println("Enter two floating-point numbers:");
one = s.nextFloat();
two = s.nextFloat();
FloatCheck fc = new FloatCheck(one, two);
if (fc.roundToTwoDec()) {
System.out.println("They are the same when rounded to two decimal places.");
} else {
System.out.println("They are different when rounded to two decimal places.");
}
if (fc.differ()) {
System.out.println("They differ by less than 0.01");
} else {
System.out.println("They do not differ by less than 0.01");
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class Grade {
private HashMap<String, Double> gradeDict = new HashMap<String, Double>();
String grade;
public Grade(String grade) {
this.grade = grade;
setupDict();
}
public double getNumericGrade() {
double gradeVal = Double.NaN;
try {
gradeVal = gradeDict.get(this.grade);
} catch(NullPointerException e){
System.out.println("Invalid Input.");
System.exit(1);
}
return 4.0 - gradeVal;
}
private void setupDict() {
gradeDict.put("A+", 0.3 * 0);
gradeDict.put("A-", 0.3 * 1);
gradeDict.put("B+", 0.3 * 2);
gradeDict.put("B-", 0.3 * 3);
gradeDict.put("C+", 0.3 * 4);
gradeDict.put("C-", 0.3 * 5);
gradeDict.put("D+", 0.3 * 6);
gradeDict.put("D-", 0.3 * 7);
gradeDict.put("F", 0.3 * 8);
}
}
import java.util.Scanner;
public class GradeTester {
public static void main(String[] args) {
String grade;
Scanner input = new Scanner(System.in);
System.out.println("Enter a letter grade:");
grade = input.nextLine();
Grade g = new Grade(grade);
System.out.println("Numeric value: " + g.getNumericGrade());
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class LexicographicOrder {
public static void main(String[] args) {
ArrayList<String> words = new ArrayList<String>();
Scanner input = new Scanner(System.in);
String[] alphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
System.out.println("Please enter three strings:");
words.add(input.nextLine());
words.add(input.nextLine());
words.add(input.nextLine());
Collections.sort(words);
for(String s : words){
System.out.println(s);
}
}
}
public class Paycheck {
private double wage, hours;
public Paycheck(double wage, double hours) {
this.wage = wage;
this.hours = hours;
}
public double calculatePay() {
if (40 >= hours) {
return wage *= hours;
}
return wage = (wage * hours) * 1.5;
}
}
import java.util.Scanner;
public class PaycheckTester {
public static void main(String[] args) {
double wage, hours;
Scanner s = new Scanner(System.in);
System.out.println("Hours Worked (ex: 11, 16.5, etc): ");
hours = s.nextDouble();
System.out.println("Wage (ex: 9.55): ");
wage = s.nextDouble();
Paycheck p = new Paycheck(wage, hours);
System.out.println("You're paycheck is: $" + p.calculatePay());
}
}
public class Sort {
float a, b, c;
float one, two, three;
public Sort(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
}
public String getSortedValues() {
if (a > b && a > c) {
System.out.println(a + "is the biggest");
one = a;
if (b > c) {
two = b;
three = c;
} else {
two = c;
three = b;
}
} else if(b > a && b > c) {
one = b;
if (a > c) {
two = a;
three = c;
} else {
two = c;
three = a;
}
} else if (c > a && c > b) {
one = c;
if (b > a) {
two = b;
three = a;
} else {
two = a;
three = b;
}
}
return "\n" + Float.toString(three) + "\n" + Float.toString(two) + "\n" + Float.toString(one) + "\n";
}
}
import java.util.Scanner;
public class SortTester {
public static void main(String[] args) {
float a, b, c;
String aString = "hello";
String bString = "hello";
Scanner input = new Scanner(System.in);
System.out.println("Please enter three numbers");
a = input.nextFloat();
b = input.nextFloat();
c = input.nextFloat();
Sort s = new Sort(a, b, c);
System.out.println("The inputs sorted order are: " + s.getSortedValues());
}
}
import java.util.HashMap;
public class UnitConverter {
private String from, to;
private double value;
private HashMap<String, Double> conversionDict = new HashMap<String, Double>();
public UnitConverter(String from, String to, double value) {
this.from = from;
this.to = to;
this.value = value;
init();
}
public double getConversion() {
String query = this.from + "_to_" + this.to;
return value * conversionDict.get(query);
}
private void init() {
conversionDict.put("in_to_ft", 0.083);
conversionDict.put("in_to_mi", 1.6E-05);
conversionDict.put("in_to_mm", 25.4);
conversionDict.put("in_to_cm", 2.54);
conversionDict.put("in_to_m", 0.025);
conversionDict.put("in_to_km", 2.5E-05);
conversionDict.put("ft_to_in", 12.0);
conversionDict.put("ft_to_mi", 0.00019);
conversionDict.put("ft_to_mm", 304.8);
conversionDict.put("ft_to_cm", 30.48);
conversionDict.put("ft_to_m", 0.3);
conversionDict.put("ft_to_km", 0.0003);
conversionDict.put("mi_to_in", 63360.0);
conversionDict.put("mi_to_ft", 5280.0);
conversionDict.put("mi_to_mm", 1.60934E+06);
conversionDict.put("mi_to_cm", 160934.0);
conversionDict.put("mi_to_m", 1609.34);
conversionDict.put("mi_to_km", 1.61);
conversionDict.put("mm_to_in", 0.039);
conversionDict.put("mm_to_mi", 6.2E-07);
conversionDict.put("mm_to_ft", 0.0033);
conversionDict.put("mm_to_cm", 0.1);
conversionDict.put("mm_to_m", 0.1);
conversionDict.put("mm_to_km", 1E-06);
conversionDict.put("cm_to_in", 0.39);
conversionDict.put("cm_to_mi", 6.2E-06);
conversionDict.put("cm_to_mm", 10.0);
conversionDict.put("cm_to_ft", 0.033);
conversionDict.put("cm_to_m", 0.01);
conversionDict.put("cm_to_km", 1E-05);
conversionDict.put("m_to_in", 39.37);
conversionDict.put("m_to_mi", 0.00062);
conversionDict.put("m_to_mm", 1000.0);
conversionDict.put("m_to_ft", 3.28084);
conversionDict.put("m_to_cm", 100.0000032);
conversionDict.put("m_to_km", 0.001000000032);
conversionDict.put("km_to_in", 39370.08);
conversionDict.put("km_to_mi", 0.62137121212121);
conversionDict.put("km_to_mm", 1000000.0319999966305);
conversionDict.put("km_to_ft", 3280.8399999999887768);
conversionDict.put("km_to_cm", 100000.00319999965723);
conversionDict.put("km_to_m", 1000.0000319999966223);
}
}
import java.util.Scanner;
public class UnitConverterTester {
public static void main(String[] args) {
String from, to;
double value;
Scanner s = new Scanner(System.in);
System.out.println("Convert from:");
from = s.nextLine().toLowerCase();
System.out.println("Convert to:");
to = s.nextLine().toLowerCase();
System.out.println("Value:");
value = s.nextDouble();
UnitConverter u = new UnitConverter(from, to, value);
System.out.println(value + " " + from + " = " + u.getConversion() + " " + to);
}
}
public class Year {
public static boolean isLeapYear(int year) {
if (year % 4 != 0) {
return false;
} else if (year % 400 == 0) {
return true;
} else if (year % 100 == 0) {
return false;
} else {
return true;
}
}
}
import java.util.Scanner;
public class YearTester {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter Year:");
int year = s.nextInt();
System.out.print(new Year().isLeapYear(year));
}
}
import java.util.Scanner;
public class YesNoChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String response;
System.out.println("Do you want to continue?");
response = input.nextLine();
handleResponse(response);
}
public static void handleResponse(String response) {
String[] yesRes = {"Y", "Yes", "OK", "Sure", "Why not?"};
String[] noRes = {"N", "No"};
String result = null;
for (int i=0; i< yesRes.length; i++) {
if (yesRes[i].toLowerCase().indexOf(response.toLowerCase()) != -1) {
result = "y";
}
}
for (int i=0; i< noRes.length; i++) {
if (noRes[i].toLowerCase().indexOf(response.toLowerCase()) != -1) {
result = "n";
}
}
if (result != null) {
if (result == "y") {
System.out.println("OK");
System.exit(1);
} else {
System.out.println("Terminating");
System.exit(1);
}
} else {
System.out.println("Bad Input");
System.exit(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment