Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
432 views

Assignment 5 Java

The document contains 9 Java programming examples demonstrating various OOP concepts like inheritance, polymorphism, interfaces, exceptions, and packages. Some key points: 1) The first example shows inheritance and method overriding where the Windows class extends the OS class and overrides the performance() method. 2) The second example calculates pay for hourly and weekly employees using abstraction via an Employee abstract class. 3) The third example implements an interface Vehicle to get details of a Two Wheeler object like name, color, etc. 4) Other examples show custom exceptions, unreachable catch blocks, exception handling and stack traces.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
432 views

Assignment 5 Java

The document contains 9 Java programming examples demonstrating various OOP concepts like inheritance, polymorphism, interfaces, exceptions, and packages. Some key points: 1) The first example shows inheritance and method overriding where the Windows class extends the OS class and overrides the performance() method. 2) The second example calculates pay for hourly and weekly employees using abstraction via an Employee abstract class. 3) The third example implements an interface Vehicle to get details of a Two Wheeler object like name, color, etc. 4) Other examples show custom exceptions, unreachable catch blocks, exception handling and stack traces.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

//Write a java program to create two classes os & windows such that windows class
extends os class & override its performance
//method using runtime polymorphism....

class Os
{
void performance()
{
System.out.println("Performance of the opreating system:");
}
}
class Windows extends Os
{
void performance()
{
System.out.println("Performance of the window system:");
}
}
public class OsWindow
{
public static void main(String args[])
{
Os o=new Windows();
o.performance();
}
}
/*
OUTPUT:::
C:\Users\aseha\Desktop\Assignment5>javac OsWindow.java
C:\Users\aseha\Desktop\Assignment5>java OsWindow
Performance of the window system:

*/

2./*
Create an abstract class employee with method getamount() which displays the amount paid
to employee. Reuse this class to
calculate the amount to be paid to weekly employee and hourly employee according to no. of
hours and total no. of weeks and
total weeks for weekly employee....
1-hour=60 min...
supose employee work 2 hour...
*/

abstract class Employee


{
String name;
double rate,pay,payWeek;
int hour,week;
//int allDay;
abstract void getamount(double rate, int hour, int week);
/*{
if(hour>30)
{
int overtime=hour-30;
}

if(week<=7)
{
allDay=rate*week;
}
}*/

}
class EmployeeAmount extends Employee
{
public void getamount(double rate, int hour, int week)
{
if(hour>15)
{
int overtime=hour-15;
pay=((rate*30)+(overtime*rate));
System.out.println("Total Hourly Basis To Paid Employee-->"+pay);

if(week>=7)
{
int allDay=week-7;
payWeek=((rate*7)+(allDay*rate));
System.out.println("Total Weekly Basis To Paid Employee-->"+payWeek);
}
}

}
}
public class EmployeePaid
{
public static void main(String args[])
{
EmployeeAmount ea=new EmployeeAmount();
ea.getamount(25.6,20,9);
}
}

/*
OUTPUT:::

C:\Users\aseha\Desktop\Assignment5>javac EmployeePaid.java
C:\Users\aseha\Desktop\Assignment5>java EmployeePaid
Total Hourly Basis To Paid Employee-->896.0
Total Weekly Basis To Paid Employee-->230.40000000000003
*/

3./*Create an interface vehicle with method getcolor(), getnumber(), getconsumption().


Calculate the fuel consumed by the particular
model,name, and color of two wheeler by implementing interface vehicle...
*/

