Skip to content

Commit 4ce080e

Browse files
committed
first commit
0 parents  commit 4ce080e

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

GaddisChallenges.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+
public class GaddisChallenges {
4+
5+
private Scanner scan = new Scanner(System.in);
6+
7+
public static void main(String[] args) {
8+
print("Starting Chapter 3 Challenges from the Gaddis book!");
9+
print("Question 1: Roman Numeral Converter");
10+
romanNumeralConverter();
11+
}
12+
13+
private void print(String text) {
14+
System.out.println(text);
15+
}
16+
17+
private void romanNumeralConverter() {
18+
print("Enter a number within the range of 1 through 10");
19+
int userNumber = scan.nextInt();
20+
21+
}
22+
}

MagicDates.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class MagicDates extends GaddisChallenges {
2+
3+
private int userMonth;
4+
private int userDay;
5+
private int userYear;
6+
7+
private int[] numDaysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
8+
public void start() {
9+
print("Ready for some magic? Let's see if you can guess a Magic Date! ");
10+
11+
print("Enter a numerical value for a month: (Example: 1 for January");
12+
int tempMonth = scan.nextInt();
13+
if(tempMonth > 0 && <= 12) {
14+
userMonth = tempMonth;
15+
}
16+
else {
17+
print("You entered a value that is out of range");
18+
}
19+
20+
print("Enter a day in the month: (Example: 15 for the 15th of the month");
21+
tempDay = scan.nextInt();
22+
if (tempDay > 0 && <= numDaysInMonth[userMonth - 1]) {
23+
userDay = tempDay;
24+
}
25+
else {
26+
print("That day is invalid");
27+
}
28+
29+
print("Enter a two-digit year: (Example: 08 for 2008");
30+
tempYear = scan.nextInt();
31+
if (tempYear.toString().length() == 2) {
32+
userYear = tempYear;
33+
}
34+
35+
if (calculateMagic()) {
36+
print("Hooray! You chose a Magic Date! " +
37+
userMonth " * " + userDay + " = " + userYear)
38+
}
39+
40+
}
41+
42+
private calculateMagic() {
43+
return (userMonth * userDay == userYear);
44+
}
45+
}

RomanNumeralConverter.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class RomanNumeralConverter extends GaddisChallenges {
2+
3+
private int userNumber;
4+
private String[] romanNumerals = ["I", "II", "III", "IV", "V",
5+
"VI", "VII", "VIII", "IX", "X"];
6+
7+
private void start() {
8+
print("Enter a number within the range of 1 through 10");
9+
userNumber = scan.nextInt();
10+
if (userNumber >= 1 && userNumber <= 10) {
11+
print("The Roman Numeral for " + userNumber
12+
+ " is " + convert(userNumber));
13+
}
14+
else {
15+
print("Error: you chose a number that is out of range")
16+
}
17+
}
18+
19+
private String convert(int number) {
20+
return romanNumerals[number-1];
21+
}
22+
23+
}

0 commit comments

Comments
 (0)