Unit 3 Homework
import java.util.Scanner; // import scanner
import java.lang.Math; // import Math methods
public class unit3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// input a, b, and c variables
System.out.println("Input a: ");
double a = sc.nextDouble();
System.out.println("Input b: ");
double b = sc.nextDouble();
System.out.println("Input c: ");
double c = sc.nextDouble();
double insideRoot = (b * b) - (4.0 * a * c); // "b^2 - 4ac" part of quadratic formula
double rt1 = (-b + Math.sqrt(insideRoot))/ 2.0*a; // -b + sqrt(b^2 - 4ac)
double rt2 = (-b - Math.sqrt(insideRoot))/ 2.0*a; // -b - sqrt(b^2 - 4ac)
System.out.println();
System.out.println("--------------------");
// if the expression inside the sqrt is below 0, sqrt of <0 is an imaginary number. If it is 0, there is one root.
if (insideRoot > 0.0) {
System.out.println("Root 1: " + rt1);
System.out.println("Root 2: " + rt2);
}
else if (insideRoot == 0.0) {
System.out.println("Root 1: " + rt1);
}
else {
System.out.println("There are no real roots");
}
}
}
unit3.main(null);
public class numberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
double input = sc.nextDouble();
System.out.println("Input is: " + input);
System.out.println("--------------------");
if (input > 0.0) {
if (input >= 1000000.0) {
System.out.println("Input is a large positive number.");
}
else if (input <= 1.0) {
System.out.println("Input is a small positive number.");
}
else {
System.out.println("Input is a positive number.");
}
}
else if (input < 0.0) {
if (Math.abs(input) >= 1000000.0) {
System.out.println("Input is a large negative number.");
}
else if (Math.abs(input) <= 1.0) {
System.out.println("Input is a small negative number.");
}
else {
System.out.println("Input is a negative number.");
}
}
else {
System.out.println("Input is 0.");
}
}
}
numberCheck.main(null);
public class numbersSame {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number: ");
double input1 = Math.round(sc.nextDouble() * 1000);
input1 /= 1000;
System.out.println("Input another number: ");
double input2 = Math.round(sc.nextDouble() * 1000);
input2 /= 1000;
System.out.println();
System.out.println("Input 1: " + input1);
System.out.println("Input 2: " + input2);
System.out.println();
System.out.println("--------------------");
if (input1 == input2) {
System.out.println("Both inputs are the same.");
}
else {
System.out.println("Inputs are different.");
}
}
}
numbersSame.main(null);
public class alphabetCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().toLowerCase();
String alphabet = "abcdefghijklmnopqrstuvwxyz";
boolean vowels = input.equals("a") || input.equals("e") || input.equals("i") || input.equals("o") || input.equals("u");
for (int i = 0; i < alphabet.length(); i++) { // compare input with each index of
char c = alphabet.charAt(i);
if (i == 25 && input.charAt(0) != c) { // if input is not in the alphabet
System.out.println("Input is not in the alphabet.");
}
else if (input.charAt(0) == c) { // if input matches a letter in alphabet, check if its vowel or consonant
if (input.length() > 1) {
System.out.println("Input is larger than 1 character.");
}
else if (vowels) {
System.out.println("Input is a vowel");
}
else {
System.out.println("Input is a consonant");
}
break;
}
}
}
}
alphabetCheck.main(null);