3.5 Compound Boolean Statements
- Nested if statements: if-statements within if-statement -- allows more flexibility to run the code; if outer if-statement is false, internal if-statement won't be evaluated
- Logical operators: && (and) ll(or) and !(not)
- Short circulated evaluation: the result of a compound Boolean expression can be determined just by looking at a few expressions
3.7 Comparing Objects
- Use '==' to see if two objects are referring to aliases for the same object
- Use '.equals()' to see if the attributes of two objects are the same
- Ex: vehicle car = new Vehicle("blue," 4); vehicle car2 = new Vehicle ("blue," 4) --> Vehicle blue car = car
- Both the vehicles have the same attributes
boolean cloudy = true;
boolean rainy = false;
if (!cloudy && !rainy) {
System.out.println("Don't forget to bring a hat!");
}
public static int numberOfLeapYears(int year1, int year2)
{
int count = 0;
for (int y = year1; y <= year2; y++) {
if (isLeapYear(y))
{
count++; }
}
return count;
}
public static int dayOfWeek(int month, int day, int year)
{
int startDay = firstDayOfYear(year);
int nthDay = dayOfYear(month, day, year);
int returnDay = (startDay + nthDay - 1) % 7;
return returnDay;
}
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("coeff a: ");
double a = input.nextDouble();
System.out.println("coeff b: ");
double b = input.nextDouble();
System.out.println("coeff c: ");
double c = input.nextDouble();
double root1 = (-b + (b * b - 4 * a * c)/(2*a));
double root2 = (-b - (b * b - 4 * a * c)/(2*a));
System.out.println("The roots are " + root1 + " and " + root2);
}
}
Problem2.main(null);
public class Problem4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("num: ");
double number = input.nextDouble();
System.out.println(number);
if (number > 0) {
System.out.println("pos");
} else if (number == 0) {
System.out.println("zero");
} else {
System.out.println("neg");
}
if (number < 1) {
System.out.println("neg");
} else if (number > 1000000) {
System.out.println("too big ");
}
}
}
Problem4.main(null);
public class Problem6 {
static double truncate(double n, int decimalPlace) {
n = n*Math.pow(10, decimalPlace);
n = Math.floor(n);
n = n/Math.pow(10, decimalPlace);
return n;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("num 1: ");
double number1 = input.nextDouble();
double num1Trun = truncate(number1, 3);
System.out.println(number1);
System.out.print("num 2: ");
double number2 = input.nextDouble();
double num2Trun = truncate(number2, 3);
System.out.println(number2);
if (num1Trun == num2Trun) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Problem6.main(null);
public class Problem8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("enter letter: ");
String letter = input.nextLine();
System.out.println(letter);
String[] vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"};
if(letter.length() > 1) {
System.out.println("not one letter");
} else if (!letter.matches("[a-zA-Z]+")) {
System.out.println("not a letter");
} else if (vowels.contains(letter)) {
System.out.println("vowel");
} else {
System.out.println("consonant");
}
input.close();
}
}
Problem8.main(null);
public class Problem10 {
public static void main(String[] args) {
int[] n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int num : n) {
System.out.println(num);
}
}
}
Problem10.main(null);
public class Problem12 {
public static void main(String[] args) {
int sum = 0;
double average;
System.out.println("Input nums:");
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
int num = input.nextInt();
System.out.println(num);
sum += num;
}
average = sum/5;
System.out.println("sum: " + sum);
System.out.println("avg: " + average);
}
}
Problem12.main(null);
public class Problem14 {
public static void main(String[] args) {
System.out.println("num: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println("terms: ");
int terms = input.nextInt();
for (int i = 0; i <= terms; i++) {
System.out.println(num + " x " + i + " = " + num*i);
}
}
}
Problem14.main(null);
public class Problem16 {
public static void main(String[] args) {
System.out.print("rows: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println(num);
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Problem16.main(null);
public class Problem18 {
public static void main(String[] args) {
System.out.print("rows: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println(num);
int count = 1;
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(count + "\t");
count++;
}
System.out.println();
}
}
}
Problem18.main(null);
public class Problem20 {
public static void main(String[] args) {
System.out.print("rows: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println(num);
int count = 1;
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(count + "\t");
count++;
}
System.out.println();
}
}
}
Problem20.main(null);