Chapter 17: Programming with Objects and Classes
In this chapter:
public class Person {
double weight;
double 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.weight / (person1.height * person1.height));
System.out.print("Person 2's BMI: ");
System.out.println(person2.weight / (person2.height * person2.height));
System.out.print("Person 3's BMI: ");
System.out.println(person3.weight / (person3.height * person3.height));
keyboard.close();
}
}
public class Country {
double debt;
double gdp;
}
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();
double ratio;
System.out.print("Country 1's ratio is ");
ratio = country1.debt / country1.gdp;
System.out.println(ratio);
if (ratio <= threshold) {
System.out.println("That's acceptable");
} else {
System.out.println("That's not acceptable");
}
System.out.println();
System.out.print("Country 2's ratio is ");
ratio = country2.debt / country2.gdp;
System.out.println(ratio);
if (ratio <= threshold) {
System.out.println("That's acceptable");
} else {
System.out.println("That's not acceptable");
}
System.out.println();
System.out.print("Country 3's ratio is ");
ratio = country3.debt / country3.gdp;
System.out.println(ratio);
if (ratio <= threshold) {
System.out.println("That's acceptable");
} else {
System.out.println("That's not acceptable");
}
System.out.println();
keyboard.close();
}
}
public class Thing {
int value1;
int value2;
}
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 has values ");
System.out.print(thing1.value1);
System.out.print(" and ");
System.out.print(thing1.value2);
System.out.println(".");
System.out.print("Thing 2 has values ");
System.out.print(thing2.value1);
System.out.print(" and ");
System.out.print(thing2.value2);
System.out.println(".");
System.out.print("Thing 3 has values ");
System.out.print(thing3.value1);
System.out.print(" and ");
System.out.print(thing3.value2);
System.out.println(".");
keyboard.close();
}
}