From a01da759d8c411bd2a93e40c98e407dfb32107f7 Mon Sep 17 00:00:00 2001 From: Tony Weber Date: Tue, 11 May 2021 10:22:41 -0400 Subject: [PATCH] s --- ch03/tony/CelsiusToFahrenheit.java | 20 ++++++++++++++++++++ ch03/tony/ConvertSeconds.java | 26 ++++++++++++++++++++++++++ ch03/tony/GuessMyNumber.java | 27 +++++++++++++++++++++++++++ hw02/tony/Calories.java | 20 ++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 ch03/tony/CelsiusToFahrenheit.java create mode 100644 ch03/tony/ConvertSeconds.java create mode 100644 ch03/tony/GuessMyNumber.java create mode 100644 hw02/tony/Calories.java diff --git a/ch03/tony/CelsiusToFahrenheit.java b/ch03/tony/CelsiusToFahrenheit.java new file mode 100644 index 0000000..7d8be9b --- /dev/null +++ b/ch03/tony/CelsiusToFahrenheit.java @@ -0,0 +1,20 @@ +import java.util.Scanner; + +/** + * Convert Celsius to Fahrenheit + */ +public class CelsiusToFahrenheit { + + public static void main(String[] args) { + double celsius; + double fahrenheit; + Scanner in = new Scanner(System.in); + System.out.print("Enter a temperature in Celsius: "); + celsius = in.nextDouble(); + in.nextLine(); + fahrenheit = celsius * 9; + fahrenheit = fahrenheit / 5; + fahrenheit = fahrenheit + 32; + System.out.printf("%.1f C = %.1f F\n", celsius, fahrenheit); + } +} diff --git a/ch03/tony/ConvertSeconds.java b/ch03/tony/ConvertSeconds.java new file mode 100644 index 0000000..94b9c4c --- /dev/null +++ b/ch03/tony/ConvertSeconds.java @@ -0,0 +1,26 @@ +import java.util.Scanner; + +/** + * Convert seconds to hours, minutes, and seconds. + */ +public class ConvertSeconds { + + public static void main(String[] args) { + final int SECONDS_PER_HOUR = 3600; + final int SECONDS_PER_MINUTE = 60; + int seconds; + int orig_seconds; + int minutes; + int hours; + Scanner in = new Scanner(System.in); + System.out.print("Enter number of seconds: "); + seconds = in.nextInt(); + in.nextLine(); + orig_seconds = seconds; + hours = seconds / SECONDS_PER_HOUR; + seconds = seconds % SECONDS_PER_HOUR; + minutes = seconds / SECONDS_PER_MINUTE; + seconds = seconds % SECONDS_PER_MINUTE; + System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds\n", orig_seconds, hours, minutes, seconds); + } +} diff --git a/ch03/tony/GuessMyNumber.java b/ch03/tony/GuessMyNumber.java new file mode 100644 index 0000000..3e053c1 --- /dev/null +++ b/ch03/tony/GuessMyNumber.java @@ -0,0 +1,27 @@ +import java.util.Random; +import java.util.Scanner; + +/** + * User guesses a random number. + */ +public class GuessMyNumber { + + public static void main(String[] args) { + int number; + int guess; + + Random random = new Random(); + number = random.nextInt(100) + 1; + + System.out.println("I'm thinking of a number between 1 and 100"); + System.out.println("(including both). Can you guess what it is?"); + System.out.print("Type a number: "); + Scanner in = new Scanner(System.in); + guess = in.nextInt(); + in.nextLine(); + + System.out.printf("Your guess is: %d\n", guess); + System.out.printf("The number I was thinking of is: %d\n", number); + System.out.printf("You were off by: %d\n", Math.abs(number - guess)); + } +} diff --git a/hw02/tony/Calories.java b/hw02/tony/Calories.java new file mode 100644 index 0000000..f98c6db --- /dev/null +++ b/hw02/tony/Calories.java @@ -0,0 +1,20 @@ + +class Calories { + + public static void main(String[] args) { + int years = 25; + int feet = 5; + int inches = 10; + int pounds = 160; + int ounces = 3; + double weight = (16 * pounds + ounces) * 0.0283495; + double height = (12 * feet + inches) * 2.54; + double bmr_base = 10 * weight + 6.25 * height - 5 * years; + System.out.print("Male BMR = "); + System.out.print(bmr_base + 5); + System.out.println(" calories/day."); + System.out.print("Female BMR = "); + System.out.print(bmr_base - 161); + System.out.println(" calories/day."); + } +}