-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEtest2.java
More file actions
65 lines (51 loc) · 1.57 KB
/
Etest2.java
File metadata and controls
65 lines (51 loc) · 1.57 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
import java.util.Scanner;
public class eTest2 {
public static void main(String[] args) {
int[] numbers = null;
boolean finished = false;
int[] testArray = null;
do{
Scanner input = new Scanner( System.in );
System.out.println("Enter an interger to add to the array (or 'quit' to finish): ");
if (input.hasNextInt()){
int[] newNumbers = new int[(numbers==null)?1:numbers.length+1];
if (numbers!= null)
System.arraycopy( numbers, 0, newNumbers, 0, numbers.length );
newNumbers[newNumbers.length-1] = input.nextInt();
numbers = newNumbers;
System.out.println("The minimum value is: "+minimumValue(numbers)+
", and the values are " +(isPalindromic(numbers) ?"": "not ") + "palindromic." );
}
else if(input.hasNext("quit")){
finished = true;
}
else{
System.out.println("Invalid input");
}
}while(!finished);
}
public static int minimumValue(int[] array){
int noValuesInArray = 0;
boolean isEmpty = false;
if(array == null){
isEmpty = true;
}
int minimumValue = array[0];
for(int index = 0; index < array.length;index ++)
if(array[index]< minimumValue)
minimumValue = array[index];
return isEmpty ? noValuesInArray : minimumValue;
}
public static boolean isPalindromic(int[] array){
int firstIndex = 0;
int lastIndex = array.length-1;
boolean isPalindromic = true;
while(lastIndex>firstIndex ){
if(array[firstIndex] != array[lastIndex])
isPalindromic = false;
firstIndex++;
lastIndex--;
}
return isPalindromic;
}
}