130 likes | 140 Views
This program covers various topics in Java, including platform independence, constructors, class methods vs instance methods, and the meaning of public, final, and abstract keywords. It also includes programs for computing functions and drawing graphs.
E N D
Answers to Midterm Exam. • 1. (a) What does the “plateform independence” mean? How is it implemented in Java? (4) • (b) If we have not defined a constructor in a class, how can an instance of the class be constructed? (2) • (c) Specify the difference between class methods and instance methods. (2) • (d) Specify the meaning of the key words “public”, “final” and “abstract” for classes. (2)
2. Write a Java program to compute the following function (20) • h(N) = f(N) + g(N), • where f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) with f(0) = 1, f(1) = 1, and f(2) = 3, and g(N) = g(N-3) *g(N-2) - 5*g(N-1) with g(0) = 1, g(1) = 2, and g(2) = 4. • class Computation { • public static int f(int x) { • if (x== 0 || x==1) return 1; • else if (x==1) return 3; • else return 3*f(x-3) + 4*f(x-2) – 5*f(x-1); • } • public static int g(int y) { • if (x== 0 ) return 1; • else if (x==1) return 2; • else if (x==2) return 4; • else return g(x-3)*g(x-2)– 5*g(x-1); • } • }
public class Computing { • public static void main(String arg[]) { • int a = Factorial.factorial(Integer.parseInt(arg[0])); • System.out.println(Computation.f(a) + Computation.g(a)); • } • }
3. Consider the following class. If we use the statement: (15) • A e = new A("gggggg");' to create an object of this class, what is the value of e.d? Explain your answer. • class A { • String a; • double b, c, d; • public A(String a, double b, double c, double d) { • this.a = a; this.b = b; this.c = c; this.d = d; • } • public A(String a, double b, double c) { • this(a, b, c, 1.0); • } • public A(String a, double b) { • this(a, b, 2.0); • } • public A(String a) { • this(a, 3.0); • } • } • e.d = 1.0
4. Analyze the following two programs and show the outputs of the programs, respectively. • Program I: (10) • class Note { • int value; • Note(int val) {value = val;} • public static final Note • middle_c = new Note(0), • c_sharp = new Note(1), • b_flat = new Note(2); • } • class Instrument { • public void play(Note n) { • System.out.println(“Instrument.play()”); • }}
class Wind extends Instrument { • public void play(Note n) { • System.out.println(“Wind.play()” + “ “ + n.value); • } • public static void tune(Instrument i) { • i.play(Note.middle_c); • } • public static void main(String[] args) { • Wind flute = new Wind(); • tune(flute); • }}
Program II: (10) • class Shape { • void draw() {} • void erase() {} • } • class Circle extends Shape { • void draw() { • System.out.println(“Circle.draw()”); • } • void erase() {System.out.println(“Circle.erase()”);}} • class Square extends Shape { • void draw() { • System.out.println(“Square.draw()”); • } • void erase() {System.out.println(“Square.erase()”);}}
class Triangle extends Shape { • void draw() { • System.out.println(“Triangle.draw()”); • } • void erase() {System.out.println(“Triangle.erase()”);}} • public class Shapes { • public static Shape randShape(int j) { • switch(j) { • case 0: return new Circle(); • case 1: return new Square(); • case 2: return new Triangle(); • default : return new Circle();}} • public static void main(String[] args) { • Shape[] s = new Shape[3]; • for (int i = 0; i < s.length; i++) • s[i] = randShape(i); • for (int i = 0; i < s.length; i++) s[i].draw();}}
4. Program I: Wind.play() 0 Program II: Circle.draw() Square.draw() Triangle.draw()
Fig. 1 5. Write a Java program which draws a graph shown in Fig. 1 on an applet. (15)
5. import java.applet.*; import java.awt.*; class DrawGraph extends Applet { public paint(Graphics g) { int x[] = {50, 25, 25, 75, 75}; int y[] = {50, 75, 100, 100, 75}; int k = x.length; g.drwaPolygon(x, y, k); g.drawLine(50, 50, 25, 100); g.drawLine(50, 50, 75, 100); } }
6. Write a Java program which computes all non-primes less than 50. (20) • Import java.lang.*; • public class Sieve { • public static void main(String[] args) { • int max = 20; //Assign a default value • try {max = Integer.parseInt(arg[0]);} • catch (Exception e) {} //Silently ignore exceptions. • //Create an array that specifies whether each number is prime or not. • Boolean[] isprime = new boolean[max+1]; • //Assume that all numbers are primes, until proven otherwise. • for (int i = 0; i <= max; i++) isprime[i] = true; • //We know that that 0 and 1 are not prime. Make a note of it. • isprime[0] = isprime[1] = false;
//To compute all primes less than max, we need to rule out multiples of all • //integers less than the square root of max. • int n = (int) Math.ceil(Math.sqrt(max)); • for (int i = 0; i <= n; i++) { • if (isprime[i]) { int k = 2; • for (int j = k*i; j <= max; j = (k ++)*i) • isprime[j] = false; } • } • int j; • for (j = ma0; j < 50; j++); //empty loop body • if (isprime[j] == false) System.out.println(j + “is not a prime} • }