Untitled
unknown
plain_text
2 years ago
6.0 kB
2
Indexable
Never
package labs; import java.util.Arrays; import java.util.Scanner; public class Lab01 { public static int[] nums; public static void problem01() { //declaring an string array that can hold 8 elements String[] words = new String[8]; } public static int[] problem02() { //declaring an int array that can hold 12 elements int[] numbers = {2, 4, 5, 8, 16, 2, -3 ,5, -9, 0, 9, 6}; return numbers; } public static int problem03() { //returning elements to 3 return nums [3]; } public static int problem04() { //ask user for integer Scanner inKey = new Scanner(System.in); System.out.println("Enter an integer: "); //if integer is in index return that number, else return -50 int userInt = inKey.nextInt(); int numsLength = nums.length; if (userInt > numsLength) { return -50; } return nums[userInt]; } public static int problem05() { //Return the last value of the nums array. System.out.println(nums); return nums[nums.length-1]; } public static boolean problem06() { //Return whether there are numbers larger than 10 in the nums array for (int i=0; i< nums.length; i++){ if (nums[i]>10){ return true; } } return false; } //Return the number of negative numbers public static int problem07() { int negative = 0; for (int i=0; i< nums.length; i++){ if (nums[i]<0){ negative ++; } } return negative; } //find smallest number in array public static int problem08() { int smallest = Integer.MAX_VALUE; for (int i=0; i< nums.length; i++){ if (nums[i]<smallest){ smallest=nums[i]; } } return smallest; } //DO NOT mess with this method!! public static void main(String[] args) { populateNums(); //#1 problem01(); //#2 //This will print out the return in a default format System.out.println(Arrays.toString(problem02())); //#3 System.out.println("Index #3 = " + problem03()); //#4 System.out.println("That index = " + problem04()); //#5 System.out.println("The last value = " + problem05()); //#6 if (problem06()) { System.out.println("There are numbers larger than 10."); } else { System.out.println("There are not numbers larger than 10."); } //#7 System.out.println("Number of negatives = " + problem07()); //#8 System.out.println("Min = " + problem08()); } //do not change this //it populates nums to a random size with random values public static void populateNums() { nums = new int[(int) (Math.random() * 10 + 5)]; for (int i = 0; i < nums.length; i++) { nums[i] = (int) (Math.random() * 23 - 10); } } } Lab 2 package labs; import java.io.File; import java.util.Arrays; import java.util.Scanner; public class Lab02 { //#1 //instance variables public static String[] color; public static String[] hex; public static boolean[] isLiked; public static void problem02() throws Exception{ //entering the file into the code Scanner inFile = new Scanner(new File("src/labs/input.txt")); int numElements = inFile.nextInt(); //arrays to use color = new String[numElements]; hex = new String[numElements]; isLiked = new boolean[numElements]; //inputing the arrays from the file for (int i = 0; i < numElements; i++) { color[i] = inFile.next(); } for (int i = 0; i < numElements; i++) { hex[i] = inFile.next(); } for (int i = 0; i < numElements; i++) { isLiked[i] = inFile.nextBoolean(); } } public static void problem03() { //user enters a int Scanner inKey = new Scanner(System.in); System.out.println("Enter an integer: "); int userInt = inKey.nextInt(); //prints the color of the unt, hex code, and if the editor likes it System.out.println(color[userInt]); System.out.println(hex[userInt]); System.out.println(isLiked[userInt]); } public static String problem04() { //prints the colors the editor likes String editorPrefer = ""; boolean isBoolFirst=true; for (int i = 0; i < isLiked.length; i++) { if (isLiked[i] == true) { if (isBoolFirst ) { editorPrefer += color[i]; isBoolFirst = false; } else{ editorPrefer += ", " + color[i]; } } } return editorPrefer; } public static String problem05() { //user enters a color Scanner inKey = new Scanner(System.in); System.out.println("Enter an color: "); String hexTrue= ""; String userColor = inKey.nextLine(); boolean colorTrue=false; //checks if color is on list, if it is says the hex code for (int i = 0; i < color.length; i++) { if (userColor.equalsIgnoreCase(color[i])) { hexTrue=hex[i]; colorTrue=true; return hexTrue.substring(1, hexTrue.length()); } } //if color is not on list prints the following - for (int i = 0; i < color.length; i++) { if (!userColor.equalsIgnoreCase(color[i])) { return ("That color is not on the list."); } } return hexTrue.substring(1, hexTrue.length()); } public static int problem06() { //says how many colors the editor likes int editorPrefer = 0; for (int i = 0; i < isLiked.length; i++) { if (isLiked[i] == true) { editorPrefer++; } } return editorPrefer; } public static String problem07() { //new editor likes opposite of other editor, tells all the colors the editor likes String editorPrefer = ""; boolean isBoolFirst=true; for (int i = 0; i < isLiked.length; i++) { if (isLiked[i] == false) { if (isBoolFirst ) { editorPrefer += color[i]; isBoolFirst = false; } else{ editorPrefer += ", " + color[i]; } } } return editorPrefer; } public static void main(String[] args) throws Exception{ //#2 problem02(); //#3 problem03(); //#4 System.out.println(problem04()); //#5 System.out.println(problem05()); //#6 System.out.println("They like " + problem06() + " colors."); //#7 System.out.println(problem07()); } }