CIS 183 Fall 2000 Dr Iren Valova 1

  • Slides: 16
Download presentation
CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter 7; public class Circle

CIS 183, Fall 2000, Dr. Iren Valova, 1 package Chapter 7; public class Circle { private double radius; public Circle(double radius) { radius=radius; } public double get. Radius() { return radius; } public double find. Area() { return radius*Math. PI; } } class Cylinder {private double length; Cylinder(double radius, double length) { Circle(radius); length = length; } public double find. Area() { return find. Area()*length; } }

CIS 183, Fall 2000, Dr. Iren Valova, 2 package Iren. Wrap; import javax. swing.

CIS 183, Fall 2000, Dr. Iren Valova, 2 package Iren. Wrap; import javax. swing. JOption. Pane; public class Parse { public static void main(String[] args) { String one, two; int num 1, num 2, sum; JOption. Pane. show. Message. Dialog(null, "Welcome to this n Program"); one=JOption. Pane. show. Input. Dialog("Enter the first numbern"); num 1=Integer. parse. Int(one); two=JOption. Pane. show. Input. Dialog("Enter the second numbern"); num 2=Integer. parse. Int(two); sum=num 1+num 2; JOption. Pane. show. Message. Dialog(null, "The sum is "+sum, "Results form summation", JOption. Pane. PLAIN_MESSAGE); } System. exit(0); }

CIS 183, Fall 2000, Dr. Iren Valova, 3 • Integer i = new Integer("23");

CIS 183, Fall 2000, Dr. Iren Valova, 3 • Integer i = new Integer("23"); • Integer i = new Integer(23); • Integer i = Integer. parse. Int("23", 8); • Double d = new Double(); • Double d = Double. value. Of("23. 45"); • int i = (Integer. value. Of("23")). int. Value(); • double d = (Double. value. Of("23. 4")). double. Value(); • int i = (Double. value. Of("23. 4")). int. Value(); • String s = (Double. value. Of("23. 4")). to. String();

CIS 183, Fall 2000, Dr. Iren Valova, 4 class Circle { private double radius;

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

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);

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. c. The code has a runtime error. The code is fine. (dependent) Circle c = new Circle (5); Cylinder c = cy; a. b. c. The code has a syntax error. The code has a runtime error. The code is fine.

package Iren. Data; public class Test extends R //class Test extends R { public

package Iren. Data; 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, 7

CIS 183, Fall 2000, Dr. Iren Valova, 8 class Test extends A { public

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 C 1 {}; class C

CIS 183, Fall 2000, Dr. Iren Valova, 9 class C 1 {}; class C 2 extends C 1 {}; class C 3 extends C 1 {}; C 2 c 2 = new C 2(); C 3 c 3 = new C 3(); Analyze the following statement: c 2 = (C 2)((C 1)c 3); a. c 3 is cast into c 2 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 C 1 C 2 C

CIS 183, Fall 2000, Dr. Iren Valova, 10 class C 1 C 2 C 3 C 4 c 1 c 2 c 3 c 4 = = {} extends C 1 { } extends C 2 { } extends C 1 {} new new C 1(); C 2(); C 3(); C 4(); Which of the following expressions evaluates to false? a. c 1 instanceof C 1 b. c 2 instanceof C 1 c. c 3 instanceof C 1 d. c 4 instanceof C 2

CIS 183, Fall 2000, Dr. Iren Valova, 11 Which of the following integer? a.

CIS 183, Fall 2000, Dr. Iren Valova, 11 Which of the following integer? a. b. c. d. statements will not convert a string s into an i i = = Integer. parse. Int(s); (new Integer(s)). int. Value(); Integer. value. Of(s); Which of the following statements will not convert a string s into a double value d? a. d = Double. parse. Double(s); b. d = (new Double(s)). double. Value(); c. d = Double. value. Of(s). double. Value(); d. All of the above.

CIS 183, Fall 2000, Dr. Iren Valova, 12 Which of the following a string

CIS 183, Fall 2000, Dr. Iren Valova, 12 Which of the following a string s? a. b. c. d. e. statements convert a double value d into s = (new Double(d)). to. String(); s = (Double. value. Of(s)). to. String(); s = new Double(d). string. Of(); s = String. string. Of(d); a and b

CIS 183, Fall 2000, Dr. Iren Valova, 13 interface A { void print(); }

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

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.

class Tag{ Tag(int marker){ System. out. println("Tag("+marker+")"); } } class Card{ Tag t 1=new

class Tag{ Tag(int marker){ System. out. println("Tag("+marker+")"); } } class Card{ Tag t 1=new Tag(6); Card(){ System. out. println("Card()"); Tag t 3=new Tag(33); } Tag t 2=new Tag(3); void f(){ System. out. println("f()"); } Tag t 3=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, 15

q. A final class can have instances. CIS 183, Fall 2000, Dr. Iren Valova,

q. A final class can have instances. CIS 183, Fall 2000, Dr. Iren Valova, 16 q. You cannot create an instance of an abstract class using the new operator. q. A final class can be extended. q. An abstract class can be extended. q. You can always successfully cast a subclass to a superclass. q. You can always successfully cast a superclass to a subclass. q. An interface can be a separate unit and can be compiled into a bytecode file. q. The order in which modifiers appear before a class or a method is important. q. Every class has a to. String() method an equals() method. q. The instanceof operator is used to determine whether an object is an instance of a certain class.