Sie sind auf Seite 1von 6

LOVELY PROFESSIONAL UNIVERSITY Assignment Modern Programming Tools & Techniques I (CSE310T)

Submitted to : Submitted by: Mr Arvind Nidhi Narang 10900190

Ques 1. Create a base class with an abstract printle( ) method that is overridden in a derived class. The overridden version of the method prints the value of an int variable defined in the derived class. At the point of definition of this variable, give it a nonzero value. In the base-class constructor, call this method. In main( ), create an object of the derived type, and then call its printle( ) method. Explain the results. Answer-1 package Derived.first; import java.util.Scanner; abstract class Baseclass { Baseclass() { printle(); } abstract void print();

} class Derivedclass extends Baseclass { int i; Derivedclass(int j ) { super(); i=j; } public void printle() { System.out.println("value of i =" +i) ; } } class abc { public static void main(String arg[]) { Derivedclass ob=new Derivedclass(3); } } Baseclass constructor is implicitly invoked in the process of creating a new Derivedclass object. therefor, the value of i=0 within base class constructor

Ques 2. Create a base class with only a non-default constructor, and a derived class with both a default and non-default constructor. In the

derived-class constructors, call the base class constructor. Explain the results with your own example. Ans 2. Answer 2:import java.util.*; class abc{ abc(int i,int j,int k){ System.out.println("i:" +i); System.out.println("j:" +j); System.out.println("k:" +k); } } class ds extends abc { ds() { super(100,200,300); System.out.println("default constructor "); } ds(int k,int l,int s) { super(k,l,s); System.out.println("non default constructor "); } } class abc1 { public static void main(String args[]) { ds o=new ds(5,3,3); } }

Ques 3. Create a class with two methods. Within the first method, call the second method twice: the first time without using this, and the second time using this. Explain the results with your own example. Ans 3. class abc { void abc1() { abc2(2);

This.abc2(3); } void abc2(int l) { System.out.println("return"+l); } public static void main(String args[]) { abc o=new abc(); o.abc(); } } class Foo { new make() {} new makeName(Str name) {} } class Bar : Foo { new make() : super() {} new makeFullName(Str? first, Str last) : super.makeName(last) {} new makeLastName(Str last) : this.makeFullName(null, last) {} } All constructor chains start with the this or super keyword. Use this to chain to one of your own constructors or super to call a parent constructor. Then the constructor to call is specified as a normal method call with the name and argument list. As a shortcut, you can omit the name if the parent constructor being called has the same name.

Ques 4. (a) Is the following class immutable? Give the reason. Class A { private int[] values; public int [] getValues() { return values; }

} Ans 4. (a) Immutable class is a class which once created, its contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class Above given class is immutable because of the following: It is a final class. Values of properties use constructor only. properties of the class are final and private. There are no setters for these properties. It does not have any methods that modify the mutable objects. It does not share references to the mutable objects.

(b) Write a method that finds the number of occurrence of specified character in the string using the following header: public static int count(String str, char a) Ans. import java.util.*; public class Occurrence { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a word: "); String str = input.nextLine(); System.out.print("Enter a letter: "); char a = input.nextChar(); int letterCheck = Occurrence.count(str, a); System.out.println("The word was: " + str); System.out.println("The letter " + a + " was found this many times: " + letterCheck); } public static int count(String str, char a) { int count = 0; for (int i = 0; i < str.length(); i++) {

if (str.charAt(i) == a) { count++; } } return count; } }

Ans 5
public class ISBN { public static void main(String[] args) { int N = new Integer.parseInt(args[0]); int sum = 0; for (int i = 2; i <= 10; i++) { int digit = N % 10; sum = sum + i * digit; N = N / 10; }

// rightmost digit

System.out.print("The full ISBN number is " + args[0]); if (sum % 11 == 1) System.out.println("X"); else if (sum % 11 == 0) System.out.println("0"); else System.out.println(11 - (sum % 11)); } }

Das könnte Ihnen auch gefallen