Primitives
A primitive refers boolean, integer (int), and double. Other primitive types are bytes, long, short, char. All of these data types can be referred to simply and hold a singular value.
All primitive variable types will be lowercase when referenced (int, boolean, etc.)
String is not a primitive data type. String is a class, and an array of char.
public class Converter {
public static void main(String[] args) {
double dollar = 1.0;
double euro = 1.002985;
double peso = 19.92457;
double pound = 0.84485;
double yen = 136.45;
System.out.println(dollar);
System.out.println(euro);
System.out.println(peso);
System.out.println(pound);
System.out.println(yen);
}
}
Converter.main(null);
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;
public class Converter {
public static void main(String[] args) {
Scanner input;
HashMap<String, Double> converter = new HashMap<String, Double>();
converter.put("dollar", 1.0);
converter.put("euro", 1.0029);
converter.put("peso", .0502);
converter.put("pound", 1.1904);
converter.put("yen", .0073);
double dollar = 1.0;
double euro = 1.002985;
double peso = 19.92457;
double pound = 0.84485;
double yen = 136.45;
int amountOfCurrency = 0;
String first = "0";
String second = "0";
input = new Scanner(System.in);
System.out.println("What is your name? ");
try {
String name = input.nextLine();
System.out.println( "Hello " + name + ". Welcome to our currency converter.");
} catch (Exception e) { // this may never happen
System.out.println(e + " is not a name.");
}
input.close();
input = new Scanner(System.in);
System.out.println("What currency would like to convert? ");
try {
first = input.nextLine();
} catch (Exception e) { // this may never happen
System.out.println(e + " is not a name.");
}
input.close();
input = new Scanner(System.in);
System.out.println("What would you like to convert it to? ");
try {
second = input.nextLine();
} catch (Exception e) { // this may never happen
System.out.println(e + " is not a name.");
}
input.close();
input = new Scanner(System.in);
System.out.println("How many " + first + "s are you converting? ");
try {
amountOfCurrency = input.nextInt();
} catch (Exception e) { // if not an integer
System.out.println("Please enter a whole number" + e);
}
input.close();
double first1 = converter.get(first);
double second1 = converter.get(second);
double output = ((amountOfCurrency * first1) / second1);
BigDecimal bd = new BigDecimal(output).setScale(2, RoundingMode.HALF_UP);
double outputRounded = bd.doubleValue();
System.out.println(amountOfCurrency + " " + first + "s is equal to " + outputRounded + " " + second + "s");
}
}
Converter.main(null);
Final Grade Calculator (calculates necessary score for the final):
First takes in boolean of whether final is in a separate category or tests category
If final is in separate category
- take in current grade - take in % of grade that is final - take in the desired grade - output % score needed on the final
If final is in a "tests" category
- take in current grade - take in % of grade that is tests category - take in current % in tests category - take in CURRENT amount of points in tests category (here integer can be used i suppose) - take in amount of points that the final is (here integer can be used i suppose) - take in the desired grade - output # of points needed on the final