Solution/ArrayLab4.docx
Code:
import java.util.Scanner;
public class ArraysLab4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size;
System.out.print("Enter number of grades: ");
size= sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter elements in array");
for(int i =0;i {
int num = sc.nextInt();
boolean valid = true;
while(valid)
{
if(num >= 1 && num <= 100)
{
arr[i] = num;
valid = false;
}
else
{
System.out.println("Enter in range 1 - 100");
}
}
}
printArray(arr);
System.out.println("\nThe average is "+getAverage(arr)+".");
System.out.println("There are "+getAboveAverage(arr)+" grades above average. ");
System.out.println("There are "+getBelowAverage(arr)+" grades below average. ");
System.out.println("The range is "+getRange(arr)+".");
}
public static void printArray(int arr[])
{
System.out.println("Elements in array");
for(int i =0 ;i
{
System.out.print(arr[i]+" ");
}
}
public static float getAverage(int arr[])
{
float average = 0;
for(int i =0 ;i
{
average = average + arr[i];
}
average = average/(float)arr.length;
return average;
}
public static int getBelowAverage(int arr[])
{
int count = 0;
float average = getAverage(arr);
for(int i =0 ;i
{
if(arr[i]
{
count++;
}
}
return count;
}
public static int getAboveAverage(int arr[])
{
int count = 0;
float average = getAverage(arr);
for(int i =0 ;i
{
if(arr[i]>average)
{
count++;
}
}
return count;
}
public static int getRange(int arr[])
{
int range = 0;
int max = arr[0];
int min= arr[0];
for(int i =0 ;i
{
if(arr[i]>max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
range = max - min;
return range;
}
}
Output:
Solution/ArrayLab4-converted.pdf
Code:
import java.util.Scanner;
public class ArraysLab4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size;
System.out.print("Enter number of grades: ");
size= sc.nextInt();
int arr[] = new int[size];
System.out.println("Enter elements in array");
for(int i =0;i
{
int num = sc.nextInt();
boolean valid = true;
while(valid)
{
if(num >= 1 && num <= 100)
{
arr[i] = num;
valid = false;
}
else
{
System.out.println("Enter in range 1 - 100");
}
}
}
printArray(arr);
System.out.println("\nThe average is "+getAverage(arr)+".");
System.out.println("There are "+getAboveAverage(arr)+" grades above average. ");
System.out.println("There are "+getBelowAverage(arr)+" grades below average. ");
System.out.println("The range is "+getRange(arr)+".");
}
public static void printArray(int arr[])
{
System.out.println("Elements in array");
for(int i =0 ;i
{
System.out.print(arr[i]+" ");
}
}
public static float getAverage(int arr[])
{
float average = 0;
for(int i =0 ;i
{
average = average + arr[i];
}
average = average/(float)arr.length;
return average;
}
public static int getBelowAverage(int arr[])
{
int count = 0;
float average = getAverage(arr);
for(int i =0 ;i
{
if(arr[i]
{
count++;
}
}
return count;
}
public static int getAboveAverage(int arr[])
{
int count = 0;
float average = getAverage(arr);
for(int i =0 ;i
{
if(arr[i]>average)
{
count++;
}
}
return count;
}
public static int getRange(int arr[])
{
int range = 0;
int max = arr[0];
int min= arr[0];
for(int i =0 ;i
{
if(arr[i]>max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}
range = max - min;
return range;
}
}
Output:
Solution/jbcps.docx
Code:
//Jon Bauer
import java.util.Scanner;
public class CPS1231Project1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter word sepeared by space: ");
String line = sc.nextLine();
String[] word = line.split("\\s+");
//part 1
for (int i = 0; i < word.length-1; ++i) {
for (int j = i + 1; j < word.length; ++j) {
if (word[i].compareTo(word[j]) > 0) {
String temp = word[i];
word[i] = word[j];
word[j] = temp;
}
}
}
System.out.print("Part 1: ");
for (int i = 0; i < word.length; i++) {
System.out.print(word[i]+" ");
}
String word2[] = new String[word.length];
for (int i=0; i
word2[i] = word[i];
//part 2
String temp;
for (int i = 0; i < word2.length; ++i) {
if((word2[i].startsWith("a"))||(word2[i].startsWith("A"))||(word2[i].startsWith("e"))||(word2[i].startsWith("E"))||(word2[i].startsWith("i"))||(word2[i].startsWith("I"))||(word2[i].startsWith("o"))||(word2[i].startsWith("O"))||(word2[i].startsWith("u"))||(word2[i].startsWith("U"))){
word2[i]+="yay";
}
else
{
temp=Character.toString(word2[i].charAt(0));
word2[i]=word2[i].substring(1);
word2[i]+=temp;
word2[i]+="ay";
}
}
System.out.print("\nPart 2: ");
for (int i = 0; i < 2; i++) {
System.out.print(word2[i]+" ");
}
//part 3
char[] ch;
char x;
ch= word[1].toCharArray();
x = ch[0];
ch[0] = ch[ch.length - 1];
ch[ch.length - 1] = x;
word[1]= String.valueOf(ch);
ch= word[2].toCharArray();
x = ch[0];
ch[0] = ch[ch.length - 1];
ch[ch.length - 1] = x;
word[2]= String.valueOf(ch);
System.out.print("\nPart 3: "+word[1]+""+word[2]);
//Part 4
String word4 = word[3];
String word5 = word[4];
System.out.println("");
String newWord = word4.substring(0, word4.length()/2 )+word5.substring(word5.length()/2 ,word5.length());
String newWord1 = word5.substring(0, word5.length()/2)+word4.substring(word4.length()/2 ,word4.length());
System.out.println("Part 4: "+newWord+""+newWord1);
}
}
Solution/jbcps-converted.pdf
Code:
//Jon Bauer
import java.util.Scanner;
public class CPS1231Project1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter word sepeared by space: ");
String line = sc.nextLine();
String[]...