Week 17

Source Code

import java.util.*;

public class Week17 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 17" + " - " + myName);

        // Menu
        System.out.println("Menu");
        System.out.println("E1 - Example 1");
        System.out.println("Q  - Quit");

        // setup Scanner
        Scanner in = new Scanner(System.in);
        System.out.print("Choice: ");
        String choice = in.nextLine();

        // switch choices
        switch (choice) {
            case "E1":
                System.out.println("Example 1");
                example1();
                break;
            case "E2":
                System.out.println("Example 2");
                example2();
                break;
            case "E3":
                System.out.println("Example 3");
                example3();
                break;
            case "E4":
                System.out.println("Example 4");
                example4();
                break;
            case "E5":
                System.out.println("Example 5");
                example5();
                break;
            case "E6":
                System.out.println("Example 6");
                example6();
                break;
            case "Q":
                System.out.println("Quitting..!");
                break;
        }
    }

    // example1 method
    public static void example1() {
        // declare and initalize an ArrayList of students
        

        // add elements
        

        // print elements
        

        // insert "Abi" at index 0
        

        // set "Devin" at index 3
        

        // print elements
        

    }

    // example2 method
    public static void example2() {
        // declare and initalize an ArrayList of numbers
        

        // add elements
        

        // get elements
        

    }
    // example3 method
    public static void example3() {
        // Create an ArrayList of top colleges
        ArrayList<String> colleges = new ArrayList<>();

        // Add top 10 colleges to the list
        colleges.add("Massachusetts Institute of Technology (MIT)");
        colleges.add("Stanford University");
        colleges.add("Harvard University");
        colleges.add("California Institute of Technology (Caltech)");
        colleges.add("University of Chicago");
        colleges.add("Princeton University");
        colleges.add("Yale University");
        colleges.add("Columbia University");
        colleges.add("University of Pennsylvania");
        colleges.add("Duke University");

        // for loop to traverse the ArrayList
        
        
    }

    // example4 method
    public static void example4() {
        // Create an ArrayList of words
        ArrayList<String> words = new ArrayList<>(Arrays.asList("Java", "is", "awesome"));

        // replace the element at index 1 (second element)
        

        
    }

    // example4 method
    public static void example5() {
        // Create an ArrayList of top global universities
        ArrayList<String> universities = new ArrayList<>();

        // Add top 10 universities globally
        universities.add("Massachusetts Institute of Technology (MIT) - USA");
        universities.add("Stanford University - USA");
        universities.add("Harvard University - USA");
        universities.add("University of Cambridge - UK");
        universities.add("University of Oxford - UK");
        universities.add("California Institute of Technology (Caltech) - USA");
        universities.add("Imperial College London - UK");
        universities.add("ETH Zurich - Switzerland");
        universities.add("University College London (UCL) - UK");
        universities.add("University of Chicago - USA");

        // Remove by index
        

        // Remove by value
        

        // Print the list of top global universities
        

    }

    // example6 method
    public static void example6() {
        // Create an ArrayList of numbers
        ArrayList<Integer> numArray = new ArrayList<>();
        numArray.add(10);
        numArray.add(-5);
        numArray.add(20);
        numArray.add(-15);
        numArray.add(30);

        // while loop
        

        // while loop and deleting
        

        // enhanced for loop
        

    }

}

W17Problem1.java

import java.util.*;

public class W17Problem1
{
    public static void main(String[] args)
    {
        System.out.println("Problem 1");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem2.java

import java.util.*;

public class W17Problem2
{
    public static void main(String[] args)
    {
        System.out.println("Problem 2");
        Scanner in = new Scanner(System.in);
        
        
    }
}

ReverseDigitsDigits.java

import java.util.*;

public class ReverseDigits
{
    /** A list of digits */
    private ArrayList<Integer> digitList;

    /** Constructs a list of digits from the given number */
    public ReverseDigits(int number)
    {
        // initialize digitList to an empty ArrayList of Integers
        digitList = new ArrayList<>();

        // Special case for number 0


        // Use a while loop to add each digit in number to digitList

            // Extract the last digit

            // Remove the last digit


        // Reverse the digits to maintain original order
        Collections.reverse(digitList);
    }

    /** Returns the string representation of the digits list */
    public String toString()
    {
        return digitList.toString();
    }

    public static void main(String[] args)
    {
        ReverseDigits d1 = new ReverseDigits(154);
        System.out.println(d1); // Output: [1, 5, 4]
    }
}

W17Problem3.java

import java.util.*;

public class W17Problem3
{
    public static void main(String[] args)
    {
        System.out.println("Problem 3");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem4.java

import java.util.*;

public class W17Problem4
{
    public static void main(String[] args)
    {
        System.out.println("Problem 4");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem5.java

import java.util.*;

public class W17Problem5
{
    public static void main(String[] args)
    {
        System.out.println("Problem 5");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem6.java

import java.util.*;

public class W17Problem6
{
    public static void main(String[] args)
    {
        System.out.println("Problem 6");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem7.java

import java.util.*;

public class W17Problem7
{
    public static void main(String[] args)
    {
        System.out.println("Problem 7");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem8.java

import java.util.*;

public class W17Problem8
{
    public static void main(String[] args)
    {
        System.out.println("Problem 8");
        Scanner in = new Scanner(System.in);
        
        
    }
}

W17Problem9.java

import java.util.*;

public class W17Problem9
{
    public static void main(String[] args)
    {
        System.out.println("Problem 9");
        
        // Rewrite this code to use an ArrayList instead of an array
        String[] toDoList = new String[3];
        toDoList[0] = "Do homework";
        toDoList[1] = "Help make dinner";
        toDoList[2] = "Call grandma";

        // changing element 1
        toDoList[1] = "Order pizza";

        System.out.println(toDoList.length + " things to do!");
        System.out.println("Here's the first thing to do: " + toDoList[0]);

        // remove item 0 and move everything down
        //  (this can be done in one method call with ArrayList)
        toDoList[0] = toDoList[1];
        toDoList[1] = toDoList[2];
        toDoList[2] = "";

        System.out.println("Here's the next thing to do: " + toDoList[0]);
        
        
    }
}

Last updated

Was this helpful?