CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter7; public class Circle { private double radius; public Circle(double radius) { radius=radius; } public double getRadius() { return radius; } public double findArea() { return radius*radius*Math.PI; } } class Cylinder {private double length; Cylinder(double radius, double length) { Circle(radius); length = length; } public double findArea() { return findArea()*length; }
CIS 183, Fall 2000, Dr. Iren Valova, 2 package IrenWrap; import javax.swing.JOptionPane; public class Parse { public static void main(String[] args) { String one,two; int num1,num2,sum; JOptionPane.showMessageDialog(null,"Welcome to this \n Program"); one=JOptionPane.showInputDialog("Enter the first number\n"); num1=Integer.parseInt(one); two=JOptionPane.showInputDialog("Enter the second number\n"); num2=Integer.parseInt(two); sum=num1+num2; JOptionPane.showMessageDialog(null,"The sum is "+sum,"Results form summation", JOptionPane.PLAIN_MESSAGE); System.exit(0); }
CIS 183, Fall 2000, Dr. Iren Valova, 3 Integer i = new Integer("23"); Integer i = new Integer(23); Integer i = Integer.parseInt("23",8); Double d = new Double(); Double d = Double.valueOf("23.45"); int i = (Integer.valueOf("23")).intValue(); double d = (Double.valueOf("23.4")).doubleValue(); int i = (Double.valueOf("23.4")).intValue(); String s = (Double.valueOf("23.4")).toString();
CIS 183, Fall 2000, Dr. Iren Valova, 4 class Circle { private double radius; public Circle(double radius) { radius = radius; } a.The program will not compile because it does not have a main method. b.The program will compile, but you cannot create an object of Circle with specified radius. c.The program has a compilation error because you cannot assign radius to radius.
CIS 183, Fall 2000, Dr. Iren Valova, 5 class Cylinder extends Circle { double length = 1; Cylinder(double radius) { Circle(radius); } a.The program compiles fine, but you cannot create an instance of Cylinder because the constructor does not specify the length of the cylinder. b.The program has a syntax error because you attempted to invoke the Circle class's constructor illegally. c.The program compiles fine, but it has a runtime error because of invoking the Circle class's constructor illegally.
CIS 183, Fall 2000, Dr. Iren Valova, 6 Cylinder cy = new Cylinder(1,1); Circle c = cy; a.The code has a syntax error. (not dependent) b.The code has a runtime error. c.The code is fine. (dependent) Circle c = new Circle (5); Cylinder c = cy; a.The code has a syntax error. b.The code has a runtime error. c.The code is fine.
CIS 183, Fall 2000, Dr. Iren Valova, 7 package IrenData; public class Test extends R //class Test extends R { public static void main(String[] args) {Test t=new Test(); //R k=new R("Hello"); //k.print(); t.print(); }} class R { public String s; /* public R(String s) { this.s=s; } */ public R() { this.s=s; } public void print() { System.out.println(s); }}
CIS 183, Fall 2000, Dr. Iren Valova, 8 class Test extends A { public static void main(String[] args) {Test t = new Test(); t.print();} } class A { String s; A(String s) { this.s = s; } public void print() { System.out.println(s); } a.The program does not compile because Test does not have a constructor Test(). b.The program would compile if the constructor in the class A were removed. c.The program compiles, but it has a runtime error due to the conflict on the method name print. d.The program runs just fine.
CIS 183, Fall 2000, Dr. Iren Valova, 9 class C1 {}; class C2 extends C1 {}; class C3 extends C1 {}; C2 c2 = new C2(); C3 c3 = new C3(); Analyze the following statement: c2 = (C2)((C1)c3); a.c3 is cast into c2 successfully. b.You will get a compilation error because you cannot cast objects from sibling classes. c.You will get a runtime error because the Java runtime system cannot perform multiple casting in nested form. d.The statement is correct.
CIS 183, Fall 2000, Dr. Iren Valova, 10 class C1 {} class C2 extends C1 { } class C3 extends C2 { } class C4 extends C1 {} C1 c1 = new C1(); C2 c2 = new C2(); C3 c3 = new C3(); C4 c4 = new C4(); Which of the following expressions evaluates to false? a.c1 instanceof C1 b.c2 instanceof C1 c.c3 instanceof C1 d.c4 instanceof C2
CIS 183, Fall 2000, Dr. Iren Valova, 11 Which of the following statements will not convert a string s into an integer? a.i = Integer.parseInt(s); b.i = (new Integer(s)).intValue(); c.i = Integer.valueOf(s).intValue(); d.i = Integer.valueOf(s); Which of the following statements will not convert a string s into a double value d? a.d = Double.parseDouble(s); b.d = (new Double(s)).doubleValue(); c.d = Double.valueOf(s).doubleValue(); d.All of the above.
CIS 183, Fall 2000, Dr. Iren Valova, 12 Which of the following statements convert a double value d into a string s? a.s = (new Double(d)).toString(); b.s = (Double.valueOf(s)).toString(); c.s = new Double(d).stringOf(); d.s = String.stringOf(d); e.a and b
CIS 183, Fall 2000, Dr. Iren Valova, 13 interface A { void print(); } class C {} class B extends C implements A { public void print() { } } class Test extends Thread { public static void main(String[] args) { B b = new B(); if (b instanceof A) System.out.println("b is an instance of A"); if (b instanceof C) System.out.println("b is an instance of C"); } a.Nothing. b.b is an instance of A. c.b is an instance of C. d.b is an instance of A followed by b is an instance of C.
CIS 183, Fall 2000, Dr. Iren Valova, 14 When you implement a method that is defined in a superclass, you __________ the original method. a. overload b. override c.copy d. call What modifier should you use on a class so that a class in the same package can access it but a class in a different package cannot access it? a.public b.private c.protected d.Use the default modifier. What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it? a.public b.private c.protected d.Use the default modifier.
CIS 183, Fall 2000, Dr. Iren Valova, 15 class Tag{ Tag(int marker){ System.out.println("Tag("+marker+")"); } class Card{ Tag t1=new Tag(6); Card(){ System.out.println("Card()"); Tag t3=new Tag(33); } Tag t2=new Tag(3); void f(){ System.out.println("f()"); } Tag t3=new Tag(9); } public class Order{ public static void main(String[] args) { Card t = new Card(); t.f(); }
CIS 183, Fall 2000, Dr. Iren Valova, 16 A final class can have instances. You cannot create an instance of an abstract class using the new operator. A final class can be extended. An abstract class can be extended. You can always successfully cast a subclass to a superclass. You can always successfully cast a superclass to a subclass. An interface can be a separate unit and can be compiled into a bytecode file. The order in which modifiers appear before a class or a method is important. Every class has a toString() method and an equals() method. The instanceof operator is used to determine whether an object is an instance of a certain class.