Trakteer

Array List

WELCOME TO THE SIDES


Ini adalah tugas kuliah algo lanjut , tugas dengan waktu terparah yang pernah ada. Soal diberikan pukul 9 malam selesai jam 6 pagi. Ini dia soalnya 1 :

Write a program that reads a file and displays
the words of that file as a list.
– Then display them in reverse order.
– Then display them with all plurals (ending in "s") capitalized.
– Then display them with all plural words removed.
–  display with all vocal words removed

Dan ini adalah source codenya :
 import java.util.*; 
   import java.io.*; 

public class tugas2 {
   public static void main(String[] args) throws FileNotFoundException {
     
    Scanner input = new Scanner(new File("c:\\list.txt"));
    input = new Scanner(new File("c:\\list.txt"));
    System.out.println("Display All Word");
    ArrayList<String> allWords = new ArrayList<String>();
    while (input.hasNext()) {
        String word = input.next();
        allWords.add(word);
    }System.out.println(allWords);

    System.out.println("Display them with plural (ending in 's') capitalized");
    ArrayList<String> CapEndS = new ArrayList<String>();
    for (int i = 0; i < allWords.size(); i++){
        String word = allWords.get(i);
    if (word.endsWith("s")) {
        CapEndS.add(allWords.get(i).toUpperCase());
    }else{CapEndS.add(allWords.get(i));
      }
        }System.out.println(CapEndS);


    System.out.println("Display them in reverse order");
    ArrayList<String> ReverseWords = new ArrayList<String>();
    for (int i = allWords.size() - 1; i >= 0; i--){
         ReverseWords.add(allWords.get(i));
    }System.out.println(ReverseWords);
   
   
    System.out.println("Display them eith plural words removed)");
    for (int i = 0; i < allWords.size(); i++) {
        String word = allWords.get(i);
    if (word.endsWith("s")) {
        allWords.remove(i);
                i--;
        }
    }System.out.println(allWords);

    System.out.println("Display vokal words removed");
    ArrayList<String> RemovedWords = new ArrayList<String>();
    for (int i = 0; i < allWords.size(); i++) {
        String word = allWords.get(i);
        RemovedWords.add(word.replaceAll("[a,i,u,e,o]", ""));
       
    }System.out.println(RemovedWords);
       
   }}

  
Soal 2 :
Write a program that reads a file full of numbers anddisplays all the numbers as a list, then:

Prints the average of the numbers.

Prints the highest and lowest number.

Filters out all of the even numbers (ones divisible by 2).



import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Tugas {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        Scanner input = new Scanner(new File("D:\\number.txt"));
        while (input.hasNextInt()) {
            int n = input.nextInt();
            numbers.add(n);
        }
        System.out.println("Number : " + numbers);
        System.out.println("Avg:" + String.valueOf(avg(numbers)));

        //show high lowest
        Collections.sort(numbers);
        System.out.println("Sort lowest : " + String.valueOf(numbers.get(0)));
        Collections.reverse(numbers);
        System.out.println("Sort highst : " + String.valueOf(numbers.get(0)));

        filterEvens(numbers);
        System.out.println("sisa bagi: " + numbers);

    }
   
    //show the average values from the given list
    public static Double avg(ArrayList<Integer> list) {
        int sum = 0;
        for (int i = list.size() - 1; i >= 0; i--) {
            sum += list.get(i);
        }
        return (double) (sum / (list.size()));
    }

    // Removes all elements with even values from the given list.
    public static void filterEvens(ArrayList<Integer> list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            int n = list.get(i);
            if (n % 2 == 0) {
                list.remove(i);
            }
        }
    }
}

Soal 3 :
Write a method reverse  that reverses the order of the  elements in an ArrayList  of strings.
Write a method capitalizePlurals  that accepts an ArrayList  of strings and replaces every word ending with an  "s" with its uppercased version.
Write a method removePlurals  that accepts an ArrayList   of strings and removes every word in the list ending with an  "s", case-insensitively.


import java.util.*;
import java.io.*;
public class arraylistfuck {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("d:\\list.txt"));
        input = new Scanner(new File("d:\\list.txt"));
        System.out.println("Display All Word");

        ArrayList<String> allWords = new ArrayList<String>();
        while (input.hasNext()) {
            String word = input.next();
            allWords.add(word);
        }
        Collections.reverse(allWords);

        System.out.println(allWords);
        System.out.println("Display them with plural (ending in 's')capitalized");
        ArrayList<String> CapEndS = new ArrayList<String>();
        for (int i = 0; i < allWords.size(); i++) {
            String word = allWords.get(i);
            if (word.endsWith("s")) {

                String sString = allWords.get(i);
                sString = Character.toString(sString.charAt(0)).toUpperCase() + sString.substring(1);
                CapEndS.add(sString);
                //CapEndS.add(allWords.get(i).toUpperCase(allWords.get(0).charAt(0)));
            } else {
                CapEndS.add(allWords.get(i));
            }
        }
        System.out.println(CapEndS);
        System.out.println("Display them in reverse order");
        ArrayList<String> ReverseWords = new ArrayList<String>();
        for (int i = allWords.size() - 1; i >= 0; i--) {
            ReverseWords.add(allWords.get(i));
        }
        System.out.println(ReverseWords);
        System.out.println("Display them eith plural words removed)");
        for (int i = 0; i < allWords.size(); i++) {
            String word = allWords.get(i);
            if (word.endsWith("s")) {
   allWords.set(i,allWords.get(i).substring(0,allWords.get(i).length()-1));
            }
        }
        System.out.println(allWords);
        System.out.println("Display vokal words removed");
        ArrayList<String> RemovedWords = new ArrayList<String>();
        for (int i = 0; i < allWords.size(); i++) {
            String word = allWords.get(i);
            RemovedWords.add(word.replaceAll("[a,i,u,e,o]", ""));
        }
        System.out.println(RemovedWords);
    }
}
 
Mohon Aktifkan Javascript!Enable JavaScript