Username and Password Checker §
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// user and password checker
// caleb b
String correctUser = "martingrove_ci";
String correctPass = "mcipassword";
Scanner input = new Scanner(System.in);
System.out.println("Enter your username: ");
String user = input.nextLine();
System.out.println("Enter your password: ");
String pass = input.nextLine();
if (user.equals(correctUser) && pass.equals(correctPass)) {
System.out.println("Welcome! :3");
} else {
System.out.println("Incorrect username or password. Please try again.");
// run the program again
main(args);
}
}
}
Detect the Season §
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// season detector
// caleb b
Scanner scanner = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");
double[] temperatures = new double[7];
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
for (int i = 0; i < temperatures.length; i++) {
System.out.println("Enter the temperature for " + days[i] + " in Celcius: ");
temperatures[i] = scanner.nextDouble();
}
double average = 0;
for (double temperature : temperatures) {
average += temperature;
}
average /= temperatures.length;
System.out.println("The average temperature is " + df.format(average) + " degrees Celcius.");
if (average < 0) {
System.out.print("It's likely Winter.");
}
else if (average > 20) {
System.out.print("It's likely Summer.");
}
else {
System.out.print("It's likely Spring or Fall.");
}
}
}
Car Motion Calculator Revisited §
import java.text.DecimalFormat;
import java.util.Scanner;
import static java.lang.Double.NaN;
public class Main {
// car motion calculator
// caleb b
private static double time = NaN;
private static double distance = NaN;
private static double speed = NaN;
private static boolean timeSpecified = false;
private static boolean distanceSpecified = false;
private static boolean speedSpecified = false;
public static void main(String[] args) {
getInput(); // get the first input
getInput(); // get the second input
DecimalFormat df = new DecimalFormat("#.##");
if (Double.isNaN(time)) {
time = distance / speed;
System.out.println("Time: " + df.format(time) + " seconds");
}
if (Double.isNaN(distance)) {
distance = speed * time;
System.out.println("Distance: " + df.format(distance) + " meters");
}
if (Double.isNaN(speed)) {
speed = distance / time;
System.out.println("Speed: " + df.format(speed) + " meters per second");
}
}
public static void getInput() {
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to specify distance, time, or speed? (d/t/s)");
String input = scan.nextLine();
String invalidInputReason = "";
if (!validateInputType(input)) {
System.out.println("Invalid input. Please try again.");
getInput();
}
System.out.println("Please enter the " + input + " in meters.");
double value = scan.nextDouble();
if (value <= 0) {
System.out.println("Input must be greater than 0. Please try again.");
getInput();
}
if (input.equalsIgnoreCase("d") || input.equalsIgnoreCase("distance")) {
if (!distanceSpecified) {
distance = value;
distanceSpecified = true;
} else {
invalidInputReason = "distance is already specified";
}
}
if (input.equalsIgnoreCase("t") || input.equalsIgnoreCase("time")) {
if (!timeSpecified) {
time = value;
timeSpecified = true;
} else {
invalidInputReason = "time is already specified";
}
}
if (input.equalsIgnoreCase("s") || input.equalsIgnoreCase("speed")) {
if (!speedSpecified) {
speed = value;
speedSpecified = true;
} else {
invalidInputReason = "speed is already specified";
}
}
if (!invalidInputReason.equals("")) {
System.out.println("Invalid input. " + invalidInputReason + ". Please try again.");
getInput();
}
}
public static boolean validateInputType(String input) {
if (input.equalsIgnoreCase("d") || input.equalsIgnoreCase("distance")) {
return true;
}
if (input.equalsIgnoreCase("t") || input.equalsIgnoreCase("time")) {
return true;
}
return input.equalsIgnoreCase("s") || input.equalsIgnoreCase("speed");
}
}
Number of Coins Calculator §
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// coin calculator
// caleb b Scanner scanner = new Scanner(System.in);
System.out.println("Enter the amount of money (0-100): ");
double money = scanner.nextDouble();
if (money < 0 || money > 100) {
System.out.println("Invalid input");
return; }
if (money == 0) {
System.out.println("No coins");
return; }
int[] coins = {25, 10, 5, 1};
int[] coinCount = new int[4];
int i = 0;
while (money > 0) {
if (money >= coins[i]) {
money -= coins[i];
coinCount[i]++;
} else {
i++;
}
}
System.out.println("Quarters: " + coinCount[0]);
System.out.println("Dimes: " + coinCount[1]);
System.out.println("Nickels: " + coinCount[2]);
System.out.println("Pennies: " + coinCount[3]);
}
}
View Next Assignment