Question 4

(b) The abstract Vegetable class has three subclasses named Potato, Brinjal and Tomato. Write an application that demonstrates how to establish this class hierarchy. Declare one instance variable of type String that indicates the color of a vegetable. Create and display instances of these objects. Override the toString() method of Object to return a string with the name of the vegetable and its color.
import java.lang.*;
abstract class Vegetable
{
String colorVeg;
abstract public String toString();
}
class Potato extends Vegetable
{
public String toString()
{
colorVeg = "Yellow";
return colorVeg;
}
}
class Brinjal extends Vegetable
{
public String toString()
{
colorVeg = "Purpul";
return colorVeg;
}
}
class Tomato extends Vegetable
{
public String toString()
{
colorVeg = "Red";
return colorVeg;
}
}
class abstractClassH2E
{
public static void main(String args[])
{
Potato p = new Potato();
Brinjal b = new Brinjal();
Tomato t = new Tomato();
Vegetable vegref;
vegref = p;
System.out.println("Color of Potato : "+vegref.toString());
vegref = b;
System.out.println("Color of Brinjal : "+vegref.toString());
vegref = t;
System.out.println("Color of Tomato : "+vegref.toString());
}
}
(a) (ii) Give output of the following program for value of y=0 and y=2:
public class Test {
public static void main(String args[]) {
try {
System.out.println("calling method a");
a();
System.out.println("return from method a");
} catch(ArithmeticException e) {
System.out.println("main: catch");
} finally {
System.out.println("main: finally");
}
}
public static void a() {
try {
int x=8,y=0;
int z=x/y;
System.out.println("value of z="+z);
} catch(NumberFormatException e) {
System.out.println("method a:catch");
} finally {
System.out.println("method a:finally");
} }}
// y = 0
// calling metod a
// method a : finally
// main : catch
// main : finally
// y = 2
// calling metod a
// value of z=4
// method a : finally
// return from method a
// main : finally
public class Test
{
public static void main(String args[])
{
try
{
System.out.println("calling method a");
a();
System.out.println("return from method a");
}
catch(ArithmeticException e)
{
System.out.println("main: catch");
}
finally
{
System.out.println("main: finally");
}
}
public static void a()
{
try
{
int x=8,y=0;
int z=x/y;
System.out.println("value of z="+z);
}
catch(NumberFormatException e)
{
System.out.println("method a:catch");
}
finally
{
System.out.println("method a:finally");
}
}
}
(b) Write a program that takes input for filename and search word from commandline arguments and checks whether that file exists or not. If exists, the program will display those lines from a file that contains given search word.
import java.io.*;
class FileExistsWordSearchH2E
{
public static void main(String args[]) throws IOException
{
int fileExistFlag;
boolean wordExists=false;
File f1h2e = new File(args[0]);
fileExistFlag = f1h2e.exists()?1:0;
if(fileExistFlag == 1)
{
BufferedReader fileReader = new BufferedReader(new FileReader(f1h2e));
String wholeLine;
String word = args[1];
while ((wholeLine = fileReader.readLine()) != null)
{
String[] wordArr = wholeLine.split(" ");
for(String wordElem:wordArr)
{
if(wordElem.equalsIgnoreCase(word))
{
System.out.println("'"+word+"'" + " exists in line '" + wholeLine+"'");
wordExists = true;
}
}
}
if(!wordExists)
System.out.println("Word Not Exists in File");
}
else
{
System.out.println("File Not Exists");
}

}
}

Make Comments..!!


Oops!! No posts from user.

Download Android App