Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 5th Edition
by Barry Burd

Chapter 19: Creating New Java Methods

In this chapter:

One, two, three

class Counter {
    int count = 0;

    void increment() {
        count++;
    }
}

public class Main {

    public static void main(String[] args) {
        Counter counter = new Counter();

        counter.increment();
        
        System.out.println(counter.count);
    }
}

Two, seven, nineteen

class Counter {
    int count = 0;

    void increment(int byHowMuch) {
        count += byHowMuch;
    }
}

public class Main {

    public static void main(String[] args) {
        Counter counter = new Counter();

        counter.increment(1);
        counter.increment(6);
        counter.increment(12);

        System.out.println(counter.count);
    }
}

Process purchases

class Purchase {
    double unitPrice;
    int quantity;
    boolean taxable;
    
    double getTotal() {
        double total = unitPrice * quantity;
        if (taxable) {
            total *= 1.05;
        }
        return total;
    }
}


class ProcessPurchases {

    public static void main(String[] args) {
        Purchase purchase1 = new Purchase();
        purchase1.unitPrice = 20.00;
        purchase1.quantity = 3;
        purchase1.taxable = true;

        Purchase purchase2 = new Purchase();
        purchase2.unitPrice = 20.00;
        purchase2.quantity = 3;
        purchase2.taxable = false;

        if (purchase1.getTotal() == purchase2.getTotal()) {
            System.out.println("No difference");
        } else {
            System.out.println("These purchases have different totals.");
        }
    }
}

Has your BMI changed since Chapter 17?

public class Person {
    double weight;
    double height;
    
    double getBmi() {
        return weight / (height * height);
    }
}


import java.util.Scanner;

public class FindBmi {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        Person person1 = new Person();
        System.out.println("Person 1");
        System.out.print("Weight? ");
        person1.weight = keyboard.nextDouble();
        System.out.print("Height? ");
        person1.height = keyboard.nextDouble();
        System.out.println();
        
        System.out.println("Person 2");
        Person person2 = new Person();
        System.out.print("Weight? ");
        person2.weight = keyboard.nextDouble();
        System.out.print("Height? ");
        person2.height = keyboard.nextDouble();
        System.out.println();
        
        System.out.println("Person 3");
        Person person3 = new Person();
        System.out.print("Weight? ");
        person3.weight = keyboard.nextDouble();
        System.out.print("Height? ");
        person3.height = keyboard.nextDouble();
        System.out.println();
        
        System.out.print("Person 1's BMI: ");
        System.out.println(person1.getBmi());
        
        System.out.print("Person 2's BMI: ");
        System.out.println(person2.getBmi());

        System.out.print("Person 3's BMI: ");
        System.out.println(person3.getBmi());

        keyboard.close();
    }

}

Two times nothing is still nothing

public class Thing {
    int value1;
    int value2;
    
    void display() {
        System.out.print("This thing has values ");
        System.out.print(value1);
        System.out.print(" and ");
        System.out.print(value2);
        System.out.println(".");
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        Thing thing1 = new Thing();
        System.out.println("Thing 1");
        System.out.print("value1? ");
        thing1.value1 = keyboard.nextInt();
        System.out.print("value2? ");
        thing1.value2 = keyboard.nextInt();
        System.out.println();
        
        System.out.println("Thing 2");
        Thing thing2 = new Thing();
        System.out.print("value1? ");
        thing2.value1 = keyboard.nextInt();
        System.out.print("value2? ");
        thing2.value2 = keyboard.nextInt();
        System.out.println();
        
        System.out.println("Thing 3");
        Thing thing3 = new Thing();
        System.out.print("value1? ");
        thing3.value1 = keyboard.nextInt();
        System.out.print("value2? ");
        thing3.value2 = keyboard.nextInt();
        System.out.println();
        
        System.out.print("Thing 1: ");
        thing1.display();
        
        System.out.print("Thing 2: ");
        thing2.display();
        
        System.out.print("Thing 3: ");
        thing3.display();

        keyboard.close();
    }

}

More macroeconomics

public class Country {
    double debt;
    double gdp;
    
    boolean hasAcceptableRatio(double threshold) {
        return debt / gdp <= threshold;
    }
}


import java.util.Scanner;

public class DebtToGdbRatio {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        Country country1 = new Country();
        System.out.println("Country 1");
        System.out.print("Debt? ");
        country1.debt = keyboard.nextDouble();
        System.out.print("GDP? ");
        country1.gdp = keyboard.nextDouble();
        System.out.println();
        
        System.out.println("Country 2");
        Country country2 = new Country();
        System.out.print("Debt? ");
        country2.debt = keyboard.nextDouble();
        System.out.print("GDP? ");
        country2.gdp = keyboard.nextDouble();
        System.out.println();
        
        System.out.println("Country 3");
        Country country3 = new Country();
        System.out.print("Debt? ");
        country3.debt = keyboard.nextDouble();
        System.out.print("GDP? ");
        country3.gdp = keyboard.nextDouble();
        System.out.println();
        
        System.out.print("Acceptable debt-to-GDP ratio? ");
        double threshold = keyboard.nextDouble();
        System.out.println();
               
        System.out.print("Country 1's ratio is ");
        if (!country1.hasAcceptableRatio(threshold)) {
            System.out.print("not ");
        }
        System.out.println("acceptable.");
        
        System.out.print("Country 2's ratio is ");
        if (!country2.hasAcceptableRatio(threshold)) {
            System.out.print("not ");
        }
        System.out.println("acceptable.");
        
        System.out.print("Country 3's ratio is ");
        if (!country3.hasAcceptableRatio(threshold)) {
            System.out.print("not ");
        }
        System.out.println("acceptable.");
        
        keyboard.close();
    }

}