Worksheet: J1 | CS 2113 Software Engineering - Spring 2023

Worksheet: J1

Worksheets are self-guided activities that reinforce lectures. They are not graded for accuracy, only for completion. Worksheets are due by Sunday night before the next lecture.

Create a new repo using all the steps in Lab 0 called yourgitusername-worksheet-J1. Then, add the following file to it:

You may submit a single file in any format; Java (with comments), text, and/or markdown (an .md file). It can be called anything (reasonable).

Note

Attempt to answer these questions before running the code. This will improve your ability to analyize and reason about code without an IDE or compiler. This skill we be helpful on the exams.

Questions

  1. Which of the following is an object and which is a basic type?

    
    int a;
    double b;
    int c[] = {1, 2, 3};
    String s = "Hello World";
    

    Reveal Solution

  2. Write a Java class Q2 that asks the user to enter the size of an array, then fills it with
    multiples of two. Finally, it generates a string representation of the array, i.e. {2, 4, 6, 8, ...}

    Reveal Solution

  3. Two part question:

    (A) What is a static method in Java?

    (B) Why does the main method need to be a static method?

    public class Hello {
        public static void main(String[] args) {
            System.out.println("hello, world");
        }
    }
    

    Reveal Solution

  4. What is the output of the following main method?

    public static void main(String args[]) {
        String str = "Java is my favorite language";
        str += '!';
        System.out.println(str + " and python is my second");
    }
    

    Reveal Solution

  5. What is the output of the following programs?

    /* Program 1 */
    public static void main(final String args[]) {
        String choice = new String("A");
        if (choice == "A") {
            System.out.println("Correct");
        }
        else {
            System.out.println("Wrong");
        }
    }
    
    /* Program 2 */
    public static void main(final String args[]) {
        String choice = new String("A");
        if (choice.equals("A")) {
            System.out.println("Correct");
        }
        else {
            System.out.println("Wrong");
        }
    }
    

    Reveal Solution

  6. Does the below program change the season? Why, or why not?

    static void change_season(String str) {
        str = "Spring";
    }
    
    public static void main(final String args[]) {
        String season = "Winter";
        change_season(season);
        System.out.println("The current season is: " + season);
    }
    

    Reveal Solution

  7. What is the output of the main method below? Please explain.

    public class Point {
        double x = 0;
        double y = 0;
    
        public Point(double x, double y) {
            x = x;
            y = y;
        }
    }
    
    public static void main(final String args[]) {
        Point point = new Point(1, 2);
        System.out.println("X: " + point.x + " Y: " + point.y);
    }
    

    Reveal Solution

  8. What principle of OOP does the private declaration for variable and functions achieve? Explain.

    Reveal Solution

  9. In the Point class below, how does Java choose between the two constructors.

    public class Point {
    
       private double x, y; 
       
       public Point(double x, double y) {
            this.x = x;
            this.y = y;
       }
    
       public Point(Point other) {
           this.x = other.getX();
           this.y = other.getY();
       }
    
    }
    

    Reveal Solution

  10. For the below questions, when the class Point is referenced, we are talking about the below class, which you can assume is fully implemented and working as described:

    public class Point {
       private double x,y; //the x,y fields
       public Point(double x,double y); //construct a point from an x,y
       public Point(Point other); //construct a point from another point
       public double getX(); //return the x component
       public double getY(); //return the y component
       public double setXY(double x, double y); //sets the x and y fields
       public String toString(); //return the string representation
       private double sum_x_y(); // Returns the sum of X and Y
    }
    
    

    Say we want to make a class that extends Point with a method that can reflect a point across the X and Y axis:

    public class CustomPoint extends Point {
        public void reflect(); // Reflects point
    }
    

    Which of the following implementations achieves this?

        // Option 1
        public void reflect() {
            x = -x;
            y = -y;
        }
    
        // Option 2
        public void reflect() {
            this.x = -this.x;
            this.y = -this.y;
        }
    
        // Option 3
        public void reflect() {
            this = Point(-x,-y);
        }
        
        // Option 4
        public void reflect() {
            double x = -this.getX();
            double y =-this.getY();
            this.setXY(x,y);
        }
        
        // Option 5
        public void reflect() {
            x = -this.getX();
            y = -this.getY();
        }
    

    Explain why.

    Reveal Solution

  11. If we add this constructor to CustomPoint:

        public CustomPoint() {
            setXY(10, 10); // Line 1
            super(0, 0); // Line 2
        }
    

    …and then run this program, what is the output?

        public static void main(final String args[]) {
            CustomPoint p = new CustomPoint();
            System.out.println(p.toString());
        }
    

    Reveal Solution

  12. What if we switch line 1 and 2 in the previous question?

    Reveal Solution

  13. If we want to re-implement sum_x_y in our custom point, but first reflect the point before returning the sum, which of the following implementations are valid? (Note: assume that reflect has a valid implementation)

        //Option 1
        public double sum_x_y() {
            this.reflect()
            return super.sum_x_y();
        }
    
        //Option 2
        public double sum_x_y() {
            this.reflect();
            return this.getX() + this.getY();
        }
    
        //Option 3
        public double custom_sum_x_y() {
            this.reflect()
            return super.sum_x_y();
        }
    
        //Option 4
        public double custom_sum_x_y() {
            this.reflect();
            return this.getX() + this.getY();
        }
    
    

    Explain your answer.

    Reveal Solution

  14. Consider the following class

    
    public class Racecar {
    
        private int number; 
        private Driver driver; //assume implemented properly
        protected String sponsor = null;
        public Racecar(int n, Driver d) {
            number = n;
            driver = d;
        }
    
        public String toString() {
            return "Car #" + number + " Driver: " + driver;
        }
        
        protected addSponsor(String sp) {
            sponsor = sp;
        }
    }
    

    Suppose we want to extend this to a FormulaOne class which has a make, e.g., Mercedes, complete the constructor and toString() method that would make this functional?

    
    public class FormulaOne extends Racecar {
        private String make;
    
        //TODO
    }
    

    Reveal Solution

  15. Using the Racecar and FormulaOne classes above, if we had a main method

    
    public static void main(String args[]) {
    
    
       Racecar r = new Racecar(/* ... some args .. */);
       r.addSponsor("Home Depot"); //<--A
    
       FormulaOne f1 = new FormulaOne(/* ... some args .. */);
       f1.addSponsor("Home Depot"); //<--B
         
    }
    

    Does the code work at mark A or mark B or neither? Explain.

    Reveal Solution

  16. Consider the UML diagram from the notes. Expand this to include an intern. An intern is like an employee, has a manager, unit, but has an expiration on their employment. How does this fit into the UML diagram?

    Additionally, come up with one additional type for this company, describe it and add it to the UML diagram.

    Include your UML diagram and explanation with this worksheet (in the file you’re submitting, or as a screenshot/photo/etc).

    Reveal Solution