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/c0INuwMD
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 classes
Letter
andA
below.abstract class Letter { protected boolean uppercase; abstract String get_name(); abstract int get_alphabet_position(); }
class A extends Letter { public String toString() { return "A"; } protected int get_alphabet_position() { return 1; } private String get_name() { return "A"; } }
What is the output of this program?
public class Main { public static void main(final String args[]) { A a = new A(); System.out.println("A: " + a.get_alphabet_position()); } }
-
What is the output of the program above if
A
is implemented as shown belowclass A extends Letter { public String toString() { return "A"; } public int get_alphabet_position() { return 1; } protected String get_name() { return "A"; } }
-
Assume that
A
is implemented such that the following program will compile and run. What is the output of the below program?public class Main { public static void foo(A a) { System.out.println("foo1: " + a.get_name()); } public static void foo(Letter a) { System.out.println("foo2: " + a.get_name()); } public static void main(final String args[]) { Letter a = (Letter) new A(); foo(a); } }