-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleIOMath.java
More file actions
154 lines (140 loc) · 4.65 KB
/
SimpleIOMath.java
File metadata and controls
154 lines (140 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
╭────────────────────────────────────────────────────╮
│ ╵
│ File: SimpleIOMath.java
│ Project: FirstProject
│
│ Created by Everett Wilber on 21/09/24 at 08:56 AM.
│ ╷
╰────────────────────────────────────────────────────╯
*/
import java.util.Scanner;
/**
* <H1>SimpleIOMath</H1>
* For this project, we were tasked with creating a class. When it is run through the main
* method will ask a series of questions. After they are answered, the program will tell the
* user some information related to the information given.
* @version 9.27.2021
* @author 24wilber
*/
public class SimpleIOMath {
/**
* The user's name
*/
private String name;
/**
* fail if this is true
*/
private boolean fail = false;
/**
* The user's age
*/
private int age;
/**
* The user's favorite number
*/
private int favNumber;
/**
* <H1>ErrorForFun</H1>
* <b>This is my extra!</b>
*
* <strike>This exception is called when the {@code smallestPrime()}
* function fails to find and return a prime of a number.</strike>
* this is thrown when there are args in the main method.
*
* @version 9.27.2021
* @author 24wilber
* @see java.lang.Exception
* @see java.lang.Throwable
* @see SimpleIOMath
*/
public class ErrorForFun extends Exception {
@Override
public String getMessage() {
return """
I eat `String[] args` for breakfast! >:O
NOM NOM
""";
}
}
/**
* Ask user questions
*/
public void promptUser() {
Scanner scan = new Scanner(System.in);
// Name
System.out.print("Question 1: What is your name? ");
this.name = scan.nextLine();
// Age
System.out.print("Question 2: How old are you? ");
this.age = scan.nextInt();
// Favorite Number
System.out.print("Question 3: What is your favorite number? ");
this.favNumber = scan.nextInt();
scan.close();
}
/**
* Print info
*/
public void printInfo() throws ErrorForFun {
System.out.println("Your name is: "+name);
System.out.println("Your age is: "+age);
System.out.println("At your next birthday, you will turn "+(age+1)+".");
System.out.println("The first prime factor of "+age+" is: "+this.smallestPrime(age));
System.out.println("Your favorite number is: "+this.favNumber);
System.out.println("Your favorite number squared is: "+((int) Math.pow(this.favNumber, 2)));
}
/**
* Get the smallest prime of a number (num)
* @param num this number's smallest prime will be returned
* @return the {@code num}'s smallest prime
* @throws ErrorForFun Throws ErrorForFun when there are args supplied to the main method.
* @author 24wilber
* @see ErrorForFun
* @see java.lang.Math
*/
private int smallestPrime(int num) throws ErrorForFun {
if (this.fail) {
throw new ErrorForFun();
}
for(int i = 2; i<=num; i++) {
if (num % i == 0) {
return i; // comment this out to see the ErrorForFun
}
}
return num; // comment this out to see the ErrorForFun
// if no prime is returned until now, throw ErrorForFun
}
/**
* init simple io math
*
*/
public SimpleIOMath(String[] args) throws ErrorForFun {
if (args.length != 0) {
this.fail = true;
}
System.out.println("* Sit yourself down, take a seat *\n" +
"* All you gotta do is repeat after me *");
this.promptUser();
System.out.println("""
I'm gonna teach you how to sing it out
Come on, come on, come on
Let me tell you what it's all about
Reading, writing, arithmetic
Are the branches of the learning tree""");
this.printInfo();
}
/**
* The main method
* @author 24wilber
* @param args the stuff you put in the program
*/
public static void main(String[] args) {
try {
SimpleIOMath obj = new SimpleIOMath(args);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("* end of program *");
}
}