1. Exercise 19 : double Exercise 20 : String CS151 Principles of Computer Science I Fall 2018 Homework 6 S Exercise 21 : foo1 is a class method since it is declared static Exercise 22 : foo2 is an instance method since it is not declared static Exercise 23 : double ans = Airplane.foo1("O Hare"); Exercise 24 : String s = a1.foo2( C ); Exercise 25 : public static void foo3(double d). It s possible that foo3 might have a return value that is ignored by the example code, but it s more likely that it s return type is void. Exercise 26 : public int foo4(string s) 2. The following is one possible implementation of the TelevisionChannel class. To save space I ve listed some methods next to each other public class Television private String name; private int number; private boolean cable; // constructors public Television() public Television(String n, int num, boolean c) name = ""; name = n; number = 0; setnumber(num); cable = false; cable = c; // accessors public String getname() public int getnumber() return name; return number; public boolean getcable() return cable; // mutators public void setname(string n) public void setnumber(string num) name = n; if (num <= 0) number = 0; public void setcable(boolean val) number = num; cable = val;
public String tostring() String ans = name + ":" + number; if (cable) ans += "(C)"; return ans; public boolean equals(televisionchannel other) if (!name.equals(other.name)) return false; if (number!= other.number) return false; return (cable == other.cable); public int numdigits() return (number+"").length(); public String channeltype() if (cable) return "cable"; return "network"; 3. Below is one possible solution. To save space all of my header comments have been removed save for one or two to show you what I expect. public class Circle private double xc, yc, radius; // constructors public Circle(double centerx, double centery, double r) xc = centerx; yc = centery; setradius(r); // accessors public double getxcenter() public double getycenter() return xc; return yc;
public double getradius() return radius; // mutators public void setxcenter(double newx) public void setycenter(double newy) xc = newx; yc = newy; public void setradius(double r) if (r < 0.0) radius = 0.0; radius = r; public boolean equals(circle c) / equals - determine if two circles are identical Circle c - the circle to compare this circle to return value: true if c has the same radius and center as this circle false otherwise / return (xc == c.xc && yc == c.yc && radius == c.radius); public String tostring() return "(" + xc + "," + yc + ") r=" + radius; public boolean inside(double x, double y) / inside - determine whether a given point lies inside this circle double x, y - coordinates of the point return value: true if point (x,y) lies within this circle false otherwise / double dx = xc -x; double dy = yc -y; return Math.sqrt(dxdx + dydy) <= radius;
public int compareto(circle c) / compareto - compares the sizes of two circles Circle c - the circle to compare this circle to return value: -1, if this circle is smaller than c 0, if this circle is same size as c 1, if this circle is larger than c / if (radius < c.radius) return -1; if (radius > c.radius) return 1; return 0; public boolean intersect(circle c) double dx = xc -c.xc; double dy = yc -c.yc; return Math.sqrt(dxdx + dydy) <= radius + c.radius; public static void main(string[] args) Circle c1 = new Circle(0,0,5); Circle c2 = new Circle(3, 2, 10); Circle c3 = new Circle(0,0,5); System.out.println("x coord = " + c1.getxcenter()); System.out.println("y coord = " + c1.getycenter()); System.out.println("radius = " + c1.getradius()); c1.setxcenter(3); c1.setycenter(2); c1.setradius(-10); c1.setradius(10); if (c1.equals(c2)) System.out.println("Circle " + c1 + " and circle " + c2 + " are the same"); System.out.println("Circle " + c1 + " and circle " + c2 + " are the different");
if (c1.equals(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " are the same"); System.out.println("Circle " + c1 + " and circle " + c3 + " are the different"); if (c1.inside(5.0, 5.0)) System.out.println("point (5,5) inside circle " + c1); System.out.println("point (5,5) not inside circle " + c1); if (c3.inside(5.0, 5.0)) System.out.println("point (5,5) inside circle " + c3); System.out.println("point (5,5) not inside circle " + c3); System.out.println("output of c1.compareto(c2) = " + c1.compareto(c2)); System.out.println("output of c1.compareto(c3) = " + c1.compareto(c3)); System.out.println("output of c3.compareto(c2) = " + c3.compareto(c2)); if (c1.intersect(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " intersect"); System.out.println("Circle " + c1 + " and circle " + c3 + " do not intersect"); c3 = new Circle(50, 50, 4); if (c1.intersect(c3)) System.out.println("Circle " + c1 + " and circle " + c3 + " intersect"); System.out.println("Circle " + c1 + " and circle " + c3 + " do not intersect");