[Max Marks: 30

]

Homework: CAP615T

Section: D1R02

Homework 1

Department of CA
Homework Title/No: 1 Course Code: C P 615T Course Title: JAVA Section No.: rd102 Course Instructor: Proff. Kumar Vishal Student’s Roll Number: RD102A22

Declaration: I declare that this assignment is my individual work. I have not copied from any other student’s work or from any other source except where due acknowledgement is made explicitly in the text, nor has any part been written for me by another person.

Student’s Signature: ADITYA SHARMA

1. Admission of professional course is subject to the following conditions: (a) (b) Marks in math >=60 Marks in physics >=50

(c)Marks in chemistry >=40 (d) Total in all three subjects >=200 or total in math and physics >=150 Given the marks in three subjects, WAP to process the applications to the list of the eligible candidates.

ANS 1.
Import java . util . Scanner ; class Admi ssion { Scanner sca=new Scanner ( System . in ) ; Int m,p,c;

Public Admission (int m , int p , intc) { This . m = m; this . p = p;

this . c = c; } void cal ( ) { Int sum = p + m + c ; if ( m > = 60 && p > = 50 && c > = 40) { If ( sum > =200 || sum - c > =150) { System . out . println ( " STUDENTS are eligible for admission " ) ; } else { System . out . println ( " STUDENTS are not eligible for admission " ) ; } } classAdmin { public static void main ( String [ ] args ) { Scanner sca = new Scanner ( System . in ) ; Int a , b , c ; System . out . println ( " Enter the marks of math " ) ; A = sca . nextInt ( ) ; System . out . println ( " Enter the marks of physics " ) ; B = sca . nextInt ( ) ;

System.out.println("Enter the marks of chemistry"); C = sca. nextInt ( ) ; Admission object=new Admission ( a , b , c ) ; Object . calculate ( ) ; } }

2. Create

base class called Shape, it contain two methods getxyvalue() and showxyvalue() for accepting co-ordinates and to displaying the same. Create a subclass called Rectangle. It also contains a method to display the length and breadth of the rectangle called showxyvalue(). Use the overriding concept.

ANS 2
import java . io . * ; class Shape { Int x , y ; public void getxyvalue ( int x , int y ) { This . x = x ; This . y = y ;

}

public {

void

showxyvalue ( )

System . out . println

( " x Is " + x ) ;

System . out . println ( " y is " + y ) ; } } Class Rectangle extends Shape { Float length , breadth , area ;

public Rectangle ( float l , float b ) { length = l ; breadth = b ; } void cal () { Area = length * breadth ; } Public void showxyvalue ( ) { Super . showxyvalue ( ) ;

System . out . println ( " Area of Rectangle is : " + area ) ;

} Public static void main ( String [ ] args ) throws IOException { Scanner Float l , b ; inti, j; System . out . println ( " Enter t he value of x " ) ; sc = new Scanner ( System . in) ;

I = sc . nextInt ( ) ;

System . out . println ( " Enter the value of y " ) ;

J = sc . nextInt ( ) ; System . out . println ( " Enter the length of Rectangle " ) ;

L = sc . nextInt ( ) ;

System . out . println ( " Enter the breadth of Rectangle " ) ;

B = sc . nextInt ( ) ;

Rectangle r = new Rectangle ( l , b ) ;

R . getxyvalue ( I , j ) ;

R . cal ( ) ;

R . showxyvalue ( ) ; } }

3. What is an ArrayIndexOutOfBounds Exception, exception and how does its use distinguish java from other language such as c and c++.

ANS

We are going to see how we can catch the exception ArrayIndexOutOfBoundException. ArrayIndexOutOfBoundException is thrown when we have to indicate that an array has been accessed with an illegal index.

Suppose we have declared an array of int and the size of the array is 6, that means that this array can store six values. Now suppose if want to access the seventh variable which does not exist, then it will throws the

exception ArrayIndexOutOfBound. It means that there is no other value instead after that we are forcing the array to give the next value.

The code of the program is given below:

ArrayIndexOutOfBoundsException Try { int a[] =new int[6]; for(int i = 0; i<7; i++) {

a[i]=i;

} } catch(Exception e) { out.println("Exception:"+e); }

(continued………) Comparison of Exception Handling in C++ and Java

Both languages use try, catch and throw keywords for exception handling, and meaning of try, catch and free blocks is also same in both languages. Following are the differences between Java and C++ exception handling.

1) In C++, all types (including primitive and pointer) can be thrown as exception. But in Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. For example, following type of code works in C++, but similar code doesn’t work in Java.

#include <iostream> using namespace std; int main() { int x = -1;

// some other try { // some other if( x < 0 ) {

throw x; } } catch (int x ) { cout << "Exception occurred: thrown value is " << x << endl; } getchar(); return 0; } Output: Exception occurred: thrown value is -1

