Compare commits

..

2 commits

Author SHA1 Message Date
Cameron Seamons
0f8de1a0b9 created compound calculator 2026-01-05 21:12:29 -07:00
Cameron Seamons
a157f62b3d explore printf variations 2026-01-05 19:33:47 -07:00
2 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,43 @@
import java.util.Scanner;
public class compountInterest {
public static void main(String[] args) {
// Compound interest calculator
Scanner scanner = new Scanner(System.in);
// Initialize variables
double principal;
double rate;
int timesCompounded;
int years;
double amount;
// get inputs
System.out.print("Enter the principal amount: ");
principal = Double.valueOf(scanner.nextLine());
System.out.print("Enter the interest rate (in %): ");
rate = Double.valueOf(scanner.nextLine()) / 100;
System.out.print("Enter the # of times compounded per year: ");
timesCompounded = Integer.valueOf(scanner.nextLine());
System.out.print("Enter the # of years: ");
years = Integer.valueOf(scanner.nextLine());
// Calculate the compound interest
amount = principal * Math.pow(1 + rate / timesCompounded, timesCompounded * years);
// Print out the results
System.out.printf("\nThe amount after %d years is: $ %.2f", years, amount);
scanner.close();
}
}

View file

@ -0,0 +1,57 @@
public class printF {
public static void main(String[] args){
String name = "Spongebob";
char firstletter = 'S';
int age = 30;
double height = 60.5;
boolean isEmployed = true;
System.out.printf("Hello %s\n", name); // %s formats a String
System.out.printf("Your name starts with a %c\n", firstletter); // %c formats a single character (char)
System.out.printf("You are %d years old\n", age); // %d formats an integer (decimal number)
System.out.printf("You are %f inches tall\n", height); // %f formats a floating-point number (double / float)
System.out.printf("Employed: %b\n", isEmployed); // %b formats a boolean (true / false)
// You can include multiple format specifiers in one print statement.
System.out.printf("%s is %d years old. He is %f inches tall\n", name, age, height);
// IMPORTANT: The values MUST be in the same order as the format specifiers.
double price = 9.99;
double price2 = -19.99;
double price3 = 19999.99;
// You can add special characters to alter how floats are shown
// a point '.' followed by a number specifies how many decimal places are shown
// A '+' shows a positive sign before the number
// // ( means that negative numbers are shown enclosed in ()
// 'space' means display a minus if negative, and a space if positive
// a ',' will add a comma grouping separator to the thousandths place
System.out.printf("The price for one is $% .2f, for two is $% .2f, and for 100 is $%,.2f\n", price, price2, price3);
// width
int id1 = 1;
int id2 = 23;
int id3 = 456;
int id4 = 7890;
// 0 = zero padding
// number = right justified padding
// negative number = left justified padding
System.out.printf("%4d\n", id1);
System.out.printf("%4d\n", id2);
System.out.printf("%4d\n", id3);
System.out.printf("%4d\n", id4);
}
}