Skip to content

Commit a01da75

Browse files
committed
s
1 parent 6b758bd commit a01da75

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

ch03/tony/CelsiusToFahrenheit.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Convert Celsius to Fahrenheit
5+
*/
6+
public class CelsiusToFahrenheit {
7+
8+
public static void main(String[] args) {
9+
double celsius;
10+
double fahrenheit;
11+
Scanner in = new Scanner(System.in);
12+
System.out.print("Enter a temperature in Celsius: ");
13+
celsius = in.nextDouble();
14+
in.nextLine();
15+
fahrenheit = celsius * 9;
16+
fahrenheit = fahrenheit / 5;
17+
fahrenheit = fahrenheit + 32;
18+
System.out.printf("%.1f C = %.1f F\n", celsius, fahrenheit);
19+
}
20+
}

ch03/tony/ConvertSeconds.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Convert seconds to hours, minutes, and seconds.
5+
*/
6+
public class ConvertSeconds {
7+
8+
public static void main(String[] args) {
9+
final int SECONDS_PER_HOUR = 3600;
10+
final int SECONDS_PER_MINUTE = 60;
11+
int seconds;
12+
int orig_seconds;
13+
int minutes;
14+
int hours;
15+
Scanner in = new Scanner(System.in);
16+
System.out.print("Enter number of seconds: ");
17+
seconds = in.nextInt();
18+
in.nextLine();
19+
orig_seconds = seconds;
20+
hours = seconds / SECONDS_PER_HOUR;
21+
seconds = seconds % SECONDS_PER_HOUR;
22+
minutes = seconds / SECONDS_PER_MINUTE;
23+
seconds = seconds % SECONDS_PER_MINUTE;
24+
System.out.printf("%d seconds = %d hours, %d minutes, and %d seconds\n", orig_seconds, hours, minutes, seconds);
25+
}
26+
}

ch03/tony/GuessMyNumber.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Random;
2+
import java.util.Scanner;
3+
4+
/**
5+
* User guesses a random number.
6+
*/
7+
public class GuessMyNumber {
8+
9+
public static void main(String[] args) {
10+
int number;
11+
int guess;
12+
13+
Random random = new Random();
14+
number = random.nextInt(100) + 1;
15+
16+
System.out.println("I'm thinking of a number between 1 and 100");
17+
System.out.println("(including both). Can you guess what it is?");
18+
System.out.print("Type a number: ");
19+
Scanner in = new Scanner(System.in);
20+
guess = in.nextInt();
21+
in.nextLine();
22+
23+
System.out.printf("Your guess is: %d\n", guess);
24+
System.out.printf("The number I was thinking of is: %d\n", number);
25+
System.out.printf("You were off by: %d\n", Math.abs(number - guess));
26+
}
27+
}

hw02/tony/Calories.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Calories {
3+
4+
public static void main(String[] args) {
5+
int years = 25;
6+
int feet = 5;
7+
int inches = 10;
8+
int pounds = 160;
9+
int ounces = 3;
10+
double weight = (16 * pounds + ounces) * 0.0283495;
11+
double height = (12 * feet + inches) * 2.54;
12+
double bmr_base = 10 * weight + 6.25 * height - 5 * years;
13+
System.out.print("Male BMR = ");
14+
System.out.print(bmr_base + 5);
15+
System.out.println(" calories/day.");
16+
System.out.print("Female BMR = ");
17+
System.out.print(bmr_base - 161);
18+
System.out.println(" calories/day.");
19+
}
20+
}

0 commit comments

Comments
 (0)