(Continued…………)
2) In C++, there is a special catch called “catch all” that can catch all kind of exceptions.

#include <iostream> using namespace std; int main() {

int x = -1; char *ptr;

ptr = new char[256];

// some other try { // some other if( x < 0 )

{

throw x;

} if(ptr == NULL)

{

throw " ptr is NULL ";

}

} catch (...) // catch all

{

cout << "Exception occurred: exiting "<< endl; exit(0);

} getchar(); return 0; } Output: Exception occurred: exiting

In Java, for all practical purposes, we can catch Exception object to catch all kind of exceptions. Because, normally we do not catch Throwable(s) other than Exception(s) (which are Errors)

catch(Exception e) { ……. }

3) In Java, there is a block called finally that is always executed after the try-catch block. This block can be used to do cleanup work. There is no such block in C++.

// creating an exception type class Test extends Exception { }

class Main { public static void main(String args[]) {

try { throw new Test(); } catch(Test t) { System.out.println("Got the Test Exception"); } finally { System.out.println("Inside finally block "); } } } Output: Got the error Inside finally block

(continued…..)
4) In C++, all exceptions are unchecked. In Java, there are two types of exceptions – checked and unchecked. See this for more details on checked vs Unchecked exceptions.

5) In Java, a new keyword throws is used to list exceptions that can be thrown by a function. In C++, there is no throws keyword, the same keyword throw is used for this purpose also.

4. Write a program that has an overloaded method. The first

method should accept no arguments; the second method will accept one string and the third method should display the message “Welcome to java” once. The second method should display the message “Welcome to Polymorphism” twice and the third method should display the message “Welcome to overloading” three times.

ANS 4

class Dis

{

void Dis String()

{

System . out . println ( " welcome to java " ) ;

}

Void Dis String ( String str1 )

{

System . out . println ( str1 ) ;

System . out . println ( " welcome to polymorphism " ) ;

System . out . println ( " welcome to polymorph2sm " ) ;

}

Void Display String ( String str1 , String str2 )

{

System . out . println ( str1 ) ;

System . out . println ( str2 ) ;

For ( int I = 0 ; I < 3 ; I ++)

System . out . println ( " welcome to function overloading " ) ;

}

Public static void main ( String [ ] args )

{

Dis

F = new Dis( ) ;

F . Dis String ( );

F . Dis String ( " one parameter only " ) ;

F . Dis String ( " one time " , " two times " ) ;

}

}

5. Why an abstract method cannot be defined as static or as final

justify with example.

ANS 5.

Abstract Methods and Classes
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: abstract void move(double shapeX, double shapeY); If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class Object { // declare fields // declare non-abstract methods abstract void draw(); } When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Abstract Classes versus Interfaces

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead

Abstract Class Example

abstract class Object { int x, y; ... void move(int newX, int newY) { ... } abstract void draw(); abstract void resize(); } Each non-abstract subclass of Object, such as Circle and Rectangle, must provide implementations for the draw and resize methods:

class Circle extendsObject { void draw() { ... } void resize() { ... } } class Rectangle extends Object { void draw() { ... } void resize() { ... } }

Class Members

An abstract class may have static fields and static methods. You can use these static members with a class reference—for example, AbstractClass.staticMethod()—as you would with any other class.

6. WAP

which define a class called TV with the following attributes, name of the company and screen size using super keyword and inheritance to create color TV class, it has a attribute TV type also a BW TV class it is also have TV type attribute in Boolean data type.

ANS 6.

Class Tv { String noc; String ss;

Public Tv ( String noc , String ss ) { This . noc = noc ; This . ss = ss ;

} } Class colorTv extends Tv { Boolean Tvtype; Public color Tv ( String n , Strings , Boolean Tvtype) { Super ( n , s ) ; This . Tvtype = Tvtype ; }

Void show Information ( ) { System . out . println ( " Name of the company : " + super . NOC) ; System . out . println ( " screen size : " + super . ss) ; System . out . println ( " Color Tv: " + Tvtype ) ; } } Class BwTv extends Tv { Boolean Tvtype ; Public BwTv ( String n , Strings , Boolean Tvtype) { Super ( n , s) ; This . Tvtype = Tvtype ; }

Void show Information ( ) { System . out . println ( " Name of the company : " + super . Noc); System . out . println ( " screen size : " + super . ss ) ; System . out . println ( " Black white Tv : " + Tvtype ) ; }

}

public class d { public static void main ( String [ ] args ) { colorTv c = new colorTv ( "Samsung " , " 22. Inches " , true); c . show Information ( ) ; BwTv d = new BwTv ( " texla " , " 33 inches " , true ) ; d . show Information ( ) ;

} }