interface vehicle
{
public String getColor();

public int getNumber();

public double getConsumption();


}
class TwoWheelr implements vehicle
{
String name,color;
int number;
double fuel;

void setData(String name, String color, int number, double fuel)


{
this.name=name;
this.color=color;
this.number=number;
this.fuel=fuel;
}

public String getName()


{
//this.name;
return name;
}
public String getColor()
{
//this.color;
return color;
}

public int getNumber()


{
//this.number;
return number;
}

public double getConsumption()


{
return fuel;
}
}
public class VehicleInterface
{
public static void main(String args[])
{
TwoWheelr tw=new TwoWheelr();
tw.setData("TVS Apache","White",4124,50.26);
String name=tw.getName();
String color=tw.getColor();
int number=tw.getNumber();
double fuel=tw.getConsumption();

System.out.println("Two Wheelr Name-->"+name+"-->Color-->"+color+"-->Number--


>"+number+"-->Fuel Consuption-->"+fuel);
}
}
/*
OUTPUT:::

C:\Users\aseha\Desktop\Assignment5>javac VehicleInterface.java
C:\Users\aseha\Desktop\Assignment5>java VehicleInterface
Two Wheelr Name-->TVS Apache-->Color-->White-->Number-->4124-->Fuel Consuption--
>50.26
*/

4.//Write a program to create your own package. Package should have more than 2-classes.
Write a class that uses he package...
package myPackageData;
public class DemoPackageClass
{
public void msgJava()
{
System.out.println("This is java class package...");
}
}
class PythonPackage
{
public void msgPython()
{
System.out.println("This is python class package...");
}
}
class AndroidPackage
{
public void msgAndorid()
{
System.out.println("This is andorid class package...");
}
}

//Accessing package DemoPackageClass.java


import myPackageData.*;
class AccissingPackage
{
public static void main(String args[])
{
myPackageData.DemoPackageClass dc=new myPackageData.DemoPackageClass();
dc.msgJava();
}
}

5.//create an coustomized exception in java...

public class CoustomizeExe


{
public static void main(String args[])
{
int res;
try{
res=12/0;
System.out.println("Result-->"+res);
}
catch(ArithmeticException ex)
{
System.out.println("Arthmatic Exception..."+ex);
}
catch(Exception e)
{
System.out.println("Exception-->"+e);
}

}
}
6.//Explain unreachable catch block error with the help of java program…

public class UnreachableError


{
public static void main(String args[])
{
int arr[]=new int[10];
try{
arr[12]=12/0;
System.out.println("Statement of array:");
}
catch(Exception ex)
{
System.out.println("Error-->"+ex);
}

System.out.println("Error can not be catch....");

}
}

7./*Write a java program to do a following...


Put in a loop so that the user is repeatedly asked for the numerator and the divisor.
For each set of data, the program prints out the result, or an informative error message
if there is a problem (division by zero or poor input data)

The program continues looping, even if there is a problem


Exit the loop when data entered for the numerator start with characters "q" or "Q".
Don't print out an error message in this case.
Don't ask for the divisor if the user just asked to quit.
Here is sample output from one run:
Enter the numerator: 12
Enter the divisor: 4
12 / 4 is 3
Enter the numerator: 12
Enter the divisor : 0
You can't divide 12 by 0
Enter the numerator: glarch
You entered bad data.
Please try again.
Enter the numerator: quit
You will need to use the method charAt() from the String class.
*/

import java.util.Scanner;
public class ProgramToDo
{
public static void main(String args[])
{
int i=1,result;
String neno=" ",deno=" ";
String s="quit";
Scanner sc=new Scanner(System.in);

try{
while(i==1)
{
System.out.println("Enter a numerator value::");
neno=sc.nextLine();

char ch=neno.charAt(0);
//System.out.print("Character="+ch);
char ch11=deno.charAt(0);
Character c2=new Character(ch);
Character c22=new Character(ch11);
if((c2=='q') || (c22=='q'))
{
break;
}

else if((c2=='Q') || (c22=='Q'))


{
break;
}

else if(neno.contains("quit") || deno.contains("quit"))


{
break;
}

else if(neno.contains("glarch") || deno.contains("glarch"))


{
System.out.println("You entered bad data.\n Please try again.");
break;
}

System.out.println("Enter a divisor value::");


deno=sc.nextLine();
int ii=Integer.parseInt(neno);
int j=Integer.parseInt(deno);
result=ii/j;
System.out.println("Result-->"+neno+"/"+deno+"="+result);
}

}
catch(ArithmeticException ae)
{
System.out.println("You can't divide "+ae);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}

8./*
Write a testing class TestTrace that contains the static main() method, and another class
CallEg that
contains three methods.
The main() method will create aCallEg object and call its methodA().
The catch{} block in main() prints a stack trace.
1.Put a statement in methodA() that divides by zero to create an ArithmeticException..
Observe the output.
2.Remove the division statement from methodA(). Change the code so that methodA() calls
methodB() which calls
methodC().
Put a statement inmethodC() that divides by zero to create an ArithmeticException.. Observe
the output.
3.Add to the code so that methodA() calls methodB() inside a try{} block, and methodB()
calls methodC() inside a try{} block.
In methodC() put the divide by zero statement inside a try{} block. After each try{} block put a
catch{} block
which catches the exception, prints a stack trace, and throws the exception object to its
caller.
Observe the output
*/

class CallEg
{
public void methodA() throws ArithmeticException
{
int result=12/0;
System.out.println("Result="+result);

public void methodB() throws ArithmeticException


{
methodC();
}

public void methodC() throws ArithmeticException


{
int result=12/0;
System.out.println("Result="+result);
}

}
class Testing
{
public void methodA() throws ArithmeticException
{
//int result=12/0;
//System.out.println("Result="+result);
methodB();
}

public void methodB() throws ArithmeticException


{
methodC();
}

public void methodC() throws ArithmeticException


{
int result=12/0;
System.out.println("Result="+result);
}
}
public class TestTrace
{

public static void main ( String[] args )


{
CallEg eg = new CallEg(); // use default constructor
Testing tt=new Testing();
try
{
eg.methodA();
tt.methodB();

}
catch ( ArithmeticException oops )
{
oops.printStackTrace();
}

/*
OUTPUT:::

C:\Users\sunil\Desktop\Assignment5>java TestTrace
java.lang.ArithmeticException: / by zero
at CallEg.methodA(TestTrace.java:22)
at TestTrace.main(TestTrace.java:44)
C:\Users\sunil\Desktop\Assignment5>javac TestTrace.java
C:\Users\sunil\Desktop\Assignment5>java TestTrace
java.lang.ArithmeticException: / by zero
at CallEg.methodC(TestTrace.java:34)
at CallEg.methodB(TestTrace.java:29)
at CallEg.methodA(TestTrace.java:24)
at TestTrace.main(TestTrace.java:48)
C:\Users\sunil\Desktop\Assignment5>
*/

9./*
Create the following program. Run it and observe that the stack trace shows only those
methods that were active at the time of the exception.
(In other words, the stack trace does not show a complete history of the calls.)

Run the program and observe the stack trace.


*/
class Divider
{
public void methodA()
{
System.out.println("Result: " + 12/4 );
}

public void methodB()


{
System.out.println("Result: " + 12/3 );
}

public void methodC()


{
System.out.println("Result: " + 12/0 );
}
}

public class TestTrace1


{

public static void main ( String[] args )


{
Divider dvdr = new Divider();

try
{
dvdr.methodA( );
dvdr.methodB( );
dvdr.methodC( );
}

catch ( ArithmeticException oops )


{
oops.printStackTrace();
}

}
/*
OUTPUT:::

C:\Users\sunil\Desktop\Assignment5>java TestTrace1
Result: 3//methodA();
Result: 4//methodB();
java.lang.ArithmeticException: / by zero
at Divider.methodC(TestTrace1.java:23)
at TestTrace1.main(TestTrace1.java:39)

*/

10./*What is Re-throwing an exception in Java? Explain it with Java Program....


Answer: There might be situations in your program where you want to both catch an
exception
in your code and also want its caller be notified about the exception.
This is possible by rethrowing the exception using throw statement.
*/
class ReThrowingExp
{
static void divide()
{
try{
int res,x,y,z;
x=12;
y=0;
res=x/y;
System.out.println(x+"/"+y+"="+res);
}
catch(ArithmeticException ae)
{
System.out.println("Exception catch in divide()");
throw ae;//Rethrow an exception....
}
}
public static void main(String args[])
{
System.out.println("Start of main()");
try{
divide();
}
catch(ArithmeticException ae)
{
System.out.println("Rethrowing exception caught in main()");
System.out.println(ae);
}
}
}
/*
OUTPUT::

C:\Users\sunil\Desktop\Assignment5>java ReThrowingExp
Start of main()
Exception catch in divide()
Rethrowing exception caught in main()
java.lang.ArithmeticException: / by zero
*/

You might also like