Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- All of chapter 7
- Self check even
- Daniel Lawson 10/4/2016
- 2.
- numbers[0] first element , numbers[9] last element if it is 10 long,numbers[numbers.length-1] last element no matter the length
- 4.
- int max = 38;
- int min = -7;
- int[] odd = new int[Math.abs(max - min+2)/2 ];
- for(int i = 0;i<odd.length;i++){
- odd[i] = min + i*2;
- }
- System.out.println(Arrays.toString(odd));
- 6.
- 0 = 3, 1 = 3, 2 3 4 = 6, 5 = 9 6 7 = -18, the array = [3,3,0,0,6,9,0,-18]
- 8.
- a. int[] a = {17,-3,42,5,9,28};
- 10.
- public static int max(int[] array){
- int max = 0;
- for(int i = 0;i<array.length;i++){
- if(array[i] > max)max = array[i];
- }
- return max;
- }
- 12.
- traversing an arrary is manipulating an array using for loops , an example of this is the max method created in the last problem.
- 14.
- the arrary contains 3, 24, 8, -5, 6, 1
- 16.
- for count change the int[] list into String[] list ,change target to a char, and then have another for loop inside hte for each loop saying for(int i = 0; i < list[n].length();i++) if list[n].charAt(i) = target count++;
- for equals change the type of list1, list 2 to String, also chang ethe if statement in the second forloop to !list1[i].equals(list2[i])
- RAW Paste Data
- Daniel Lawson 10/4/2016
- 2.
- numbers[0] first element , numbers[9] last element if it is 10 long,numbers[numbers.length-1] last element no matter the length
- 4.
- int max = 38;
- int min = -7;
- int[] odd = new int[Math.abs(max - min+2)/2 ];
- for(int i = 0;i<odd.length;i++){
- odd[i] = min + i*2;
- }
- System.out.println(Arrays.toString(odd));
- 6.
- 0 = 3, 1 = 3, 2 3 4 = 6, 5 = 9 6 7 = -18, the array = [3,3,0,0,6,9,0,-18]
- 8.
- a. int[] a = {17,-3,42,5,9,28};
- 10.
- public static int max(int[] array){
- int max = 0;
- for(int i = 0;i<array.length;i++){
- if(array[i] > max)max = array[i];
- }
- return max;
- }
- 12.
- traversing an arrary is manipulating an array using for loops , an example of this is the max method created in the last problem.
- 14.
- the arrary contains 3, 24, 8, -5, 6, 1
- 16.
- for count change the int[] list into String[] list ,change target to a char, and then have another for loop inside hte for each loop saying for(int i = 0; i < list[n].length();i++) if list[n].charAt(i) = target count++;
- for equals change the type of list1, list 2 to String, also chang ethe if statement in the second forloop to !list1[i].equals(list2[i])
- Weather:
- /**
- * Created by danie on 9/30/2016.
- */
- import java.util.*;
- public class weather {
- public static void main(String[] args){
- Scanner s = new Scanner(System.in);
- System.out.print("How many day's high temp? ");
- int[] day = new int[s.nextInt()];
- double average = 0;
- for(int i =0;i<day.length;i++){
- System.out.print("Enter day " + (i+1) +": ");
- day[i] = s.nextInt();
- average+=day[i];
- }
- average /= day.length;
- System.out.printf("Average temp= %.1f\n",average);
- int count = 0;
- for(int i = 0;i<day.length;i++){
- if(day[i] > average){
- count++;
- }
- }
- System.out.println(count + " days were above average");
- System.out.println("Temperatures: " + Arrays.toString(day));
- Arrays.sort(day);
- System.out.println("Two coldest days: " + day[0] + ", " + day[1]);
- System.out.println("Two hottest days: " + day[day.length -1] + ", "+ day[day.length-2]);
- }
- }
- public class mostCommon { //gets the most common number (
- public static void main(String[] args){
- System.out.println(most(655534));
- }
- public static int most(int n){
- int[] howMany = new int[10];
- while(n>0){
- int temp = n % 10;
- howMany[temp]++;
- n /= 10;
- }
- int most = 0;
- int valueOfMost = 0;
- for(int i = 0; i <10;i++){
- if(howMany[i] > most){
- most = howMany[i];
- valueOfMost = i;
- }
- }
- return valueOfMost;
- }
- }
- /**
- * Created by danie on 10/5/2016.
- */
- //Merges multiple arrays
- import java.util.*;
- public class merge {
- public static void main(String[] args){
- int[] food = {1,2,3,4,5};
- int[] ood = {6,7,8,9,10};
- int[] od = {11,12,13,14,15};
- System.out.println(Arrays.toString(merge(food,ood)));
- System.out.println(Arrays.toString(merge3(food,ood,od)));
- }
- public static int[] merge(int[] a1, int[] a2){
- int[] merge = new int[a1.length + a2.length];
- for(int i = 0; i < merge.length;i++){
- if(i < a1.length){
- merge[i] = a1[i];
- }
- else{
- merge[i] = a2[i - a1.length];
- }
- }
- return merge;
- }
- public static int[] merge3(int[] a1, int[] a2, int[] a3){
- return merge(merge(a1,a2),a3);
- }
- }
- //Finds the median of the string
- /**
- * Created by danie on 10/17/2016.
- */
- import java.util.*;
- public class median {
- public static void main(String[] args){
- int[] test = {1,2,4,3,5,6,7,9};
- System.out.println(median(test));//prints 5
- }
- public static int median(int[] a){
- int[] sort = Arrays.copyOf(a,a.length);
- Arrays.sort(sort);
- return sort[sort.length/2];
- }
- }
- //reverses the order of a string
- public class copy {
- public static void main(String args[]){
- int[] values = {1,2,3,4,5,6,45};
- reverseOrder(values);
- System.out.println(Arrays.toString(values));
- }
- public static void reverseOrder(int[] a){
- for(int i = 0;i<a.length/2;i++){
- int temp = a[i];
- a[i] = a[a.length -1 - i];
- a[a.length-1-i] = temp;
- }
- }
- }
- public class attendanceProgram {
- /*
- yynyyynayayynyyyayanyyyaynayyayyanayyyanyayna
- ayyanyyyyayanaayyanayyyananayayaynyayayynynya
- yyayaynyyayyanynnyyyayyanayaynannnyyayyayayny */ // the file to read from
- public static void main(String[] args)throws FileNotFoundException{
- File f = new File ("C:\\Users\\danie\\IdeaProjects\\project7\\src\\textfile.txt");
- Scanner s = new Scanner(f);
- int section = 1;
- while(s.hasNextLine()){
- String line = s.nextLine();
- int[] lineValue = new int[5];
- double[] lineValueAverage = new double[5];
- for(int i = 0;i<9;i++){
- for(int j = 0;j<5;j++){
- if(line.charAt(5*i+j)=='y')lineValue[j]+=3;
- if(line.charAt(5*i+j)=='n')lineValue[j]+=2;
- lineValue[j] = Math.min(20,lineValue[j]);
- }
- }
- for(int i = 0; i < 5; i++) lineValueAverage[i] = lineValue[i] * 5;
- System.out.println("Section " +section);
- System.out.println("Student points: " + Arrays.toString(lineValue));
- System.out.println("Student points: " + Arrays.toString(lineValueAverage));
- System.out.println();
- section++;
- }
- }
- }
- /**
- * Created by danie on 10/17/2016.
- */
- import java.io.*;
- import java.util.*;
- /*
- hello hi asdf y y y 123456 12
- 1234567890 1234567890
- */ // file being read from
- public class wordLengths {
- public static void main(String[] args)throws FileNotFoundException{
- File f = new File("C:\\Users\\danie\\IdeaProjects\\project7\\src\\wordLengths.txt");
- wordLengths(f);
- }
- public static void wordLengths (File f)throws FileNotFoundException{
- Scanner l = new Scanner(f);
- Scanner s = new Scanner(f);
- int length = 0;
- while(l.hasNext()){
- String temp = l.next();
- length = Math.max(temp.length(),length);
- }
- int[] count = new int[length];
- while(s.hasNext()){
- String temp = s.next();
- count[temp.length()-1]++;
- }
- for(int i = 0;i <count.length;i++){
- System.out.printf(" %4d: %4d ",i+1,count[i] );
- for(int j = 0;j<count[i];j++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- }
- // Grocery list with arrays
- /**
- * Created by danie on 9/29/2016.
- */
- public class GroceryList {
- public static final int size = 10;
- private GroceryItemOrder[] total = new GroceryItemOrder[size];
- private int count = 0;
- public GroceryList(){
- for(int i = 0;i<total.length;i++)total[i] = new GroceryItemOrder("null",0,0);
- };
- public void add(GroceryItemOrder item){
- total[count] = item;
- count++;
- }
- public double getTotalCost(){//returns the total cost of all the items
- double sum = 0;
- for(int i = 0;i< total.length; i++)sum+=total[i].getCost();
- return sum;
- }
- }
- /**
- * Created by danie on 9/29/2016.
- */
- public class GroceryItemOrder {
- private String name;
- private int quantity;
- private double pricePerUnit;
- public GroceryItemOrder(String name, int quantity, double pricePerUnit){
- this.name = name;
- this.quantity = quantity;
- this.pricePerUnit = pricePerUnit;
- }
- public double getCost(){
- return quantity * pricePerUnit;
- }
- public void setQuantity(int quantity){
- this.quantity = quantity;
- }
- public void setPricePerUnit(int cost){
- pricePerUnit = cost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment