Assignment 5 Java
Assignment 5 Java
//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...
*/
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
*/
interface vehicle
{
public String getColor();
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...");
}
}
}
}
6.//Explain unreachable catch block error with the help of java program…
}
}
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;
}
}
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);
}
class Testing
{
public void methodA() throws ArithmeticException
{
//int result=12/0;
//System.out.println("Result="+result);
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.)
try
{
dvdr.methodA( );
dvdr.methodB( );
dvdr.methodC( );
}
}
/*
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)
*/
C:\Users\sunil\Desktop\Assignment5>java ReThrowingExp
Start of main()
Exception catch in divide()
Rethrowing exception caught in main()
java.lang.ArithmeticException: / by zero
*/