Worksheet: J2
Worksheets are self-guided activities that reinforce lectures. They are due Thursdays the week they are assigned.
Please submit your answers to the questions as comments in a J2.md markdown file you’ll be writing in this lab. To render your file, create a github repo and upload your file there – it can be viewed in your web browser.
Grading rubric and submission
When you are done, submit your J2.md file to BB.
You will be graded on the following:
| Item | Points |
| Answers are completed (for content) | 100 |
Lab this week
Review the instructions for Lab 1 before you arrive at your lab on Wednesday, so you can make the best use of your limited time there!
Questions
-
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"; -
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"); } } -
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"); } } -
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); } -
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); } -
What principle of OOP does the
privatedeclaration for variable and functions achieve? Explain. -
In the
Pointclass 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(); } } -
For the below questions, when the class
Pointis 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
Pointwith 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.
-
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()); } -
What if we switch line 1 and 2 in the previous question?
-
If we want to override
sum_x_yin our custom point, but first reflect the point before returning the sum, which of the following implementations are valid? (Note: assume thatreflecthas 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.
-
What is the point of the
protectedmodifier? Why do we have it and how does it work in terms of inheritance? -
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
FormulaOneclass which has a make, e.g., Mercedes. Complete the constructor andtoString()method.public class FormulaOne extends Racecar { private String make; //TODO } -
Using the
RacecarandFormulaOneclasses above, if we had a main method in a different class than either of those,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
Aor markBor neither? Explain. -
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?
Include your UML diagram and explanation below in this markdown file.