Array-2 > centeredAverage
public int centeredAverage(int[] nums) {
int min = nums[0]; // initial variables to hold min/max/total values
int max = nums[0];
int total = 0;
for (int i = 0; i < nums.length; i++) // checks for min/max values
{
min = Math.min(min, nums[i]);
max = Math.max(max, nums[i]);
}
for (int i = 0; i < nums.length; i++) // adds all array values
{
total += nums[i];
}
return (total - min - max)/(nums.length - 2); // subtracts the largest & smallest values
// and divides it by the total numbers minus 2
}
0 notes
Array-2 > bigDiff
public int bigDiff(int[] nums) {
int min = nums[0]; // declare variables to hold values to double check
int max = nums[0];
for (int i = 0; i < nums.length; i++)
{
min = Math.min(min, nums[i]); // checks between current min and value at i
max = Math.max(max, nums[i]);
}
return (max - min); // returns int value of difference
}
0 notes
READ ME
Please check the READ ME section for information regarding CodingBat & coding work.
1 note
·
View note
Array-2 > countEvens
public int countEvens(int[] nums) {
int evenNums = 0;
for (int i = 0; i < nums.length; i++) // cycles through array values
{
if (nums[i] % 2 == 0) // if it is a multiple of 2, it is even
{
evenNums++; // adds 1 to an even number counter
}
}
return evenNums;
}
0 notes
Array-1 > sum2
public int sum2(int[] nums) {
if (nums.length >= 2) // if length is greater than 2, returns sum of first two values
{
return nums[0] + nums[1];
}
else if (nums.length == 1) // if length equals 1, returns first and only value
{
return nums[0];
}
return 0; // else returns 0
}
0 notes
Array-1 > maxEnd3
public int[] maxEnd3(int[] nums) {
if (nums[0] > nums[2]) // sets all to first value
{
int[] newNums = {nums[0], nums[0], nums[0]};
return newNums;
}
else
{
int[] newNums = {nums[2], nums[2], nums[2]}; // sets all to last value
return newNums;
}
}
0 notes
Array-1 > reverse3
public int[] reverse3(int[] nums) {
int[] newNums = {nums[2], nums[1], nums[0]}; // rearranges value into new array
return newNums;
}
0 notes
Array-1 > rotateLeft3
public int[] rotateLeft3(int[] nums) {
int[] newNums = {nums[1], nums[2], nums[0]}; // rearranges value into new array
return newNums;
}
0 notes
Array-1 > sum3
public int sum3(int[] nums) {
return (nums[0] + nums[1] + nums[2]); // returns sum of array values
}
0 notes
Array-1 > commonEnd
public boolean commonEnd(int[] a, int[] b) {
return (a[0] == b[0] || a[a.length - 1] == b[b.length - 1]); // returns true if either first characters or last characters are the same
}
0 notes
Array-1 > makePi
public int[] makePi() {
int[] nums = {3, 1, 4}; // inserts pi values into new array
return nums;
}
0 notes
Array-1 > sameFirstLast
public boolean sameFirstLast(int[] nums) {
return (nums.length >= 1 && nums[0] == nums[nums.length - 1]); // returns true if length is greater than one and the first and last values are equal
}
0 notes
Array-1 > firstLast6
public boolean firstLast6(int[] nums) {
return (nums[0] == 6 || nums[nums.length- 1] == 6); // returns true if first or last value is 6
}
0 notes
P9E
import java.util.Scanner;
import java.io.*;
public class P9E
{
Scanner reader = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
System.out.println(fileContents("sample1.txt", "sober"));
System.out.println(fileContents("sample1.txt", "a sober"));
System.out.println(fileContents("sample2.txt", "a fish"));
}
public static boolean fileContents(String fileName, String word) throws IOException
{
Scanner fileReader = new Scanner(new File(fileName));
String check = "";
while (fileReader.hasNext())
{
check += fileReader.nextLine();
}
if (check.contains(word))
{
return true;
}
return false;
}
}
0 notes
String-2 > countCode
public int countCode(String str) {
int count = 0;
for (int i = 0; i < str.length() - 3; i++)
{
if (str.substring(i, i + 2).equals("co")
&& str.charAt(i + 3) == ('e'))
{
count++;
}
}
return count;
}
0 notes
String2 > catDog
public boolean catDog(String str) {
int catCount = 0;
int dogCount = 0;
for (int i = 0; i < str.length() - 2; i++)
{
if (str.substring(i, i + 3).equals("dog"))
{
dogCount++;
}
if (str.substring(i, i + 3).equals("cat"))
{
catCount++;
}
}
if (catCount == dogCount)
{
return true;
}
return false;
}
0 notes
String-2 > countHi
public int countHi(String str) {
int count = 0;
for (int i = 0; i < str.length() - 1; i++)
{
String testString = str.substring(i, i + 2);
if (testString.equals("hi"))
{
count++;
}
}
return count;
}
0 notes