Homework 1

 

  1. Study the java code in the lesson 1 Basic Java Code link. Try compiling and running the code. Try making some modifications to the code, recompiling and running it.
  2. Explain the term short circuit and write your own code example.

In the evaluation of a Boolean expression consisting of <Boolean expression 1> && <Boolean expression 2>, if <Boolean expression 1 is false, <Boolean expression 2> is not evaluated since the overall expression must be false. Likewise, in the evaluation of <Boolean expression 1> || <Boolean expression 2>, if <Boolean expression 1> is true, <Boolean expression 2> is not evaluated since the overall expression must be true. See the Basic Java Code shortCircuit function for an example.

  1. Write a public class named Person as follows:
    1. The class will have two private member variables: int id; and String name
    2. It will have a public constructor that takes two arguments: an int id and a String name. Assign the parameters to the appropriate member variables.
    3. Override the equals method so that two objects with the same id are equal.
    4. Override the toString method to create a String formatted as the id with a field width of 5 followed by a tab followed by the name.

package quiz1;

 

public class Person {

    private int id;

    private String name;

   

    public Person(int id, String name) {

        this.id = id;

        this.name = name;

    }

   

    public boolean equals(Object other) {

        return ((Person)other).id == id;

    }

   

    public String toString() {

        return String.format("%5d\t%s", id, name);

    }

   

}

  1. Create a 1 dimensional Person array of size 3. Pass the array to a function that assigns a Person object to each element of the array. Pass the array to another function that uses an iterated for loop to write out the contents of the array.

package quiz1;

 

public class Quiz {

   

    public static void assign(Person[] people){

        String[] names = {"Bugs Bunny",

                          "Wilma Flinstone",

                          "Barney Rubble",

                          "Porky Pig"};

       for (int i = 0; i < people.length && i < names.length; i++){

            people[i] = new Person(i, names[i]);

       }                

    }

   

    public static void show(Person[] people) {

        for (Person p : people) {

            System.out.println(p);

        }

    }

   

    public static void main(String[] args) {

        Person[] people = new Person[3];

        assign(people);

        show(people);

    } 

}

  1. Create a 2 dimensional int array of size 2 x 2. Use nested while loops to assign values.

        int[][] array = new int[2][2];

        int i=0, j=0;

        while(i < array.length) {

            while (j < array[i].length) {

                array[i][j++] = array.length * i + j;

            }

            i++;

        }

  1. Use a for loop to calculate the sum of the elements in the following array.

int[] array = {1, 2, 3, 4};

        int[] array = {1, 2, 3, 4};

        int sum = 0;

        for(int k = 0;k < array.length; k++) {

            sum += array[k];

        }

  1. Write a switch statement that takes a char argument. The switch statement should assign a value to a String variable according to the value of the char argument as follows:

char

String

M

Monday

T

Tuesday

W

Wednesday

R

Thursday

F

Friday

anything else

Not a weekday

        String zDay = "";

        char cDay = ' ';

        switch (cDay) {

            case 'M': zDay = "Monday"; break;

            case 'T': zDay = "Tuesday"; break;

            case 'W': zDay = "Wednesday"; break;

            case 'R': zDay = "Thursday"; break;

            case 'F': zDay = "Friday"; break;

            default: zDay = "Not a weekday";

        }

  1. Write an application that reads user input from the command line into a StringBuilder object and then uses the reverse method of the object to write out the input backwards to a file.

package quiz1;

 

import java.io.PrintWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

 

public class FileApp {

   

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

       

        try {

            PrintWriter out = new PrintWriter(new FileWriter("out.txt"), true);

            String str = "";

            do {

                str = in.next();

                StringBuilder builder = new StringBuilder (str);

                out.println(builder.reverse());

            } while (!str.equalsIgnoreCase("quit"));

            out.close();

            in.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

  1. Write code that uses a StringTokenizer object to extract the tokens from Big:boys,don’t;cry using “:,;” as the delimiter.

        String str = "Big:boys,don’t;cry ";

        StringTokenizer tokenizer = new StringTokenizer(str, ":;, ");

        while(tokenizer.hasMoreTokens()) {

            System.out.println(tokenizer.nextToken());

        }

  1. What do each of the following Linux commands do?
    1. ls –l *.java

Display a long listing of file names in the current working directory  ending in .java

    1. ls –ld ~

Display a long listing of just the directory name of your home directory.

    1. pwd

Display the path of your current working directory.

    1. mkdir person

Create a sub directory of your current working directory named person.

    1. diff foo.txt bar.txt

Display the differences between file foo.txt and file bar.txt.

    1. cd person

Change you working directory to the person sub-directory of your current working directory.

    1. ln foo/bar.txt bar/foo.txt

Creates a link in sub-directory bar called foo.txt to the existing file bar.txt in sub-directory foo. This is a hard link, so these two names may actually be considered the same file. If you are interested in the distinction between hard links and soft links (created with the –s option of ln, see Bullet Proof Unix).

    1. cp *.java ../person

Copies all files with names ending in .java to the sibling directory of the current working directory called person.

    1. chmod ug+x *.class

Adds execution permission to the user and group of all files with names ending in .class.