Worksheet: J0/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.
- Github Classroom Link: https://classroom.github.com/a/e1tWK6V2
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
-
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";
-
Write a Java program
Q2.java
that is roughly the equivalent of this C code#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int c; printf("Enter an array length greater than 0 and less than or equal to 15:\n"); scanf("%d", &c); if(c < 0 || c > 15) { fprintf(stderr,"ERROR: Invalid length %d\n", c); exit(1); } double * a = calloc(c, sizeof(double)); for(int i = 0; i < c; i++) { if(i > 0) { a[i] = (a[i - 1] + 2) *3.15; }else { a[i] = 10.8; } } char str[1024]; strcpy(str, "{ ");//copy start of string for(int i = 0; i < c; i++) { //format print into cur_str char cur_str[32]; snprintf(cur_str,32,"%d\n", i, (int) a[i]); //concatenate cur_str onto str strcat(str, cur_str); } strcat(str," }"); //results prints a as { x y z ... w } printf("%s\n", str); }
-
Draw a memory diagram of your resulting
Q2.java
program you wrote for above? Be sure to label thestack
and theheap
-
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 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"); }
-
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
private
declaration for variable and functions achieve? Explain. -
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(); } }
-
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); //return the x component 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.
-
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 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 thatreflect
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?
-
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 andtoString()
method that would make this functional?public class FormulaOne extends Racecar { private String make; //TODO }
-
Using the
Racecar
andFormulaOne
classes above, if we had a main methodpublic 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 markB
or neither? Explain. -
Consider the UML diagram exercise 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.