class lab8_1 {
public static void main(String[] args) {
int counter = 0;
Scanner in = new Scanner(System.in);
Random rand = new Random();
int[] randomIntegers = new int[10];
System.out.println("Provide a value: ");
int value = in.nextInt();
for (int i = 0; i < randomIntegers.length; i++) {
randomIntegers[i] = 1+rand.nextInt(20);
if (value == randomIntegers[i]) {
counter++;
}
System.out.println(randomIntegers[i] + "");
}
System.out.println("You have " + counter + " matches");
}
}
class lab8_2 {
public static void main(String[] args) {
int counter = 0;
int temp;
int loc = 0;
Scanner in = new Scanner(System.in);
Random rand = new Random();
int[] randomIntegers = new int[20];
randomIntegers[0] =1+ rand.nextInt(100);
temp = randomIntegers[0];
for (int i = 0; i < 20; i++) {
randomIntegers[i] = 1+rand.nextInt(100);
if (temp > randomIntegers[i]) {
temp = randomIntegers[i];
loc = i;
}
System.out.println(randomIntegers[i] + "");
}
System.out.println("The minimun number is: " + temp + " and is at location: " + loc);
}
}
then it computes
1 4 9 16 9 7 4 9 11
1 – 4 + 9 – 16 + 9 – 7 + 4 – 9 + 11 = –2
class lab8_3 {
public static void main(String[] args) {
System.out.println("Enter the elements of the array: ");
System.out.println("Example: 1 4 9 16 9 7 4 9 11 and press ENTER");
Scanner in = new Scanner(System.in);
String arrayElements = in.nextLine();
String[] nums = arrayElements.split(" ");
int[] intArray = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
intArray[i] = Integer.parseInt(nums[i]);
}
calculateSum(intArray);
}
public static void calculateSum(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) {
sum += array[i];
if (i != 0) {
System.out.print(" + " + array[i]);
} else {
System.out.print(array[i]);
}
} else {
sum -= array[i];
System.out.print(" - " + array[i]);
}
}
System.out.println(" = " + sum);
}
}
Challenge 4:Write a program that determines if an array contains two adjacent duplicate elements.
class lab8_4 {
public static void main(String[] args) {
int [] values ={10,20,15};
if(checkDuplicates(values)==true){
System.out.println("" + checkDuplicates(values));
System.out.println("The array contains two adjacent duplicate elements");
}else {
System.out.println("The array doesnt contain two adjacent duplicate elements");
}
}
public static boolean checkDuplicates(int [] array){
HashSet<Integer> hSet = new HashSet<Integer>();
for( int i = 0; i < array.length; i++ )
{
if( !hSet.add(array[i]) )
return true;
}
return false;
}
}
1 – 4 + 9 – 16 + 9 – 7 + 4 – 9 + 11 = –2
class lab8_3 {
public static void main(String[] args) {
System.out.println("Enter the elements of the array: ");
System.out.println("Example: 1 4 9 16 9 7 4 9 11 and press ENTER");
Scanner in = new Scanner(System.in);
String arrayElements = in.nextLine();
String[] nums = arrayElements.split(" ");
int[] intArray = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
intArray[i] = Integer.parseInt(nums[i]);
}
calculateSum(intArray);
}
public static void calculateSum(int[] array) {
int sum = 0;
for (int i = 0; i < array.length; i++) {
if (i % 2 == 0) {
sum += array[i];
if (i != 0) {
System.out.print(" + " + array[i]);
} else {
System.out.print(array[i]);
}
} else {
sum -= array[i];
System.out.print(" - " + array[i]);
}
}
System.out.println(" = " + sum);
}
}
Challenge 4:Write a program that determines if an array contains two adjacent duplicate elements.
class lab8_4 {
public static void main(String[] args) {
int [] values ={10,20,15};
if(checkDuplicates(values)==true){
System.out.println("" + checkDuplicates(values));
System.out.println("The array contains two adjacent duplicate elements");
}else {
System.out.println("The array doesnt contain two adjacent duplicate elements");
}
}
public static boolean checkDuplicates(int [] array){
HashSet<Integer> hSet = new HashSet<Integer>();
for( int i = 0; i < array.length; i++ )
{
if( !hSet.add(array[i]) )
return true;
}
return false;
}
}
No comments:
Post a Comment