- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework3.java
More file actions
Latest commit
285 lines (198 loc) · 9.59 KB
/
Copy pathHomework3.java
File metadata and controls
285 lines (198 loc) · 9.59 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//Nandar Soe, Homework3
//Creating Scanner
importjava.util.Scanner;
publicclassHomework3 {
/** Main Method */
publicstaticvoidmain(String[] args) {
//Creating new Scanner
Scannerinput = newScanner(System.in);
//Result of Question 1
System.out.print("Please input an integer: ");
intnumber = input.nextInt();
System.out.println(primeFactors(number));
//Results of Question 2
System.out.print("Please write a sentence: ");
Stringsentence = input.next();
sentence += input.nextLine();
System.out.println(avgLength(sentence));
//Results of Question 3
System.out.print("Please input a word: ");
StringcWords = input.next();
cWords += input.nextLine();
System.out.println(countMyCs(cWords));
//Results of Question 4
salesTax();
//Results of Question 5
System.out.print("Please input first integer: ");
intint1 = input.nextInt();
System.out.print("Please input second integer: ");
intint2 = input.nextInt();
printFibonacci(int1, int2);
}
/** Question 1, Prime Factors */
/*This method takes in an integer as an input and stores it in "number" and returns its prime factors*/
publicstaticintprimeFactors(intnumber) {
//Declaration of integer variable "count"
intcount = 0; //Initial count is set to "0"
//This loop checks all the factors of the int "number" starting from 2
for (intfactor = 2; 2 < number; factor++) {
//Loops to check whether the "factor" is a factor of the integer "number"
while (number%factor == 0) {
//If statement used to checks whether the "factor" is prime
//Call in the Method "isPrime(int number)"(Line 86) to check whether the number "factor" is prime
if (isPrime(factor)) {
//If the number "factor" is prime, it excecutes the following commands:
count++; //Increases "count" by 1 if number is prime
//Printing Factors
if (factor < number)
System.out.print(factor + ", "); //Prints the first to second before the last "factor"
else
System.out.print(factor + " "); //Prints the last factor
number = number/factor; //Divides "number" by "factor" and stores in "number"
}
}
}
//Returns the number of prime factors the "number" has
returncount;
}
/**This method is part of Question 1*/
/*This method checks whether the integer "number" is a prime number*/
publicstaticbooleanisPrime(intnumber) {
//"number" is set to prime as default
booleanisPrime = true;
//Loop to check whether the "number" is prime
//"number" is checked with the integer "tester" until "tester" is less than or equal to the sqare root of "number"
for (inttester = 2; tester <= (int)(Math.sqrt(number)) ; tester++) {
//Checks whether the "number" is divisible by any integer less than or equal to the sqare root of "number"
if (number % tester == 0) {
//if "number" is divisible by an integer less than or equal to the square root of "number", the "number" is not a prime number
returnfalse;
}
}
//otherwise returns "number" is a prime number
returnisPrime;
}
/**Question 2, Length of Words*/
/*This method counts the words in the String "s1" and returns the average length of the words*/
publicstaticdoubleavgLength(Strings1) {
//Declaration of Double Variables
doublecount = 0.0; //Variable "count" stores the number of words in the string
doubleblank = 0.0; //Variable "blank" stores the number of spaces in the string
//Loop to check each individual character in the string until the last character in the string
for (inti=0; i < s1.length() ; i++) {
//If statment used to check whether the charater at "i" on the string is a punctuation
//Calls upon the method "isPunctuation(char c1)" (Line 146) to check whether the character at "i" is a punctuation
if (!(isPunctuation(s1.charAt(i)))) {
//If the character at "i" is not a punctuation:
count++; //Word "count" increases by 1
}
//Checks whether the character at "i" is a blank space
if (s1.charAt(i) == ' '){
//If the character at "i" is a blank space:
blank++; //"blank" count increases by 1
}
}
//Computes the average length of the word by dividing word "count" with the number of "blank" spaces
doubleaverageLengthOfWord = count/(blank+1.0); //Then stores the value in the variable "averageLengthOfWord"
//Returns the double value sorted in the variable "averageLengthOfWord"
returnaverageLengthOfWord;
}
/**This method is part of Question 2*/
/*This method checks wheter the character "c1" is a punctuation*/
publicstaticbooleanisPunctuation(charc1) {
//boolean isPunctuation is set to true by default
booleanisPunctuation = true;
//Changes the character into an int value
inti1 = c1;
//Checks whether the character "c1" is between A-Z
if (i1>=65 && i1<=90) {
//if it is between A-Z, isPuntuation is set to false
returnfalse;
}
//Checks whether the character "c1" is between a-z
elseif (i1>=97 && i1<=122) {
//if it is between a-z, isPuntuation is set to false
returnfalse;
}
//Returns "c1" "isPuncation" otherwise
returnisPunctuation;
}
/**Question 3, Counting Cs and Replacing Cs */
/*This method takes the String "s1" as input and returns the number of 'C' & 'c'. Then it replaces the C/c with dashes and prints the new string*/
publicstaticintcountMyCs(Strings1) {
//Declaration of Variables
intcount = 0; //Initial "count" of C/c is set to 0 and stored in the variable "count"
//Loop to check each individual character in the string
for (inti1 = 0; i1<s1.length() ; i1++) {
//Converts the string value at "i1" to char
charc = s1.charAt(i1); //and stores it in the variable "c"
//Checks whether the character at i1 is 'C' or 'c'
if (s1.charAt(i1) != 'c' && s1.charAt(i1)!= 'C')
//If the character at i1 is not 'C' or 'c' execute the following command:
System.out.print(c); //Print the character
else {
//If the character at i1 is 'C' or 'c' execute the following command:
System.out.print("-"); //Print dash "-"
count++; //"count" of C/c increases by 1
}
}
//Returns the number of 'C 'or 'c' in the string "s1"
returncount;
}
/**Question 4, Sales Tax */
/*This method is used to calculate the "subtotal", "sales tax" at 8%, and "total" based on the user input*/
/*This method takes in no input and does not return anything*/
publicstaticvoidsalesTax() {
//Creating Scanner named "input"
Scannerinput = newScanner(System.in);
//Declaration of variables
Stringsprice = "$0.00"; //User string input is stored in the variable "sprice"
doublesubTotal = 0; //Sum of the total prices is stored in the double variable "subTotal", with the initial value set to 0.
doublevalue; //double value of the String sprice is stored in the double variable "value"
doubletax = 0; //Total tax based on the subTotal is stored in the double variable "tax", with the initial value set to 0.
doubletotal = 0; //Sum of "tax" and "subTotal" is stored in the double variable "total" with the initial value set to 0.
//Loop to prompt user to enter the next price until the user enters "X"
while(sprice.charAt(0) != '\u0058') {
System.out.print("Enter price: "); //Prints "Enter price: "
sprice = input.next(); //Takes in user input and stores it in the String variable "sprice"
//If the user input is not "X", excute the following commands:
if (sprice.charAt(0) != '\u0058') {
sprice = sprice.replace("$", ""); //Removes the '$' character from the string "sprice"
value = Double.parseDouble(sprice); //Converts the String "sprice" into a double and stores in the variable "value"
subTotal += value; //Computes the sum of "subTotal" and "value" and stores in the variable "subTotal"
tax = 0.08*subTotal; //Computes the "tax" by multiplying "subTotal" with 8% and stores in the variable "tax"
total = subTotal + tax; //Computes teh total price by the sum of "subTotal" and "tax" and stores in the variable "total"
}
//If the user input is "X", excute the following commands:
else {
//Print the results
System.out.printf("SubTotal : $%6.2f\n", subTotal); //Prints out the total price without tax
System.out.printf("Tax: $%6.2f\n", tax); //Prints out the total tax
System.out.printf("Total: $%6.2f\n", total); //Prints of the total price with tax
}
}
}
/** Question 5, Fibonacci sequence */
/*This method takes in 2 integers "int1" and "int2 and prints the next 10 numbers in a Fibonacci sequence starting with the addition of int1 and int2*/
/*This method does not return anything*/
publicstaticvoidprintFibonacci(intint1, intint2) {
//Loop used to produce the next 10 numbers in the sequence
for (inti = 0 ; i<10 ; i++) {
//Computes the sum of int1 and in2
intsum = int1+int2; //and stores in the variable "sum"
//If it is between 1-9th value of the sequence, excute the following command
if (i<9) {
System.out.print(sum + ", "); //Print the "sum" and a comma sign
}
//It if is on the 10th value of the squence, excute the following command
else {
System.out.print(sum); //Print the "sum"
}
//Changes the literals in the variable "int1" and "int2"
int1 = int2; //value of "int2" is now stored in the variable "int1"
int2 = sum; //value of "sum" is now stored in the variable "int2"
}
//Prints a blank line
System.out.println();
}
}