-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDsipNum.java
More file actions
180 lines (160 loc) · 6.58 KB
/
DsipNum.java
File metadata and controls
180 lines (160 loc) · 6.58 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dsip;
/**
*
* @author miryn
*/
public class DsipNum {
public static int readNum() throws Exception
{
int number = 0;
int digitNumber = 0;
char digitAscii = '0';
digitAscii = (char)System.in.read();
while ( digitAscii != '\n')
{
digitNumber = digitAscii - '0';
number = number * 10 + digitNumber;
digitAscii = (char)System.in.read();
}
return number;
}
public static boolean Primes (int num) //Finds if a number is prime
{
int counter = 2;
boolean result = true;
while ( num > counter && result == true)
{
if ( num%counter == 0)
result = false;
counter++;
}
return result;
}
public static boolean Squares (int num) //Finds if a number is a perfect square
{
int counter = 1;
boolean result = false;
while ( num > counter && result == false)
{
if ( num == counter * counter)
result = true;
else
counter++;
}
return result;
}
public static boolean Digits (int num, int lastDigit) //Finds if a number ends on a certain number
{
boolean result;
int remainder = num%10;
if ( remainder == lastDigit)
result = true;
else
result = false;
return result;
}
public static boolean Factors ( int num, int factorAmount) //Finds a if a number has a certain amount of factors
{
int count = 1;
int counter = 0;
boolean result = false;
while ( num > count && result == false)
{
if ( counter == factorAmount )
result = true;
else if ( num%count == 0 )
{
counter++;
count++;
}
else
count++;
}
return result;
}
/*public static String Choices () throws Exception //This is an unused subprogram that can combine characters into strings, maybe we can use this later?
{
String combined = "";
char contained = 0;
contained = (char)System.in.read();
while ( contained != '\n' )
{
combined = combined + contained;
contained = (char)System.in.read();
}
return combined;
}
*/
public static void main(String[] args) {
int number = 0;
int count = 0;
boolean isPrime = false;
boolean isSeven = false;
boolean isFive = false;
boolean isSquare;
char userAnswer = 'y';
boolean giveNum = true;
char[] array = new char[]{'E','F','G','H','Z'}; //Arrays are just easier to work with :(
char choice = 0;
while ( userAnswer == 'y')
{
try
{
System.out.println("Please enter a number");
number = readNum();
count = number - 1;
System.out.println("What do you want to do?\n(A:Find a last digit of 7\nB:Find all prime numbers\nC:Find all numbers with 5 factors\nD:Find all square numbers");
//choice = Choices();
choice = (char)System.in.read();
while (choice != '\n') //@ This part of the code can probably be translated into a subprogram at some point. However I do not understand how outputs will work with subprograms and if I could possibly input an array either.
{
if (choice == 'A')
array[0] = 'A';
else if (choice == 'B')
array[1] = 'B';
else if (choice == 'C')
array[2] = 'C';
else if (choice == 'D')
array[3] = 'D';
choice = (char)System.in.read();
}
while ( count > 0)
{
isSeven = Digits(count, 7); //Still need to find a more efficient method. CHECK if and only if user choice demands it
isPrime = Primes(count);
isFive = Factors(count, 5);
isSquare = Squares(count);
//we want to take user choice and allow it to work for any amount of inputs
//so what should happen is if the userinput = something it will
if (array[0] == 'A' && isSeven == false) //using an array that stores a character over another one, we can compare certain characters easier to determine the user's choice.
giveNum = false; //Can we use an if statement inside another if statement as to check if isSeven = false and then to change giveNum to this value
else if (array[1] == 'B' && isPrime == false)
giveNum = false;
else if (array[2] == 'C' && isFive == false)
giveNum = false;
else if (array[3] == 'D' && isSquare == false)
giveNum = false;
//Ultimately this is useless, due to the fact that certain options shouldn't return anything at all. No Prime Number is Square, No square numbers end in seven and so on, ultimately giving them the option was useless,
if (giveNum == true) //This was good practice in arrays and we can use this style of sorting later. see the note titled @
System.out.println(count);
count = count - 1;
giveNum = true;
}
System.out.println("Want to go again?(y/n)");
userAnswer = (char)System.in.read();
System.in.read();
}
catch ( Exception e)
{
System.out.println("Keyboard error");
break;
}
array = new char[]{'E','F','G','H','Z'};
}
}
}