Reset author name to chosen name ✨
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.exam2_mvn_practice_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Consecutive4Equals {
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
java.util.Scanner input = new java.util.Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.print("Enter the number of values, -1 to quit: ");
|
||||
int size = input.nextInt();
|
||||
if (size == -1) {
|
||||
System.out.println("goodbye!");
|
||||
break;
|
||||
}
|
||||
int[] values = new int[size];
|
||||
System.out.print("Enter the values: ");
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
values[i] = input.nextInt();
|
||||
}
|
||||
if (isConsecutiveFour(values)) {
|
||||
System.out.println("The list has consecutive fours.");
|
||||
} else {
|
||||
System.out.println("The list has no consecutive fours.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isConsecutiveFour(int[] values)
|
||||
{
|
||||
int numOccurance = 0;
|
||||
int iterable = 0;
|
||||
int consecutiveFourDetected = 0;
|
||||
for (int i = 0; i < values.length - 1; i++) {
|
||||
if (values[i] == values[iterable]) {
|
||||
numOccurance++;
|
||||
if (i != 0) {
|
||||
iterable++;
|
||||
}
|
||||
} else {
|
||||
numOccurance = 0;
|
||||
}
|
||||
if (numOccurance >= 4) {
|
||||
consecutiveFourDetected++;
|
||||
}
|
||||
|
||||
}
|
||||
if (consecutiveFourDetected != 0) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
|
||||
*/
|
||||
|
||||
package com.chloefontenot.exam2_mvn_practice_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Exam2_mvn_Practice_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.exam2_mvn_practice_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class PrintArrayReversedEven {
|
||||
public static void main(String[] args)
|
||||
{
|
||||
printRE(new int[] {1, 2, 3});
|
||||
printRE(new int[2]);
|
||||
printRE(new int[] {2, 4, 7, 8});
|
||||
}
|
||||
public static void printRE(int[] array) {
|
||||
int arrayLength = 0;
|
||||
for (int i = 0; i <= array.length - 1; i++) {
|
||||
// determine array length.
|
||||
if (array[i] % 2 == 0) {
|
||||
arrayLength++;
|
||||
}
|
||||
}
|
||||
// Now create the return array.
|
||||
int[] returnArray = new int[arrayLength];
|
||||
int arrayI = 0;
|
||||
for (int i = 0; i <= array.length - 1; i++) {
|
||||
if (array[i] % 2 == 0) {
|
||||
// Add even numbers to array.
|
||||
returnArray[arrayI] = array[i];
|
||||
arrayI++;
|
||||
}
|
||||
}
|
||||
// Reverse items in the array.
|
||||
int[] reversedReturnArray = new int[arrayI];
|
||||
int reverseArrayIterable = arrayI - 1;
|
||||
for (int i = 0; i <= returnArray.length - 1; i++) {
|
||||
reversedReturnArray[reverseArrayIterable] = returnArray[i];
|
||||
reverseArrayIterable--;
|
||||
}
|
||||
// Now print the array.
|
||||
for (int i = 0; i <= reversedReturnArray.length - 1; i++) {
|
||||
System.out.print(reversedReturnArray[i] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.chloefontenot.exam2_mvn_practice_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class SortCharsInString {
|
||||
|
||||
public static String sort(String s)
|
||||
{
|
||||
// Read string chars, and sort them
|
||||
char array[] = s.toCharArray();
|
||||
char temp;
|
||||
int i = 0;
|
||||
while (i < array.length) {
|
||||
int j = i + 1;
|
||||
while (j < array.length) {
|
||||
// Compare each char, if one is larger, swap them
|
||||
if (array[j] < array[i]) {
|
||||
temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Rebuild string
|
||||
String rebuiltString = "";
|
||||
for (char charInArray : array) {
|
||||
rebuiltString += charInArray;
|
||||
}
|
||||
return rebuiltString;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
java.util.Scanner input = new java.util.Scanner(System.in);
|
||||
System.out.print("Enter a string: ");
|
||||
String s = input.nextLine();
|
||||
System.out.println("Sorted string is " + sort(s));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user