Version 1 5. (10 Points) What is the output of the following code: int total = 0; int i = 0; while( total < 90 ) { switch( i ) { case 0: total += 30; i = 1; break; case 1: i = 2; total -= 15; case 2: i = 0; total += 15; break; System.out.println(total); 5. (30 Points) The Java class named Song has four private instance variables: title, which is a String representing the title of the song. artist, which is a String representing the performer of the song. length, which is a int representing the length of the song in seconds. composer, which is a String representing the composer of the song. a. (10 Points) Write a contructor for the class that accepts parameters for all intance variables and intantiates an object Song with those values:
Version 1 b. (15 Points) Write the public boolean equals(object obj) method to compare Songs for equality. (You can assume that all instance variable have getters): c. (5 Points) Write the public String tostring() method to return a String containing all the information about a Song: 5. (10 Points) Write a static method named swap that swaps two elements in an array of int values. The method should accept the following parameters: An int named index1 representing the index of one element being swapped. An int named index2 representing the index of the other element being swapped. An int array named array containing the values being swapped. The method should not return anything.
Version 1 5. (40 Points) Write a class named SumDif that extends JFrame and contains the following elements: Two JTextField s (both start with a 0 as their text) Two JButton s (one called Sum the other called Diff ) One JLabel The user should be able to use your GUI to enter two numbers using the JTextField s. If the user presses the Sum button, your GUI should add the two numbers and display the result in the JLabel. If the user presses the Diff button, your GUI should subtract one number from the other and display the result in the JLabel. Please note that you can convert a String s to an int like this: int num = new Integer(s).intValue(); Also, note that if s does not contain a parsable integer, the above Integer constructor would throw a NumberFormatException. Your code should be able to handle this exception without crashing.
Version 1
Version 1 5. (30 Points) Given the following classes, show the output from running the Q4_1 class public class A1 { private String color,name; private int length; public A1(String color, int length) { this.color = color; this.length = length; this.name = "A1"; public void setname(string name) { this.name = name; public int getlength() { return length; public String tostring() { return "A1 [name = " + name + " color = " + color + " length = " + length + "]"; public class B1 extends A1 { private int width; public B1(String color, int length, int width) { super(color,length); this.width = width; super.setname("b1"); public int getarea() { return getlength() * width; public String tostring() { String s = super.tostring(); s = s + "\nb1 [width = " + width + " area = " + getarea() + "]"; return s; public class C1 extends B1 { private int height; public C1(String color, int length, int width, int height) { super(color, length, width); this.height = height; super.setname("c1"); public int getvolume() { return getarea() * height; public String tostring() { String s = super.tostring(); s = s + "\nc1 [height = " + height + " volume = " + getvolume() + "]"; return s; public class Q4_1 { public static void main(string[] args) { A1 a, a1; B1 b, b1; C1 c1; a1 = new A1("Blue",2); System.out.println(a1); b1 = new B1("Green",6,4); System.out.println(b1); c1 = new C1("Grey",3,2,5); System.out.println(c1); a = b1; System.out.println(a); b = c1; System.out.println(b);
Version 1 Page Intentionally Left Blank
Version 2 1. (10 Points) What is the output of the following code: int total = 0; int i = 0; while( total < 60 ) { switch( i ) { case 0: total += 20; i = 1; break; case 1: i = 2; total -= 10; case 2: i = 0; total += 10; break; System.out.println(total); 2. (30 Points) The Java class named Course has four private instance variables: title, which is a String representing the title of the course. instructor, which is a String representing the instructor of the course. Credits, which is a int representing the number of credits of the course. department, which is a String representing the department of the course. a. (10 Points) Write a contructor for the class that accepts parameters for all intance variables and intantiates an object Course with those values:
Version 2 b. (15 Points) Write the public boolean equals(object obj) method to compare Courses for equality. (You can assume that all instance variable have getters): c. (5 Points) Write the public String tostring() method to return a String containing all the information about a Course: 3. (10 Points) Write a static method named swap that swaps two elements in an array of double values. The method should accept the following parameters: An int named index1 representing the index of one element being swapped. An int named index2 representing the index of the other element being swapped. A double array named array containing the values being swapped. The method should not return anything.
Version 2 5. (40 Points) Write a class named MulDiv that extends JFrame and contains the following elements: Two JTextField s (both start with a 0 as their text) Two JButton s (one called Mul the other called Div ) One JLabel The user should be able to use your GUI to enter two numbers using the JTextField s. If the user presses the Mul button, your GUI should multiphy the two numbers and display the result in the JLabel. If the user presses the Div button, your GUI should divide one number by the other and display the result in the JLabel. Please note that you can convert a String s to an int like this: int num = new Integer(s).intValue(); Also, note that if s does not contain a parsable integer, the above Integer constructor would throw a NumberFormatException. Your code should be able to handle this exception without crashing. You should also handle the ArithmeticException that could result when you divide by 0.
Version 2
Version 2 5. (30 Points) Given the following classes, show the output from running the Q4_2 class public class A2 { private String color,name; private int length; public A2(String color, int length) { this.color = color; this.length = length; this.name = "A2"; public void setname(string name) { this.name = name; public int getlength() { return length; public String tostring() { return "A2 [name = " + name + " color = " + color + " length = " + length + "]"; public class B2 extends A2 { private int width; public B2(String color, int length, int width) { super(color,length); this.width = width; super.setname("b2"); public int getarea() { return getlength() * width; public String tostring() { String s = super.tostring(); s = s + "\nb2 [width = " + width + " area = " + getarea() + "]"; return s; public class C2 extends B2 { private int height; public C2(String color, int length, int width, int height) { super(color, length, width); this.height = height; super.setname("c2"); public int getvolume() { return getarea() * height; public String tostring() { String s = super.tostring(); s = s + "\nc2 [height = " + height + " volume = " + getvolume() + "]"; return s; public class Q4_2 { public static void main(string[] args) { A2 a, a1; B2 b, b1; C2 c1; a1 = new A2("Grey", 5); System.out.println(a1); b1 = new B2("Blue", 4, 4); System.out.println(b1); c1 = new C2("Red", 2, 4, 3); System.out.println(c1); a = b1; System.out.println(a); b = c1; System.out.println(b);
Version 2 Page Intentionally Left Blank