Skip to content

Commit ffe1919

Browse files
ch03
1 parent 3e6f002 commit ffe1919

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎ch03/CelsiusToFahrenheit.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Converts Celsius to Fahrenheit.
5+
*/
6+
7+
public class CelsiusToFahrenheit {
8+
public static void main(String[] args){
9+
Scanner in = new Scanner(System.in);
10+
11+
//prompt the user and get the value
12+
System.out.print("What is the temperature in Celsius? ");
13+
double celsius = in.nextDouble();
14+
final double COEFFICIENT = 9.0 / 5.0;
15+
final int VALUE_TO_ADD = 32;
16+
17+
//convert and output the result
18+
double fahrenheit = celsius * COEFFICIENT + VALUE_TO_ADD;
19+
System.out.printf("%.1f C = %.1f F", celsius, fahrenheit);
20+
}
21+
22+
}

‎ch03/Convert3.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* This program converts a total number of seconds
5+
* to hours, minutes, and seconds.
6+
*/
7+
8+
public class Convert3 {
9+
10+
public static void main(String[] args){
11+
Scanner in = new Scanner(System.in);
12+
final int SECONDS_PER_MINUTE = 60;
13+
final int MINUTES_PER_HOUR = 60;
14+
int hour = 0, minute = 0;
15+
int second;
16+
17+
//prompt the user and get the value
18+
System.out.print("Exactly how many seconds? ");
19+
int secondInput = in.nextInt();
20+
21+
//convert and output the result
22+
minute += secondInput / SECONDS_PER_MINUTE;
23+
second = secondInput % SECONDS_PER_MINUTE;
24+
hour += minute / MINUTES_PER_HOUR;
25+
minute = minute % MINUTES_PER_HOUR;
26+
System.out.printf("%d seconds = %d hours, %d minutes and %d seconds", secondInput, hour, minute, second);
27+
}
28+
}

‎ch03/GuessStarter.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
import java.util.Random;
2+
import java.util.Scanner;
3+
import static java.lang.Math.abs;
24

35
/**
4-
* Starter code for the "Guess My Number" exercise.
6+
* "Guess My Number" exercise.
57
*/
68
public class GuessStarter {
79

810
public static void main(String[] args) {
11+
Scanner in = new Scanner(System.in);
12+
13+
System.out.println("I'm thinking of a number between 1 and 100");
14+
System.out.println("(including both). Can you guess what it is?");
15+
16+
//prompt the user and get the value
17+
System.out.print("Type a number: ");
18+
int guess = in.nextInt();
19+
System.out.printf("Your guess is: %d\n", guess);
20+
921
// pick a random number
1022
Random random = new Random();
1123
int number = random.nextInt(100) + 1;
24+
System.out.print("The number I was thinking of is: ");
1225
System.out.println(number);
26+
27+
//compute and display the difference between the user's guess and the number that was generated
28+
System.out.printf("You were off by: %d", abs(number - guess));
29+
30+
1331
}
1432

1533
}

0 commit comments

Comments
 (0)